interface.py (1753B)
1 # external imports 2 from chainlib.eth.tx import TxFormat 3 from chainlib.eth.tx import TxFactory 4 from chainlib.jsonrpc import JSONRPCRequest 5 from chainlib.eth.constant import ZERO_ADDRESS 6 from chainlib.eth.contract import ABIContractEncoder 7 from chainlib.eth.contract import ABIContractType 8 from hexathon import add_0x 9 10 class EthWriter(TxFactory): 11 12 def __single_address_method(self, method, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC): 13 enc = ABIContractEncoder() 14 enc.method(method) 15 enc.typ(ABIContractType.ADDRESS) 16 enc.address(address) 17 data = enc.get() 18 tx = self.template(sender_address, contract_address, use_nonce=True) 19 tx = self.set_code(tx, data) 20 tx = self.finalize(tx, tx_format) 21 return tx 22 23 24 def add_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC): 25 return self.__single_address_method('addWriter', contract_address, sender_address, address, tx_format) 26 27 28 def delete_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC): 29 return self.__single_address_method('deleteWriter', contract_address, sender_address, address, tx_format) 30 31 32 def is_writer(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None): 33 j = JSONRPCRequest(id_generator) 34 o = j.template() 35 o['method'] = 'eth_call' 36 enc = ABIContractEncoder() 37 enc.method('isWriter') 38 enc.typ(ABIContractType.ADDRESS) 39 enc.address(address) 40 data = add_0x(enc.get()) 41 tx = self.template(sender_address, contract_address) 42 tx = self.set_code(tx, data) 43 o['params'].append(self.normalize(tx)) 44 return o