Add minter and burner python interface and test code
This commit is contained in:
1
python/eth_minter/__init__.py
Normal file
1
python/eth_minter/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .minter import EthMinter
|
||||
25
python/eth_minter/minter.py
Normal file
25
python/eth_minter/minter.py
Normal file
@@ -0,0 +1,25 @@
|
||||
# external imports
|
||||
from chainlib.eth.tx import TxFormat
|
||||
from chainlib.eth.tx import TxFactory
|
||||
from chainlib.jsonrpc import JSONRPCRequest
|
||||
from chainlib.eth.constant import ZERO_ADDRESS
|
||||
from chainlib.eth.contract import ABIContractEncoder
|
||||
from chainlib.eth.contract import ABIContractType
|
||||
from chainlib.eth.contract import abi_decode_single
|
||||
from hexathon import add_0x
|
||||
|
||||
|
||||
class EthMinter(TxFactory):
|
||||
|
||||
def mint_to(self, contract_address, sender_address, recipient_address, value, tx_format=TxFormat.JSONRPC):
|
||||
enc = ABIContractEncoder()
|
||||
enc.method('mintTo')
|
||||
enc.typ(ABIContractType.ADDRESS)
|
||||
enc.typ(ABIContractType.UINT256)
|
||||
enc.address(recipient_address)
|
||||
enc.uint256(value)
|
||||
data = enc.get()
|
||||
tx = self.template(sender_address, contract_address, use_nonce=True)
|
||||
tx = self.set_code(tx, data)
|
||||
tx = self.finalize(tx, tx_format)
|
||||
return tx
|
||||
1
python/eth_minter/unittest/__init__.py
Normal file
1
python/eth_minter/unittest/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .interface import TestEthMinterInterface
|
||||
51
python/eth_minter/unittest/base.py
Normal file
51
python/eth_minter/unittest/base.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
import datetime
|
||||
|
||||
# external imports
|
||||
from chainlib.eth.unittest.ethtester import EthTesterCase
|
||||
from chainlib.connection import RPCConnection
|
||||
from chainlib.eth.nonce import RPCNonceOracle
|
||||
from chainlib.eth.tx import receipt
|
||||
from chainlib.eth.tx import TxFactory
|
||||
from chainlib.eth.tx import TxFormat
|
||||
from chainlib.eth.contract import ABIContractEncoder
|
||||
from chainlib.eth.contract import ABIContractType
|
||||
|
||||
# local imports
|
||||
from eth_minter import EthMinter
|
||||
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
contract_dir = os.path.join(script_dir, '..', '..', 'solidity')
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestEthMinter(EthTesterCase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestEthMinter, self).setUp()
|
||||
|
||||
self.conn = RPCConnection.connect(self.chain_spec, 'default')
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn)
|
||||
|
||||
f = open(os.path.join(contract_dir, 'MinterTest.bin'))
|
||||
code = f.read()
|
||||
f.close()
|
||||
|
||||
txf = TxFactory(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
tx = txf.template(self.accounts[0], None, use_nonce=True)
|
||||
|
||||
self.initial_supply = 1024
|
||||
enc = ABIContractEncoder()
|
||||
args = enc.get()
|
||||
|
||||
tx = txf.set_code(tx, code + args)
|
||||
(tx_hash_hex, o) = txf.build(tx)
|
||||
self.conn.do(o)
|
||||
o = receipt(tx_hash_hex)
|
||||
r = self.conn.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
self.address = r['contract_address']
|
||||
logg.debug('published minter test contract with hash {}'.format(r))
|
||||
19
python/eth_minter/unittest/interface.py
Normal file
19
python/eth_minter/unittest/interface.py
Normal file
@@ -0,0 +1,19 @@
|
||||
# external imports
|
||||
from chainlib.eth.tx import receipt
|
||||
from chainlib.eth.nonce import RPCNonceOracle
|
||||
|
||||
# local imports
|
||||
from eth_minter import EthMinter
|
||||
|
||||
|
||||
class TestEthMinterInterface:
|
||||
|
||||
def test_supply(self):
|
||||
self.alice = self.accounts[1]
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
c = EthMinter(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.alice, self.initial_supply)
|
||||
self.rpc.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(r['status'],1)
|
||||
Reference in New Issue
Block a user