Skip to content

Commit 0f50a8d

Browse files
authored
RouterV2 (#117)
The `RouterV2` contract supports swaps along paths that include `Pair` and `StablePool` types of pools. ### Swap Path Each `swap*` method takes as the arguments `path: Vec<Step>` and `token_out: AccountId`, except for the `swap*_for_native` - here we know that the `token_out` is always the `wrapped_native` token. The `Step` struct has two fields: `Step { token_in: AccountId, pool_id: AccountId }`. A valid path must consist of at least one `Step`. E.g `path: [Step(token_a, pool_1) , Step(token_b, pool_2) ], token_out: token_c` can be visualized like this: ```shell pool_1 pool_2 / \ / \ token_a -> token_b -> token_c ``` ### 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 the `Factory` contract. In the new Router, when adding liquidity to a `Pair` pool, the caller has an option to provide the `Pair` address. If the address is not provided, the contract calls the `Factory` to create a new Pair. It fails if `Pair` 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` or `StablePool`). The pools are cached when swapping through or managing liquidity in a valid pool (permissionless).
2 parents 3923451 + a09a8b2 commit 0f50a8d

File tree

15 files changed

+3146
-9
lines changed

15 files changed

+3146
-9
lines changed

Cargo.lock

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

amm/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ help: # Show help for each of the Makefile recipes.
77
AMM_CONTRACTS = ./contracts
88
AMM_CONTRACTS_PATHS := $(shell find $(AMM_CONTRACTS) -mindepth 1 -maxdepth 1 -type d)
99

10-
CONTRACTS := factory_contract pair_contract router_contract stable_pool_contract mock_rate_provider_contract
10+
CONTRACTS := factory_contract pair_contract router_contract router_v2_contract stable_pool_contract mock_rate_provider_contract
1111

1212
INK_DEV_IMAGE := "public.ecr.aws/p6e8q1z1/ink-dev:2.1.0"
1313
SCRIPT_DIR := $(shell cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

amm/contracts/router_v2/Cargo.toml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[package]
2+
name = "router_v2_contract"
3+
version = "0.1.0"
4+
authors = ["Cardinal Cryptography"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
ink = { version = "=4.3.0", default-features = false }
9+
10+
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = [
11+
"derive",
12+
] }
13+
scale-info = { version = "2.9", default-features = false, features = [
14+
"derive",
15+
], optional = true }
16+
17+
psp22 = { version = "=0.2.2", default-features = false }
18+
wrapped-azero = { git = "https://github.com/Cardinal-Cryptography/wAZERO.git", default-features = false, features = [
19+
"ink-as-dependency",
20+
] }
21+
22+
traits = { path = "../../traits", default-features = false }
23+
amm-helpers = { path = "../../../helpers", default-features = false }
24+
25+
[lib]
26+
name = "router_v2_contract"
27+
path = "lib.rs"
28+
29+
[features]
30+
default = ["std"]
31+
std = [
32+
"ink/std",
33+
"psp22/std",
34+
"scale/std",
35+
"scale-info",
36+
"scale-info/std",
37+
"traits/std",
38+
]
39+
ink-as-dependency = []

0 commit comments

Comments
 (0)