erc20-faucet

ERC20 token faucet
Log | Files | Refs

commit 836dc95653788b7c39a6e34316a870a1bc6ccbf1
parent e3cee0101823dc64e0a810260a350f59bb7390b1
Author: nolash <dev@holbrook.no>
Date:   Fri, 30 Apr 2021 19:41:43 +0200

Rehabilitate test to successful give

Diffstat:
Mpython/erc20_single_shot_faucet/interface.py | 27+++++++++++++++++++++++++++
Mpython/tests/test_basic.py | 43++++++++++++++++++++++++++++++++++++++++++-
2 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/python/erc20_single_shot_faucet/interface.py b/python/erc20_single_shot_faucet/interface.py @@ -9,6 +9,9 @@ from chainlib.eth.contract import ( ABIContractEncoder, ABIContractType, ) +from chainlib.eth.tx import ( + TxFormat, + ) from chainlib.jsonrpc import jsonrpc_template from hexathon import add_0x @@ -17,6 +20,30 @@ logg = logging.getLogger().getChild(__name__) class Faucet(TxFactory): + def give_to(self, contract_address, sender_address, beneficiary_address, tx_format=TxFormat.JSONRPC): + enc = ABIContractEncoder() + enc.method('giveTo') + enc.typ(ABIContractType.ADDRESS) + enc.address(beneficiary_address) + 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 + + + def set_amount(self, contract_address, sender_address, amount, tx_format=TxFormat.JSONRPC): + enc = ABIContractEncoder() + enc.method('setAmount') + enc.typ(ABIContractType.UINT256) + enc.uint256(amount) + 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 + + def usable_for(self, contract_address, address, sender_address=ZERO_ADDRESS): o = jsonrpc_template() o['method'] = 'eth_call' diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py @@ -18,6 +18,7 @@ from chainlib.eth.contract import ( abi_decode_single, ABIContractType, ) +from chainlib.eth.erc20 import ERC20 from chainlib.eth.nonce import RPCNonceOracle from chainlib.eth.constant import ZERO_ADDRESS from giftable_erc20_token import GiftableToken @@ -67,7 +68,47 @@ class TestFaucet(EthTesterCase): def test_basic(self): - pass + nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) + c = Faucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[1]) + self.conn.do(o) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.assertEqual(r['status'], 1) + + + def test_amount(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) + c = Faucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash_hex, o) = c.set_amount(self.address, self.accounts[0], 1024) + self.conn.do(o) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.assertEqual(r['status'], 1) + + ct = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash_hex, o) = ct.mint_to(self.token_address, self.accounts[0], self.address, 2048) + self.conn.do(o) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[1]) + self.conn.do(o) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.assertEqual(r['status'], 1) + + ct = ERC20(self.chain_spec) + o = ct.balance_of(self.token_address, self.accounts[1], sender_address=self.accounts[0]) + r = self.conn.do(o) + + amount = ct.parse_balance(r) + self.assertEqual(amount, 1024) if __name__ == '__main__':