Contract con_lend_test3


Contract Code


  
1 import currency as tau
2 import con_lusd_lst001 as lusd
3
4 lending = Hash(default_value="")
5 approve = Hash(default_value="")
6
7 @export
8 def supply(key: str, tau_amount: float, tau_fee_amount: float,
9 time_in_hours: int, lusd_amount_collateral: float, for_address: str = None):
10
11 assert not lending[key], 'Lending KEY already exists'
12 assert tau_amount > 0, "TAU amount must be > 0!"
13 assert tau_fee_amount > 0, "TAU fee amount must be > 0!"
14 assert time_in_hours > 0, "Timeframe must be > 0!"
15 assert lusd_amount_collateral > 0, "Collateral amount must be > 0!"
16
17 tau.transfer_from(amount=tau_amount, to=ctx.this, main_account=ctx.caller)
18
19 lending[key] = {
20 "tau_amount": tau_amount,
21 "tau_fee_amount": tau_fee_amount,
22 "time_in_hours": time_in_hours,
23 "lusd_amount_collateral": lusd_amount_collateral,
24 "for_address": for_address,
25 "start_date": now,
26 "end_date": now + datetime.timedelta(hours=time_in_hours),
27 "lender": ctx.caller,
28 "borrower": None,
29 "state": "OPEN"
30 }
31
32 return str(lending[key])
33
34 @export
35 def borrow(key: str):
36 assert lending[key], 'KEY does not exist!'
37
38 data = lending[key]
39
40 if data["for_address"]:
41 assert data["for_address"] == ctx.caller, f"Reserved for {data['for_address']}"
42
43 assert data["state"] == "OPEN", f"Wrong state: {data['state']}"
44 assert data["end_date"] > now, "Lending enddate reached"
45
46 tau.transfer(amount=data["tau_amount"], to=ctx.caller)
47 lusd.transfer_from(amount=data["lusd_amount_collateral"], to=ctx.this, main_account=ctx.caller)
48
49 data["state"] = "BORROWED"
50 data["borrower"] = ctx.caller
51 lending[key] = data
52
53 return str(lending[key])
54
55 @export
56 def payback(key: str):
57 assert lending[key], 'KEY does not exist!'
58
59 data = lending[key]
60
61 assert data["borrower"] == ctx.caller, "You are not the borrower"
62 assert data["end_date"] > now, "Enddate exceeded, colletaral lost"
63 assert data["state"] == "BORROWED", f"Wrong state: {data['state']}"
64
65 total_tau_amount = data["tau_amount"] + data["tau_fee_amount"]
66 tau.transfer_from(amount=total_tau_amount, to=data["lender"], main_account=ctx.caller)
67 lusd.transfer(amount=data["lusd_amount_collateral"], to=ctx.caller)
68
69 data["state"] = "RETURNED"
70 lending[key] = data
71
72 return str(lending[key])
73
74 @export
75 def cancel(key: str):
76 assert lending[key], 'KEY does not exist!'
77
78 data = lending[key]
79
80 assert data["lender"] == ctx.caller, "You are not the lender"
81 assert data["state"] == "OPEN", f"Wrong state: {data['state']}"
82
83 tau.transfer(amount=data["tau_amount"], to=ctx.caller)
84
85 data["state"] = "CANCELED"
86 lending[key] = data
87
88 return str(lending[key])
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 tau.transfer(amount=data["tau_amount"], to=ctx.caller)
101 lusd.transfer(amount=data["lusd_amount_collateral"], to=ctx.caller)
102
103 data["state"] = "LIQUIDATED"
104 lending[key] = data
105
106 return str(lending[key])
107

Byte Code

e3000000000000000000000000090000004000000073aa000000640064016c005a01640064016c025a03650464026403640464058d035a05650464026403640664058d035a066507640383016413650865096509650a6509650864079c0664086409840583015a0b6507640383016508640a9c01640b640c840483015a0c6507640383016508640a9c01640d640e840483015a0d6507640383016508640a9c01640f6410840483015a0e6507640383016508640a9c0164116412840483015a0f640153002914e9000000004eda00da0e636f6e5f6c656e645f7465737433da076c656e64696e672903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65da07617070726f76652906da036b6579da0a7461755f616d6f756e74da0e7461755f6665655f616d6f756e74da0d74696d655f696e5f686f757273da166c7573645f616d6f756e745f636f6c6c61746572616cda0b666f725f61646472657373630600000000000000060000000b00000043000000739e00000074007c0019000c00731274016401830182017c0164026b04732274016403830182017c0264026b04733274016404830182017c0364026b04734274016405830182017c0464026b047352740164068301820174026a037c0174046a0574046a0664078d0301007c017c027c037c047c057407740774086a097c0364088d01170074046a0664006409640a9c0a74007c003c00740a74007c00190083015300290b4e7a1a4c656e64696e67204b455920616c72656164792065786973747372010000007a1754415520616d6f756e74206d757374206265203e2030217a1b5441552066656520616d6f756e74206d757374206265203e2030217a1654696d656672616d65206d757374206265203e2030217a1e436f6c6c61746572616c20616d6f756e74206d757374206265203e2030212903da06616d6f756e74da02746fda0c6d61696e5f6163636f756e742901da05686f757273da044f50454e290a720a000000720b000000720c000000720d000000720e000000da0a73746172745f64617465da08656e645f64617465da066c656e646572da08626f72726f776572da057374617465290bda095f5f6c656e64696e67da0e417373657274696f6e4572726f72da03746175da0d7472616e736665725f66726f6dda03637478da0474686973da0663616c6c6572da036e6f77da086461746574696d65da0974696d6564656c7461da0373747229067209000000720a000000720b000000720c000000720d000000720e000000a90072240000007202000000da06737570706c7907000000731a00000000031201100110011001100114010201040102010a010c010e0172250000002901720900000063010000000000000002000000050000004300000073c400000074007c0019007310740164018301820174007c0019007d017c016402190072407c016402190074026a036b027340740164037c01640219009b009d02830182017c016404190064056b02735e740164067c01640419009b009d02830182017c016407190074046b047372740164088301820174056a067c016409190074026a03640a8d02010074076a087c01640b190074026a0974026a03640c8d030100640d7c0164043c0074026a037c01640e3c007c0174007c003c00740a74007c00190083015300290f4e7a134b455920646f6573206e6f7420657869737421720e0000007a0d526573657276656420666f7220721800000072130000007a0d57726f6e672073746174653a2072150000007a174c656e64696e6720656e64646174652072656163686564720a0000002902720f0000007210000000720d0000002903720f00000072100000007211000000da08424f52524f5745447217000000290b7219000000721a000000721d000000721f0000007220000000721b000000da087472616e73666572da046c757364721c000000721e000000722300000029027209000000da0464617461722400000072240000007202000000da06626f72726f7719000000731c000000000210010801080106011a011e01140114010e010a0108010a010801722a00000063010000000000000003000000050000004300000073b600000074007c0019007310740164018301820174007c0019007d017c016402190074026a036b02732e74016403830182017c016404190074046b04734274016405830182017c016406190064076b027360740164087c01640619009b009d02830182017c01640919007c01640a190017007d0274056a067c027c01640b190074026a03640c8d03010074076a087c01640d190074026a03640e8d020100640f7c0164063c007c0174007c003c00740974007c0019008301530029104e7a134b455920646f6573206e6f742065786973742172170000007a18596f7520617265206e6f742074686520626f72726f77657272150000007a21456e64646174652065786365656465642c20636f6c6c65746172616c206c6f7374721800000072260000007a0d57726f6e672073746174653a20720a000000720b00000072160000002903720f00000072100000007211000000720d0000002902720f0000007210000000da0852455455524e4544290a7219000000721a000000721d000000721f0000007220000000721b000000721c000000722800000072270000007223000000290372090000007229000000da10746f74616c5f7461755f616d6f756e74722400000072240000007202000000da077061796261636b2b0000007318000000000210010801160114011e0110010c010a01140108010801722d000000630100000000000000020000000400000043000000737c00000074007c0019007310740164018301820174007c0019007d017c016402190074026a036b02732e74016403830182017c016404190064056b02734c740164067c01640419009b009d028301820174046a057c016407190074026a0364088d02010064097c0164043c007c0174007c003c00740674007c00190083015300290a4e7a134b455920646f6573206e6f742065786973742172160000007a16596f7520617265206e6f7420746865206c656e646572721800000072130000007a0d57726f6e672073746174653a20720a0000002902720f0000007210000000da0843414e43454c454429077219000000721a000000721d000000721f000000721b00000072270000007223000000290272090000007229000000722400000072240000007202000000da0663616e63656c3b000000731000000000021001080116011e01140108010801722f00000063010000000000000002000000040000004300000073a400000074007c0019007310740164018301820174007c0019007d017c016402190074026a036b02732e74016403830182017c016404190074046b00734274016405830182017c016406190064076b027360740164087c01640619009b009d028301820174056a067c016409190074026a03640a8d02010074076a067c01640b190074026a03640a8d020100640c7c0164063c007c0174007c003c00740874007c00190083015300290d4e7a134b455920646f6573206e6f742065786973742172160000007a16596f7520617265206e6f7420746865206c656e64657272150000007a13456e6464617465206e6f742072656163686564721800000072260000007a0d57726f6e672073746174653a20720a0000002902720f0000007210000000720d000000da0a4c49515549444154454429097219000000721a000000721d000000721f0000007220000000721b000000722700000072280000007223000000290272090000007229000000722400000072240000007202000000da096c6971756964617465470000007314000000000210010801160114011e011401140108010801723100000029014e2910da0863757272656e6379721b000000da0f636f6e5f6c7573645f6c73743030317228000000da04486173687219000000da095f5f617070726f7665da085f5f6578706f72747223000000da05666c6f6174da03696e747225000000722a000000722d000000722f00000072310000007224000000722400000072240000007202000000da083c6d6f64756c653e01000000731a000000080108010e010e0306021c10060110110601100f0601100b0601