Implement chainlib gen

This commit is contained in:
lash 2023-02-14 07:23:59 +00:00
parent cf1e716b5a
commit c31649fed2
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 97 additions and 15 deletions

View File

@ -3,3 +3,6 @@ from .token import (
DemurrageTokenSettings,
DemurrageRedistribution,
)
from .token import create
from .token import bytecode
from .token import args

View File

@ -78,28 +78,34 @@ class DemurrageToken(ERC20, SealedContract, ExpiryContract):
__abi = {}
__bytecode = {}
def constructor(self, sender_address, settings, tx_format=TxFormat.JSONRPC):
code = DemurrageToken.bytecode()
enc = ABIContractEncoder()
enc.string(settings.name)
enc.string(settings.symbol)
enc.uint256(settings.decimals)
enc.uint256(settings.demurrage_level)
enc.uint256(settings.period_minutes)
enc.address(settings.sink_address)
code += enc.get()
def constructor(self, sender_address, settings, tx_format=TxFormat.JSONRPC, version=None):
code = self.cargs(settings.name, settings.symbol, settings.decimals, settings.demurrage_level, settings.period_minutes, settings.sink_address, version=version)
tx = self.template(sender_address, None, use_nonce=True)
tx = self.set_code(tx, code)
return self.finalize(tx, tx_format)
@staticmethod
def gas(code=None):
return 4000000
def cargs(name, symbol, decimals, demurrage_level, period_minutes, sink_address, version=None):
code = DemurrageToken.bytecode()
enc = ABIContractEncoder()
enc.string(name)
enc.string(symbol)
enc.uint256(decimals)
enc.uint256(demurrage_level)
enc.uint256(period_minutes)
enc.address(sink_address)
code += enc.get()
return code
@staticmethod
def abi(multi=True):
def gas(code=None):
return 6000000
@staticmethod
def abi():
name = 'DemurrageTokenSingleNocap'
if DemurrageToken.__abi.get(name) == None:
f = open(os.path.join(data_dir, name + '.json'), 'r')
@ -109,7 +115,7 @@ class DemurrageToken(ERC20, SealedContract, ExpiryContract):
@staticmethod
def bytecode(multi=True):
def bytecode(version=None):
name = 'DemurrageTokenSingleNocap'
if DemurrageToken.__bytecode.get(name) == None:
f = open(os.path.join(data_dir, name + '.bin'), 'r')
@ -665,3 +671,25 @@ class DemurrageToken(ERC20, SealedContract, ExpiryContract):
def parse_total_burned(self, v):
return abi_decode_single(ABIContractType.UINT256, v)
def bytecode(**kwargs):
return DemurrageToken.bytecode(version=kwargs.get('version'))
def create(**kwargs):
return DemurrageToken.cargs(
kwargs['name'],
kwargs['symbol'],
kwargs['decimals'],
kwargs['demurragelevel'],
kwargs['redistributionperiod'],
kwargs['sinkaddress'],
version=kwargs.get('version'))
def args(v):
if v == 'create':
return (['name', 'symbol', 'decimals', 'demurragelevel', 'redistributionperiod', 'sinkaddress'], ['version'],)
elif v == 'default' or v == 'bytecode':
return ([], ['version'],)
raise ValueError('unknown command: ' + v)

View File

@ -1,4 +1,4 @@
chainlib-eth~=0.4.11
eth-erc20~=0.5.0
funga-eth~=0.6.0
dexif~=0.0.1
dexif~=0.0.2

51
python/tests/test_mint.py Normal file
View File

@ -0,0 +1,51 @@
# standard imports
import os
import unittest
import json
import logging
# external imports
from chainlib.eth.constant import ZERO_ADDRESS
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.tx import receipt
from chainlib.eth.block import block_latest
from chainlib.eth.block import block_by_number
# local imports
from erc20_demurrage_token import DemurrageToken
# test imports
from erc20_demurrage_token.unittest import TestDemurrageDefault
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
testdir = os.path.dirname(__file__)
class TestAmounts(TestDemurrageDefault):
def test_mint(self):
o = block_latest()
r = self.rpc.do(o)
o = block_by_number(r)
r = self.rpc.do(o)
tb = r['timestamp']
self.backend.time_travel(self.start_time + 800)
o = block_latest()
r = self.rpc.do(o)
o = block_by_number(r)
r = self.rpc.do(o)
ta = r['timestamp']
logg.info('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>< {} {} {}'.format(tb, ta, ta-tb))
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1000)
r = self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
if __name__ == '__main__':
unittest.main()