eth-erc721

ERC-721 'NFT' token interface with example developer badge token contract
Info | Log | Files | Refs | LICENSE

base.py (7898B)


      1 # standard imports
      2 import logging
      3 
      4 # external imports
      5 from hexathon import strip_0x
      6 from chainlib.eth.nonce import RPCNonceOracle
      7 from chainlib.eth.tx import receipt
      8 from chainlib.eth.constant import ZERO_ADDRESS
      9 from chainlib.jsonrpc import JSONRPCException
     10 
     11 # local imports
     12 from eth_badgetoken import BadgeToken 
     13 
     14 logg = logging.getLogger(__name__)
     15 
     16 
     17 class TestInterface:
     18 
     19     def _mint(self, recipient, token_id):
     20         nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
     21         c = BadgeToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     22         (tx_hash_hex, o) = c.mint_to(self.address, self.accounts[0], recipient, token_id)
     23         r = self.rpc.do(o)
     24 
     25         o = receipt(tx_hash_hex)
     26         r = self.conn.do(o)
     27         self.assertEqual(r['status'], 1)
     28         return c
     29 
     30 
     31     def test_token_owner(self):
     32         token_bytes = b'\xee' * 32
     33         token_id = int.from_bytes(token_bytes, byteorder='big')
     34         c = self._mint(self.accounts[1], token_id)
     35 
     36         o = c.owner_of(self.address, token_id, sender_address=self.accounts[0])
     37         r = self.rpc.do(o)
     38         owner_address = c.parse_owner(r)
     39         self.assertEqual(strip_0x(self.accounts[1]), owner_address)
     40 
     41         o = c.token_of_owner_by_index(self.address, self.accounts[1], 0, sender_address=self.accounts[0])
     42         r = self.rpc.do(o)
     43         self.assertEqual(token_bytes.hex(), strip_0x(r))
     44 
     45 
     46     def test_token_transfer_ownership(self):
     47         token_bytes = [
     48             b'\xee' * 32,
     49             b'\xdd' * 32,
     50             b'\xcc' * 32,
     51             ]
     52         token_ids = []
     53         for t in token_bytes:
     54             token_id = int.from_bytes(t, byteorder='big')
     55             token_ids.append(token_id)
     56             c = self._mint(self.accounts[0], token_id)
     57 
     58         o = c.total_supply(self.address, sender_address=self.accounts[0])
     59         r = self.rpc.do(o)
     60         supply = c.parse_total_supply(r)
     61         self.assertEqual(supply, 3)
     62 
     63         (tx_hash_hex, o) = c.transfer_ownership(self.address, self.accounts[0], self.accounts[4])
     64         r = self.rpc.do(o)
     65         o = receipt(tx_hash_hex)
     66         r = self.conn.do(o)
     67         self.assertEqual(r['status'], 1)
     68 
     69         for t in token_ids:
     70             o = c.owner_of(self.address, token_id, sender_address=self.accounts[0])
     71             r = self.rpc.do(o)
     72             owner_address = c.parse_owner(r)
     73             self.assertEqual(strip_0x(self.accounts[4]), owner_address)
     74 
     75     def test_mint(self):
     76         token_bytes = b'\xee' * 32
     77         token_id = int.from_bytes(token_bytes, byteorder='big')
     78         c = self._mint(self.accounts[1], token_id)
     79 
     80         o = c.token_by_index(self.address, 0, sender_address=self.accounts[0])
     81         r = self.rpc.do(o)
     82         self.assertEqual(token_bytes.hex(), strip_0x(r))
     83 
     84         o = c.total_supply(self.address, sender_address=self.accounts[0])
     85         r = self.rpc.do(o)
     86         supply = c.parse_total_supply(r)
     87         self.assertEqual(supply, 1)
     88 
     89 
     90     def test_approve(self):
     91         token_bytes = b'\xee' * 32
     92         token_id = int.from_bytes(token_bytes, byteorder='big')
     93         c = self._mint(self.accounts[1], token_id)
     94 
     95         nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
     96         c = BadgeToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
     97         (tx_hash_hex, o) = c.approve(self.address, self.accounts[1], self.accounts[2], token_id)
     98         r = self.rpc.do(o)
     99 
    100         o = receipt(tx_hash_hex)
    101         r = self.conn.do(o)
    102         self.assertEqual(r['status'], 1)
    103 
    104         o = c.get_approved(self.address, token_id, sender_address=self.accounts[0])
    105         r = self.rpc.do(o)
    106         approved_address = c.parse_get_approved(r)
    107         self.assertEqual(approved_address, strip_0x(self.accounts[2]))
    108 
    109         nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
    110         c = BadgeToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    111         (tx_hash_hex, o) = c.transfer_from(self.address, self.accounts[2], self.accounts[1], self.accounts[3], token_id)
    112         r = self.rpc.do(o)
    113 
    114         o = receipt(tx_hash_hex)
    115         r = self.conn.do(o)
    116         self.assertEqual(r['status'], 1)
    117 
    118         o = c.owner_of(self.address, token_id, sender_address=self.accounts[0])
    119         r = self.rpc.do(o)
    120         owner_address = c.parse_owner_of(r)
    121         self.assertEqual(owner_address, strip_0x(self.accounts[3]))
    122 
    123         o = c.get_approved(self.address, token_id, sender_address=self.accounts[0])
    124         r = self.rpc.do(o)
    125         approved_address = c.parse_get_approved(r)
    126         self.assertEqual(approved_address, strip_0x(ZERO_ADDRESS))
    127 
    128 
    129     def test_transfer(self):
    130         token_bytes = b'\xee' * 32
    131         token_id = int.from_bytes(token_bytes, byteorder='big')
    132         c = self._mint(self.accounts[1], token_id)
    133 
    134         nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
    135         c = BadgeToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    136         (tx_hash_hex, o) = c.transfer_from(self.address, self.accounts[1], self.accounts[1], self.accounts[2], token_id)
    137         r = self.rpc.do(o)
    138 
    139         o = receipt(tx_hash_hex)
    140         r = self.conn.do(o)
    141         self.assertEqual(r['status'], 1)
    142 
    143         o = c.token_of_owner_by_index(self.address, self.accounts[1], 0, sender_address=self.accounts[0])
    144         with self.assertRaises(JSONRPCException):
    145             r = self.rpc.do(o)
    146 
    147         o = c.token_of_owner_by_index(self.address, self.accounts[2], 0, sender_address=self.accounts[0])
    148         r = self.rpc.do(o)
    149         self.assertEqual(token_bytes.hex(), strip_0x(r))
    150      
    151 
    152     def test_token_uri(self):
    153         token_bytes = b'\xee' * 32
    154         token_id = int.from_bytes(token_bytes, byteorder='big')
    155         c = self._mint(self.accounts[1], token_id)
    156 
    157         o = c.token_uri(self.address, token_id, sender_address=self.accounts[0])
    158         r = self.rpc.do(o)
    159         uri = c.parse_token_uri(r)
    160         self.assertEqual(uri, 'sha256:' + token_bytes.hex()) 
    161 
    162 
    163     def test_operator(self):
    164         token_bytes = b'\xee' * 32
    165         token_id = int.from_bytes(token_bytes, byteorder='big')
    166         c = self._mint(self.accounts[1], token_id)
    167 
    168         nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
    169         c = BadgeToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    170         (tx_hash_hex, o) = c.set_operator(self.address, self.accounts[1], self.accounts[2])
    171         r = self.rpc.do(o)
    172 
    173         o = receipt(tx_hash_hex)
    174         r = self.conn.do(o)
    175         self.assertEqual(r['status'], 1)
    176 
    177         o = c.is_operator(self.address, self.accounts[1], self.accounts[2], sender_address=self.accounts[0])
    178         r = self.rpc.do(o)
    179         isop = c.parse_is_operator(r)
    180         self.assertTrue(isop)
    181 
    182         (tx_hash_hex, o) = c.remove_operator(self.address, self.accounts[1], self.accounts[2])
    183         r = self.rpc.do(o)
    184 
    185         o = receipt(tx_hash_hex)
    186         r = self.conn.do(o)
    187         self.assertEqual(r['status'], 1)
    188 
    189         o = c.is_operator(self.address, self.accounts[1], self.accounts[2], sender_address=self.accounts[0])
    190         r = self.rpc.do(o)
    191         isop = c.parse_is_operator(r)
    192         self.assertFalse(isop)
    193 
    194         (tx_hash_hex, o) = c.set_operator(self.address, self.accounts[1], self.accounts[2])
    195         r = self.rpc.do(o)
    196 
    197         nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc)
    198         c = BadgeToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
    199         (tx_hash_hex, o) = c.transfer_from(self.address, self.accounts[2], self.accounts[1], self.accounts[3], token_id)
    200         r = self.rpc.do(o)
    201 
    202         o = receipt(tx_hash_hex)
    203         r = self.conn.do(o)
    204         self.assertEqual(r['status'], 1)
    205 
    206         o = c.owner_of(self.address, token_id, sender_address=self.accounts[0])
    207         r = self.rpc.do(o)
    208         owner_address = c.parse_owner(r)
    209         self.assertEqual(owner_address, strip_0x(self.accounts[3]))