Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 807b37b
Author: Eric Zhong <[email protected]>
Date:   Tue Aug 2 21:51:31 2022 -0700

    Add test for mixedRoute priceImpact

commit a4b22f3
Merge: 12ba497 3f996ce
Author: Eric Zhong <[email protected]>
Date:   Tue Aug 2 21:10:01 2022 -0700

    Merge branch 'main' into eric/support-mixed-route-paths

commit 12ba497
Author: Eric Zhong <[email protected]>
Date:   Tue Aug 2 21:06:21 2022 -0700

    Export utils.index

commit 4e3f85a
Author: Eric Zhong <[email protected]>
Date:   Sat Jul 30 19:15:53 2022 -0700

    Revisions

commit bca28a9
Author: Eric Zhong <[email protected]>
Date:   Mon Jul 25 17:42:26 2022 -0400

    Revisions: clean up dividingMixedRoutes util, unused vars in swapRouter + comments

commit c78d766
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 18:31:18 2022 -0400

    Untrack tarball

commit 06e9d75
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 18:29:13 2022 -0400

    Unskip test

commit ac9f51f
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 18:29:04 2022 -0400

    Revert yarn.lock to main

commit 68387db
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 14:07:41 2022 -0400

    Resolve TODOS

commit bf3f285
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 14:05:21 2022 -0400

    Separate some helpers into utils/index.ts

commit 32d6a4c
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 13:32:07 2022 -0400

    Add notice comment

commit 718b218
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 13:30:16 2022 -0400

    Remove exactIn from mixedRoute/trade

commit b696e9e
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 12:39:12 2022 -0400

    Add tests for tradeType validation

commit 17c964d
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 12:27:01 2022 -0400

    Add mixedRoute to all tests in swapRouter.ts

commit d609beb
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 12:05:15 2022 -0400

    Update invariant messages for tradeType validation for mixedRoutes

commit 7cfd339
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 12:04:05 2022 -0400

    Fix bug where routerMustCustody was not true for unwrap weth

commit 3a64dc9
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 21 11:10:16 2022 -0400

    Add mixedRoute tests for worstExecutionPrices

commit b5de080
Author: Eric Zhong <[email protected]>
Date:   Wed Jul 20 18:19:26 2022 -0400

    Add rest of tests involving mixedRoutes in generic trade test

commit 27717dc
Author: Eric Zhong <[email protected]>
Date:   Wed Jul 20 17:56:24 2022 -0400

    Add a bunch of tests for Trades with MixedRouteSDK

commit 967a96b
Author: Eric Zhong <[email protected]>
Date:   Wed Jul 20 16:58:43 2022 -0400

    remove comment

commit 87b1e7a
Author: Eric Zhong <[email protected]>
Date:   Wed Jul 20 15:47:24 2022 -0400

    Revert changes to entities/route to implement IRoute

commit e2ba061
Author: Eric Zhong <[email protected]>
Date:   Wed Jul 20 15:06:46 2022 -0400

    MixedRouteSDK implements IRoute

commit 05f38d3
Author: Eric Zhong <[email protected]>
Date:   Wed Jul 20 14:50:11 2022 -0400

    fix bug in encodeSwaps that would reassign types

commit 014a2a3
Author: Eric Zhong <[email protected]>
Date:   Wed Jul 20 14:13:54 2022 -0400

    Fix single hop in encoding mixedroutes

commit 66346b0
Author: Eric Zhong <[email protected]>
Date:   Wed Jul 20 13:40:59 2022 -0400

    Fix section MixedRoute construction again

commit 5aed2f7
Author: Eric Zhong <[email protected]>
Date:   Fri Jul 15 16:20:18 2022 -0400

    Figure out mid price calculations

commit ac39c9d
Author: Eric Zhong <[email protected]>
Date:   Fri Jul 15 11:40:39 2022 -0400

    Add some more complex mixedRoute tests

commit fa59131
Author: Eric Zhong <[email protected]>
Date:   Fri Jul 15 11:27:09 2022 -0400

    Add more tests to add more coverage for mixedRoute creation

commit f70d9da
Author: Eric Zhong <[email protected]>
Date:   Fri Jul 15 11:14:00 2022 -0400

    Fix bug in building mixedRoutes from sections

commit 29dee12
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 14 12:09:06 2022 -0400

    Import BestTradeOptions from v3-sdk

commit dcda2c9
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 14 11:55:29 2022 -0400

    Fix call to encodeMixedRoutetoPath

commit e10cb4f
Author: Eric Zhong <[email protected]>
Date:   Thu Jul 14 11:52:47 2022 -0400

    Revisions: change mixedRouteWrapper to remove tokenPath and remove param for encodeMixedRouteToPath

commit 02e24e8
Author: Eric Zhong <[email protected]>
Date:   Mon Jul 11 12:47:53 2022 -0400

    Self revisions, change naming, doc comments, etc.

commit c096c4e
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 30 17:15:57 2022 -0400

    Support single hop in MixedRoute and verify that calldata encoding is correct

commit cfc6fef
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 30 16:29:26 2022 -0400

    Add all tests for MixedRouteTrade

commit 98513b6
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 30 15:34:59 2022 -0400

    Add tests for MixedRouteTrade

commit 1b96502
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 30 15:34:49 2022 -0400

    add missed pair-specific logic in bestTradeExactIn

commit 0ba5795
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 30 14:56:29 2022 -0400

    Update swap router

commit 1b19d84
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 30 14:56:15 2022 -0400

    simplify mixedRoute/trade.ts

commit dd025a8
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 30 11:49:19 2022 -0400

    fix input output issue with new sections

commit f89b9af
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 29 18:45:45 2022 -0400

    section batching is broken rn need to fix mixedRouteSDK creation

commit 502676f
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 29 18:04:51 2022 -0400

    Add batching and fix typo in encodeMixedRouteToPath to pass basic test

commit 7430838
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 29 17:34:57 2022 -0400

    add consecutive chunking logic for encoding mixedRoute

commit ca4af84
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 29 17:16:12 2022 -0400

    fix amountOut condition for mixedRoute

commit d0317ce
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 29 17:09:38 2022 -0400

    clean up

commit 59632f9
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 29 17:06:25 2022 -0400

    cleaning up encodeMixedRoutePaths

commit 53bfdc0
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 29 16:50:28 2022 -0400

    call data successfull!

commit c498b4e
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 29 16:22:14 2022 -0400

    add basic semi broken calldata generation for mixedroute trades

commit 5993697
Author: Eric Zhong <[email protected]>
Date:   Tue Jun 28 14:06:06 2022 -0400

    change var assignment in fromRoute

commit 54804f8
Author: Eric Zhong <[email protected]>
Date:   Tue Jun 28 13:55:09 2022 -0400

    some changes while debugging

commit 3914891
Author: Eric Zhong <[email protected]>
Date:   Mon Jun 27 17:27:31 2022 -0400

    clean up swapRouter.ts to add encodeSwap func for mixedRoute

commit 272bcaf
Author: Eric Zhong <[email protected]>
Date:   Mon Jun 27 16:03:16 2022 -0400

    fix broken tests

commit b73ab12
Author: Eric Zhong <[email protected]>
Date:   Mon Jun 27 16:01:33 2022 -0400

    rescued!

commit 12b9679
Author: Eric Zhong <[email protected]>
Date:   Mon Jun 27 14:40:22 2022 -0400

    un add tarball

commit 3185287
Author: Eric Zhong <[email protected]>
Date:   Mon Jun 27 14:39:19 2022 -0400

    bump v3-sdk to 3.8.3

commit a97018c
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 22 16:38:25 2022 -0400

    pin v3-sdk at 3.8.2

commit 86bb048
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 22 14:27:10 2022 -0400

    Add implementaiton for generic MixedRouteSDK class, and refactor

commit a19ceaf
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 22 12:32:57 2022 -0400

    remove comment

commit 7528a67
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 22 12:25:51 2022 -0400

    remove .onlys

commit 6bc0c11
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 22 12:22:10 2022 -0400

    Add final encodePath tests for mixed route

commit f81420b
Author: Eric Zhong <[email protected]>
Date:   Wed Jun 22 12:16:53 2022 -0400

    add encodeMixedRoutetoPath tests

commit c64946d
Author: Eric Zhong <[email protected]>
Date:   Fri Jun 17 15:41:05 2022 -0400

    Add another tokenPath test

commit ad06d4b
Author: Eric Zhong <[email protected]>
Date:   Fri Jun 17 15:35:47 2022 -0400

    Clean up some comments

commit 02f2f21
Author: Eric Zhong <[email protected]>
Date:   Fri Jun 17 15:30:51 2022 -0400

    Clean up tests

commit 1729c4b
Author: Eric Zhong <[email protected]>
Date:   Fri Jun 17 15:26:41 2022 -0400

    Add basic test for MixedRoute midPrice, math TBD

commit 1a56f48
Author: Eric Zhong <[email protected]>
Date:   Fri Jun 17 15:17:24 2022 -0400

    refactor tests

commit 42d5006
Author: Eric Zhong <[email protected]>
Date:   Fri Jun 17 15:08:53 2022 -0400

    Add basic tests for V2 route mid prices

commit f7da4fc
Author: Eric Zhong <[email protected]>
Date:   Fri Jun 17 14:46:47 2022 -0400

    Add tests to ensure backwards compatability with v3 midprice

commit a764781
Author: Eric Zhong <[email protected]>
Date:   Fri Jun 17 14:42:47 2022 -0400

    Initial idea for midPrice support in mixedRoute

commit 8e7f66c
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 16 18:11:14 2022 -0400

    Add comment

commit a957d6f
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 16 18:08:59 2022 -0400

    Add basic tests for MixedRoute tokenPath creation

commit 3706854
Author: Eric Zhong <[email protected]>
Date:   Thu Jun 16 17:51:29 2022 -0400

    Add mixedRoute class and create encodeMixedRouteToPath
  • Loading branch information
zhongeric committed Aug 4, 2022
1 parent 3f996ce commit 8c9ff06
Show file tree
Hide file tree
Showing 16 changed files with 4,087 additions and 52 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ export const ADDRESS_THIS = '0x0000000000000000000000000000000000000002'

export const ZERO = JSBI.BigInt(0)
export const ONE = JSBI.BigInt(1)

// = 1 << 23 or 100000000000000000000000
export const V2_FEE_PATH_PLACEHOLDER = 8388608
473 changes: 473 additions & 0 deletions src/entities/mixedRoute/route.test.ts

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions src/entities/mixedRoute/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import invariant from 'tiny-invariant'

import { Currency, Price, Token } from '@uniswap/sdk-core'
import { Pool } from '@uniswap/v3-sdk'
import { Pair } from '@uniswap/v2-sdk'

type TPool = Pair | Pool

/**
* Represents a list of pools or pairs through which a swap can occur
* @template TInput The input token
* @template TOutput The output token
*/
export class MixedRouteSDK<TInput extends Currency, TOutput extends Currency> {
public readonly pools: TPool[]
public readonly path: Token[]
public readonly input: TInput
public readonly output: TOutput

private _midPrice: Price<TInput, TOutput> | null = null

/**
* Creates an instance of route.
* @param pools An array of `TPool` objects (pools or pairs), ordered by the route the swap will take
* @param input The input token
* @param output The output token
*/
public constructor(pools: TPool[], input: TInput, output: TOutput) {
invariant(pools.length > 0, 'POOLS')

const chainId = pools[0].chainId
const allOnSameChain = pools.every((pool) => pool.chainId === chainId)
invariant(allOnSameChain, 'CHAIN_IDS')

const wrappedInput = input.wrapped
invariant(pools[0].involvesToken(wrappedInput), 'INPUT')

invariant(pools[pools.length - 1].involvesToken(output.wrapped), 'OUTPUT')

/**
* Normalizes token0-token1 order and selects the next token/fee step to add to the path
* */
const tokenPath: Token[] = [wrappedInput]
for (const [i, pool] of pools.entries()) {
const currentInputToken = tokenPath[i]
invariant(currentInputToken.equals(pool.token0) || currentInputToken.equals(pool.token1), 'PATH')
const nextToken = currentInputToken.equals(pool.token0) ? pool.token1 : pool.token0
tokenPath.push(nextToken)
}

this.pools = pools
this.path = tokenPath
this.input = input
this.output = output ?? tokenPath[tokenPath.length - 1]
}

public get chainId(): number {
return this.pools[0].chainId
}

/**
* Returns the mid price of the route
*/
public get midPrice(): Price<TInput, TOutput> {
if (this._midPrice !== null) return this._midPrice

const price = this.pools.slice(1).reduce(
({ nextInput, price }, pool) => {
return nextInput.equals(pool.token0)
? {
nextInput: pool.token1,
price: price.multiply(pool.token0Price),
}
: {
nextInput: pool.token0,
price: price.multiply(pool.token1Price),
}
},
this.pools[0].token0.equals(this.input.wrapped)
? {
nextInput: this.pools[0].token1,
price: this.pools[0].token0Price,
}
: {
nextInput: this.pools[0].token0,
price: this.pools[0].token1Price,
}
).price

return (this._midPrice = new Price(this.input, this.output, price.denominator, price.numerator))
}
}
Loading

0 comments on commit 8c9ff06

Please sign in to comment.