2021-04-30 12:46:53 +02:00
|
|
|
# standard imports
|
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
from chainlib.eth.tx import (
|
2023-02-05 05:54:50 +01:00
|
|
|
TxFactory,
|
|
|
|
TxFormat,
|
|
|
|
)
|
2021-04-30 12:46:53 +02:00
|
|
|
from chainlib.eth.contract import (
|
2023-02-05 05:54:50 +01:00
|
|
|
ABIContractEncoder,
|
|
|
|
ABIContractDecoder,
|
|
|
|
ABIContractType,
|
|
|
|
abi_decode_single,
|
|
|
|
)
|
2021-04-30 12:46:53 +02:00
|
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
2021-08-25 12:06:22 +02:00
|
|
|
from chainlib.jsonrpc import JSONRPCRequest
|
2021-04-30 12:46:53 +02:00
|
|
|
from chainlib.eth.error import RequestMismatchException
|
|
|
|
from hexathon import (
|
2023-02-05 05:54:50 +01:00
|
|
|
add_0x,
|
|
|
|
strip_0x,
|
|
|
|
)
|
2021-04-30 12:46:53 +02:00
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
class AccountsIndex(TxFactory):
|
|
|
|
|
|
|
|
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-04-30 12:46:53 +02:00
|
|
|
return tx
|
|
|
|
|
|
|
|
def add(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
|
|
|
|
return self.__single_address_method('add', contract_address, sender_address, address, tx_format)
|
|
|
|
|
2023-02-12 08:51:58 +01:00
|
|
|
def remove(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
|
|
|
|
return self.__single_address_method('remove', contract_address, sender_address, address, tx_format)
|
|
|
|
|
2023-02-12 07:07:14 +01:00
|
|
|
def activate(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
|
|
|
|
return self.__single_address_method('activate', contract_address, sender_address, address, tx_format)
|
|
|
|
|
|
|
|
def deactivate(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC):
|
|
|
|
return self.__single_address_method('deactivate', contract_address, sender_address, address, tx_format)
|
2021-04-30 12:46:53 +02:00
|
|
|
|
2023-02-12 08:51:58 +01:00
|
|
|
|
|
|
|
def time(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None):
|
|
|
|
j = JSONRPCRequest(id_generator)
|
|
|
|
o = j.template()
|
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('time')
|
|
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
|
|
enc.address(address)
|
|
|
|
data = add_0x(enc.get())
|
|
|
|
tx = self.template(sender_address, contract_address)
|
|
|
|
tx = self.set_code(tx, data)
|
|
|
|
o['params'].append(self.normalize(tx))
|
|
|
|
return o
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-08-25 12:06:22 +02:00
|
|
|
def have(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None):
|
|
|
|
j = JSONRPCRequest(id_generator)
|
|
|
|
o = j.template()
|
2021-04-30 12:46:53 +02:00
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('have')
|
|
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
|
|
enc.address(address)
|
|
|
|
data = add_0x(enc.get())
|
|
|
|
tx = self.template(sender_address, contract_address)
|
|
|
|
tx = self.set_code(tx, data)
|
|
|
|
o['params'].append(self.normalize(tx))
|
|
|
|
return o
|
|
|
|
|
2023-02-12 07:07:14 +01:00
|
|
|
def is_active(self, contract_address, address, sender_address=ZERO_ADDRESS, id_generator=None):
|
|
|
|
j = JSONRPCRequest(id_generator)
|
|
|
|
o = j.template()
|
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('isActive')
|
|
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
|
|
enc.address(address)
|
|
|
|
data = add_0x(enc.get())
|
|
|
|
tx = self.template(sender_address, contract_address)
|
|
|
|
tx = self.set_code(tx, data)
|
|
|
|
o['params'].append(self.normalize(tx))
|
|
|
|
return o
|
|
|
|
|
|
|
|
|
|
|
|
def entry_count(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None):
|
2021-08-25 12:06:22 +02:00
|
|
|
j = JSONRPCRequest(id_generator)
|
|
|
|
o = j.template()
|
2021-04-30 12:46:53 +02:00
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('entryCount')
|
|
|
|
data = add_0x(enc.get())
|
|
|
|
tx = self.template(sender_address, contract_address)
|
|
|
|
tx = self.set_code(tx, data)
|
|
|
|
o['params'].append(self.normalize(tx))
|
|
|
|
return o
|
|
|
|
|
2021-08-25 12:06:22 +02:00
|
|
|
def count(self, contract_address, sender_address=ZERO_ADDRESS, id_generator=None):
|
|
|
|
return self.entry_count(contract_address, sender_address=sender_address, id_generator=id_generator)
|
2023-02-12 08:51:58 +01:00
|
|
|
|
2021-08-25 12:06:22 +02:00
|
|
|
def entry(self, contract_address, idx, sender_address=ZERO_ADDRESS, id_generator=None):
|
|
|
|
j = JSONRPCRequest(id_generator)
|
|
|
|
o = j.template()
|
2021-04-30 12:46:53 +02:00
|
|
|
o['method'] = 'eth_call'
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('entry')
|
|
|
|
enc.typ(ABIContractType.UINT256)
|
|
|
|
enc.uint256(idx)
|
|
|
|
data = add_0x(enc.get())
|
|
|
|
tx = self.template(sender_address, contract_address)
|
|
|
|
tx = self.set_code(tx, data)
|
|
|
|
o['params'].append(self.normalize(tx))
|
|
|
|
return o
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def parse_account(self, v):
|
|
|
|
return abi_decode_single(ABIContractType.ADDRESS, v)
|
|
|
|
|
2021-05-13 17:55:05 +02:00
|
|
|
@classmethod
|
|
|
|
def parse_entry_count(self, v):
|
|
|
|
return abi_decode_single(ABIContractType.UINT256, v)
|
|
|
|
|
2021-04-30 12:46:53 +02:00
|
|
|
@classmethod
|
|
|
|
def parse_have(self, v):
|
|
|
|
return abi_decode_single(ABIContractType.BOOLEAN, v)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def parse_add_request(self, v):
|
|
|
|
v = strip_0x(v)
|
|
|
|
cursor = 0
|
|
|
|
enc = ABIContractEncoder()
|
|
|
|
enc.method('add')
|
|
|
|
enc.typ(ABIContractType.ADDRESS)
|
|
|
|
r = enc.get()
|
|
|
|
l = len(r)
|
|
|
|
m = v[:l]
|
|
|
|
if m != r:
|
|
|
|
raise RequestMismatchException(v)
|
|
|
|
cursor += l
|
|
|
|
|
|
|
|
dec = ABIContractDecoder()
|
|
|
|
dec.typ(ABIContractType.ADDRESS)
|
2023-02-05 05:54:50 +01:00
|
|
|
dec.val(v[cursor:cursor + 64])
|
2021-04-30 12:46:53 +02:00
|
|
|
r = dec.decode()
|
2023-02-05 05:54:50 +01:00
|
|
|
return r
|