eth-erc20

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

test_giftable.py (4318B)


      1 # standard imports
      2 import os
      3 import unittest
      4 import json
      5 import logging
      6 import datetime
      7 
      8 # external imports
      9 from chainlib.eth.constant import ZERO_ADDRESS
     10 from chainlib.eth.nonce import RPCNonceOracle
     11 from chainlib.eth.tx import receipt
     12 from chainlib.eth.block import (
     13         block_latest,
     14         block_by_number,
     15     )
     16 
     17 # local imports
     18 from giftable_erc20_token import GiftableToken
     19 from giftable_erc20_token.unittest import TestGiftableExpireToken
     20 
     21 logging.basicConfig(level=logging.DEBUG)
     22 logg = logging.getLogger()
     23 
     24 testdir = os.path.dirname(__file__)
     25 
     26 
     27 class TestExpire(TestGiftableExpireToken):
     28 
     29     def setUp(self):
     30         super(TestExpire, self).setUp()
     31 
     32 
     33     def test_expires(self):
     34         mint_amount = self.initial_supply
     35         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     36         c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     37 
     38         (tx_hash, o) = c.transfer(self.address, self.accounts[0], self.accounts[1], int(mint_amount / 2))
     39         r = self.rpc.do(o)
     40         o = receipt(tx_hash)
     41         r = self.rpc.do(o)
     42         self.assertEqual(r['status'], 1)
     43 
     44         self.backend.time_travel(self.expire + 60)
     45         o = block_latest()
     46         r = self.rpc.do(o)
     47         o = block_by_number(r)
     48         r = self.rpc.do(o)
     49         self.assertGreaterEqual(r['timestamp'], self.expire)
     50         
     51         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     52         c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     53         (tx_hash, o) = c.transfer(self.address, self.accounts[0], self.accounts[1], 1)
     54         r = self.rpc.do(o)
     55         o = receipt(tx_hash)
     56         r = self.rpc.do(o)
     57         self.assertEqual(r['status'], 0)
     58         
     59         nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
     60         c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     61         (tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[0], 1)
     62         r = self.rpc.do(o)
     63         o = receipt(tx_hash)
     64         r = self.rpc.do(o)
     65         self.assertEqual(r['status'], 0)
     66 
     67         o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
     68         r = self.rpc.do(o)
     69         balance = c.parse_balance(r)
     70         self.assertEqual(balance, int(mint_amount / 2))
     71 
     72         o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
     73         r = self.rpc.do(o)
     74         balance += c.parse_balance(r)
     75         self.assertEqual(balance, mint_amount)
     76 
     77         o = c.total_supply(self.address, sender_address=self.accounts[0])
     78         r = self.rpc.do(o)
     79         supply = c.parse_balance(r)
     80         self.assertEqual(supply, mint_amount)
     81 
     82 
     83     def test_burn(self):
     84         mint_amount = self.initial_supply
     85         nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
     86         c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     87         (tx_hash, o) = c.burn(self.address, self.accounts[1], int(mint_amount / 4))
     88         self.rpc.do(o)
     89         o = receipt(tx_hash)
     90         r = self.rpc.do(o)
     91         self.assertEqual(r['status'], 0)
     92 
     93         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     94         c = GiftableToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     95         (tx_hash, o) = c.burn(self.address, self.accounts[0], int(mint_amount / 4))
     96         self.rpc.do(o)
     97         o = receipt(tx_hash)
     98         r = self.rpc.do(o)
     99         self.assertEqual(r['status'], 1)
    100 
    101         o = c.burned(self.address, sender_address=self.accounts[0])
    102         r = self.rpc.do(o)
    103         burned = c.parse_balance(r)
    104         self.assertEqual(burned, int(mint_amount / 4))
    105 
    106         o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0])
    107         r = self.rpc.do(o)
    108         balance = c.parse_balance(r)
    109         self.assertEqual(balance, mint_amount - burned)
    110 
    111         o = c.total_supply(self.address, sender_address=self.accounts[0])
    112         r = self.rpc.do(o)
    113         balance = c.parse_balance(r)
    114         self.assertEqual(balance, mint_amount - burned)
    115 
    116         o = c.total_minted(self.address, sender_address=self.accounts[0])
    117         r = self.rpc.do(o)
    118         balance = c.parse_balance(r)
    119         self.assertEqual(balance, mint_amount)
    120 
    121 
    122 if __name__ == '__main__':
    123     unittest.main()