From 139eb13f719b93e547884d9361f1de6dbb40a320 Mon Sep 17 00:00:00 2001 From: nolash Date: Fri, 30 Apr 2021 08:37:21 +0200 Subject: [PATCH] Add EIP173, custom 'Owned' support --- CHANGELOG | 2 ++ chainlib/eth/owned.py | 14 +++++--- tests/test_owned.py | 83 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 93 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 66a531c..d2601e1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -5,3 +5,5 @@ * Add eth subscribe monitor * Add eth erc20 transfer script * Add eth gas send script + * Add eth eip165 support + * Add eth eip173, custom "owned" support diff --git a/chainlib/eth/owned.py b/chainlib/eth/owned.py index c058118..9178d76 100644 --- a/chainlib/eth/owned.py +++ b/chainlib/eth/owned.py @@ -4,16 +4,22 @@ from hexathon import ( ) # local imports -from .contract import ( +from chainlib.eth.contract import ( ABIContractEncoder, ABIContractDecoder, ABIContractType, + abi_decode_single, ) from chainlib.jsonrpc import jsonrpc_template +from chainlib.eth.tx import ( + TxFactory, + TxFormat, + ) +from chainlib.eth.constant import ZERO_ADDRESS class EIP173(TxFactory): - def transfer_ownership(self, contract_address, sender_address, new_owner_address): + def transfer_ownership(self, contract_address, sender_address, new_owner_address, tx_format=TxFormat.JSONRPC): enc = ABIContractEncoder() enc.method('transferOwnership') enc.typ(ABIContractType.ADDRESS) @@ -45,7 +51,7 @@ class EIP173(TxFactory): class Owned(EIP173): - def accept_ownership(self, contract_address, sender_address): + def accept_ownership(self, contract_address, sender_address, tx_format=TxFormat.JSONRPC): enc = ABIContractEncoder() enc.method('acceptOwnership') data = add_0x(enc.get()) @@ -55,7 +61,7 @@ class Owned(EIP173): return tx - def take_ownership(self, contract_address, sender_address, resource_address): + def take_ownership(self, contract_address, sender_address, resource_address, tx_format=TxFormat.JSONRPC): enc = ABIContractEncoder() enc.method('takeOwnership') enc.typ(ABIContractType.ADDRESS) diff --git a/tests/test_owned.py b/tests/test_owned.py index cb893b7..61a6ad9 100644 --- a/tests/test_owned.py +++ b/tests/test_owned.py @@ -12,7 +12,10 @@ from chainlib.eth.tx import ( TxFactory, receipt, ) -from chainlib.eth.eip165 import EIP165 +from chainlib.eth.owned import ( + EIP173, + Owned, + ) logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() @@ -45,7 +48,83 @@ class TestOwned(EthTesterCase): def test_owned(self): - pass + c = EIP173(self.chain_spec) + o = c.owner(self.address, sender_address=self.accounts[0]) + r = self.conn.do(o) + owner = c.parse_owner(r) + self.assertEqual(owner, self.accounts[0]) + + + def test_transfer_ownership(self): + nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn) + gas_oracle = OverrideGasOracle(limit=8000000, conn=self.conn) + c = EIP173(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) + (tx_hash_hex, o) = c.transfer_ownership(self.address, self.accounts[2], self.accounts[1]) + r = self.conn.do(o) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.assertEqual(r['status'], 0) + + nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) + c = EIP173(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) + (tx_hash_hex, o) = c.transfer_ownership(self.address, self.accounts[0], self.accounts[1]) + r = self.conn.do(o) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.assertEqual(r['status'], 1) + + + def test_accept_ownership(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) + gas_oracle = OverrideGasOracle(limit=8000000, conn=self.conn) + c = Owned(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) + (tx_hash_hex, o) = c.transfer_ownership(self.address, self.accounts[0], self.accounts[1]) + r = self.conn.do(o) + + nonce_oracle = RPCNonceOracle(self.accounts[2], self.conn) + c = Owned(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) + (tx_hash_hex, o) = c.accept_ownership(self.address, self.accounts[2]) + r = self.conn.do(o) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.assertEqual(r['status'], 0) + + nonce_oracle = RPCNonceOracle(self.accounts[1], self.conn) + c = Owned(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) + (tx_hash_hex, o) = c.accept_ownership(self.address, self.accounts[1]) + r = self.conn.do(o) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.assertEqual(r['status'], 1) + + o = c.owner(self.address, sender_address=self.accounts[0]) + r = self.conn.do(o) + owner = c.parse_owner(r) + self.assertEqual(owner, self.accounts[1]) + + + def test_take_ownership(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.conn) + gas_oracle = OverrideGasOracle(limit=8000000, conn=self.conn) + c = Owned(self.chain_spec, nonce_oracle=nonce_oracle, gas_oracle=gas_oracle, signer=self.signer) + (tx_hash_hex, o) = c.transfer_ownership(self.address, self.accounts[0], self.address) + r = self.conn.do(o) + + (tx_hash_hex, o) = c.take_ownership(self.address, self.accounts[0], self.address) + r = self.conn.do(o) + + o = receipt(tx_hash_hex) + r = self.conn.do(o) + self.assertEqual(r['status'], 1) + + o = c.owner(self.address, sender_address=self.accounts[0]) + r = self.conn.do(o) + owner = c.parse_owner(r) + self.assertEqual(owner, self.address) if __name__ == '__main__':