Contract con_lending


Contract Code


  
1 I = importlib
2
3 import currency as tau
4 import con_lusd_lst001 as lusd
5
6 lending = Hash(default_value="")
7 approve = Hash(default_value="")
8 withdrawals = Hash(default_value="")
9
10 OPERATORS = [
11 "ae7d14d6d9b8443f881ba6244727b69b681010e782d4fe482dbfb0b6aca02d5d",
12 "1df02727c7cc50776e08532f088c17b31be0cb8a302df1c506c8c114a72c0b8c"
13 ]
14
15 @export
16 def supply(key: str, tau_amount: float, tau_fee_amount: float,
17 time_in_hours: int, lusd_amount_collateral: float, for_address: str = None):
18
19 assert not lending[key], 'Lending KEY already exists'
20 assert tau_amount > 0, "TAU amount must be > 0!"
21 assert tau_fee_amount > 0, "TAU fee amount must be > 0!"
22 assert time_in_hours > 0, "Timeframe must be > 0!"
23 assert lusd_amount_collateral > 0, "Collateral amount must be > 0!"
24
25 tau.transfer_from(amount=tau_amount, to=ctx.this, main_account=ctx.caller)
26
27 lending[key] = {
28 "tau_amount": tau_amount,
29 "tau_fee_amount": tau_fee_amount,
30 "time_in_hours": time_in_hours,
31 "lusd_amount_collateral": lusd_amount_collateral,
32 "for_address": for_address,
33 "start_date": now,
34 "end_date": now + datetime.timedelta(hours=time_in_hours),
35 "lender": ctx.caller,
36 "borrower": None,
37 "state": "OPEN"
38 }
39
40 @export
41 def borrow(key: str):
42 assert lending[key], 'KEY does not exist!'
43
44 data = lending[key]
45
46 if data["for_address"]:
47 assert data["for_address"] == ctx.caller, f"Reserved for {data['for_address']}"
48
49 assert data["state"] == "OPEN", f"Wrong state: {data['state']}"
50 assert data["end_date"] > now, "Lending enddate reached"
51
52 tau.transfer(amount=data["tau_amount"], to=ctx.caller)
53 lusd.transfer_from(amount=data["lusd_amount_collateral"], to=ctx.this, main_account=ctx.caller)
54
55 data["state"] = "BORROWED"
56 data["borrower"] = ctx.caller
57 lending[key] = data
58
59 @export
60 def payback(key: str):
61 assert lending[key], 'KEY does not exist!'
62
63 data = lending[key]
64
65 assert data["borrower"] == ctx.caller, "You are not the borrower"
66 assert data["end_date"] > now, "Enddate exceeded, colletaral lost"
67 assert data["state"] == "BORROWED", f"Wrong state: {data['state']}"
68
69 total_tau_amount = data["tau_amount"] + data["tau_fee_amount"]
70 tau.transfer_from(amount=total_tau_amount, to=data["lender"], main_account=ctx.caller)
71 lusd.transfer(amount=data["lusd_amount_collateral"], to=ctx.caller)
72
73 data["state"] = "RETURNED"
74 lending[key] = data
75
76 @export
77 def cancel(key: str):
78 assert lending[key], 'KEY does not exist!'
79
80 data = lending[key]
81
82 assert data["lender"] == ctx.caller, "You are not the lender"
83 assert data["state"] == "OPEN", f"Wrong state: {data['state']}"
84
85 tau.transfer(amount=data["tau_amount"], to=ctx.caller)
86
87 data["state"] = "CANCELED"
88 lending[key] = data
89
90 @export
91 def liquidate(key: str):
92 assert lending[key], 'KEY does not exist!'
93
94 data = lending[key]
95
96 assert data["lender"] == ctx.caller, "You are not the lender"
97 assert data["end_date"] < now, "Enddate not reached"
98 assert data["state"] == "BORROWED", f"Wrong state: {data['state']}"
99
100 lusd.transfer(amount=data["lusd_amount_collateral"], to=ctx.caller)
101
102 data["state"] = "LIQUIDATED"
103 lending[key] = data
104
105 @export
106 def agree_to_withdraw(contract: str, amount: str):
107 assert_caller_is_operator()
108
109 agreed = True
110 agreement = "agreed"
111 key = f"{contract}#{amount}"
112 withdrawals[key, ctx.caller] = agreement
113
114 for op in OPERATORS:
115 if withdrawals[key, op] != withdrawals[key, ctx.caller]:
116 agreed = False
117 break
118
119 if agreed:
120 withdrawals[key] = agreement
121
122 for op in OPERATORS:
123 withdrawals[key, op] = ""
124
125 return f'{key} = {agreement}'
126
127 @export
128 def withdraw(contract: str, amount: str):
129 assert_caller_is_operator()
130
131 key = f"{contract}#{amount}"
132
133 assert withdrawals[key] == "agreed", "No agreement met"
134
135 I.import_module(contract).transfer(amount=decimal(amount), to=ctx.caller)
136 withdrawals[key] = "done"
137
138 return f"{key} = {withdrawals[key]}"
139
140 def assert_caller_is_operator():
141 assert ctx.caller in OPERATORS, "Executable only by operator"
142

Byte Code

e3000000000000000000000000090000004000000073fc00000065005a01640064016c025a03640064016c045a05650664026403640464058d035a07650664026403640664058d035a08650664026403640764058d035a096408640967025a0a650b64038301641d650c650d650d650e650d650c640a9c06640b640c840583015a0f650b64038301650c640d9c01640e640f840483015a10650b64038301650c640d9c0164106411840483015a11650b64038301650c640d9c0164126413840483015a12650b64038301650c640d9c0164146415840483015a13650b64038301650c650c64169c0264176418840483015a14650b64038301650c650c64169c026419641a840483015a15641b641c84005a1664015300291ee9000000004eda00da0b636f6e5f6c656e64696e67da076c656e64696e672903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65da07617070726f7665da0b7769746864726177616c73da4061653764313464366439623834343366383831626136323434373237623639623638313031306537383264346665343832646266623062366163613032643564da40316466303237323763376363353037373665303835333266303838633137623331626530636238613330326466316335303663386331313461373263306238632906da036b6579da0a7461755f616d6f756e74da0e7461755f6665655f616d6f756e74da0d74696d655f696e5f686f757273da166c7573645f616d6f756e745f636f6c6c61746572616cda0b666f725f61646472657373630600000000000000060000000b00000043000000739600000074007c0019000c00731274016401830182017c0164026b04732274016403830182017c0264026b04733274016404830182017c0364026b04734274016405830182017c0464026b047352740164068301820174026a037c0174046a0574046a0664078d0301007c017c027c037c047c057407740774086a097c0364088d01170074046a0664006409640a9c0a74007c003c0064005300290b4e7a1a4c656e64696e67204b455920616c72656164792065786973747372010000007a1754415520616d6f756e74206d757374206265203e2030217a1b5441552066656520616d6f756e74206d757374206265203e2030217a1654696d656672616d65206d757374206265203e2030217a1e436f6c6c61746572616c20616d6f756e74206d757374206265203e2030212903da06616d6f756e74da02746fda0c6d61696e5f6163636f756e742901da05686f757273da044f50454e290a720d000000720e000000720f00000072100000007211000000da0a73746172745f64617465da08656e645f64617465da066c656e646572da08626f72726f776572da057374617465290ada095f5f6c656e64696e67da0e417373657274696f6e4572726f72da03746175da0d7472616e736665725f66726f6dda03637478da0474686973da0663616c6c6572da036e6f77da086461746574696d65da0974696d6564656c74612906720c000000720d000000720e000000720f00000072100000007211000000a90072260000007202000000da06737570706c790c000000731800000000031201100110011001100114010201040102010a010c0172270000002901720c00000063010000000000000002000000050000004300000073bc00000074007c0019007310740164018301820174007c0019007d017c016402190072407c016402190074026a036b027340740164037c01640219009b009d02830182017c016404190064056b02735e740164067c01640419009b009d02830182017c016407190074046b047372740164088301820174056a067c016409190074026a03640a8d02010074076a087c01640b190074026a0974026a03640c8d030100640d7c0164043c0074026a037c01640e3c007c0174007c003c0064005300290f4e7a134b455920646f6573206e6f742065786973742172110000007a0d526573657276656420666f7220721b00000072160000007a0d57726f6e672073746174653a2072180000007a174c656e64696e6720656e64646174652072656163686564720d00000029027212000000721300000072100000002903721200000072130000007214000000da08424f52524f574544721a000000290a721c000000721d000000722000000072220000007223000000721e000000da087472616e73666572da046c757364721f00000072210000002902720c000000da0464617461722600000072260000007202000000da06626f72726f771d000000731a000000000210010801080106011a011e01140114010e010a0108010a01722c00000063010000000000000003000000050000004300000073ae00000074007c0019007310740164018301820174007c0019007d017c016402190074026a036b02732e74016403830182017c016404190074046b04734274016405830182017c016406190064076b027360740164087c01640619009b009d02830182017c01640919007c01640a190017007d0274056a067c027c01640b190074026a03640c8d03010074076a087c01640d190074026a03640e8d020100640f7c0164063c007c0174007c003c006400530029104e7a134b455920646f6573206e6f7420657869737421721a0000007a18596f7520617265206e6f742074686520626f72726f77657272180000007a21456e64646174652065786365656465642c20636f6c6c65746172616c206c6f7374721b00000072280000007a0d57726f6e672073746174653a20720d000000720e000000721900000029037212000000721300000072140000007210000000290272120000007213000000da0852455455524e45442909721c000000721d000000722000000072220000007223000000721e000000721f000000722a00000072290000002903720c000000722b000000da10746f74616c5f7461755f616d6f756e74722600000072260000007202000000da077061796261636b2e0000007316000000000210010801160114011e0110010c010a0114010801722f000000630100000000000000020000000400000043000000737400000074007c0019007310740164018301820174007c0019007d017c016402190074026a036b02732e74016403830182017c016404190064056b02734c740164067c01640419009b009d028301820174046a057c016407190074026a0364088d02010064097c0164043c007c0174007c003c0064005300290a4e7a134b455920646f6573206e6f742065786973742172190000007a16596f7520617265206e6f7420746865206c656e646572721b00000072160000007a0d57726f6e672073746174653a20720d000000290272120000007213000000da0843414e43454c45442906721c000000721d00000072200000007222000000721e00000072290000002902720c000000722b000000722600000072260000007202000000da0663616e63656c3d000000730e00000000021001080116011e01140108017231000000630100000000000000020000000400000043000000738800000074007c0019007310740164018301820174007c0019007d017c016402190074026a036b02732e74016403830182017c016404190074046b00734274016405830182017c016406190064076b027360740164087c01640619009b009d028301820174056a067c016409190074026a03640a8d020100640b7c0164063c007c0174007c003c0064005300290c4e7a134b455920646f6573206e6f742065786973742172190000007a16596f7520617265206e6f7420746865206c656e64657272180000007a13456e6464617465206e6f742072656163686564721b00000072280000007a0d57726f6e672073746174653a207210000000290272120000007213000000da0a4c4951554944415445442907721c000000721d000000722000000072220000007223000000722a00000072290000002902720c000000722b000000722600000072260000007202000000da096c6971756964617465480000007310000000000210010801160114011e01140108017233000000290272060000007212000000630200000000000000060000000500000043000000739000000074008300010064017d0264027d037c009b0064037c019b009d037d047c0374017c0474026a0366023c00782c740444005d247d0574017c047c056602190074017c0474026a03660219006b03723064047d025000713057007c02728c7c0374017c043c007818740444005d107d05640574017c047c0566023c00716a57007c049b0064067c039b009d0353006400530029074e54da06616772656564fa01234672020000007a03203d202905da1b5f5f6173736572745f63616c6c65725f69735f6f70657261746f72da0d5f5f7769746864726177616c7372200000007222000000da094f50455241544f52532906720600000072120000007234000000da0961677265656d656e74720c000000da026f70722600000072260000007202000000da1161677265655f746f5f776974686472617754000000731c00000000020601040104010e010e010a011a0104010601040108010a011001723b000000630200000000000000030000000400000043000000735c0000007400830001007c009b0064017c019b009d037d0274017c02190064026b027328740264038301820174036a047c0083016a0574067c01830174076a0864048d020100640574017c023c007c029b00640674017c0219009b009d03530029074e723500000072340000007a104e6f2061677265656d656e74206d6574290272120000007213000000da04646f6e657a03203d20290972360000007237000000721d000000da0149da0d696d706f72745f6d6f64756c657229000000da07646563696d616c72200000007222000000290372060000007212000000720c000000722600000072260000007202000000da08776974686472617766000000730c000000000206010e0114011a0108017240000000630000000000000000000000000200000043000000731600000074006a0174026b06731274036401830182016400530029024e7a1b45786563757461626c65206f6e6c79206279206f70657261746f722904722000000072220000007238000000721d000000722600000072260000007226000000720200000072360000007000000073020000000001723600000029014e2917da09696d706f72746c6962723d000000da0863757272656e6379721e000000da0f636f6e5f6c7573645f6c7374303031722a000000da0448617368721c000000da095f5f617070726f766572370000007238000000da085f5f6578706f7274da03737472da05666c6f6174da03696e747227000000722c000000722f00000072310000007233000000723b000000724000000072360000007226000000722600000072260000007202000000da083c6d6f64756c653e01000000732e0000000401080108010e010e01060108010201060306021c0f060110100601100e0601100a0601100b0601121106011209