Skip to content
This repository was archived by the owner on Dec 7, 2022. It is now read-only.
This repository was archived by the owner on Dec 7, 2022. It is now read-only.

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

@tomekozlowski

Description

@tomekozlowski

I've made a simplest python test based on the formulas here:

import math as m

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


a1 = r01_0
b1 = r01_1

a2 = r10_0
b2 = r10_1

a = a1 * b1 - a2 * b2
b = 2 * b1 * b2 * (a1 + a2)
c = b1 * b2 * (a1 * b2 - a2 * b1)

m_ = b**2 - 4 * a * c

sqrtM = m.sqrt(m_);

x1 = (-b + sqrtM) / (2 * a);
x2 = (-b - sqrtM) / (2 * a);


if x1 > 0 and x1 < b1 and x1 < b2:
    amount = x1
else:
    amount = x2

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


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

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


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

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


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

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

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

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

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

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

Output of this:

opt_in: -176602540378.44
amnz out:  10602145808.88565
amnz out:  10183149306.464203
profitz:  186785689684.90808
[!!] verifying +
if amounts were higher by 1000, the profits should be less!
profits are...
...186785688686.02304
[!!] verifying -
if amounts were lower by 1000, the profits should be less!
profits are...
...186785690683.79312

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions