token.py (4680B)
1 # standard imports 2 import os 3 4 # external imports 5 from chainlib.eth.tx import ( 6 TxFormat, 7 TxFactory, 8 ) 9 from chainlib.eth.contract import ( 10 ABIContractEncoder, 11 ABIContractType, 12 abi_decode_single, 13 ) 14 from chainlib.jsonrpc import JSONRPCRequest 15 from chainlib.eth.constant import ZERO_ADDRESS 16 from hexathon import ( 17 add_0x, 18 strip_0x, 19 ) 20 21 # local imports 22 from eth_erc721 import ERC721 23 24 moddir = os.path.dirname(__file__) 25 datadir = os.path.join(moddir, 'data') 26 27 28 class BadgeToken(ERC721): 29 30 __abi = None 31 __bytecode = None 32 33 @staticmethod 34 def abi(): 35 if BadgeToken.__abi == None: 36 f = open(os.path.join(datadir, 'BadgeToken.json'), 'r') 37 BadgeToken.__abi = json.load(f) 38 f.close() 39 return BadgeToken.__abi 40 41 42 @staticmethod 43 def bytecode(version=None): 44 if BadgeToken.__bytecode == None: 45 f = open(os.path.join(datadir, 'BadgeToken.bin')) 46 BadgeToken.__bytecode = f.read() 47 f.close() 48 return BadgeToken.__bytecode 49 50 51 @staticmethod 52 def gas(code=None): 53 return 3500000 54 55 56 def constructor(self, sender_address, name, symbol, declarator, tx_format=TxFormat.JSONRPC, version=None): 57 code = self.cargs(name, symbol, declarator, version=version) 58 tx = self.template(sender_address, None, use_nonce=True) 59 tx = self.set_code(tx, code) 60 return self.finalize(tx, tx_format) 61 62 @staticmethod 63 def cargs(name, symbol, declarator, version=None): 64 declarator = strip_0x(declarator) 65 code = BadgeToken.bytecode() 66 enc = ABIContractEncoder() 67 enc.string(name) 68 enc.string(symbol) 69 enc.address(declarator) 70 code += enc.get() 71 return code 72 73 74 def mint_to(self, contract_address, sender_address, address, token_id, tx_format=TxFormat.JSONRPC): 75 enc = ABIContractEncoder() 76 enc.method('mintTo') 77 enc.typ(ABIContractType.ADDRESS) 78 enc.typ(ABIContractType.UINT256) 79 enc.address(address) 80 enc.uint256(token_id) 81 data = enc.get() 82 tx = self.template(sender_address, contract_address, use_nonce=True) 83 tx = self.set_code(tx, data) 84 tx = self.finalize(tx, tx_format) 85 return tx 86 87 88 def create_time(self, contract_address, token_id, sender_address=ZERO_ADDRESS, id_generator=None): 89 j = JSONRPCRequest(id_generator) 90 o = j.template() 91 o['method'] = 'eth_call' 92 enc = ABIContractEncoder() 93 enc.method('createTime') 94 enc.typ(ABIContractType.UINT256) 95 enc.uint256(token_id) 96 data = add_0x(enc.get()) 97 tx = self.template(sender_address, contract_address) 98 tx = self.set_code(tx, data) 99 o['params'].append(self.normalize(tx)) 100 o['params'].append('latest') 101 o = j.finalize(o) 102 return o 103 104 105 def start_time(self, contract_address, token_id, sender_address=ZERO_ADDRESS, id_generator=None): 106 j = JSONRPCRequest(id_generator) 107 o = j.template() 108 o['method'] = 'eth_call' 109 enc = ABIContractEncoder() 110 enc.method('startTime') 111 enc.typ(ABIContractType.UINT256) 112 enc.uint256(token_id) 113 data = add_0x(enc.get()) 114 tx = self.template(sender_address, contract_address) 115 tx = self.set_code(tx, data) 116 o['params'].append(self.normalize(tx)) 117 o['params'].append('latest') 118 o = j.finalize(o) 119 return o 120 121 122 def end_time(self, contract_address, token_id, sender_address=ZERO_ADDRESS, id_generator=None): 123 j = JSONRPCRequest(id_generator) 124 o = j.template() 125 o['method'] = 'eth_call' 126 enc = ABIContractEncoder() 127 enc.method('endTime') 128 enc.typ(ABIContractType.UINT256) 129 enc.uint256(token_id) 130 data = add_0x(enc.get()) 131 tx = self.template(sender_address, contract_address) 132 tx = self.set_code(tx, data) 133 o['params'].append(self.normalize(tx)) 134 o['params'].append('latest') 135 o = j.finalize(o) 136 return o 137 138 139 @classmethod 140 def parse_time(self, v): 141 return abi_decode_single(ABIContractType.UINT256, v) 142 143 144 def bytecode(**kwargs): 145 return BadgeToken.bytecode(version=kwargs.get('version')) 146 147 148 def create(**kwargs): 149 return BadgeToken.cargs(kwargs['name'], kwargs['symbol'], kwargs['declarator'], version=kwargs.get('version')) 150 151 152 def args(v): 153 if v == 'create': 154 return (['name', 'symbol', 'declarator'], ['version'],) 155 elif v == 'default' or v == 'bytecode': 156 return ([], ['version'],) 157 raise ValueError('unknown command: ' + v)