Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use automatic routing #531

Merged
merged 68 commits into from
Dec 5, 2022
Merged

feat: use automatic routing #531

merged 68 commits into from
Dec 5, 2022

Conversation

KirillDogadin-std
Copy link
Contributor

@KirillDogadin-std KirillDogadin-std commented Nov 10, 2022

Closes #527

Checklist:

  • issue number linked above after pound (#)
    • replace "Closes " with "Contributes to" or other if this PR does not close the issue
  • issue checkboxes are all addressed
  • manually checked my feature / not applicable
  • wrote tests / not applicable
  • attached screenshots / not applicable

@KirillDogadin-std
Copy link
Contributor Author

I've added some codechanges that already indicate what code adjustment i'm intending to implement. Please go through them and point out some decisions that are incorrect/in fact harmful.

E.g. https://github.com/sidestream-tech/unified-auctions-ui/pull/531/files#diff-c6b4bdf1b82a84a00b56df1404f49c064a303b5919e55e1335c7afecaab1a7edR12 seems questionable.

core/src/calleeFunctions/helpers/uniswapV2.ts Show resolved Hide resolved
core/src/calleeFunctions/CurveLpTokenUniv3Callee.ts Outdated Show resolved Hide resolved
core/src/calleeFunctions/helpers/uniswapV3.ts Outdated Show resolved Hide resolved
core/src/calleeFunctions/index.ts Outdated Show resolved Hide resolved
core/src/calleeFunctions/index.ts Outdated Show resolved Hide resolved
core/src/types.ts Outdated Show resolved Hide resolved
core/src/types.ts Outdated Show resolved Hide resolved
@KirillDogadin-std
Copy link
Contributor Author

Encountering error when route is called

TypeError: Cannot read properties of undefined (reading 'sortsBefore')
    at V3PoolProvider.getPoolAddress (pool-provider.js?276b:86:1)
    at CachingV3PoolProvider.getPoolAddress (caching-pool-provider.js?9cf4:67:1)
    at CachingV3PoolProvider.getPools (caching-pool-provider.js?9cf4:30:1)
    at getHighestLiquidityV3NativePool (gas-factory-helpers.js?fa4f:32:1)
    at V3HeuristicGasModelFactory.buildGasModel (v3-heuristic-gas-model.js?385b:110:1)
    at async Promise.all (:3000/index 0)
    at async AlphaRouter.route (alpha-router.js?d7e0:329:1)

@KirillDogadin-std
Copy link
Contributor Author

The call of fetchAutoRouteInformation on production env works, for local setup it does not:

Possible reason: chain id is actually hardcoded :/ . SO when the router is created on main, the chain id corresponds to the actual one. On local setup it does not. Even on main branch the error appears (in the dashboard). So i would guess that setting the actual chain id should work (via fetching it with the existing method).
But in this case the error is thrown:

Error: No address for Uniswap Multicall Contract on chain id: 1337

@valiafetisov do you have the instant idea on how to solve this? If not, i then go investigate

@valiafetisov
Copy link
Contributor

The call of fetchAutoRouteInformation on production env works, for local setup it does not:

If by "local setup" you mean "hardhat fork", then yes. It seems to be hardcoded:

const router = new AlphaRouter({ chainId: 1, provider });

Maybe making it dynamic will already solve the error?

@KirillDogadin-std
Copy link
Contributor Author

KirillDogadin-std commented Nov 15, 2022

So i would guess that setting the actual chain id should work (via fetching it with the existing method).
But in this case the error is thrown:

This paragraph was intended to let know that I've already tried your suggestion above. See the error is that is thrown ('No address for Uniswap Multicall...') in the same comment.

@valiafetisov
Copy link
Contributor

valiafetisov commented Nov 15, 2022

This paragraph was intended

Sorry, I thought that reinstalling the dependencies solved the original error #531 (comment)

No address for Uniswap Multicall Contract on chain id: 1337

This error is expected, since 1337 is a chain id that exist only for us. Uniswap SDK doesn't have an idea what is this chain id, as they have to unfortunately still hardcode some staff per chain id in the SDK (eg contract addresses). So for the hardhat fork we would still need to provide chain id that the hardhat is forking, not the one it is calling itself. This will solve the above problem and will bring us back to the initial:

TypeError: Cannot read properties of undefined (reading 'sortsBefore')

Which according to a quick search originates from https://stackoverflow.com/questions/64674034/uniswap-typeerror-cannot-read-property-sortsbefore-of-undefined invalid Token instance creation. We create it via getUniswapTokenBySymbol where decimalChainId is passed derived from network instead of using the same as passed to the AlphaRouter – so the mismatch is formed

@KirillDogadin-std
Copy link
Contributor Author

Currently some tests started to fail after the 'eth-a' collateral was extended with the automatic routing config.
One of the tests is Fetches not liquidatable vault and it goes into the process of extracting the route automatically. it hangs during AlphaRouter.route() call in getUniswapAutoRoute function.

See commit 87dfced which pinpoints the place.
To reproduce you can run npm run test -- ./test/vault-test.ts (the very first test fails / times out for me)

Adjusting the mocha test timeout to 100 seconds instead of 40s did not solve the problem.

@valiafetisov
Copy link
Contributor

valiafetisov commented Nov 16, 2022

It's certainly the timeout. Dashboard on the hardhat first time took a very long time for me to load. So a lot of requests are made to get all pools, I assume. Not sure how to go about it, but to fix tests (until we have cache) we might want to skip this fetching

@valiafetisov
Copy link
Contributor

valiafetisov commented Dec 1, 2022

tests also fail for crveth with Dog/not-unsafe

  • Can you please cherry-pick 925b3d9 to temporary fix the tests?

Comment on lines +93 to +96
'Uniswap V3 Autorouter': {
callee: 'UniswapV3Callee',
automaticRouter: true,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎄

core/src/calleeFunctions/index.ts Outdated Show resolved Hide resolved
valiafetisov
valiafetisov previously approved these changes Dec 1, 2022
Copy link
Contributor

@valiafetisov valiafetisov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests fixed, code approved

Copy link
Contributor

@LukSteib LukSteib left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • UI Test on goerli looks good ✅
  • Tested keeper on goerli and executed ETH auction via swap (including auth txs) ✅
  • Tested keeper on hardhat fork with ETH-A simulation running
    • authorisation steps successfully executed ✅
    • for swap tx I get the following error in the console (see below)
ℹ collateral keeper: auction "ETH-A:840": attempting swap execution                                                       
 ERROR  collateral keeper: unexpected error: Cannot read properties of undefined (reading 'addresses')

Raising this as I am not able to reproduce it on main branch.

@KirillDogadin-std
Copy link
Contributor Author

KirillDogadin-std commented Dec 5, 2022

Current bugs seem to be related to:

  1. feat: use automatic routing #531 (comment) not being fulfilled properly. At some cases the pools are not pregenerated apparently.
  2. routeToPool logic does not cover the edgecase where the route is empty array. Therefore for this case empty pools will be generated

@LukSteib
Copy link
Contributor

LukSteib commented Dec 5, 2022

I need more context / info in order to properly decide how to proceed here:

  • Is the problem of not properly pregenerated pools only related to hardhat fork?
  • Do we know how to tackle the problems described above and only need more time to address?

@KirillDogadin-std
Copy link
Contributor Author

Is the problem of not properly pregenerated pools only related to hardhat fork?

no, this is a code logic issue.

Do we know how to tackle the problems described above and only need more time to address?

(1) requires me to put more thinking in to figure out how to wire up things properly, @valiafetisov might come up with the fix faster.
(2) is simplier, it's just an algorithmic issue.

@LukSteib
Copy link
Contributor

LukSteib commented Dec 5, 2022

(1) requires me to put more thinking in to figure out how to wire up things properly, @valiafetisov might come up with the fix faster.

Ok, then I'd suggest to have @valiafetisov look into it in order to get this PR over the last mile and proceed with release.

@valiafetisov
Copy link
Contributor

Yes, it seems like routeToPool will produce invalid pool based on exchanges[marketId].route. It should include starting token into the array. Eg if ETH-A['Uniswap V3'].route is [], the pool should include ETH, DAI, but currently (as far as I understood) only is DAI. Overall, the getRouteSteps + routeToPool seems to be overly complicated

@KirillDogadin-std
Copy link
Contributor Author

I've fixed (2), the side effect of this seems to be advance for (1) - the error is changed

now with

  1. Hardhat fork
  2. ETH-A auction simulation
  3. choosing autorouter

The error is "Error: VM Exception while processing transaction: reverted with reason string 'Too little received'" from JSON-RPC


I was able to use the old Uniswap V3 router to execute swap

Comment on lines 78 to 80
const route =
'route' in calleeConfig ? calleeConfig.route : await getCalleeAutoRoute(network, collateral, marketId, amount);
return await routeToPool(network, route, collateral.symbol);
Copy link
Contributor

@valiafetisov valiafetisov Dec 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As pointed out in the issue several times:

We should expect that intermediate tokens might be tokens that are not part of the collateral config

we CAN NOT assume that the route provided from the auto router will ONLY contain symbols that are known to the maker. Therefore we CAN NOT convert automatic route to pool. Instead, the information provided by the AlphaRouter should contain enough information to build a pool object: addresses, symbols and fees.

Comment on lines +49 to +56
const pools = await routeToPool(network, route, collateral.symbol, fees);
const daiAmount = await convertCollateralToDaiUsingPool(
network,
collateral.symbol,
marketId,
collateralAmount,
pools
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a better explanation would've been: previously we expected that pools (/routes) are static. Now we should receive and store pools together with prices (because re-requesting price via auto-router can change the pool) – so if the pool is generated, it have to be stored in the auction together with the price (unless the price is not an auction price, but just a reference price)

@valiafetisov
Copy link
Contributor

valiafetisov commented Dec 5, 2022

Was able to execute transaction using auto router on a fork (at least that's how it looks)
Screenshot 2022-12-05 at 19 42 22

But two problems outlined above still there:

  • We're regenerating pools in a different moment than when we fetch prices (so the price displayed and the route displayed are different and requires two requests)
  • There will be error during pool generation in case auto router returns symbol that is not present in the chainlog (not sure how it will be handled, hard to simulate)

@LukSteib
Copy link
Contributor

LukSteib commented Dec 5, 2022

Next round of tests:

@LukSteib
Copy link
Contributor

LukSteib commented Dec 5, 2022

But two problems outlined above still there:

We're regenerating pools in a different moment than when we fetch prices (so the price displayed and the route displayed are different and requires two requests)
There will be error during pool generation in case auto router returns symbol that is not present in the chainlog (not sure how it will be handled, hard to simulate)

We will tackle these problems in a dedicated follow up issue and proceed with the merge of this PR for now.

@LukSteib LukSteib merged commit 0c51938 into main Dec 5, 2022
@LukSteib LukSteib deleted the multi-callee branch December 5, 2022 19:45
@KirillDogadin-std KirillDogadin-std mentioned this pull request Dec 7, 2022
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend multi-callee with auto router
3 participants