Add custom user info, decimals to values

This commit is contained in:
nolash 2021-11-06 11:37:21 +01:00
parent f0ba197b08
commit e3c002082b
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 48 additions and 11 deletions

View File

@ -9,6 +9,7 @@ from cic_eth_registry.lookup.tokenindex import TokenIndexLookup
from cic_types.ext.metadata import MetadataRequestsHandler from cic_types.ext.metadata import MetadataRequestsHandler
from cic_types.models.person import Person from cic_types.models.person import Person
from chainlib.eth.address import to_checksum_address from chainlib.eth.address import to_checksum_address
from hexathon import add_0x
# local imports # local imports
from clicada.tx import TxGetter from clicada.tx import TxGetter
@ -78,20 +79,24 @@ def execute(ctrl):
r = user_address_store.by_address(user_address) r = user_address_store.by_address(user_address)
print("""Phone: {} print("""Phone: {}
EVM address: {} Network address: {}
Chain: {}
Name: {} Name: {}
Registered: {} Registered: {}
Gender: {} Gender: {}
Location: {} Location: {}
Products: {} Products: {}
Tags: {}
Balances:""".format( Balances:""".format(
ctrl.get('_IDENTIFIER'), ctrl.get('_IDENTIFIER'),
user_address, add_0x(user_address),
ctrl.chain().common_name(),
str(r), str(r),
datetime.datetime.fromtimestamp(r.date_registered).ctime(), datetime.datetime.fromtimestamp(r.date_registered).ctime(),
r.gender, r.gender,
r.location['area_name'], r.location['area_name'],
','.join(r.products), ','.join(r.products),
','.join(r.tags),
) )
) )
@ -99,14 +104,17 @@ Balances:""".format(
seen_tokens = {} seen_tokens = {}
for tx_src in txs['data']: for tx_src in txs['data']:
tx = ResolvedTokenTx.from_dict(tx_src) tx = ResolvedTokenTx.from_dict(tx_src)
tx.resolve(token_store, user_address_store, update=ctrl.get('_FORCE')) tx.resolve(token_store, user_address_store, show_decimals=True, update=ctrl.get('_FORCE'))
tx_lines.append(tx) tx_lines.append(tx)
seen_tokens[tx.source_token_label] = tx.source_token seen_tokens[tx.source_token_label] = tx.source_token
seen_tokens[tx.destination_token_label] = tx.destination_token seen_tokens[tx.destination_token_label] = tx.destination_token
for k in seen_tokens.keys(): for k in seen_tokens.keys():
(token_symbol, token_decimals) = token_store.by_address(seen_tokens[k])
balance = token_balance(ctrl.chain(), ctrl.conn(), seen_tokens[k], user_address) balance = token_balance(ctrl.chain(), ctrl.conn(), seen_tokens[k], user_address)
print("\t{} {}".format(k, balance)) fmt = '{:.' + str(token_decimals) + 'f}'
decimal_balance = fmt.format(balance / (10 ** token_decimals))
print("\t{} {}".format(token_symbol, decimal_balance))
print() print()
for l in tx_lines: for l in tx_lines:

View File

@ -23,6 +23,8 @@ class ResolvedTokenTx(TokenTx):
super(ResolvedTokenTx, self).__init__() super(ResolvedTokenTx, self).__init__()
self.source_token_name = None self.source_token_name = None
self.destination_token_name = None self.destination_token_name = None
self.source_token_decimals = None
self.destination_token_decimals = None
self.symmetric = True self.symmetric = True
self.sender_entity = None self.sender_entity = None
self.recipient_entity = None self.recipient_entity = None
@ -30,22 +32,26 @@ class ResolvedTokenTx(TokenTx):
def resolve_tokens(self, token_store, show_decimals=False, update=False): def resolve_tokens(self, token_store, show_decimals=False, update=False):
(token_symbol, token_decimals) = token_store.by_address(self.source_token) (token_symbol, token_decimals) = token_store.by_address(self.source_token)
self.source_token_decimals = token_decimals
self.source_token_label = token_symbol self.source_token_label = token_symbol
token_value = self.to_value / (10 ** token_decimals) token_value = self.to_value / (10 ** token_decimals)
show_token_decimals = token_decimals
if not show_decimals: if not show_decimals:
token_decimals = 0 show_token_decimals = 0
fmt = '{:.' + str(token_decimals) + 'f}' fmt = '{:.' + str(show_token_decimals) + 'f}'
self.from_value_label = fmt.format(token_value) self.from_value_label = fmt.format(token_value)
if self.destination_token != self.source_token: if self.destination_token != self.source_token:
self.symmetric = False self.symmetric = False
(token_symbol, token_decimals) = token_store.by_address(self.destination_token) (token_symbol, token_decimals) = token_store.by_address(self.destination_token)
show_token_decimals = token_decimals
self.destination_token_label = token_symbol self.destination_token_label = token_symbol
self.destination_token_decimals = token_decimals
token_value = self.to_value / (10 ** token_decimals) token_value = self.to_value / (10 ** token_decimals)
if not show_decimals: if not show_decimals:
token_decimals = 0 show_token_decimals = 0
fmt = '{:.' + str(token_decimals) + 'f}' fmt = '{:.' + str(show_token_decimals) + 'f}'
self.to_value_label = fmt.format(token_value) self.to_value_label = fmt.format(token_value)
@ -93,7 +99,7 @@ class ResolvedTokenTx(TokenTx):
def __str__(self): def __str__(self):
if self.symmetric: if self.symmetric:
return '{}: {} => {}\t{} {}'.format( return '{}\t{} => {}\t{} {}'.format(
self.date_block_label, self.date_block_label,
self.sender_label, self.sender_label,
self.recipient_label, self.recipient_label,

View File

@ -24,6 +24,18 @@ from clicada.error import ExpiredRecordError
logg = logging.getLogger(__name__) logg = logging.getLogger(__name__)
class Account(Person):
def __init__(self):
super(Account, self).__init__()
self.tags = []
def apply_custom(self, custom_data):
self.tags = custom_data['tags']
class FileUserStore: class FileUserStore:
def __init__(self, chain_spec, label, store_base_path, ttl): def __init__(self, chain_spec, label, store_base_path, ttl):
@ -199,7 +211,16 @@ class FileUserStore:
return address return address
data = r.json() data = r.json()
person = Person() person = Account()
person_data = person.deserialize(person_data=data) person_data = person.deserialize(person_data=data)
self.put(address, json.dumps(person_data.serialize())) self.put(address, json.dumps(person_data.serialize()))
getter = MetadataRequestsHandler(MetadataPointer.CUSTOM, bytes.fromhex(address))
r = None
try:
r = getter.query()
person_data.apply_custom(r.json())
except requests.exceptions.HTTPError as e:
pass
return person_data return person_data

View File

@ -1,5 +1,7 @@
usumbufu~=0.3.2 usumbufu~=0.3.2
confini~=0.5.1 confini~=0.5.1
cic-eth-registry~=0.6.1 cic-eth-registry~=0.6.1
cic-types~=0.2.1a3 cic-types~=0.2.1a4
phonenumbers==8.12.12 phonenumbers==8.12.12
eth-erc20~=0.1.2
hexathon~=0.1.0