erc20-transfer-authorization

Simple approval escrow for ERC20 spending
Info | Log | Files | Refs

test_transfer.py (5059B)


      1 # standard imports
      2 import os
      3 import unittest
      4 import logging
      5 
      6 # external imports
      7 from chainlib.eth.nonce import RPCNonceOracle
      8 from chainlib.eth.address import is_same_address
      9 from chainlib.eth.tx import receipt
     10 from giftable_erc20_token import GiftableToken
     11 from eth_erc20 import ERC20
     12 
     13 # local imports
     14 from erc20_transfer_authorization import TransferAuthorization
     15 from erc20_transfer_authorization.unittest import TestBase
     16 
     17 logg = logging.getLogger()
     18 
     19 testdir = os.path.dirname(__file__)
     20 
     21 
     22 class TestTransfer(TestBase):
     23 
     24     def setUp(self):
     25         super(TestTransfer, self).setUp()
     26         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     27         self.c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     28 
     29         c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     30         (tx_hash_hex, o) = c.mint_to(self.token_address, self.accounts[0], self.accounts[9], 10000)
     31         self.rpc.do(o)
     32         o = receipt(tx_hash_hex)
     33         r = self.rpc.do(o)
     34         self.assertEqual(r['status'], 1)
     35 
     36         nonce_oracle = RPCNonceOracle(self.accounts[9], self.rpc)
     37         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     38         (tx_hash_hex, o) = c.approve(self.token_address, self.accounts[9], self.address, 1024)
     39         self.rpc.do(o)
     40         o = receipt(tx_hash_hex)
     41         r = self.rpc.do(o)
     42         self.assertEqual(r['status'], 1)
     43 
     44 
     45     def test_premature_transfer(self):
     46         (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[9], self.accounts[2], self.token_address, 1024)
     47         self.rpc.do(o)
     48         o = receipt(tx_hash_hex)
     49         r = self.rpc.do(o)
     50         self.assertEqual(r['status'], 1)
     51 
     52         (tx_hash_hex, o) = self.c.check_result(self.address, self.accounts[0], 1)
     53         r = self.rpc.do(o)
     54         o = receipt(tx_hash_hex)
     55         r = self.rpc.do(o)
     56         self.assertEqual(r['status'], 1)
     57 
     58         (tx_hash_hex, o) = self.c.execute_request(self.address, self.accounts[0], 1)
     59         r = self.rpc.do(o)
     60         o = receipt(tx_hash_hex)
     61         r = self.rpc.do(o)
     62         self.assertEqual(r['status'], 0)
     63 
     64         o = self.c.requests(self.address, 1, sender_address=self.accounts[0])
     65         r = self.rpc.do(o)
     66         request = self.c.parse_request(r)
     67         self.assertTrue(request.is_voting())
     68         self.assertFalse(request.is_executed())
     69 
     70 
     71     def test_reject_transfer(self):
     72         (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[9], self.accounts[2], self.token_address, 1024)
     73         self.rpc.do(o)
     74         o = receipt(tx_hash_hex)
     75         r = self.rpc.do(o)
     76         self.assertEqual(r['status'], 1)
     77 
     78         (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 1, 1)
     79         self.rpc.do(o)
     80 
     81         (tx_hash_hex, o) = self.c.nay(self.address, self.accounts[0], 1)
     82         self.rpc.do(o)
     83 
     84         (tx_hash_hex, o) = self.c.check_result(self.address, self.accounts[0], 1)
     85         r = self.rpc.do(o)
     86        
     87         (tx_hash_hex, o) = self.c.execute_request(self.address, self.accounts[0], 1)
     88         r = self.rpc.do(o)
     89         o = receipt(tx_hash_hex)
     90         r = self.rpc.do(o)
     91         self.assertEqual(r['status'], 0)
     92 
     93         o = self.c.requests(self.address, 1, sender_address=self.accounts[0])
     94         r = self.rpc.do(o)
     95         request = self.c.parse_request(r)
     96         self.assertTrue(request.is_rejected())
     97         self.assertFalse(request.is_voting())
     98         self.assertFalse(request.is_executed())
     99 
    100 
    101     def test_transfer(self):
    102         (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[9], self.accounts[2], self.token_address, 1024)
    103         self.rpc.do(o)
    104        
    105         (tx_hash_hex, o) = self.c.yay(self.address, self.accounts[0], 1)
    106         self.rpc.do(o)
    107 
    108         (tx_hash_hex, o) = self.c.check_result(self.address, self.accounts[0], 1)
    109         r = self.rpc.do(o)
    110 
    111         (tx_hash_hex, o) = self.c.execute_request(self.address, self.accounts[0], 1)
    112         r = self.rpc.do(o)
    113         o = receipt(tx_hash_hex)
    114         r = self.rpc.do(o)
    115         self.assertEqual(r['status'], 1)
    116 
    117         o = self.c.requests(self.address, 1, sender_address=self.accounts[0])
    118         r = self.rpc.do(o)
    119         request = self.c.parse_request(r)
    120         logg.debug('request {}'.format(request))
    121         self.assertTrue(request.is_transferred())
    122         self.assertTrue(request.is_executed())
    123 
    124         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
    125         c = ERC20(self.chain_spec, nonce_oracle=nonce_oracle)
    126         o = c.balance(self.token_address, self.accounts[9], sender_address=self.accounts[0])
    127         r = self.rpc.do(o)
    128         balance = c.parse_balance(r)
    129         self.assertEqual(balance, 10000-1024)
    130 
    131         (tx_hash_hex, o) = self.c.execute_request(self.address, self.accounts[0], 1)
    132         r = self.rpc.do(o)
    133         o = receipt(tx_hash_hex)
    134         r = self.rpc.do(o)
    135         self.assertEqual(r['status'], 0)
    136 
    137 
    138 if __name__ == '__main__':
    139     unittest.main()