Skip to content

Commit

Permalink
Add full SOR support for MixedRoutes (Uniswap#101)
Browse files Browse the repository at this point in the history
* add quoterV3 abi

* Add on-chain-quote-provider, accepts MixedRoute type input

* Add IQuoterV3 for types

* Add helpers for new quote provider

* bump v3-sdk to ^3.8.3

* Add log

* pin v3-sdk at 3.8.2

* set v3-sdk to ^3.7.0 but install to resolve to latest

* Add MixedRouteWithValidQuote typings and wrangle with typescript about string encodings

* finish adding getMixedQuotes

* Add hack to estimate gas for mixed route

* Create on-chain-heuristic-gas-model and add to alpha router

* Fix errors, ready for specific integ test for v3 quoter

* fix routeToString error

* prevent round tripping in IL routes

* change to .symbol

* Add tests using new on quoterV3 to quote mixed paths

* Save state

* refactor SOR to accomdate new sdk change

* Stuck at out of gas error on v3

* currently broken

* update package-lock.json

* pin ethersproject/providers at 5.4.0 and ethers at 5.6.1 to fix quoteProvider bug

* pass all standard integ tets :)

* add guard against quoterv2 address in on chain provider

* remove some comments

* remove block pinning

* add caret

* clean up integ tests file

* remove some console.logs

* package-lock add caret

* some fixes

* implement candidatePools for mixedRoutes

* combine v3 and v2 candidate pools

* revisions

* fix errors from merge from main

* some comments

* bump tsnode 10.7.0

* Change mixedRouteHeuristicGasModel

* -1 for num hops in v2 case

* Just some comments for debugging

* testing more mixed routes

* rmeove comment

* Create OnChainQuoteProvider generic class for v3 and mixed to inherit from

* large refactor - remove v3 quote provider files\

* all unit tests pass when we don't query for mixedroutes which makes sense

* Add validation checks against malformed routes param, and interface conditional

* revert package.json to main

* Save state

* Add blockGasLimit, remove legacy testing stuff

* Feat->feat branch add heuristics to mixed routes selection (Uniswap#117)

* Add basic logic for comparing relative liquidity between pools v2 and v3

* Add first heuristic for mixed routes - relative liq in v2 pools

* trying to get reserveUSD to be returned from v2

* Create instance of subgraph provider and call explictly

* create custom subgraphProvider arg for v2 in mixedRoute

* update to use reserveUSD

* feat: add stale config (Uniswap#116)

* [Docs] Added section in README on how to add a new chain to SOR (Uniswap#119)

* add comment

* Fix errors caused by bumping router-sdk

* Update mixedRouteQuoteProvider params to avoid gasError

* Remove exactOutput branch in methodParameters

* remove unsued testing stuff

* Remove maxApproval check

* Save state

* add testing stuff

* Clean up testing

* Change wording in comments for on chain quoter and add TODOs

* Clean up comments, logs, and etc. before merge into feat branch

Co-authored-by: marktoda <[email protected]>
Co-authored-by: raghava-pamula <[email protected]>

* Fix validation check for exact_input

* Remove unused calculateL1Fee from MixedRouteGasModel

* Add optional argument to onChainQuoteProvider for addressesLookup

* Guard against case where addressLookup is not provided nor override

* Replace QuoterV3 abi with new MixedRouteQuoterV1 abi

* Bump ts-node to ^10.8.1 inline with main

* Add disableMixedRoutesConsideration key to routingConfig

* Change constructor params for OnChainQuoteProvider to have boolean flag

* Add newly deployed Quoter address for testing

* Fix import error in base-command

* Add TODO for unit test and fix comment

* USe router-sdk typing for protcols key in config

* Change routeAmountsToString value for MIXED to V2 + V3 for interface

* Fix compile error with mocked mixedRouteQuoteProvider

* Disable mixedRoutes consideration for routeToRatio to pass unit tests

* Remove console.logs and add some log.infos

* Change behavior for non v2 liq chains to allow construction of class, but restrict usage

* Do not construct mixedRouteQuoteProvider for non V2 supported chains

* Fix comments, other non crit

* self revisions

* Remove skip, console.logs

* Minor changes from offline disc

Co-authored-by: Sara Reynolds <[email protected]>

* Initial revisions, add comments, refactor some code for readibility

* Add logic for routing across MIXED protocols explictly, fialing unit tests

* Remove while/while logic in mixed-route-heuristic-gas-model and add router-sdk func

* Allow token round trip in mixedRoute route generation

* Oops fix error in checking token amounts in validateExecuteSwap

* Add mixedRoute explicit cases for unit test

* Add test where mixedRoute is selected, and check output

* Unskip tests

* locally bump package-json router-sdk version

* bump local router-sdk version

* Use v3PoolHelper funcs in mixedRouteHeuristicGasModel

* Refactor gas models to use IOnChainGasFactory, and for mixedRoute to extend v3Factory class

* Use published router-sdk1.3.0 version

* Remove failing test

* remove console.log

* Add base unit test for exactOut. not called for mixedRouteQuotes

* feat -> feat Eric/refactor gas factories (Uniswap#155)

* Refactor gas models to use IOnChainGasFactory, and for mixedRoute to extend v3Factory class

* now both mixed route and v3 extend IOnChainFactory,

* protected -> private

* Restrict return generic type for IOnChainGasModel to V3 or Mixed

* Change buildGasModel to universally take an object as params, and add v2 helpers

* Use highest native V2 pool in the case that the v3 does not exist

* Fix unit tests for new inputs

* Move COST_PER_UNINIT_TICK to v3/gas-costs

* change inTermsOfToken to token

* Remove skipped tests

* remove logger

* feat -> feat add cli support mixed routes (Uniswap#157)

* Add flag in CLI quote.ts and update .route() call

* Add mixedRoutes query into README.md

* bump @uniswap/swap-router-contracts to 1.3.0

* Add protocol flag to SOR version of SDK routes (Uniswap#156)

* Save state before fetching

* Initial commit for refacto

* Add unit test to make sure exactOut is not being called for mixed

* Remove disable flag and add forceMixedRoutes flag, adjust logic + tests

* Comments and revisions

* Remove unsued inputs

* Update natspec, comments

* Add test that swap is null when exactOut + mixed

* Add integ-tests for other chains

* Change naming of getV2NativePool

* Remove comment

* fix comments

* remove .only

* Fix natspec

* Fix candidate pools logic, and change naming of params + gasmodel

* Remove some legacy stuff

* Remove MixedRouteQuoter abis

* Update readme

* Change validateRoutes logic

* Add integ tests for external on chain quote provider class

* resolve new router-sdk to 1.3.0

* Fix validation and add tests

* Add some comments

* remove .only

* finish up the test

* untrack binaries

* rollback changes, since we can't use new quoter with v3 since not deployed

* Fix comment abt forceMixedRoutes

Co-authored-by: marktoda <[email protected]>
Co-authored-by: raghava-pamula <[email protected]>
Co-authored-by: Sara Reynolds <[email protected]>
  • Loading branch information
4 people authored Aug 10, 2022
1 parent d14e1df commit 9e656a8
Show file tree
Hide file tree
Showing 28 changed files with 2,650 additions and 613 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ Calldata: 0x414bf389000000000000000000000000dac17f958d2ee523a2206206994597c13d83
blockNumber: "13239188"
estimatedGasUsed: "113000"
gasPriceWei: "116690684398"
./bin/cli quote --tokenIn 0x0391D2021f89DC339F60Fff84546EA23E337750f --tokenOut 0x4d224452801ACEd8B2F0aebE155379bb5D594381 --amount 10000 --exactIn --recipient 0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B --protocols v2,v3,mixed
Best Route:
[V2 + V3] 100.00% = BOND -- [0x6591c4BcD6D7A1eb4E537DA8B78676C1576Ba244] --> USDC -- 0.3% [0xB07Fe2F407F971125D4EB1977f8aCEe8846C7324] --> APE
Raw Quote Exact In:
10437.85
Gas Adjusted Quote In:
10433.83
Gas Used Quote Token: 4.018625
Gas Used USD: 29.669402
Calldata: 0x5ae401dc0000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000000e4472b43f300000000000000000000000000000000000000000000021e19e0c9bab240000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000391d2021f89dc339f60fff84546ea23e337750f000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000104b858183f00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000080000000000000000000000000ab5801a7d398351b8be11c439e05c5b3259aec9b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002358df5b3b4459a3f5b000000000000000000000000000000000000000000000000000000000000002ba0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000bb84d224452801aced8b2f0aebe155379bb5d59438100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Value: 0x00
blockNumber: "15303839"
estimatedGasUsed: "434000"
gasPriceWei: "38218865879"
Total ticks crossed: 7
```

## Rinkeby
Expand Down
4 changes: 2 additions & 2 deletions cli/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import {
LegacyRouter,
MetricLogger,
NodeJSCache,
OnChainQuoteProvider,
routeAmountsToString,
RouteWithValidQuote,
setGlobalLogger,
setGlobalMetric,
TokenProvider,
UniswapMulticallProvider,
V3PoolProvider,
V3QuoteProvider,
V3RouteWithValidQuote,
} from '../src';
import { LegacyGasPriceProvider } from '../src/providers/legacy-gas-price-provider';
Expand Down Expand Up @@ -253,7 +253,7 @@ export abstract class BaseCommand extends Command {
chainId,
multicall2Provider,
poolProvider: new V3PoolProvider(chainId, multicall2Provider),
quoteProvider: new V3QuoteProvider(
quoteProvider: new OnChainQuoteProvider(
chainId,
provider,
multicall2Provider
Expand Down
7 changes: 7 additions & 0 deletions cli/commands/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export class Quote extends BaseCommand {
exactOut: flags.boolean({ required: false }),
protocols: flags.string({ required: false }),
forceCrossProtocol: flags.boolean({ required: false, default: false }),
forceMixedRoutes: flags.boolean({
required: false,
default: false,
}),
};

async run() {
Expand All @@ -59,6 +63,7 @@ export class Quote extends BaseCommand {
chainId: chainIdNumb,
protocols: protocolsStr,
forceCrossProtocol,
forceMixedRoutes,
} = flags;

if ((exactIn && exactOut) || (!exactIn && !exactOut)) {
Expand Down Expand Up @@ -130,6 +135,7 @@ export class Quote extends BaseCommand {
distributionPercent,
protocols,
forceCrossProtocol,
forceMixedRoutes,
}
);
} else {
Expand Down Expand Up @@ -162,6 +168,7 @@ export class Quote extends BaseCommand {
distributionPercent,
protocols,
forceCrossProtocol,
forceMixedRoutes,
}
);
}
Expand Down
151 changes: 26 additions & 125 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
},
"dependencies": {
"@uniswap/default-token-list": "^2.0.0",
"@uniswap/router-sdk": "^1.2.0",
"@uniswap/swap-router-contracts": "1.2.0",
"@uniswap/router-sdk": "^1.3.0",
"@uniswap/swap-router-contracts": "^1.3.0",
"@uniswap/token-lists": "^1.0.0-beta.25",
"@uniswap/v2-sdk": "^3.0.1",
"@uniswap/v3-sdk": "^3.7.0",
Expand Down
2 changes: 1 addition & 1 deletion src/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './legacy-gas-price-provider';
export * from './multicall-provider';
export * from './multicall-uniswap-provider';
export * from './on-chain-gas-price-provider';
export * from './on-chain-quote-provider';
export * from './static-gas-price-provider';
export * from './swap-router-provider';
export * from './token-provider';
Expand All @@ -25,7 +26,6 @@ export * from './v2/uri-subgraph-provider';
export * from './v3/caching-pool-provider';
export * from './v3/caching-subgraph-provider';
export * from './v3/pool-provider';
export * from './v3/quote-provider';
export * from './v3/static-subgraph-provider';
export * from './v3/subgraph-provider';
export * from './v3/subgraph-provider-with-fallback';
Expand Down
Loading

0 comments on commit 9e656a8

Please sign in to comment.