eth-erc20

ERC20 interface and example giftable token contract
Info | Log | Files | Refs | LICENSE

factory.py (5307B)


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