eth-accounts-index/python/eth_accounts_index/registry.py

67 lines
2.0 KiB
Python
Raw Permalink Normal View History

2021-03-31 16:07:23 +02:00
# standard imports
import os
2023-02-05 05:54:50 +01:00
import json
2023-02-05 05:54:50 +01:00
import pathlib
2021-03-31 16:07:23 +02:00
# external imports
from chainlib.eth.tx import (
2023-02-05 05:54:50 +01:00
TxFormat,
)
2021-03-31 16:07:23 +02:00
from chainlib.eth.contract import (
2023-02-05 05:54:50 +01:00
ABIContractEncoder,
ABIContractType,
)
2021-03-31 16:07:23 +02:00
# local imports
2021-04-30 22:04:34 +02:00
from .interface import AccountsIndex
2020-11-16 18:25:50 +01:00
moddir = os.path.dirname(__file__)
datadir = os.path.join(moddir, 'data')
class AccountRegistry(AccountsIndex):
2020-11-16 18:25:50 +01:00
__abi = None
__bytecode = None
2020-11-16 18:25:50 +01:00
@staticmethod
def abi():
2023-02-05 05:54:50 +01:00
if AccountRegistry.__abi is None:
with open(os.path.join(datadir, 'AccountsIndex.json'), 'r') as f:
AccountRegistry.__abi = json.load(f)
2020-11-16 18:25:50 +01:00
return AccountRegistry.__abi
@staticmethod
def bytecode():
2023-02-05 05:54:50 +01:00
if AccountRegistry.__bytecode is None:
AccountRegistry.__bytecode = pathlib.Path(
os.path.join(datadir, 'AccountsIndex.bin')
).read_text()
2020-11-16 18:25:50 +01:00
return AccountRegistry.__bytecode
2021-03-31 16:07:23 +02:00
@staticmethod
def gas(code=None):
2021-04-30 22:04:34 +02:00
return 1200000
2021-03-31 16:07:23 +02:00
def constructor(self, sender_address):
code = AccountRegistry.bytecode()
tx = self.template(sender_address, None, use_nonce=True)
tx = self.set_code(tx, code)
return self.build(tx)
def __single_address_method(self, method, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
enc = ABIContractEncoder()
enc.method(method)
enc.typ(ABIContractType.ADDRESS)
enc.address(address)
data = enc.get()
tx = self.template(sender_address, contract_address, use_nonce=True)
tx = self.set_code(tx, data)
2023-02-05 05:54:50 +01:00
tx = self.finalize(tx, tx_format)
2021-03-31 16:07:23 +02:00
return tx
def add_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('addWriter', contract_address, sender_address, address, tx_format)
2021-03-31 16:07:23 +02:00
def delete_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
return self.__single_address_method('deleteWriter', contract_address, sender_address, address, tx_format)