eth-erc721

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

commit 8649775406d3eccc030a78aac3deb8e7a3dad342
parent 24c707e60904409e68e22d8546dea8f73447c4b9
Author: nolash <dev@holbrook.no>
Date:   Fri,  7 May 2021 22:06:08 +0200

Parse token owner address

Diffstat:
Mpython/eth_devbadge/token.py | 27+++++++++++++++++++++++++++
Mpython/tests/test_app.py | 24+++++++++++++++++++++---
2 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/python/eth_devbadge/token.py b/python/eth_devbadge/token.py @@ -9,6 +9,13 @@ from chainlib.eth.tx import ( from chainlib.eth.contract import ( ABIContractEncoder, ABIContractType, + abi_decode_single, + ) +from chainlib.jsonrpc import jsonrpc_template +from chainlib.eth.constant import ZERO_ADDRESS +from hexathon import ( + add_0x, + strip_0x, ) # local imports @@ -70,3 +77,23 @@ class BadgeToken(TxFactory): tx = self.set_code(tx, data) tx = self.finalize(tx, tx_format) return tx + + + def owner_of(self, contract_address, token_id, sender_address=ZERO_ADDRESS): + o = jsonrpc_template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('ownerOf') + enc.typ(ABIContractType.UINT256) + enc.uint256(token_id) + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address) + tx = self.set_code(tx, data) + o['params'].append(self.normalize(tx)) + o['params'].append('latest') + return o + + + @classmethod + def parse_owner_of(self, v): + return abi_decode_single(ABIContractType.ADDRESS, v) diff --git a/python/tests/test_app.py b/python/tests/test_app.py @@ -43,17 +43,35 @@ class Test(EthTesterCase): r = self.conn.do(o) self.address = to_checksum_address(r['contract_address']) - - def test_mint(self): + + def _mint(self, recipient, token_id): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = BadgeToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - (tx_hash_hex, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], int.from_bytes(b'\xee' * 32, byteorder='big')) + (tx_hash_hex, o) = c.mint_to(self.address, self.accounts[0], recipient, token_id) r = self.rpc.do(o) o = receipt(tx_hash_hex) r = self.conn.do(o) self.assertEqual(r['status'], 1) + return c + + + def test_mint(self): + token_id = int.from_bytes(b'\xee' * 32, byteorder='big') + c = self._mint(self.accounts[1], token_id) + + + def test_owner(self): + token_id = int.from_bytes(b'\xee' * 32, byteorder='big') + c = self._mint(self.accounts[1], token_id) + + o = c.owner_of(self.address, token_id, sender_address=self.accounts[0]) + r = self.rpc.do(o) + owner_address = c.parse_owner_of(r) + + self.assertEqual(self.accounts[1], owner_address) + if __name__ == '__main__': unittest.main()