test_basic.py (4812B)
1 # standard imports 2 import os 3 import unittest 4 import json 5 import logging 6 7 # external imports 8 from chainlib.eth.unittest.ethtester import EthTesterCase 9 from chainlib.connection import RPCConnection 10 from chainlib.eth.nonce import RPCNonceOracle 11 from chainlib.eth.address import to_checksum_address 12 from chainlib.eth.tx import ( 13 receipt, 14 transaction, 15 TxFormat, 16 ) 17 from chainlib.eth.contract import ( 18 abi_decode_single, 19 ABIContractType, 20 ) 21 from eth_erc20 import ERC20 22 from chainlib.eth.nonce import RPCNonceOracle 23 from chainlib.eth.constant import ZERO_ADDRESS 24 from giftable_erc20_token import GiftableToken 25 26 # local imports 27 from erc20_faucet import Faucet 28 from erc20_faucet.faucet import SingleShotFaucet 29 from eth_owned import ERC173 30 31 logging.basicConfig(level=logging.DEBUG) 32 logg = logging.getLogger() 33 34 35 class TestFaucet(EthTesterCase): 36 37 def setUp(self): 38 super(TestFaucet, self).setUp() 39 self.conn = RPCConnection.connect(self.chain_spec, 'default') 40 nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) 41 c = SingleShotFaucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 42 (tx_hash, o) = c.store_constructor(self.accounts[0]) 43 r = self.conn.do(o) 44 logg.debug('store published with hash {}'.format(r)) 45 46 o = receipt(r) 47 r = self.conn.do(o) 48 self.assertEqual(r['status'], 1) 49 self.store_address = to_checksum_address(r['contract_address']) 50 logg.debug('store contract {}'.format(self.store_address)) 51 52 53 ct = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 54 (tx_hash_hex, o) = ct.constructor(self.accounts[0], 'Foo Token', 'FOO', 6) 55 r = self.conn.do(o) 56 logg.debug('token published with hash {}'.format(r)) 57 58 o = receipt(r) 59 r = self.conn.do(o) 60 self.token_address = to_checksum_address(r['contract_address']) 61 logg.debug('token contract {}'.format(self.store_address)) 62 63 (tx_hash, o) = c.constructor(self.accounts[0], self.token_address, self.store_address, ZERO_ADDRESS) 64 r = self.conn.do(o) 65 logg.debug('faucet published with hash {}'.format(r)) 66 o = receipt(r) 67 r = self.conn.do(o) 68 self.assertEqual(r['status'], 1) 69 70 self.address = to_checksum_address(r['contract_address']) 71 logg.debug('faucet contract {}'.format(self.address)) 72 73 c_owned = ERC173(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 74 (tx_hash, o) = c_owned.transfer_ownership(self.store_address, self.accounts[0], self.address) 75 r = self.conn.do(o) 76 o = receipt(r) 77 r = self.conn.do(o) 78 self.assertEqual(r['status'], 1) 79 80 81 def test_basic(self): 82 nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) 83 c = Faucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 84 (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2]) 85 self.conn.do(o) 86 87 o = receipt(tx_hash_hex) 88 r = self.conn.do(o) 89 self.assertEqual(r['status'], 1) 90 91 92 def test_amount(self): 93 nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) 94 c = Faucet(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 95 (tx_hash_hex, o) = c.set_amount(self.address, self.accounts[0], 1024) 96 self.conn.do(o) 97 98 o = receipt(tx_hash_hex) 99 r = self.conn.do(o) 100 self.assertEqual(r['status'], 1) 101 102 ct = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 103 (tx_hash_hex, o) = ct.mint_to(self.token_address, self.accounts[0], self.address, 2048) 104 self.conn.do(o) 105 106 o = receipt(tx_hash_hex) 107 r = self.conn.do(o) 108 self.assertEqual(r['status'], 1) 109 110 (tx_hash_hex, o) = c.give_to(self.address, self.accounts[0], self.accounts[2]) 111 self.conn.do(o) 112 113 o = receipt(tx_hash_hex) 114 r = self.conn.do(o) 115 self.assertEqual(r['status'], 1) 116 117 ct = ERC20(self.chain_spec) 118 o = ct.balance_of(self.token_address, self.accounts[2], sender_address=self.accounts[0]) 119 r = self.conn.do(o) 120 121 amount = ct.parse_balance(r) 122 self.assertEqual(amount, 1024) 123 124 125 def test_signatures(self): 126 snake = Faucet(self.chain_spec).signature_for('give_to') 127 camel = Faucet(self.chain_spec).signature_for('giveTo') 128 self.assertEqual(snake, camel) 129 method = Faucet(self.chain_spec).method_for(snake) 130 self.assertEqual(method, 'give_to') 131 hx = snake.hex().ljust(64+8, 'f') 132 method = Faucet(self.chain_spec).method_for(hx) 133 self.assertEqual(method, 'give_to') 134 135 if __name__ == '__main__': 136 unittest.main()