Revert "Improve test coverage"

This reverts commit 2d79c863ce.
This commit is contained in:
nolash 2021-06-26 10:34:31 +02:00
parent 2d79c863ce
commit 00eeaeb7cb
15 changed files with 145 additions and 277 deletions

View File

@ -1,7 +1,3 @@
* 0.0.10-pending
- Upgrade chainlib dependency, providing customizable jsonrpc id
* 0.0.9-unreleased
* 0.0.8-unreleased
* 0.0.7-unreleased * 0.0.7-unreleased
- Implement keystore signing. - Implement keystore signing.
- Change makefile to compile contract for Byzantium. - Change makefile to compile contract for Byzantium.

View File

@ -19,13 +19,13 @@ from chainlib.eth.tx import (
TxFactory, TxFactory,
TxFormat, TxFormat,
) )
from chainlib.jsonrpc import JSONRPCRequest
from chainlib.eth.contract import ( from chainlib.eth.contract import (
ABIContractEncoder, ABIContractEncoder,
ABIContractDecoder, ABIContractDecoder,
ABIContractType, ABIContractType,
abi_decode_single, abi_decode_single,
) )
from chainlib.jsonrpc import jsonrpc_template
from chainlib.eth.error import RequestMismatchException from chainlib.eth.error import RequestMismatchException
logg = logging.getLogger() logg = logging.getLogger()
@ -34,9 +34,8 @@ logg = logging.getLogger()
class ERC20(TxFactory): class ERC20(TxFactory):
def balance_of(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None): def balance_of(self, contract_address, address, sender_address=ZERO_ADDRESS):
j = JSONRPCRequest(id_generator) o = jsonrpc_template()
o = j.template()
o['method'] = 'eth_call' o['method'] = 'eth_call'
enc = ABIContractEncoder() enc = ABIContractEncoder()
enc.method('balanceOf') enc.method('balanceOf')
@ -47,17 +46,15 @@ class ERC20(TxFactory):
tx = self.set_code(tx, data) tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx)) o['params'].append(self.normalize(tx))
o['params'].append('latest') o['params'].append('latest')
o = j.finalize(o)
return o return o
def balance(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None): def balance(self, contract_address, address, sender_address=ZERO_ADDRESS):
return self.balance_of(contract_address, address, sender_address=ZERO_ADDRESS, id_generator=id_generator) return self.balance_of(contract_address, address, sender_address=ZERO_ADDRESS)
def symbol(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None): def symbol(self, contract_address, sender_address=ZERO_ADDRESS):
j = JSONRPCRequest(id_generator) o = jsonrpc_template()
o = j.template()
o['method'] = 'eth_call' o['method'] = 'eth_call'
enc = ABIContractEncoder() enc = ABIContractEncoder()
enc.method('symbol') enc.method('symbol')
@ -66,13 +63,11 @@ class ERC20(TxFactory):
tx = self.set_code(tx, data) tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx)) o['params'].append(self.normalize(tx))
o['params'].append('latest') o['params'].append('latest')
o = j.finalize(o)
return o return o
def name(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None): def name(self, contract_address, sender_address=ZERO_ADDRESS):
j = JSONRPCRequest(id_generator) o = jsonrpc_template()
o = j.template()
o['method'] = 'eth_call' o['method'] = 'eth_call'
enc = ABIContractEncoder() enc = ABIContractEncoder()
enc.method('name') enc.method('name')
@ -81,13 +76,11 @@ class ERC20(TxFactory):
tx = self.set_code(tx, data) tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx)) o['params'].append(self.normalize(tx))
o['params'].append('latest') o['params'].append('latest')
o = j.finalize(o)
return o return o
def decimals(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None): def decimals(self, contract_address, sender_address=ZERO_ADDRESS):
j = JSONRPCRequest(id_generator) o = jsonrpc_template()
o = j.template()
o['method'] = 'eth_call' o['method'] = 'eth_call'
enc = ABIContractEncoder() enc = ABIContractEncoder()
enc.method('decimals') enc.method('decimals')
@ -96,13 +89,11 @@ class ERC20(TxFactory):
tx = self.set_code(tx, data) tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx)) o['params'].append(self.normalize(tx))
o['params'].append('latest') o['params'].append('latest')
o = j.finalize(o)
return o return o
def total_supply(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None): def total_supply(self, contract_address, sender_address=ZERO_ADDRESS):
j = JSONRPCRequest(id_generator) o = jsonrpc_template()
o = j.template()
o['method'] = 'eth_call' o['method'] = 'eth_call'
enc = ABIContractEncoder() enc = ABIContractEncoder()
enc.method('totalSupply') enc.method('totalSupply')
@ -111,11 +102,10 @@ class ERC20(TxFactory):
tx = self.set_code(tx, data) tx = self.set_code(tx, data)
o['params'].append(self.normalize(tx)) o['params'].append(self.normalize(tx))
o['params'].append('latest') o['params'].append('latest')
o = j.finalize(o)
return o return o
def transfer(self, contract_address, sender_address, recipient_address, value, tx_format=TxFormat.JSONRPC, id_generator=None): def transfer(self, contract_address, sender_address, recipient_address, value, tx_format=TxFormat.JSONRPC):
enc = ABIContractEncoder() enc = ABIContractEncoder()
enc.method('transfer') enc.method('transfer')
enc.typ(ABIContractType.ADDRESS) enc.typ(ABIContractType.ADDRESS)
@ -125,7 +115,7 @@ class ERC20(TxFactory):
data = add_0x(enc.get()) data = add_0x(enc.get())
tx = self.template(sender_address, contract_address, use_nonce=True) tx = self.template(sender_address, contract_address, use_nonce=True)
tx = self.set_code(tx, data) tx = self.set_code(tx, data)
tx = self.finalize(tx, tx_format, id_generator=id_generator) tx = self.finalize(tx, tx_format)
return tx return tx
@ -179,11 +169,6 @@ class ERC20(TxFactory):
return abi_decode_single(ABIContractType.UINT256, v) return abi_decode_single(ABIContractType.UINT256, v)
@classmethod
def parse_total_supply(self, v):
return abi_decode_single(ABIContractType.UINT256, v)
@classmethod @classmethod
def parse_transfer_request(self, v): def parse_transfer_request(self, v):
v = strip_0x(v) v = strip_0x(v)

View File

@ -27,6 +27,10 @@ from eth_abi import encode_single
# external imports # external imports
from chainlib.eth.address import to_checksum from chainlib.eth.address import to_checksum
from chainlib.jsonrpc import (
jsonrpc_template,
jsonrpc_result,
)
from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.gas import ( from chainlib.eth.gas import (
OverrideGasOracle, OverrideGasOracle,
@ -40,8 +44,7 @@ from eth_erc20 import ERC20
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger() logg = logging.getLogger()
default_eth_provider = os.environ.get('RPC_PROVIDER') default_abi_dir = os.environ.get('ETH_ABI_DIR', '/usr/share/local/cic/solidity/abi')
if default_eth_provider == None:
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545') default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
argparser = argparse.ArgumentParser() argparser = argparse.ArgumentParser()
@ -50,7 +53,6 @@ argparser.add_argument('-a', '--token-address', dest='a', required=True, type=st
argparser.add_argument('-f', '--format', dest='f', type=str, default='terminal', help='Output format [terminal (default), raw, brief]') argparser.add_argument('-f', '--format', dest='f', type=str, default='terminal', help='Output format [terminal (default), raw, brief]')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string') argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress') argparser.add_argument('-u', '--unsafe', dest='u', action='store_true', help='Auto-convert address to checksum adddress')
argparser.add_argument('--seq', action='store_true', help='Use sequential rpc ids')
argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('address', type=str, help='Account address') argparser.add_argument('address', type=str, help='Account address')

View File

@ -24,9 +24,7 @@ from hexathon import (
strip_0x, strip_0x,
) )
from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.connection import EthHTTPConnection
from chainlib.jsonrpc import ( from chainlib.jsonrpc import jsonrpc_template
IntSequenceGenerator,
)
from chainlib.eth.nonce import ( from chainlib.eth.nonce import (
RPCNonceOracle, RPCNonceOracle,
OverrideNonceOracle, OverrideNonceOracle,
@ -47,12 +45,9 @@ logg = logging.getLogger()
logging.getLogger('web3').setLevel(logging.WARNING) logging.getLogger('web3').setLevel(logging.WARNING)
logging.getLogger('urllib3').setLevel(logging.WARNING) logging.getLogger('urllib3').setLevel(logging.WARNING)
default_eth_provider = os.environ.get('RPC_PROVIDER') default_abi_dir = '/usr/local/share/cic/solidity/abi'
if default_eth_provider == None:
default_eth_provider = os.environ.get('ETH_PROVIDER', 'http://localhost:8545')
argparser = argparse.ArgumentParser() argparser = argparse.ArgumentParser()
argparser.add_argument('-p', '--provider', dest='p', default=default_eth_provider, type=str, help='Web3 provider url (http only)') argparser.add_argument('-p', '--provider', dest='p', default='http://localhost:8545', type=str, help='Web3 provider url (http only)')
argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed') argparser.add_argument('-w', action='store_true', help='Wait for the last transaction to be confirmed')
argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed') argparser.add_argument('-ww', action='store_true', help='Wait for every transaction to be confirmed')
argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string') argparser.add_argument('-i', '--chain-spec', dest='i', type=str, default='evm:ethereum:1', help='Chain specification string')
@ -64,7 +59,6 @@ argparser.add_argument('-s', '--send', dest='s', action='store_true', help='Send
argparser.add_argument('--nonce', type=int, help='Override nonce') argparser.add_argument('--nonce', type=int, help='Override nonce')
argparser.add_argument('--gas-price', dest='gas_price', type=int, help='Override gas price') argparser.add_argument('--gas-price', dest='gas_price', type=int, help='Override gas price')
argparser.add_argument('--gas-limit', dest='gas_limit', type=int, help='Override gas limit') argparser.add_argument('--gas-limit', dest='gas_limit', type=int, help='Override gas limit')
argparser.add_argument('--seq', action='store_true', help='Use sequential rpc ids')
argparser.add_argument('-v', action='store_true', help='Be verbose') argparser.add_argument('-v', action='store_true', help='Be verbose')
argparser.add_argument('-vv', action='store_true', help='Be more verbose') argparser.add_argument('-vv', action='store_true', help='Be more verbose')
argparser.add_argument('recipient', type=str, help='Recipient account address') argparser.add_argument('recipient', type=str, help='Recipient account address')
@ -97,33 +91,22 @@ if args.y != None:
logg.debug('now have key for signer address {}'.format(signer_address)) logg.debug('now have key for signer address {}'.format(signer_address))
signer = EIP155Signer(keystore) signer = EIP155Signer(keystore)
rpc_id_generator = None
if args.seq:
rpc_id_generator = IntSequenceGenerator()
auth = None
#if os.environ.get('RPC_AUTHENTICATION') == 'custom_token':
if os.environ.get('RPC_AUTHENTICATION') == 'basic':
from chainlib.auth import BasicAuth
#auth = CustomHeaderTokenAuth('x-api-key', os.environ['RPC_AUTHENTICATION_STRING'])
auth = BasicAuth(os.environ['RPC_USERNAME'], os.environ['RPC_PASSWORD'])
conn = EthHTTPConnection(args.p) conn = EthHTTPConnection(args.p)
nonce_oracle = None nonce_oracle = None
if args.nonce != None: if args.nonce != None:
nonce_oracle = OverrideNonceOracle(signer_address, args.nonce, id_generator=rpc_id_generator) nonce_oracle = OverrideNonceOracle(signer_address, args.nonce)
else: else:
nonce_oracle = RPCNonceOracle(signer_address, conn, id_generator=rpc_id_generator) nonce_oracle = RPCNonceOracle(signer_address, conn)
#def _max_gas(code=None): def _max_gas(code=None):
# return 8000000 return 8000000
gas_oracle = None gas_oracle = None
if args.gas_price != None or args.gas_limit != None: if args.gas_price != None or args.gas_limit != None:
gas_oracle = OverrideGasOracle(price=args.gas_price, limit=args.gas_limit, id_generator=rpc_id_generator, conn=conn) gas_oracle = OverrideGasOracle(price=args.gas_price, limit=args.gas_limit)
else: else:
gas_oracle = RPCGasOracle(conn, code_callback=ERC20.gas, id_generator=rpc_id_generator) gas_oracle = RPCGasOracle(conn, code_callback=_max_gas)
chain_spec = ChainSpec.from_chain_str(args.i) chain_spec = ChainSpec.from_chain_str(args.i)
chain_id = chain_spec.network_id() chain_id = chain_spec.network_id()
@ -135,8 +118,8 @@ send = args.s
g = ERC20(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) g = ERC20(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
def balance(token_address, address, id_generator=None): def balance(token_address, address):
o = g.balance(token_address, address, id_generator=id_generator) o = g.balance(token_address, address)
r = conn.do(o) r = conn.do(o)
hx = strip_0x(r) hx = strip_0x(r)
return int(hx, 16) return int(hx, 16)
@ -148,22 +131,18 @@ def main():
raise ValueError('invalid checksum address') raise ValueError('invalid checksum address')
if logg.isEnabledFor(logging.DEBUG): if logg.isEnabledFor(logging.DEBUG):
sender_balance = balance(args.a, signer_address, id_generator=rpc_id_generator) logg.debug('sender {} balance after: {}'.format(signer_address, balance(args.a, signer_address)))
recipient_balance = balance(args.a, recipient, id_generator=rpc_id_generator) logg.debug('recipient {} balance after: {}'.format(recipient, balance(args.a, recipient)))
logg.debug('sender {} balance after: {}'.format(signer_address, sender_balance))
logg.debug('recipient {} balance after: {}'.format(recipient, recipient_balance))
(tx_hash_hex, o) = g.transfer(args.a, signer_address, recipient, value, id_generator=rpc_id_generator) (tx_hash_hex, o) = g.transfer(args.a, signer_address, recipient, value)
if send: if send:
conn.do(o) conn.do(o)
if block_last: if block_last:
r = conn.wait(tx_hash_hex) r = conn.wait(tx_hash_hex)
if logg.isEnabledFor(logging.DEBUG): if logg.isEnabledFor(logging.DEBUG):
sender_balance = balance(args.a, signer_address, id_generator=rpc_id_generator) logg.debug('sender {} balance after: {}'.format(signer_address, balance(args.a, signer_address)))
recipient_balance = balance(args.a, recipient, id_generator=rpc_id_generator) logg.debug('recipient {} balance after: {}'.format(recipient, balance(args.a, recipient)))
logg.debug('sender {} balance after: {}'.format(signer_address, sender_balance))
logg.debug('recipient {} balance after: {}'.format(recipient, recipient_balance))
if r['status'] == 0: if r['status'] == 0:
logg.critical('VM revert. Wish I could tell you more') logg.critical('VM revert. Wish I could tell you more')
sys.exit(1) sys.exit(1)

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_minter","type":"address"},{"indexed":true,"internalType":"address","name":"_beneficiary","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"TransferFrom","type":"event"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"addMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"mintTo","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"removeMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"_sum","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}] [{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"TransferFrom","type":"event"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"addMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"mintTo","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"removeMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]

View File

@ -40,7 +40,6 @@ class GiftableToken(TxFactory):
def gas(code=None): def gas(code=None):
return 1500000 return 1500000
@staticmethod @staticmethod
def abi(): def abi():
if GiftableToken.__abi == None: if GiftableToken.__abi == None:

View File

@ -1,4 +1,4 @@
confini~=0.3.6rc3 confini~=0.3.6rc3
crypto-dev-signer~=0.4.14b6 crypto-dev-signer~=0.4.14b3
chainlib~=0.0.4a1 chainlib~=0.0.3a1
potaahto~=0.0.1a2 potaahto~=0.0.1a2

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = eth-erc20 name = eth-erc20
version = 0.0.10a1 version = 0.0.9a2
description = ERC20 interface and simple contract with deployment script that lets any address mint and gift itself tokens. description = ERC20 interface and simple contract with deployment script that lets any address mint and gift itself tokens.
author = Louis Holbrook author = Louis Holbrook
author_email = dev@holbrook.no author_email = dev@holbrook.no

25
python/tests/conftest.py Normal file
View File

@ -0,0 +1,25 @@
# standard imports
import os
# external imports
import pytest
from crypto_dev_signer.keystore.dict import DictKeystore
from crypto_dev_signer.eth.signer import ReferenceSigner as EIP155Signer
@pytest.fixture(scope='session')
def keystore():
ks = DictKeystore()
pk = os.urandom(32)
ks.import_raw_key(pk)
return ks
@pytest.fixture(scope='session')
def signer(
keystore,
):
s = EIP155Signer(keystore)
return s

View File

@ -0,0 +1,74 @@
# standard imports
import logging
import os
# external imports
from hexathon import (
strip_0x,
add_0x,
)
from chainlib.eth.address import to_checksum_address
from chainlib.eth.tx import (
unpack,
TxFormat,
)
from chainlib.eth.pytest import *
# local imports
from eth_erc20 import ERC20
logg = logging.getLogger()
contract_address = to_checksum_address('0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef')
benefactor_address = to_checksum_address('0xefdeadbeefdeadbeefdeadbeefdeadbeefdeadbe')
# TODO: use unittest instead
def test_erc20_balance(
default_chain_spec,
):
e = ERC20(default_chain_spec,)
holder_address = to_checksum_address('0xbeefdeadbeefdeadbeefdeadbeefdeadbeefdead')
o = e.balance_of(contract_address, holder_address)
assert len(o['params'][0]['data']) == 64 + 8 + 2
assert o['params'][0]['data'][:10] == add_0x('70a08231')
def test_erc20_decimals(
default_chain_spec,
):
e = ERC20(default_chain_spec)
o = e.decimals(contract_address)
assert o['params'][0]['data'] == add_0x('313ce567')
def test_erc20_transfer(
keystore,
signer,
default_chain_spec,
):
e = ERC20(default_chain_spec, signer=signer)
addresses = keystore.list()
(tx_hash_hex, o) = e.transfer(contract_address, addresses[0], benefactor_address, 1024)
def test_erc20_parse_transfer_request(
keystore,
signer,
default_chain_spec,
):
e = ERC20(default_chain_spec, signer=signer)
addresses = keystore.list()
(tx_hash_hex, o) = e.transfer(contract_address, addresses[0], benefactor_address, 1024, tx_format=TxFormat.RLP_SIGNED)
b = bytes.fromhex(strip_0x(o))
#chain_spec = ChainSpec('evm', 'foo', 1, 'bar')
tx = unpack(b, default_chain_spec)
r = ERC20.parse_transfer_request(tx['data'])
assert r[0] == benefactor_address
assert r[1] == 1024

View File

@ -1,189 +0,0 @@
# standard imports
import logging
import unittest
# external imports
from chainlib.eth.unittest.ethtester import EthTesterCase
from chainlib.connection import RPCConnection
from chainlib.eth.nonce import RPCNonceOracle
from chainlib.eth.gas import OverrideGasOracle
from chainlib.eth.tx import (
transaction,
receipt,
)
from chainlib.eth.address import to_checksum_address
# local imports
from giftable_erc20_token import GiftableToken
from eth_erc20 import ERC20
logg = logging.getLogger(__name__)
class TestToken(EthTesterCase):
def setUp(self):
super(TestToken, self).setUp()
self.conn = RPCConnection.connect(self.chain_spec, 'default')
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
self.symbol = 'FOO'
self.name = 'Foo Token'
self.decimals = 16
(tx_hash, o) = c.constructor(self.accounts[0], self.name, self.symbol, self.decimals)
r = self.conn.do(o)
logg.debug('deployed with hash {}'.format(r))
o = receipt(r)
r = self.conn.do(o)
self.address = to_checksum_address(r['contract_address'])
self.initial_supply = 1 << 40
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], self.initial_supply)
r = self.conn.do(o)
o = receipt(tx_hash)
r = self.conn.do(o)
self.assertEqual(r['status'], 1)
def test_balance(self):
c = ERC20(self.chain_spec)
o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
r = self.conn.do(o)
balance = ERC20.parse_balance(r)
self.assertEqual(self.initial_supply, balance)
def test_supply(self):
c = ERC20(self.chain_spec)
o = c.total_supply(self.address, sender_address=self.accounts[0])
r = self.conn.do(o)
supply = ERC20.parse_total_supply(r)
self.assertEqual(self.initial_supply, supply)
def test_name(self):
c = ERC20(self.chain_spec)
o = c.name(self.address, sender_address=self.accounts[0])
r = self.conn.do(o)
name = ERC20.parse_name(r)
self.assertEqual(self.name, name)
def test_symbol(self):
c = ERC20(self.chain_spec)
o = c.symbol(self.address, sender_address=self.accounts[0])
r = self.conn.do(o)
symbol = ERC20.parse_symbol(r)
self.assertEqual(self.symbol, symbol)
def test_transfer(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
gas_oracle = OverrideGasOracle(limit=100000, conn=self.conn)
c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash, o) = c.transfer(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)
o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
r = self.rpc.do(o)
balance = c.parse_balance(r)
self.assertEqual(balance, self.initial_supply - 1000)
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
r = self.rpc.do(o)
balance = c.parse_balance(r)
self.assertEqual(balance, 1000)
o = transaction(tx_hash)
r = self.rpc.do(o)
data = c.parse_transfer_request(r['data'])
self.assertEqual(data[0], self.accounts[1])
self.assertEqual(data[1], 1000)
def test_transfer_from(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
gas_oracle = OverrideGasOracle(limit=100000, conn=self.conn)
c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash, o) = c.approve(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)
o = transaction(tx_hash)
r = self.rpc.do(o)
data = c.parse_approve_request(r['data'])
self.assertEqual(data[0], self.accounts[1])
self.assertEqual(data[1], 1000)
nonce_oracle = RPCNonceOracle(self.accounts[1], conn=self.conn)
c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash, o) = c.transfer_from(self.address, self.accounts[1], self.accounts[0], self.accounts[2], 1001)
r = self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 0)
o = transaction(tx_hash)
r = self.rpc.do(o)
data = c.parse_transfer_from_request(r['data'])
self.assertEqual(data[0], self.accounts[0])
self.assertEqual(data[1], self.accounts[2])
self.assertEqual(data[2], 1001)
(tx_hash, o) = c.transfer_from(self.address, self.accounts[1], self.accounts[0], self.accounts[2], 1000)
r = self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
r = self.rpc.do(o)
balance = c.parse_balance(r)
self.assertEqual(balance, self.initial_supply - 1000)
o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0])
r = self.rpc.do(o)
balance = c.parse_balance(r)
self.assertEqual(balance, 1000)
def test_revoke_approve(self):
nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
gas_oracle = OverrideGasOracle(limit=100000, conn=self.conn)
c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash, o) = c.approve(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)
(tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 999)
r = self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 0)
(tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 0)
r = self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 1)
nonce_oracle = RPCNonceOracle(self.accounts[1], conn=self.conn)
c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
(tx_hash, o) = c.transfer_from(self.address, self.accounts[1], self.accounts[0], self.accounts[2], 1)
r = self.rpc.do(o)
o = receipt(tx_hash)
r = self.rpc.do(o)
self.assertEqual(r['status'], 0)
if __name__ == '__main__':
unittest.main()

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_minter","type":"address"},{"indexed":true,"internalType":"address","name":"_beneficiary","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"TransferFrom","type":"event"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"addMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"mintTo","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"removeMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"_sum","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}] [{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"TransferFrom","type":"event"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"addMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"mintTo","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"removeMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}]

View File

@ -82,16 +82,13 @@ contract GiftableToken {
// Implements ERC20 // Implements ERC20
function approve(address _spender, uint256 _value) public returns (bool) { function approve(address _spender, uint256 _value) public returns (bool) {
if (_value > 0) { allowance[msg.sender][_spender] += _value;
require(allowance[msg.sender][_spender] == 0);
}
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value); emit Approval(msg.sender, _spender, _value);
return true; return true;
} }
// Implements EIP165 // Implements EIP165
function supportsInterface(bytes4 _sum) public returns (bool) { function supportsInterface(bytes4 _sum) {
if (_sum == 0xc6bb4b70) { // ERC20 if (_sum == 0xc6bb4b70) { // ERC20
return true; return true;
} }