Add EIP173, custom 'Owned' support

This commit is contained in:
nolash 2021-04-30 08:37:21 +02:00
parent f26abe04ab
commit 139eb13f71
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 93 additions and 6 deletions

View File

@ -5,3 +5,5 @@
* Add eth subscribe monitor * Add eth subscribe monitor
* Add eth erc20 transfer script * Add eth erc20 transfer script
* Add eth gas send script * Add eth gas send script
* Add eth eip165 support
* Add eth eip173, custom "owned" support

View File

@ -4,16 +4,22 @@ from hexathon import (
) )
# local imports # local imports
from .contract import ( from chainlib.eth.contract import (
ABIContractEncoder, ABIContractEncoder,
ABIContractDecoder, ABIContractDecoder,
ABIContractType, ABIContractType,
abi_decode_single,
) )
from chainlib.jsonrpc import jsonrpc_template from chainlib.jsonrpc import jsonrpc_template
from chainlib.eth.tx import (
TxFactory,
TxFormat,
)
from chainlib.eth.constant import ZERO_ADDRESS
class EIP173(TxFactory): 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 = ABIContractEncoder()
enc.method('transferOwnership') enc.method('transferOwnership')
enc.typ(ABIContractType.ADDRESS) enc.typ(ABIContractType.ADDRESS)
@ -45,7 +51,7 @@ class EIP173(TxFactory):
class Owned(EIP173): 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 = ABIContractEncoder()
enc.method('acceptOwnership') enc.method('acceptOwnership')
data = add_0x(enc.get()) data = add_0x(enc.get())
@ -55,7 +61,7 @@ class Owned(EIP173):
return tx 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 = ABIContractEncoder()
enc.method('takeOwnership') enc.method('takeOwnership')
enc.typ(ABIContractType.ADDRESS) enc.typ(ABIContractType.ADDRESS)

View File

@ -12,7 +12,10 @@ from chainlib.eth.tx import (
TxFactory, TxFactory,
receipt, receipt,
) )
from chainlib.eth.eip165 import EIP165 from chainlib.eth.owned import (
EIP173,
Owned,
)
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger() logg = logging.getLogger()
@ -45,7 +48,83 @@ class TestOwned(EthTesterCase):
def test_owned(self): 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__': if __name__ == '__main__':