evm-booking

EVM smart contract for ERC20 backed time slot booking
Info | Log | Files | Refs | README

test_base.py (8602B)


      1 # standard imports
      2 import unittest
      3 import logging
      4 import os
      5 from chainlib.eth.nonce import RPCNonceOracle
      6 from chainlib.eth.tx import receipt
      7 from eth_erc20 import ERC20
      8 from giftable_erc20_token import GiftableToken
      9 
     10 # local imports
     11 from evm_booking.unittest import TestBooking
     12 from evm_booking import Booking
     13 
     14 
     15 logging.basicConfig(level=logging.DEBUG)
     16 logg = logging.getLogger()
     17 
     18 
     19 class TestBookingBase(TestBooking):
     20 
     21     def setUp(self):
     22         super(TestBookingBase, self).setUp()
     23         self.publish()
     24 
     25 
     26     def test_base(self):
     27         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
     28         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     29         (tx_hash_hex, o) = c.consume(self.address, self.accounts[0], 42, 13)
     30         self.rpc.do(o)
     31         o = receipt(tx_hash_hex)
     32         r = self.rpc.do(o)
     33         self.assertEqual(r['status'], 0)
     34 
     35         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     36         (tx_hash_hex, o) = c.approve(self.token_address, self.accounts[0], self.address, self.initial_supply)
     37         self.rpc.do(o)
     38 
     39         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     40         (tx_hash_hex, o) = c.consume(self.address, self.accounts[0], 42, 13)
     41         self.rpc.do(o)
     42         o = receipt(tx_hash_hex)
     43         r = self.rpc.do(o)
     44         self.assertEqual(r['status'], 1)
     45 
     46         c = ERC20(self.chain_spec)
     47         o = c.balance_of(self.token_address, self.accounts[0], sender_address=self.accounts[0])
     48         r = self.rpc.do(o)
     49         balance = c.parse_balance_of(r)
     50         expected_balance = self.initial_supply - (self.resolution_unit * 13)
     51         self.assertEqual(balance, expected_balance)
     52 
     53         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     54         (tx_hash_hex, o) = c.consume(self.address, self.accounts[0], 42, 1)
     55         self.rpc.do(o)
     56         o = receipt(tx_hash_hex)
     57         r = self.rpc.do(o)
     58         self.assertEqual(r['status'], 0)
     59  
     60         (tx_hash_hex, o) = c.consume(self.address, self.accounts[0], 42+13-1, 1)
     61         self.rpc.do(o)
     62         o = receipt(tx_hash_hex)
     63         r = self.rpc.do(o)
     64         self.assertEqual(r['status'], 0)
     65 
     66         (tx_hash_hex, o) = c.consume(self.address, self.accounts[0], 41, 1)
     67         self.rpc.do(o)
     68         o = receipt(tx_hash_hex)
     69         r = self.rpc.do(o)
     70         self.assertEqual(r['status'], 1)
     71 
     72         (tx_hash_hex, o) = c.consume(self.address, self.accounts[0], 42+13, 1)
     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 
     79     def test_axx(self):
     80         nonce_oracle = RPCNonceOracle(self.alice, conn=self.rpc)
     81         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     82         (tx_hash_hex, o) = c.share(self.address, self.alice, 42, 13)
     83         self.rpc.do(o)
     84         o = receipt(tx_hash_hex)
     85         r = self.rpc.do(o)
     86         self.assertEqual(r['status'], 0)
     87 
     88         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
     89         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     90         (tx_hash_hex, o) = c.share(self.address, self.accounts[0], 42, 13)
     91         self.rpc.do(o)
     92         o = receipt(tx_hash_hex)
     93         r = self.rpc.do(o)
     94         self.assertEqual(r['status'], 1)
     95 
     96         (tx_hash_hex,o ) = c.add_writer(self.address, self.accounts[0], self.alice)
     97         self.rpc.do(o)
     98         o = receipt(tx_hash_hex)
     99         r = self.rpc.do(o)
    100         self.assertEqual(r['status'], 1)
    101 
    102         nonce_oracle = RPCNonceOracle(self.alice, conn=self.rpc)
    103         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    104         (tx_hash_hex, o) = c.share(self.address, self.alice, 133, 7)
    105         self.rpc.do(o)
    106         o = receipt(tx_hash_hex)
    107         r = self.rpc.do(o)
    108         self.assertEqual(r['status'], 1)
    109 
    110     def test_deposit_excess(self):
    111         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
    112         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    113         (tx_hash_hex, o) = c.approve(self.token_address, self.accounts[0], self.address, self.initial_supply)
    114         self.rpc.do(o)
    115 
    116         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    117         (tx_hash_hex, o) = c.share(self.address, self.accounts[0], 42, 13)
    118         self.rpc.do(o)
    119         o = receipt(tx_hash_hex)
    120         r = self.rpc.do(o)
    121         self.assertEqual(r['status'], 1)
    122 
    123         (tx_hash_hex, o) = c.deposit(self.address, self.accounts[0])
    124         self.rpc.do(o)
    125         o = receipt(tx_hash_hex)
    126         r = self.rpc.do(o)
    127         self.assertEqual(r['status'], 1)
    128 
    129         c = ERC20(self.chain_spec)
    130         o = c.balance_of(self.token_address, self.accounts[0], sender_address=self.accounts[0])
    131         r = self.rpc.do(o)
    132         balance = c.parse_balance_of(r)
    133         expected_balance = self.initial_supply - (self.resolution_unit * 13)
    134         self.assertEqual(balance, expected_balance)
    135 
    136         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    137         (tx_hash_hex, o) = c.deposit(self.address, self.accounts[0])
    138         self.rpc.do(o)
    139         o = receipt(tx_hash_hex)
    140         r = self.rpc.do(o)
    141         self.assertEqual(r['status'], 1)
    142 
    143         c = ERC20(self.chain_spec)
    144         o = c.balance_of(self.token_address, self.accounts[0], sender_address=self.accounts[0])
    145         r = self.rpc.do(o)
    146         balance = c.parse_balance_of(r)
    147         self.assertEqual(balance, expected_balance)
    148 
    149         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    150         (tx_hash_hex, o) = c.share(self.address, self.accounts[0], 133, 7)
    151         self.rpc.do(o)
    152         o = receipt(tx_hash_hex)
    153         r = self.rpc.do(o)
    154         self.assertEqual(r['status'], 1)
    155 
    156         (tx_hash_hex, o) = c.deposit(self.address, self.accounts[0])
    157         self.rpc.do(o)
    158         o = receipt(tx_hash_hex)
    159         r = self.rpc.do(o)
    160         self.assertEqual(r['status'], 1)
    161 
    162         c = ERC20(self.chain_spec)
    163         o = c.balance_of(self.token_address, self.accounts[0], sender_address=self.accounts[0])
    164         r = self.rpc.do(o)
    165         balance = c.parse_balance_of(r)
    166         expected_balance = self.initial_supply - (self.resolution_unit * (13 + 7))
    167         self.assertEqual(balance, expected_balance)
    168 
    169 
    170     def test_raw(self):
    171         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
    172         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    173         (tx_hash_hex, o) = c.approve(self.token_address, self.accounts[0], self.address, self.initial_supply)
    174         self.rpc.do(o)
    175 
    176         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    177         (tx_hash_hex, o) = c.share(self.address, self.accounts[0], 42, 13)
    178         self.rpc.do(o)
    179         o = receipt(tx_hash_hex)
    180         r = self.rpc.do(o)
    181         self.assertEqual(r['status'], 1)
    182 
    183         (tx_hash_hex, o) = c.consume(self.address, self.accounts[0], 133, 7)
    184         self.rpc.do(o)
    185         o = receipt(tx_hash_hex)
    186         r = self.rpc.do(o)
    187         self.assertEqual(r['status'], 1)
    188 
    189         o = c.raw(self.address, sender_address=self.accounts[0], count=160)
    190         r = self.rpc.do(o)
    191         field = c.parse_raw(r)
    192         self.assertEqual("0000000000fc7f000000000000000000e00f0000", field)
    193 
    194 
    195     def test_expire(self):
    196         c = Booking(self.chain_spec)
    197         o = c.expires(self.address, sender_address=self.accounts[0])
    198         r = self.rpc.do(o)
    199         expire_timestamp = int(self.booking_expire.timestamp())
    200         self.assertEqual(int(r, 16), expire_timestamp)
    201 
    202         nonce_oracle = RPCNonceOracle(self.accounts[0], conn=self.rpc)
    203         c = ERC20(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    204         (tx_hash_hex, o) = c.approve(self.token_address, self.accounts[0], self.address, self.initial_supply)
    205         self.rpc.do(o)
    206 
    207         c = Booking(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    208         self.backend.time_travel(int(self.booking_expire.timestamp() + 1))
    209         (tx_hash_hex, o) = c.share(self.address, self.accounts[0], 42, 13)
    210         self.rpc.do(o)
    211         o = receipt(tx_hash_hex)
    212         r = self.rpc.do(o)
    213         self.assertEqual(r['status'], 1)
    214 
    215         c = ERC20(self.chain_spec)
    216         o = c.balance_of(self.token_address, self.accounts[0], sender_address=self.accounts[0])
    217         r = self.rpc.do(o)
    218         balance = c.parse_balance_of(r)
    219         self.assertEqual(balance, self.initial_supply)
    220 
    221 
    222 if __name__ == '__main__':
    223     unittest.main()