token.py (2768B)
1 # standard imports 2 import logging 3 import os 4 import enum 5 6 # external imports 7 from chainlib.eth.constant import ZERO_ADDRESS 8 from chainlib.eth.constant import ZERO_CONTENT 9 from chainlib.eth.contract import ( 10 ABIContractEncoder, 11 ABIContractDecoder, 12 ABIContractType, 13 abi_decode_single, 14 ) 15 from chainlib.eth.jsonrpc import to_blockheight_param 16 from chainlib.eth.error import RequestMismatchException 17 from chainlib.eth.tx import ( 18 TxFactory, 19 TxFormat, 20 ) 21 from chainlib.jsonrpc import JSONRPCRequest 22 from chainlib.block import BlockSpec 23 from hexathon import ( 24 add_0x, 25 strip_0x, 26 ) 27 from chainlib.eth.cli.encode import CLIEncoder 28 29 # local imports 30 from erc20_limiter.data import data_dir 31 32 logg = logging.getLogger() 33 34 35 class LimiterTokenRegistry(TxFactory): 36 37 __abi = None 38 __bytecode = None 39 40 def constructor(self, sender_address, holder_address, limiter_address, tx_format=TxFormat.JSONRPC, version=None): 41 code = self.cargs(holder_address, limiter_address, version=version) 42 tx = self.template(sender_address, None, use_nonce=True) 43 tx = self.set_code(tx, code) 44 return self.finalize(tx, tx_format) 45 46 47 @staticmethod 48 def cargs(holder_address, limiter_address, version=None): 49 code = LimiterTokenRegistry.bytecode(version=version) 50 enc = ABIContractEncoder() 51 enc.address(holder_address) 52 enc.address(limiter_address) 53 args = enc.get() 54 code += args 55 logg.debug('constructor code: ' + args) 56 return code 57 58 59 @staticmethod 60 def gas(code=None): 61 return 4000000 62 63 64 @staticmethod 65 def abi(): 66 if LimiterTokenRegistry.__abi == None: 67 f = open(os.path.join(data_dir, 'LimiterTokenRegistry.json'), 'r') 68 LimiterTokenRegistry.__abi = json.load(f) 69 f.close() 70 return LimiterTokenRegistry.__abi 71 72 73 @staticmethod 74 def bytecode(version=None): 75 if LimiterTokenRegistry.__bytecode == None: 76 f = open(os.path.join(data_dir, 'LimiterTokenRegistry.bin')) 77 LimiterTokenRegistry.__bytecode = f.read() 78 f.close() 79 return LimiterTokenRegistry.__bytecode 80 81 82 def have(self, contract_address, token_address, sender_address=ZERO_ADDRESS, id_generator=None): 83 j = JSONRPCRequest(id_generator) 84 o = j.template() 85 o['method'] = 'eth_call' 86 enc = ABIContractEncoder() 87 enc.method('have') 88 enc.typ(ABIContractType.ADDRESS) 89 enc.address(token_address) 90 data = add_0x(enc.get()) 91 tx = self.template(sender_address, contract_address) 92 tx = self.set_code(tx, data) 93 o['params'].append(self.normalize(tx)) 94 o['params'].append('latest') 95 o = j.finalize(o) 96 return o