Contract con_altswap


Contract Code


  
1 I = importlib
2 token_interface = [I.Func('transfer', args=('amount', 'to')), I.Func(
3 'approve', args=('amount', 'to')), I.Func('transfer_from', args=(
4 'amount', 'to', 'main_account'))]
5
6 pools = Hash(default_value=False)
7 pools_creation_values = Hash(default_value=False)
8 pool_weights = Hash(default_value=False)
9 lp_tokens_users = Hash(default_value=0.0)
10 lp_tokens_total = Hash(default_value=0.0)
11 lp_tokens_approvals = Hash(default_value=0.0)
12
13 fee = Variable()
14 operator = Variable()
15
16 @construct
17 def seed():
18 fee.set(0.4)
19 operator.set("ff61544ea94eaaeb5df08ed863c4a938e9129aba6ceee5f31b6681bdede11b89")
20
21 @export
22 def createWeightedPool(poolName: str, assets: dict, weights: dict):
23 assert pools[poolName] == False, "A pool with this name already exists"
24 sum_assets = 0
25 for contract, amount in assets.items():
26 assert amount > 0, "Amounts need to be more then 0"
27 sum_assets += amount
28 for contract, amount in assets.items():
29 assert amount == sum_assets / 100 * (weights[contract] * 100), "Token Amount and Weights are not matching"
30 for contract, amount in assets.items():
31 token = I.import_module(contract)
32 assert I.enforce_interface(token, token_interface
33 ), f'Invalid token interface for {contract}!'
34 token.transfer_from(amount=amount, to=ctx.this, main_account=ctx.caller)
35 pools[poolName, contract] = amount
36 pools_creation_values[poolName, contract] = amount
37 pool_weights[poolName, contract] = weights[contract]
38 lp_tokens_users[poolName, ctx.caller] = 100
39 lp_tokens_total[poolName] = 100
40
41 return poolName
42
43
44 @export
45 def addLiquidity(poolName: str, assets: dict):
46 sum_assets_deposit = 0
47 sum_assets = 0
48 for contract, amount in assets.items():
49 assert amount > 0, "Amounts need to be more then 0"
50 sum_assets_deposit += amount
51 sum_assets += pools_creation_values[poolName, contract]
52 for contract,amount in assets.items():
53 assert amount == sum_assets_deposit / 100 * (pool_weights[poolName, contract] * 100), "Token Amount and Weights are not matching"
54 for contract, amount in assets.items():
55 token = I.import_module(contract)
56 assert I.enforce_interface(token, token_interface
57 ), f'Invalid token interface for {contract}!'
58 token.transfer_from(amount=amount, to=ctx.this, main_account=ctx.caller)
59 pools[poolName, contract] += amount
60
61 total_lp_points = lp_tokens_total[poolName]
62 lp_to_mint = (sum_assets_deposit / sum_assets) * 100
63 lp_tokens_total[poolName] += lp_to_mint
64 lp_tokens_users[poolName, ctx.caller] += lp_to_mint
65
66 return lp_to_mint
67
68
69 @export
70 def removeLiquidity(poolName: str, amountLPTokens: float):
71 assert amountLPTokens <= lp_tokens_users[poolName, ctx.caller], "You don't own that many LP points"
72 assert lp_tokens_total[poolName] - amountLPTokens > 1, "You need to leave atleast 1 LP point"
73 assert amountLPTokens > 0, "Needs to more then 0"
74 total_lp_points = lp_tokens_total[poolName]
75 for contract in pools[poolName].all():
76 amount = pools[poolName, contract] / total_lp_points * amountLPTokens
77 token.transfer(amount=amount, to=ctx.caller)
78 pools[poolName, contract] -= amount
79 lp_tokens_total[poolName] -= amountLPTokens
80 lp_tokens_users[poolName, ctx.caller] -= amountLPTokens
81
82
83 @export
84 def transferLiquidity(poolName: str, amountLPTokens: float, to: str):
85 assert amountLPTokens <= lp_tokens_users[poolName, ctx.caller], "You don't own that many LP points"
86 assert amountLPTokens > 0, "Needs to more then 0"
87 lp_tokens_users[poolName, ctx.caller] -= amountLPTokens
88 lp_tokens_users[poolName, to] += amountLPTokens
89
90 @export
91 def approveLiquidity(poolName: str, amountLPTokens: float, main_account: str, to: str):
92 assert amountLPTokens <= lp_tokens_users[poolName, ctx.caller], "You don't own that many LP points"
93 assert amountLPTokens > 0, "Needs to more then 0"
94 lp_tokens_approvals[poolName, ctx.caller, to] += amountLPTokens
95
96 @export
97 def transferLiquidityFrom(poolName: str, amountLPTokens: float, main_account: str, to: str):
98 assert amountLPTokens <= lp_tokens_users[poolName, main_account], "Not enough LP points to send"
99 assert amountLPTokens > 0, "Needs to more then 0"
100 lp_tokens_users[poolName, main_account] -= amountLPTokens
101 lp_tokens_users[poolName, to] += amountLPTokens
102 lp_tokens_approvals[poolName, main_account, to] -= amountLPTokens
103
104
105 @export
106 def swap(poolName: str, amountFrom: float, contractFrom: str, contractTo: str):
107 assert amountFrom > 0, "Needs to more then 0"
108 fee_amount = amountFrom/100*fee.get()
109 swap_amount = amountFrom - fee_amount
110 local_balances = [pools[poolName, contractFrom], pools[poolName, contractTo]]
111 local_weights = [pool_weights[poolName, contractFrom], pool_weights[poolName, contractTo]]
112 final_amount = calc_out_given_in(local_balances[0], local_weights[0], local_balances[1], local_weights[1], swap_amount)
113 fromToken = I.import_module(contractFrom)
114 toToken = I.import_module(contractTo)
115 fromToken.transfer_from(amount=swap_amount, to=ctx.this, main_account=ctx.caller)
116 fromToken.transfer_from(amount=fee_amount, to=operator.get(), main_account=ctx.caller)
117 pools[poolName, contractFrom] += swap_amount
118 toToken.transfer(amount=final_amount, to= ctx.caller)
119 pools[poolName, contractTo] -= swap_amount
120 return final_amount
121
122
123 def calc_out_given_in(balance_in: decimal,
124 weight_in: decimal,
125 balance_out: decimal,
126 weight_out: decimal,
127 amount_in: decimal):
128
129 denominator = balance_in + amount_in
130 base = div(balance_in, denominator)
131 exponent = div(weight_in, weight_out)
132 power = powerIt(base, exponent)
133
134 return mul(balance_out, complement(power))
135
136
137 def mul(a: decimal, b: decimal):
138 return a*b
139
140
141 def div(a: decimal, b: decimal):
142 result = a/b
143 return result
144
145
146 def complement(a: decimal):
147 return (1 - a) if a < 1 else 0
148
149
150 def powerIt(a: decimal,b:decimal):
151 return a**b
152
153
154
155

Byte Code

e3000000000000000000000000070000004000000073bc01000065005a0165016a026400643864038d0265016a026404643964038d0265016a026405643a64038d0267035a036504640764086409640a8d035a05650464076408640b640a8d035a06650464076408640c640a8d035a0765046508640d83016408640e640a8d035a0965046508640d83016408640f640a8d035a0a65046508640d830164086410640a8d035a0b650c6408641164128d025a0d650c6408641364128d025a0e6414641584005a0f65106408830165116512651264169c0364176418840483015a136510640883016511651264199c02641a641b840483015a1465106408830165116515641c9c02641d641e840483015a16651064088301651165156511641f9c0364206421840483015a17651064088301651165156511651164229c0464236424840483015a18651064088301651165156511651164229c0464256426840483015a19651064088301651165156511651164279c0464286429840483015a1a65086508650865086508642a9c05642b642c84045a1b65086508642d9c02642e642f84045a1c65086508642d9c026430643184045a1d650864329c016433643484045a1e65086508642d9c026435643684045a1f64375300293bda087472616e73666572da06616d6f756e74da02746f2901da0461726773da07617070726f7665da0d7472616e736665725f66726f6dda0c6d61696e5f6163636f756e7446da0b636f6e5f616c7473776170da05706f6f6c732903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65da15706f6f6c735f6372656174696f6e5f76616c756573da0c706f6f6c5f776569676874737a03302e30da0f6c705f746f6b656e735f7573657273da0f6c705f746f6b656e735f746f74616cda136c705f746f6b656e735f617070726f76616c73da036665652902720b000000720c000000da086f70657261746f72630000000000000000000000000300000043000000731c00000074006a017402640183018301010074036a016402830101006400530029034e7a03302e34da40666636313534346561393465616165623564663038656438363363346139333865393132396162613663656565356633316236363831626465646531316238392904da055f5f666565da03736574da07646563696d616cda0a5f5f6f70657261746f72a90072190000007219000000da00da045f5f5f5f14000000730600000000010e010401721b0000002903da08706f6f6c4e616d65da06617373657473da0777656967687473630300000000000000070000000600000043000000730e01000074007c00190064016b027314740164028301820164037d03782c7c016a02830044005d205c027d047d057c0564036b04733a74016404830182017c037c0537007d037122570078347c016a02830044005d285c027d047d057c057c0364051b007c027c0419006405140014006b02735074016406830182017150570078767c016a02830044005d6a5c027d047d0574036a047c0483017d0674036a057c067406830273b4740164077c049b0064089d03830182017c066a077c0574086a0974086a0a64098d0301007c0574007c007c0466023c007c05740b7c007c0466023c007c027c041900740c7c007c0466023c00718657006405740d7c0074086a0a66023c006405740e7c003c007c005300290a4e467a244120706f6f6c20776974682074686973206e616d6520616c726561647920657869737473e9000000007a1e416d6f756e7473206e65656420746f206265206d6f7265207468656e2030e9640000007a29546f6b656e20416d6f756e7420616e64205765696768747320617265206e6f74206d61746368696e677a1c496e76616c696420746f6b656e20696e7465726661636520666f7220fa01212903720200000072030000007207000000290fda075f5f706f6f6c73da0e417373657274696f6e4572726f72da056974656d73da0149da0d696d706f72745f6d6f64756c65da11656e666f7263655f696e74657266616365da0f746f6b656e5f696e746572666163657206000000da03637478da0474686973da0663616c6c6572da175f5f706f6f6c735f6372656174696f6e5f76616c756573da0e5f5f706f6f6c5f77656967687473da115f5f6c705f746f6b656e735f7573657273da115f5f6c705f746f6b656e735f746f74616c2907721c000000721d000000721e000000da0a73756d5f617373657473720b0000007202000000da05746f6b656e72190000007219000000721a000000da126372656174655765696768746564506f6f6c1a0000007326000000000214010401120110010c0112011a010a0112010a010e010e0114020c010c0114010e01080172320000002902721c000000721d000000630200000000000000090000000600000043000000732201000064017d0264017d03783c7c016a00830044005d305c027d047d057c0564016b04732a74016402830182017c027c0537007d027c0374027c007c046602190037007d037112570078387c016a00830044005d2c5c027d047d057c057c0264031b0074037c007c04660219006403140014006b02735074016404830182017150570078627c016a00830044005d565c027d047d0574046a057c0483017d0674046a067c067407830273b8740164057c049b0064069d03830182017c066a087c0574096a0a74096a0b64078d030100740c7c007c046602050019007c05370003003c00718a5700740d7c0019007d077c027c031b00640314007d08740d7c00050019007c08370003003c00740e7c0074096a0b6602050019007c08370003003c007c08530029084e721f0000007a1e416d6f756e7473206e65656420746f206265206d6f7265207468656e203072200000007a29546f6b656e20416d6f756e7420616e64205765696768747320617265206e6f74206d61746368696e677a1c496e76616c696420746f6b656e20696e7465726661636520666f722072210000002903720200000072030000007207000000290f72240000007223000000722c000000722d000000722500000072260000007227000000722800000072060000007229000000722a000000722b0000007222000000722f000000722e0000002909721c000000721d000000da1273756d5f6173736574735f6465706f7369747230000000720b00000072020000007231000000da0f746f74616c5f6c705f706f696e7473da0a6c705f746f5f6d696e7472190000007219000000721a000000da0c6164644c697175696469747932000000732a000000000204010401120110010801140112010a0114010a0112010a010e010e011402180108010c011001160172360000002902721c000000da0e616d6f756e744c50546f6b656e7363020000000000000005000000050000004300000073c20000007c0174007c0074016a02660219006b01731a740364018301820174047c0019007c01180064026b04733274036403830182017c0164046b047342740364058301820174047c0019007d02787274057c0019006a06830044005d627d0374057c007c03660219007c021b007c0114007d0474076a087c0474016a0264068d02010074057c007c036602050019007c04380003003c0074047c00050019007c01380003003c0074007c0074016a026602050019007c01380003003c00715857006400530029074e7a21596f7520646f6e2774206f776e2074686174206d616e79204c5020706f696e7473e9010000007a24596f75206e65656420746f206c656176652061746c656173742031204c5020706f696e74721f0000007a144e6565647320746f206d6f7265207468656e20302902720200000072030000002909722e0000007229000000722b0000007223000000722f0000007222000000da03616c6c723100000072010000002905721c00000072370000007234000000720b000000720200000072190000007219000000721a000000da0f72656d6f76654c69717569646974794c0000007318000000000214010601060112011001080112011401100114011001723a0000002903721c0000007237000000720300000063030000000000000003000000040000004300000073580000007c0174007c0074016a02660219006b01731a74036401830182017c0164026b04732a740364038301820174007c0074016a026602050019007c01380003003c0074007c007c026602050019007c01370003003c006400530029044e7a21596f7520646f6e2774206f776e2074686174206d616e79204c5020706f696e7473721f0000007a144e6565647320746f206d6f7265207468656e20302904722e0000007229000000722b00000072230000002903721c0000007237000000720300000072190000007219000000721a000000da117472616e736665724c69717569646974795c000000730a00000000021401060110011601723b0000002904721c00000072370000007207000000720300000063040000000000000004000000040000004300000073460000007c0174007c0074016a02660219006b01731a74036401830182017c0164026b04732a740364038301820174047c0074016a027c036603050019007c01370003003c006400530029044e7a21596f7520646f6e2774206f776e2074686174206d616e79204c5020706f696e7473721f0000007a144e6565647320746f206d6f7265207468656e20302905722e0000007229000000722b0000007223000000da155f5f6c705f746f6b656e735f617070726f76616c732904721c00000072370000007207000000720300000072190000007219000000721a000000da10617070726f76654c69717569646974796500000073080000000003140106011001723d000000630400000000000000040000000400000043000000736a0000007c0174007c007c02660219006b01731874016401830182017c0164026b047328740164038301820174007c007c026602050019007c01380003003c0074007c007c036602050019007c01370003003c0074027c007c027c036603050019007c01380003003c006400530029044e7a1c4e6f7420656e6f756768204c5020706f696e747320746f2073656e64721f0000007a144e6565647320746f206d6f7265207468656e20302903722e0000007223000000723c0000002904721c00000072370000007207000000720300000072190000007219000000721a000000da157472616e736665724c697175696469747946726f6d6e000000730c000000000312010601100114011401723e0000002904721c000000da0a616d6f756e7446726f6dda0c636f6e747261637446726f6dda0a636f6e7472616374546f6304000000000000000b000000060000004300000073f20000007c0164016b04731074006402830182017c0164031b0074016a02830014007d047c017c0418007d0574037c007c026602190074037c007c036602190067027d0674047c007c026602190074047c007c036602190067027d0774057c06640119007c07640119007c06640419007c07640419007c0583057d0874066a077c0283017d0974066a077c0383017d0a7c096a087c0574096a0a74096a0b64058d0301007c096a087c04740c6a02830074096a0b64058d03010074037c007c026602050019007c05370003003c007c0a6a0d7c0874096a0b64068d02010074037c007c036602050019007c05380003003c007c08530029074e721f0000007a144e6565647320746f206d6f7265207468656e2030722000000072380000002903720200000072030000007207000000290272020000007203000000290e72230000007215000000da036765747222000000722d000000da135f5f63616c635f6f75745f676976656e5f696e7225000000722600000072060000007229000000722a000000722b00000072180000007201000000290b721c000000723f00000072400000007241000000da0a6665655f616d6f756e74da0b737761705f616d6f756e74da0e6c6f63616c5f62616c616e636573da0d6c6f63616c5f77656967687473da0c66696e616c5f616d6f756e74da0966726f6d546f6b656eda07746f546f6b656e72190000007219000000721a000000da047377617079000000732600000000021001100108010e010a010c010c010e0112010a010a010a010a010c010a01140110011401724b0000002905da0a62616c616e63655f696eda097765696768745f696eda0b62616c616e63655f6f7574da0a7765696768745f6f7574da09616d6f756e745f696e63050000000000000009000000040000004300000073340000007c007c0417007d0574007c007c0583027d0674007c017c0383027d0774017c067c0783027d0874027c0274037c0883018302530029014e2904da055f5f646976da095f5f706f7765724974da055f5f6d756cda0c5f5f636f6d706c656d656e742909724c000000724d000000724e000000724f0000007250000000da0b64656e6f6d696e61746f72da0462617365da086578706f6e656e74da05706f77657272190000007219000000721a000000724300000090000000730a000000000208010a010a010a0172430000002902da0161da016263020000000000000002000000020000004300000073080000007c007c011400530029014e721900000029027259000000725a00000072190000007219000000721a000000725300000099000000730200000000017253000000630200000000000000030000000200000043000000730c0000007c007c011b007d027c02530029014e721900000029037259000000725a000000da06726573756c7472190000007219000000721a00000072510000009d00000073040000000001080172510000002901725900000063010000000000000001000000020000004300000073140000007c0064016b00721064017c00180053006402530029034e7238000000721f00000072190000002901725900000072190000007219000000721a0000007254000000a200000073020000000001725400000063020000000000000002000000020000004300000073080000007c007c011300530029014e721900000029027259000000725a00000072190000007219000000721a0000007252000000a60000007302000000000172520000004e29027202000000720300000029027202000000720300000029037202000000720300000072070000002920da09696d706f72746c69627225000000da0446756e637228000000da04486173687222000000722c000000722d0000007217000000722e000000722f000000723c000000da085661726961626c6572150000007218000000721b000000da085f5f6578706f7274da03737472da046469637472320000007236000000da05666c6f6174723a000000723b000000723d000000723e000000724b00000072430000007253000000725100000072540000007252000000721900000072190000007219000000721a000000da083c6d6f64756c653e01000000734e000000040110010e010a010e01060108010601080108010a0108010a0108010a010c010c03080606011417060112190601120f060114080601040112070601040112090601161604011208100410050e04