ccyanxyz / uniswap-arbitrage-analysis

Uniswap arbitrage problem analysis
1.89k stars 581 forks source link

Formula for optimal in incorrect, doesn't pass simple test. #15

Open tomekozlowski opened 2 years ago

tomekozlowski commented 2 years ago

Formulas extracted from functions getEaEb() and getOptimalAmount(). [btw, python should have snake case naming, pls contain the pokemon syntax disease inside JS ecosystem]

r01_0 = 10*10**9
r01_1 = 10*10**9
r10_0 =  5*10**9
r10_1 = 15*10**9

Ea_1= (1000 * r01_0 * r10_1) / (1000 * r10_1) + (997 * r01_1); 
Eb_1= (997  * r01_1 * r10_0) / (1000 * r10_1) + (997 * r01_1);

import math as m

optimal_in = m.sqrt(Ea_1 * Eb_1 * 997 * 1000)
optimal_in = abs((optimal_in - (Ea_1 * 1000)) / 997)

print( f'opt_in: {optimal_in:.2f}')

def getAmountOut(
    amountIn,
    reserveIn,
    reserveOut
):
    amountInWithFee = amountIn * 997
    numerator = amountInWithFee * reserveOut
    denominator = (reserveIn * 1000) + amountInWithFee
    amountOut = numerator / denominator
    return amountOut

amountz1 = getAmountOut(optimal_in, r01_0, r01_1)
amountz2 = getAmountOut(amountz1, r10_0, r10_1)

print("amnz out: ", amountz1)
print("amnz out: ", amountz2)

pr = amountz2 - optimal_in
print("profitz: ", pr)

print("[!!] verifying +")
print("if amounts were higher by 1000, the profits should be less!")
print("profits are...")

amountz1 = getAmountOut(optimal_in+1000, r01_0, r01_1)
amountz2 = getAmountOut(amountz1, r10_0, r10_1)
pr = amountz2 - (optimal_in+1000)

print(f"...{pr}")

print("[!!] verifying -")
print("if amounts were lower by 1000, the profits should be less!")
print("profits are...")

amountz1 = getAmountOut(optimal_in-1000, r01_0, r01_1)
amountz2 = getAmountOut(amountz1, r10_0, r10_1)
pr = amountz2 - (optimal_in-1000)

print(f"...{pr}")

Output:

opt_in: 18370234838.05
amnz out:  6468318502.5644455
amnz out:  8449160071.675033
profitz:  -9921074766.375118
[!!] verifying +
if amounts were higher by 1000, the profits should be less!
profits are...
...-9921075695.435955
[!!] verifying -
if amounts were lower by 1000, the profits should be less!
profits are...
...-9921073837.31429
jeanbmar commented 1 year ago

Your transcriptions of Ea_1 and Eb_1 are wrong, check parenthesis