Closed xenide closed 5 months ago
Remaining questions:
type(uint128).max
fullMulDiv
, it's almost impossible to overflow given any real prices + amounts type(uint128).max
Setting both limits to type(uint128).max
will ensure that calculations will never revert with fullMulDiv.
On the topic of routing, I think we need to be supporting prices from multiple hops (referred to as composite prices from now on) as our AMM might not have direct pairs for all of them.
Take for example we want to know the price of DAI/EUR, but there is no such direct pair, but only the following pairs:
We can have a mapping of price routes, which needs to be actively maintained by governance. Like the priceCache mapping, it's also sorted, with the lower token being token0. The implications are that:
priceCache
only stores raw prices of pairs, but never derived values cross multiplied from raw pricesgetQuote
will then first go look for the DAI/EURC price inroutes
. It sees that DAI/EURC is a composite route as it has more than 2 items in the array. It iterates starting from the first pair (DAI/USDC) It sees that there is a route defined for DAI/USDC, and that it is a direct route, and looks it up onpriceCache
. If it's not a direct route (i.e. composite route), then we recursively go calculate this route first and wait for return. Subsequently the function does the same for obtaining the USDC/EURC price. The function needs to sort the tokens in order before looking up to find the route as the route is also stored in sequence.We can limit the number of hops to any arbitrary number as needed (e.g. 3).
Similarly, for the inverse route, the function will first sort the tokens in the sequence. Then multiply and invert accordingly.
E.g. query of EURC/DAI
If there is no route for a given pair defined by the priceCache, the PriceCache will return a
NoRoute()
error