2021-10-24 15:29:41 +02:00
|
|
|
# standard imports
|
2021-08-24 21:28:17 +02:00
|
|
|
import logging
|
|
|
|
|
2021-04-30 13:11:22 +02:00
|
|
|
# external imports
|
2021-06-28 09:29:37 +02:00
|
|
|
from chainlib.jsonrpc import JSONRPCRequest
|
2021-04-30 13:11:22 +02:00
|
|
|
from chainlib.eth.contract import (
|
|
|
|
ABIContractEncoder,
|
|
|
|
ABIContractType,
|
|
|
|
abi_decode_single,
|
|
|
|
)
|
|
|
|
from chainlib.eth.tx import TxFactory
|
|
|
|
from hexathon import (
|
|
|
|
add_0x,
|
|
|
|
)
|
|
|
|
from chainlib.eth.constant import (
|
|
|
|
ZERO_ADDRESS,
|
|
|
|
)
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
from .encoding import (
|
|
|
|
to_identifier,
|
|
|
|
)
|
|
|
|
|
2021-08-24 21:28:17 +02:00
|
|
|
logg = logging.getLogger(__name__)
|
2021-04-30 13:11:22 +02:00
|
|
|
|
|
|
|
class Registry(TxFactory):
|
|
|
|
|
2021-06-28 09:29:37 +02:00
|
|
|
def address_of(self, contract_address, identifier_string, sender_address=ZERO_ADDRESS, id_generator=None):
|
|
|
|
j = JSONRPCRequest(id_generator)
|
|
|
|
o = j.template()
|
2021-04-30 13:11:22 +02:00
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('addressOf')
|
|
|
|
enc.typ(ABIContractType.BYTES32)
|
|
|
|
identifier = to_identifier(identifier_string)
|
2021-08-24 21:28:17 +02:00
|
|
|
logg.debug('identifier {} {}'.format(identifier, identifier_string))
|
2021-04-30 13:11:22 +02:00
|
|
|
enc.bytes32(identifier)
|
|
|
|
data = add_0x(enc.encode())
|
|
|
|
tx = self.template(sender_address, contract_address)
|
|
|
|
tx = self.set_code(tx, data)
|
|
|
|
o['params'].append(self.normalize(tx))
|
2021-06-28 09:29:37 +02:00
|
|
|
o = j.finalize(o)
|
2021-04-30 13:11:22 +02:00
|
|
|
return o
|
|
|
|
|
|
|
|
|
2023-03-25 17:48:40 +01:00
|
|
|
def identifier(self, contract_address, idx, sender_address=ZERO_ADDRESS, id_generator=None):
|
|
|
|
j = JSONRPCRequest(id_generator)
|
|
|
|
o = j.template()
|
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('identifier')
|
|
|
|
enc.typ(ABIContractType.UINT256)
|
2023-03-26 07:45:20 +02:00
|
|
|
enc.uint256(idx)
|
2023-03-25 17:48:40 +01:00
|
|
|
data = add_0x(enc.encode())
|
|
|
|
tx = self.template(sender_address, contract_address)
|
|
|
|
tx = self.set_code(tx, data)
|
|
|
|
o['params'].append(self.normalize(tx))
|
|
|
|
o = j.finalize(o)
|
|
|
|
return o
|
|
|
|
|
|
|
|
|
|
|
|
def identifier_count(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None):
|
2023-03-26 07:45:20 +02:00
|
|
|
return self.call_noarg('identifierCount', contract_address, sender_address=sender_address, id_generator=id_generator)
|
2023-03-25 17:48:40 +01:00
|
|
|
|
|
|
|
|
2021-04-30 13:11:22 +02:00
|
|
|
@classmethod
|
|
|
|
def parse_address_of(self, v):
|
|
|
|
return abi_decode_single(ABIContractType.ADDRESS, v)
|