eth-erc20

ERC20 interface and example giftable token contract
Info | Log | Files | Refs | LICENSE

base.py (6429B)


      1 # standard imports
      2 import logging
      3 
      4 # external imports
      5 from chainlib.eth.unittest.ethtester import EthTesterCase
      6 from chainlib.eth.nonce import RPCNonceOracle
      7 from chainlib.eth.gas import OverrideGasOracle
      8 from chainlib.eth.tx import (
      9         transaction,
     10         receipt,
     11         )
     12 from hexathon import strip_0x
     13 
     14 # local imports
     15 from eth_erc20 import ERC20
     16 
     17 logging.basicConfig(level=logging.DEBUG)
     18 logg = logging.getLogger(__name__)
     19 
     20 
     21 class TestInterface:
     22 
     23     def test_balance(self):
     24         c = ERC20(self.chain_spec)
     25         o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
     26         r = self.conn.do(o)
     27         balance = ERC20.parse_balance(r)
     28         self.assertEqual(self.initial_supply, balance)
     29 
     30 
     31     def test_supply(self):
     32         c = ERC20(self.chain_spec)
     33         o = c.total_supply(self.address, sender_address=self.accounts[0])
     34         r = self.conn.do(o)
     35         supply = ERC20.parse_total_supply(r)
     36         self.assertEqual(self.initial_supply, supply)
     37 
     38 
     39     def test_name(self):
     40         c = ERC20(self.chain_spec)
     41         o = c.name(self.address, sender_address=self.accounts[0])
     42         r = self.conn.do(o)
     43         name = ERC20.parse_name(r)
     44         self.assertEqual(self.name, name)
     45 
     46 
     47     def test_symbol(self):
     48         c = ERC20(self.chain_spec)
     49         o = c.symbol(self.address, sender_address=self.accounts[0])
     50         r = self.conn.do(o)
     51         symbol = ERC20.parse_symbol(r)
     52         self.assertEqual(self.symbol, symbol)
     53 
     54 
     55     def test_direct_transfer(self):
     56         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
     57         gas_oracle = OverrideGasOracle(limit=100000, conn=self.conn)
     58         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
     59 
     60         (tx_hash, o) = c.transfer(self.address, self.accounts[0], self.accounts[1], 1000)
     61         self.rpc.do(o)
     62         o = receipt(tx_hash)
     63         r = self.rpc.do(o)
     64         self.assertEqual(r['status'], 1)
     65 
     66         o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
     67         r = self.rpc.do(o)
     68         balance = c.parse_balance(r)
     69         self.assertEqual(balance, self.initial_supply - 1000)
     70 
     71         o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
     72         r = self.rpc.do(o)
     73         balance = c.parse_balance(r)
     74         self.assertEqual(balance, 1000)
     75 
     76         o = transaction(tx_hash)
     77         r = self.rpc.do(o)
     78         data = c.parse_transfer_request(r['data'])
     79         self.assertEqual(data[0], strip_0x(self.accounts[1]))
     80         self.assertEqual(data[1], 1000)
     81 
     82 
     83     def test_transfer_from(self):
     84         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
     85         gas_oracle = OverrideGasOracle(limit=100000, conn=self.conn)
     86         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
     87         (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 1000)
     88         self.rpc.do(o)
     89         o = receipt(tx_hash)
     90         r = self.rpc.do(o)
     91         self.assertEqual(r['status'], 1)
     92 
     93         o = c.allowance(self.address, self.accounts[0], self.accounts[1], sender_address=self.accounts[0])
     94         r = self.rpc.do(o)
     95         allowance = c.parse_allowance(r)
     96         self.assertEqual(allowance, 1000)
     97 
     98         o = transaction(tx_hash)
     99         r = self.rpc.do(o)
    100         data = c.parse_approve_request(r['data'])
    101         self.assertEqual(data[0], strip_0x(self.accounts[1]))
    102         self.assertEqual(data[1], 1000)
    103 
    104         nonce_oracle = RPCNonceOracle(self.accounts[1], conn=self.conn)
    105         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
    106         (tx_hash, o) = c.transfer_from(self.address, self.accounts[1], self.accounts[0], self.accounts[2], 1001)
    107         r = self.rpc.do(o)
    108         o = receipt(tx_hash)
    109         r = self.rpc.do(o)
    110         self.assertEqual(r['status'], 0)
    111 
    112         o = transaction(tx_hash)
    113         r = self.rpc.do(o)
    114         data = c.parse_transfer_from_request(r['data'])
    115         self.assertEqual(data[0], strip_0x(self.accounts[0]))
    116         self.assertEqual(data[1], strip_0x(self.accounts[2]))
    117         self.assertEqual(data[2], 1001)
    118 
    119         (tx_hash, o) = c.transfer_from(self.address, self.accounts[1], self.accounts[0], self.accounts[2], 1000)
    120         r = self.rpc.do(o)
    121         o = receipt(tx_hash)
    122         r = self.rpc.do(o)
    123         self.assertEqual(r['status'], 1)
    124 
    125         o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
    126         r = self.rpc.do(o)
    127         balance = c.parse_balance(r)
    128         self.assertEqual(balance, self.initial_supply - 1000)
    129 
    130         o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0])
    131         r = self.rpc.do(o)
    132         balance = c.parse_balance(r)
    133         self.assertEqual(balance, 1000)
    134 
    135 
    136     def test_revoke_approve(self):
    137         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.conn)
    138         gas_oracle = OverrideGasOracle(limit=100000, conn=self.conn)
    139         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
    140         (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 1000)
    141         r = self.rpc.do(o)
    142         o = receipt(tx_hash)
    143         r = self.rpc.do(o)
    144         self.assertEqual(r['status'], 1)
    145         
    146         (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 999)
    147         r = self.rpc.do(o)
    148         o = receipt(tx_hash)
    149         r = self.rpc.do(o)
    150         self.assertEqual(r['status'], 0)
    151      
    152         (tx_hash, o) = c.approve(self.address, self.accounts[0], self.accounts[1], 0)
    153         r = self.rpc.do(o)
    154         o = receipt(tx_hash)
    155         r = self.rpc.do(o)
    156         self.assertEqual(r['status'], 1)
    157  
    158         o = c.allowance(self.address, self.accounts[0], self.accounts[1], sender_address=self.accounts[0])
    159         r = self.rpc.do(o)
    160         allowance = c.parse_allowance(r)
    161         self.assertEqual(allowance, 0)
    162 
    163         nonce_oracle = RPCNonceOracle(self.accounts[1], conn=self.conn)
    164         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle)
    165         (tx_hash, o) = c.transfer_from(self.address, self.accounts[1], self.accounts[0], self.accounts[2], 1)
    166         r = self.rpc.do(o)
    167         o = receipt(tx_hash)
    168         r = self.rpc.do(o)
    169         self.assertEqual(r['status'], 0)