mirror of
https://github.com/chaintool-py/eth-erc20.git
synced 2026-04-27 21:01:04 +02:00
Add unittest for static token
This commit is contained in:
1
python/static_token/__init__.py
Normal file
1
python/static_token/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .factory import *
|
||||
3
python/static_token/data/__init__.py
Normal file
3
python/static_token/data/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
import os
|
||||
|
||||
data_dir = os.path.realpath(os.path.dirname(__file__))
|
||||
60
python/static_token/factory.py
Normal file
60
python/static_token/factory.py
Normal file
@@ -0,0 +1,60 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
|
||||
# external imports
|
||||
from chainlib.eth.tx import (
|
||||
TxFactory,
|
||||
TxFormat,
|
||||
)
|
||||
from chainlib.hash import keccak256_string_to_hex
|
||||
from chainlib.eth.contract import (
|
||||
ABIContractEncoder,
|
||||
ABIContractType,
|
||||
)
|
||||
|
||||
# local imports
|
||||
from static_token.data import data_dir
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class StaticToken(TxFactory):
|
||||
|
||||
__abi = None
|
||||
__bytecode = None
|
||||
|
||||
def constructor(self, sender_address, name, symbol, decimals, supply, tx_format=TxFormat.JSONRPC):
|
||||
code = StaticToken.bytecode()
|
||||
enc = ABIContractEncoder()
|
||||
enc.string(name)
|
||||
enc.string(symbol)
|
||||
enc.uint256(decimals)
|
||||
enc.uint256(supply)
|
||||
code += enc.get()
|
||||
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 2000000
|
||||
|
||||
|
||||
@staticmethod
|
||||
def abi():
|
||||
if StaticToken.__abi == None:
|
||||
f = open(os.path.join(data_dir, 'StaticToken.json'), 'r')
|
||||
StaticToken.__abi = json.load(f)
|
||||
f.close()
|
||||
return StaticToken.__abi
|
||||
|
||||
|
||||
@staticmethod
|
||||
def bytecode():
|
||||
if StaticToken.__bytecode == None:
|
||||
f = open(os.path.join(data_dir, 'StaticToken.bin'))
|
||||
StaticToken.__bytecode = f.read()
|
||||
f.close()
|
||||
return StaticToken.__bytecode
|
||||
1
python/static_token/unittest/__init__.py
Normal file
1
python/static_token/unittest/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .base import TestStaticToken
|
||||
36
python/static_token/unittest/base.py
Normal file
36
python/static_token/unittest/base.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# standard imports
|
||||
import logging
|
||||
import time
|
||||
|
||||
# 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.address import to_checksum_address
|
||||
|
||||
# local imports
|
||||
from static_token import StaticToken
|
||||
from eth_erc20.unittest import TestInterface
|
||||
|
||||
logg = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestStaticToken(EthTesterCase, TestInterface):
|
||||
|
||||
def setUp(self):
|
||||
super(TestStaticToken, self).setUp()
|
||||
self.conn = RPCConnection.connect(self.chain_spec, 'default')
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
|
||||
c = StaticToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
self.symbol = 'FOO'
|
||||
self.name = 'Foo Token'
|
||||
self.decimals = 16
|
||||
self.initial_supply = 1 << 24
|
||||
(tx_hash, o) = c.constructor(self.accounts[0], self.name, self.symbol, self.decimals, self.initial_supply)
|
||||
self.rpc.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
self.address = to_checksum_address(r['contract_address'])
|
||||
logg.debug('published statictoken on address {} with hash {}'.format(self.address, tx_hash))
|
||||
Reference in New Issue
Block a user