contract.py (1855B)
1 # standard imports 2 import os 3 4 # external iports 5 from chainlib.eth.tx import ( 6 TxFactory, 7 TxFormat, 8 receipt, 9 ) 10 from chainlib.eth.contract import ( 11 ABIContractEncoder, 12 #ABIContractDecoder, 13 ABIContractType, 14 ) 15 from hexathon import add_0x 16 17 script_dir = os.path.realpath(os.path.dirname(__file__)) 18 data_dir = script_dir 19 20 class TestContract(TxFactory): 21 22 __abi = None 23 __bytecode = None 24 25 @staticmethod 26 def gas(code=None): 27 return 1000000 28 29 30 @staticmethod 31 def abi(): 32 if TestContract.__abi == None: 33 f = open(os.path.join(data_dir, 'TestContract.json'), 'r') 34 TestContract.__abi = json.load(f) 35 f.close() 36 return TestContract.__abi 37 38 39 @staticmethod 40 def bytecode(): 41 if TestContract.__bytecode == None: 42 f = open(os.path.join(data_dir, 'TestContract.bin')) 43 TestContract.__bytecode = f.read() 44 f.close() 45 return TestContract.__bytecode 46 47 48 def constructor(self, sender_address, tx_format=TxFormat.JSONRPC, id_generator=None): 49 code = TestContract.bytecode() 50 tx = self.template(sender_address, None, use_nonce=True) 51 tx = self.set_code(tx, code) 52 return self.finalize(tx, tx_format, id_generator=id_generator) 53 54 55 def foo(self, contract_address, sender_address, x, y, tx_format=TxFormat.JSONRPC, id_generator=None): 56 enc = ABIContractEncoder() 57 enc.method('foo') 58 enc.typ(ABIContractType.UINT256) 59 enc.typ(ABIContractType.BYTES32) 60 enc.uint256(x) 61 enc.bytes32(y) 62 data = add_0x(enc.get()) 63 tx = self.template(sender_address, contract_address, use_nonce=True) 64 tx = self.set_code(tx, data) 65 tx = self.finalize(tx, tx_format, id_generator=id_generator) 66 return tx