2020-11-13 23:19:48 +01:00
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
2020-11-16 18:25:50 +01:00
|
|
|
moddir = os.path.dirname(__file__)
|
|
|
|
datadir = os.path.join(moddir, 'data')
|
|
|
|
|
|
|
|
|
2020-11-14 00:09:43 +01:00
|
|
|
class AccountRegistry:
|
2020-11-13 23:19:48 +01:00
|
|
|
|
2020-11-16 18:25:50 +01:00
|
|
|
__abi = None
|
|
|
|
__bytecode = None
|
2020-11-13 23:19:48 +01:00
|
|
|
|
|
|
|
def __init__(self, w3, address, signer_address=None):
|
2020-11-16 18:25:50 +01:00
|
|
|
abi = AccountRegistry.abi()
|
|
|
|
AccountRegistry.bytecode()
|
|
|
|
self.contract = w3.eth.contract(abi=abi, address=address)
|
2020-11-13 23:19:48 +01:00
|
|
|
self.w3 = w3
|
|
|
|
if signer_address != None:
|
|
|
|
self.signer_address = signer_address
|
|
|
|
else:
|
|
|
|
if type(self.w3.eth.defaultAccount).__name__ == 'Empty':
|
|
|
|
self.w3.eth.defaultAccount = self.w3.eth.accounts[0]
|
|
|
|
self.signer_address = self.w3.eth.defaultAccount
|
|
|
|
|
|
|
|
|
2020-11-16 18:25:50 +01:00
|
|
|
@staticmethod
|
|
|
|
def abi():
|
|
|
|
if AccountRegistry.__abi == None:
|
2021-01-06 17:56:44 +01:00
|
|
|
f = open(os.path.join(datadir, 'AccountsIndex.json'), 'r')
|
2020-11-16 18:25:50 +01:00
|
|
|
AccountRegistry.__abi = json.load(f)
|
|
|
|
f.close()
|
|
|
|
return AccountRegistry.__abi
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def bytecode():
|
|
|
|
if AccountRegistry.__bytecode == None:
|
2020-12-07 16:41:48 +01:00
|
|
|
f = open(os.path.join(datadir, 'AccountsIndex.bin'))
|
2020-11-16 18:25:50 +01:00
|
|
|
AccountRegistry.__bytecode = f.read()
|
|
|
|
f.close()
|
|
|
|
return AccountRegistry.__bytecode
|
|
|
|
|
|
|
|
|
2020-11-13 23:19:48 +01:00
|
|
|
def add(self, address):
|
|
|
|
gasPrice = self.w3.eth.gasPrice;
|
2020-11-16 19:19:13 +01:00
|
|
|
nonce = self.w3.eth.getTransactionCount(self.signer_address, 'pending')
|
|
|
|
tx = self.contract.functions.add(address).buildTransaction({
|
|
|
|
'gasPrice': gasPrice,
|
|
|
|
'gas': 100000,
|
|
|
|
'from': self.signer_address,
|
|
|
|
'nonce': nonce,
|
|
|
|
})
|
|
|
|
logg.debug('tx {}'.format(tx))
|
2020-11-13 23:19:48 +01:00
|
|
|
tx_hash = self.contract.functions.add(address).transact({
|
|
|
|
'gasPrice': gasPrice,
|
|
|
|
'gas': 100000,
|
|
|
|
'from': self.signer_address,
|
2020-11-16 19:19:13 +01:00
|
|
|
'nonce': nonce,
|
2020-11-13 23:19:48 +01:00
|
|
|
})
|
2020-11-16 19:19:13 +01:00
|
|
|
return tx_hash
|
2020-11-13 23:19:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def count(self):
|
|
|
|
return self.contract.functions.count().call()
|
|
|
|
|
|
|
|
|
|
|
|
def have(self, address):
|
|
|
|
r = self.contract.functions.accountsIndex(address).call()
|
|
|
|
return r != 0
|
|
|
|
|
|
|
|
|
2021-02-19 20:17:15 +01:00
|
|
|
def get_index(self, i):
|
|
|
|
return self.contract.functions.accounts(i).call()
|
|
|
|
|
|
|
|
|
2020-11-13 23:19:48 +01:00
|
|
|
def last(self, n):
|
|
|
|
c = self.count()
|
|
|
|
lo = c - n - 1
|
|
|
|
if lo < 0:
|
|
|
|
lo = 0
|
|
|
|
accounts = []
|
|
|
|
for i in range(c - 1, lo, -1):
|
|
|
|
a = self.contract.functions.accounts(i).call()
|
|
|
|
accounts.append(a)
|
|
|
|
return accounts
|