Skip to content

Commit

Permalink
描述性訊息
Browse files Browse the repository at this point in the history
  • Loading branch information
rayliao1031 committed Apr 3, 2024
1 parent 3a1e06b commit 80ff459
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
40 changes: 30 additions & 10 deletions Arbitrage.py
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}")
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ Please complete the report problem below:
Provide your profitable path, the amountIn, amountOut value for each swap, and your final reward (your tokenB balance).

> Solution
>tokenB->tokenA->tokenD->tokenB
> Swap details:
> tokenB->tokenA: amountIn = 5.0, amountOut = 5.655321988655322
> tokenA->tokenD: amountIn = 5.655321988655322, amountOut = 2.4587813170979333
> tokenD->tokenB: amountIn = 2.4587813170979333, amountOut = 3.7707653049598298
> Final reward (tokenB balance): 3.7707653049598298
## Problem 2
What is slippage in AMM, and how does Uniswap V2 address this issue? Please illustrate with a function as an example.
Expand Down

0 comments on commit 80ff459

Please sign in to comment.