-
Notifications
You must be signed in to change notification settings - Fork 8
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
RouterV2 #117
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally looks good, but I'm not sure the calculate_amounts_in
is correct.
Also, as you noted, adding liquidity to stable pools with wrapping requires two actions, so I'm afraid it has to be added there
Co-authored-by: Grzegorz Gawryał <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One general idea (as discussed offline): it seems that caching in the current form is largely unnecessary and adds complexity - since we get AccountId
in each Step
path anyway there should be no need to keep a permissioned record of pools (as long as we are able to distinguish between pair and stable pool using on-chain data eg making a cross-contract call).
It might be advantageous to create an automatic cache, which role would be to limit number of cross-contract calls, but would not require contract owner and would be simple on the implementation side.
For adding liquidity to pair contract we can create an interface that takes Option<AccountId>
of the pool - if None
, then new pair would be created.
@ggawryal what do you think?
amm/traits/router_v2.rs
Outdated
/// | ||
/// Starts with `amount_in` and token address under `path[0].token_in`. | ||
/// | ||
/// If `path[i].pool_id` is `None`, it attemps the swap through |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I see Step
now has no Option
field
let wazero = wazero::setup(&mut session); | ||
let router = router_v2::setup(&mut session, factory.into(), wazero.into()); | ||
|
||
let initial_reserves = vec![100000 * ONE_USDT, 100000 * ONE_USDC]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would suggest to create constants instead of using numerical values directly whenever possible
vec![], | ||
); | ||
|
||
_ = session |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: not to use _ =
, if it's an error then it should be checked/unwrapped, if it's just an unused value then ignore it altogether
_ = session | |
session |
Agreed. Decoding the pool type might be a bit harder, but even if we had to add an extra flag to the
It might, but currently cache doesn't limit anything, it only helps to find the correct pool address for a given pair of tokens, right? If so, I wouldn't bother. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Allow adding liquidity to StablePools via Router.
The
RouterV2
contract supports swaps along paths that includePair
andStablePool
types of pools.Swap Path
Each
swap*
method takes as the argumentspath: Vec<Step>
andtoken_out: AccountId
, except for theswap*_for_native
- here we know that thetoken_out
is always thewrapped_native
token.The
Step
struct has two fields:Step { token_in: AccountId, pool_id: AccountId }
. A valid path must consist of at least oneStep
.E.g
path: [Step(token_a, pool_1) , Step(token_b, pool_2) ], token_out: token_c
can be visualized like this:Compatibility
The old Router had a "pair caching" mechanism which allowed caching custom
Pair
contracts. Also, when adding liquidity to a non-existent pair, the pair was created via theFactory
contract.In the new Router, when adding liquidity to a
Pair
pool, the caller has an option to provide thePair
address. If the address is not provided, the contract calls theFactory
to create a new Pair. It fails ifPair
for given tokens already exists.The new Router caches pools solely for optimization purposes. It is allowed to swap through any valid pool contract (
Pair
orStablePool
). The pools are cached when swapping through or managing liquidity in a valid pool (permissionless).