-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a1e06b
commit 80ff459
Showing
2 changed files
with
36 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,32 @@ | ||
def get_amount_out(amount_in, reserve_in, reserve_out): | ||
# Uniswap V2 的費率是0.3%,所以費後金額是997份 | ||
amount_in_with_fee = amount_in * 997 | ||
numerator = amount_in_with_fee * reserve_out | ||
denominator = reserve_in * 1000 + amount_in_with_fee | ||
amount_out = numerator / denominator | ||
return amount_out | ||
|
||
# 定義流動性池 | ||
liquidity = { | ||
("tokenA", "tokenB"): (17, 10), | ||
("tokenA", "tokenC"): (11, 7), | ||
("tokenA", "tokenD"): (15, 9), | ||
("tokenA", "tokenE"): (21, 5), | ||
("tokenB", "tokenC"): (36, 4), | ||
("tokenB", "tokenD"): (13, 6), | ||
("tokenB", "tokenE"): (25, 3), | ||
("tokenC", "tokenD"): (30, 12), | ||
("tokenC", "tokenE"): (10, 8), | ||
("tokenD", "tokenE"): (60, 25), | ||
("tokenB", "tokenA"): (10, 17), # tokenB to tokenA 的流動性 | ||
("tokenA", "tokenD"): (15, 9), # tokenA to tokenD 的流動性 | ||
("tokenD", "tokenB"): (6, 13), # tokenD to tokenB 的流動性 | ||
} | ||
|
||
# 初始代幣及數量 | ||
initial_amount = 5 | ||
|
||
# 交易路徑 | ||
path = [ | ||
("tokenB", "tokenA"), | ||
("tokenA", "tokenD"), | ||
("tokenD", "tokenB"), | ||
] | ||
|
||
# 按照路徑交易 | ||
amount = initial_amount | ||
for (from_token, to_token) in path: | ||
reserve_in, reserve_out = liquidity[(from_token, to_token)] | ||
amount = get_amount_out(amount, reserve_in, reserve_out) | ||
|
||
print(f"Final path: {'->'.join([p[0] for p in path])}->{path[-1][1]}, tokenB balance={amount}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters