Contract con_my_first_contract


Contract Code


  
1 #create some state
2 balances = Hash(default_value=0)
3
4 #seed initial balances
5 @construct
6 def seed():
7 balances['me'] = 1000000
8
9 #transfer the caller's funds to another account
10 @export
11 def transfer(amount: float, to: str):
12 assert amount > 0, 'Cannot send negative balances!'
13 assert balances[ctx.caller] >= amount, 'Insufficient Funds to send!'
14
15 # remove amount from senders account
16 balances[ctx.caller] -= amount
17 # add amount to receivers account
18 balances[to] += amount
19
20 # give permission to the spender to spend your funds
21 @export
22 def approve(amount: float, to: str):
23 assert amount > 0, 'Cannot send negative balances!'
24
25 # Add the amount to the callers
26 balances[ctx.caller, to] += amount
27
28 # transfer someone's funds to another account
29 @export
30 def transfer_from(amount: float, to: str, main_account: str):
31 assert amount > 0, 'Cannot send negative balances!'
32
33 assert balances[main_account, ctx.caller] >= amount, 'Not enough coins approved to send! You have {} and are trying to spend {}'\
34 .format(balances[main_account, ctx.caller], amount)
35 assert balances[main_account] >= amount, 'Not enough coins to send!'
36
37 # reduce the approval amount by the amount being spent
38 balances[main_account, ctx.caller] -= amount
39
40 # remove amount spent from the main account
41 balances[main_account] -= amount
42 # add the amount spent to the receivers account
43 balances[to] += amount

Byte Code

e300000000000000000000000005000000400000007364000000650064006401640264038d035a016404640584005a026503640183016504650564069c0264076408840483015a066503640183016504650564069c026409640a840483015a07650364018301650465056505640b9c03640c640d840483015a08640e5300290fe900000000da15636f6e5f6d795f66697273745f636f6e7472616374da0862616c616e6365732903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65630000000000000000000000000300000043000000730c0000006401740064023c006400530029034e6940420f00da026d652901da0a5f5f62616c616e636573a90072090000007209000000da00da045f5f5f5f0500000073020000000001720b0000002902da06616d6f756e74da02746f630200000000000000020000000400000043000000734c0000007c0064016b0473107400640283018201740174026a0319007c006b0573267400640383018201740174026a03050019007c00380003003c0074017c01050019007c00370003003c006400530029044e72010000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a1b496e73756666696369656e742046756e647320746f2073656e64212904da0e417373657274696f6e4572726f727208000000da03637478da0663616c6c65722902720c000000720d00000072090000007209000000720a000000da087472616e7366657209000000730800000000021001160112017211000000630200000000000000020000000400000043000000732a0000007c0064016b0473107400640283018201740174026a037c016602050019007c00370003003c006400530029034e72010000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573212904720e0000007208000000720f00000072100000002902720c000000720d00000072090000007209000000720a000000da07617070726f76651100000073040000000002100172120000002903720c000000720d000000da0c6d61696e5f6163636f756e74630300000000000000030000000500000043000000738a0000007c0064016b047310740064028301820174017c0274026a03660219007c006b05733c740064036a0474017c0274026a03660219007c0083028301820174017c0219007c006b057350740064048301820174017c0274026a036602050019007c00380003003c0074017c02050019007c00380003003c0074017c01050019007c00370003003c006400530029054e72010000007a1e43616e6e6f742073656e64206e656761746976652062616c616e636573217a494e6f7420656e6f75676820636f696e7320617070726f76656420746f2073656e642120596f752068617665207b7d20616e642061726520747279696e6720746f207370656e64207b7d7a194e6f7420656e6f75676820636f696e7320746f2073656e64212905720e0000007208000000720f0000007210000000da06666f726d61742903720c000000720d000000721300000072090000007209000000720a000000da0d7472616e736665725f66726f6d170000007310000000000210010c010c01140114011601100172150000004e2909da04486173687208000000720b000000da085f5f6578706f7274da05666c6f6174da03737472721100000072120000007215000000720900000072090000007209000000720a000000da083c6d6f64756c653e01000000731000000006010803080406011207060112050601