From f6a9f8011f745950c5d0c1c2744c227d3ce058d4 Mon Sep 17 00:00:00 2001 From: Andrew Chiw Date: Tue, 21 Apr 2020 16:14:30 +0200 Subject: [PATCH] renamed one letter vars in abcurve --- abcurve.py | 95 ++++++++++++++++++++++--------------------------- abcurve_test.py | 46 +++++++++++++----------- 2 files changed, 67 insertions(+), 74 deletions(-) diff --git a/abcurve.py b/abcurve.py index cd51ba5..06c180b 100644 --- a/abcurve.py +++ b/abcurve.py @@ -1,76 +1,65 @@ -#value function for a given state (R,S) -def invariant(R,S,kappa): - return (S**kappa)/R +#value function for a given state (reserve,supply) +def invariant(reserve,supply,kappa): + return (supply**kappa)/reserve #given a value function (parameterized by kappa) -#and an invariant coeficient V0 -#return Supply S as a function of reserve R -def supply(R, kappa, V0): - return (V0*R)**(1/kappa) +#and an invariant coefficient invariant +#return Supply supply as a function of reserve +def supply(reserve, kappa, invariant): + return (invariant*reserve)**(1/kappa) #given a value function (parameterized by kappa) -#and an invariant coeficient V0 -#return a spot price P as a function of reserve R -def spot_price(R, kappa, V0): - return kappa*R**((kappa-1)/kappa)/V0**(1/kappa) +#and an invariant coeficient invariant +#return a spot price P as a function of reserve +def spot_price(reserve, kappa, invariant): + return kappa*reserve**((kappa-1)/kappa)/invariant**(1/kappa) -#for a given state (R,S) +#for a given state (reserve,supply) #given a value function (parameterized by kappa) -#and an invariant coeficient V0 -#deposit deltaR to Mint deltaS -#with realized price deltaR/deltaS -def mint(deltaR, R,S, kappa, V0): - deltaS = (V0*(R+deltaR))**(1/kappa)-S - realized_price = deltaR/deltaS - return deltaS, realized_price +#and an invariant coeficient invariant +#deposit d_reserve to Mint d_supply +#with realized price d_reserve/d_supply +def mint(d_reserve, reserve,supply, kappa, invariant): + d_supply = (invariant*(reserve+d_reserve))**(1/kappa)-supply + realized_price = d_reserve/d_supply + return d_supply, realized_price -#for a given state (R,S) +#for a given state (reserve,supply) #given a value function (parameterized by kappa) -#and an invariant coeficient V0 -#burn deltaS to Withdraw deltaR -#with realized price deltaR/deltaS -def withdraw(deltaS, R,S, kappa, V0): - deltaR = R-((S-deltaS)**kappa)/V0 - realized_price = deltaR/deltaS - return deltaR, realized_price +#and an invariant coeficient invariant +#burn d_supply to Withdraw d_reserve +#with realized price d_reserve/d_supply +def withdraw(d_supply, reserve,supply, kappa, invariant): + d_reserve = reserve-((supply-d_supply)**kappa)/invariant + realized_price = d_reserve/d_supply + return d_reserve, realized_price class AugmentedBondingCurve: - def __init__(self, initial_reserve, initial_token_supply, kappa=2): - """Create a stateful bonding curve. + def __init__(self, reserve_initial, token_supply_initial, kappa=2): + """Create a stateless bonding curve. - initial_reserve (millions of DAI) - initial_token_supply (millions) + reserve_initial (millions of DAI) + token_supply_initial (millions) kappa (the exponent part of the curve, default is 2) """ - self.initial_reserve = initial_reserve - self.initial_token_supply = initial_token_supply - - self.current_reserve = self.initial_reserve - self.current_token_supply = self.initial_token_supply - self.kappa = kappa - self.invariant = (self.initial_token_supply**kappa) / self.initial_reserve + self.invariant = invariant(reserve_initial, token_supply_initial, kappa) def __repr__(self): - return "ABC Reserve {} million; Token_Supply {} million; Current Token Price {}".format(self.current_reserve, self.current_token_supply, self.get_token_price()) + return "ABC Kappa: {}, Invariant: {}".format(self.kappa, self.invariant) - def deposit(self, dai_million): + def deposit(self, dai_millions, current_reserve, current_token_supply): # Returns number of new tokens minted, and their realized price - tokens, realized_price = mint(dai_million, self.current_reserve, self.current_token_supply, self.kappa, self.invariant) - - self.current_reserve += dai_million - self.current_token_supply += tokens + tokens, realized_price = mint(dai_millions, current_reserve, current_token_supply, self.kappa, self.invariant) return tokens, realized_price - def burn(self, tokens_million): + def burn(self, tokens_millions, current_reserve, current_token_supply): # Returns number of DAI that will be returned (excluding exit tribute) when the user burns their tokens, with their realized price - dai_million, realized_price = withdraw(tokens_million, self.current_reserve, self.current_token_supply, self.kappa, self.invariant) - self.current_reserve -= dai_million - self.current_token_supply -= tokens_million - return dai_million, realized_price + dai_millions, realized_price = withdraw(tokens_millions, current_reserve, current_token_supply, self.kappa, self.invariant) + return dai_millions, realized_price - def get_token_price(self): - return spot_price(self.current_reserve, self.kappa, self.invariant) + def get_token_price(self, current_reserve): + return spot_price(current_reserve, self.kappa, self.invariant) - def get_token_supply(self): - return supply(self.current_reserve, self.kappa, self.invariant) + def get_token_supply(self, current_reserve): + return supply(current_reserve, self.kappa, self.invariant) diff --git a/abcurve_test.py b/abcurve_test.py index b3745b7..7cde8f8 100644 --- a/abcurve_test.py +++ b/abcurve_test.py @@ -1,43 +1,47 @@ -from abcurve import AugmentedBondingCurve +from abcurve import AugmentedBondingCurve, invariant, supply, spot_price, mint, withdraw import unittest +class TestOriginalEquations(unittest.TestCase): + def test_magnitude_orders(self): + # The equations are supposed to be used with 1 (million). + # But this makes things impractical from a programming perspective because you don't want to remember which variable is denoted in units what. + # Hopefully things work just the same if you put in 1000,000,000 instead of 1e3. + r = 10000 + s = 1000000 + kappa = 2 + i = invariant(r, s, kappa) + print(i) + class TestAugmentedBondingCurve(unittest.TestCase): def test_get_token_supply(self): # R = 1, S0 = 1, V0 = 1.0, kappa = 2 should give this data: # [0 1 2 3 4 5 6 7 8 9] -> [0.0, 1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178, 2.6457513110645907, 2.8284271247461903, 3.0] abc = AugmentedBondingCurve(1, 1, kappa=2) - self.assertEqual(abc.get_token_supply(), 1) + self.assertEqual(abc.get_token_supply(1), 1) - abc.current_reserve = 2 - self.assertEqual(abc.get_token_supply(), 1.4142135623730951) + self.assertEqual(abc.get_token_supply(2), 1.4142135623730951) def test_get_token_price(self): # [0 1 2 3 4 5 6 7 8 9] -> [0.0, 0.02, 0.0282842712474619, 0.034641016151377546, 0.04, 0.044721359549995794, 0.04898979485566356, 0.052915026221291815, 0.0565685424949238, 0.06] abc = AugmentedBondingCurve(1, 1, kappa=2) - self.assertEqual(abc.get_token_price(), 2.0) + self.assertEqual(abc.get_token_price(1), 2.0) - abc.current_reserve = 2 - self.assertEqual(abc.get_token_price(), 2.8284271247461903) + self.assertEqual(abc.get_token_price(2), 2.8284271247461903) def test_deposit(self): abc = AugmentedBondingCurve(1, 1, kappa=2) - old_current_reserve = abc.current_reserve - old_token_supply = abc.current_token_supply - # print(abc) - tokens, realized_price = abc.deposit(4) - # print("The current price is", realized_price, "and you will get", tokens, "million tokens") - self.assertEqual(abc.current_token_supply, old_token_supply + tokens) - self.assertEqual(abc.current_reserve, old_current_reserve + 4) + old_current_reserve = 1 + old_token_supply = 1 # print(abc) + # Deposit 4 million DAI, given that the current reserve pool is 1 million DAI and there are 1 million tokens + tokens, realized_price = abc.deposit(4, 1, 1) + print("The current price is", realized_price, "and you will get", tokens, "million tokens") + self.assertEqual(tokens, 1.2360679774997898) + self.assertEqual(realized_price, 3.2360679774997894) def test_burn(self): abc = AugmentedBondingCurve(1, 1, kappa=2) - dai_million_returned, realized_price = abc.burn(0.5) + dai_million_returned, realized_price = abc.burn(0.5, 1, 1) self.assertEqual(dai_million_returned, 0.75) - self.assertEqual(realized_price, 1.5) - - self.assertEqual(abc.current_reserve, 0.25) - self.assertEqual(abc.current_token_supply, 0.5) - self.assertLess(abc.current_reserve, abc.initial_reserve) - self.assertLess(abc.current_token_supply, abc.initial_token_supply) \ No newline at end of file + self.assertEqual(realized_price, 1.5) \ No newline at end of file