2022-01-30 21:07:37 +01:00
|
|
|
|
# standard imports
|
|
|
|
|
import logging
|
2022-01-31 00:20:46 +01:00
|
|
|
|
import os
|
2022-01-30 21:07:37 +01:00
|
|
|
|
|
|
|
|
|
# external imports
|
|
|
|
|
from eth_accounts_index import AccountsIndex
|
2022-01-31 00:20:46 +01:00
|
|
|
|
from erc20_faucet import Faucet
|
|
|
|
|
from cic_contracts.erc20 import ERC20
|
2022-01-30 21:07:37 +01:00
|
|
|
|
from chainlib.eth.error import RequestMismatchException
|
2022-01-31 00:20:46 +01:00
|
|
|
|
from chainlib.eth.address import AddressChecksum
|
|
|
|
|
from chainlib.eth.constant import ZERO_ADDRESS
|
|
|
|
|
from hexathon import uniform
|
2022-01-30 21:07:37 +01:00
|
|
|
|
|
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
2022-01-31 00:20:46 +01:00
|
|
|
|
faucet_token_cache = {}
|
|
|
|
|
token_cache = {}
|
|
|
|
|
|
2022-01-30 21:07:37 +01:00
|
|
|
|
|
|
|
|
|
def get_method(chain_str, tx):
|
|
|
|
|
data = tx.payload
|
|
|
|
|
s = None
|
2022-01-31 00:20:46 +01:00
|
|
|
|
if len(data) < 8:
|
|
|
|
|
raise ValueError('not minimum signature length')
|
|
|
|
|
|
|
|
|
|
data = uniform(data[:8])
|
2022-01-30 21:07:37 +01:00
|
|
|
|
|
2022-01-31 00:20:46 +01:00
|
|
|
|
chainmap = addrmap.get(chain_str)
|
|
|
|
|
if chainmap != None:
|
|
|
|
|
contractmap = chainmap.get(uniform(tx.inputs[0]))
|
|
|
|
|
if contractmap != None:
|
|
|
|
|
m = contractmap.get(data)
|
|
|
|
|
if m != None:
|
|
|
|
|
return m
|
|
|
|
|
|
|
|
|
|
chainmap = sigmap.get(chain_str)
|
|
|
|
|
if chainmap != None:
|
|
|
|
|
return chainmap.get(data)
|
|
|
|
|
|
|
|
|
|
|
2022-02-27 13:18:57 +01:00
|
|
|
|
def apply(c, result, chain_spec, conn, block, tx, db_session=None):
|
|
|
|
|
s = result.get()
|
2022-01-30 21:07:37 +01:00
|
|
|
|
try:
|
2022-01-31 00:20:46 +01:00
|
|
|
|
m = get_method(str(chain_spec), tx)
|
2022-01-30 21:07:37 +01:00
|
|
|
|
except ValueError as e:
|
2022-02-27 13:18:57 +01:00
|
|
|
|
return True
|
2022-01-30 21:07:37 +01:00
|
|
|
|
|
2022-02-27 13:18:57 +01:00
|
|
|
|
if m == None:
|
|
|
|
|
return True
|
2022-01-30 21:07:37 +01:00
|
|
|
|
|
2022-02-27 13:18:57 +01:00
|
|
|
|
r = m(c, result, chain_spec, conn, block, tx)
|
|
|
|
|
return r
|
2022-01-31 00:20:46 +01:00
|
|
|
|
|
2022-01-30 21:07:37 +01:00
|
|
|
|
|
2022-02-27 13:18:57 +01:00
|
|
|
|
def account_registry_add(c, result, chain_spec, conn, block, tx):
|
2022-01-30 21:07:37 +01:00
|
|
|
|
try:
|
|
|
|
|
o = AccountsIndex.parse_add_request(tx.payload)
|
2022-01-31 00:20:46 +01:00
|
|
|
|
s = '{} block {} tx {} account registration for {}'.format(c, block.number, tx.index, o[0])
|
2022-02-27 13:18:57 +01:00
|
|
|
|
result.set(s)
|
2022-01-31 00:20:46 +01:00
|
|
|
|
except RequestMismatchException as e:
|
2022-02-27 13:18:57 +01:00
|
|
|
|
return True
|
2022-01-31 00:20:46 +01:00
|
|
|
|
|
2022-02-27 13:18:57 +01:00
|
|
|
|
return False
|
2022-01-31 00:20:46 +01:00
|
|
|
|
|
|
|
|
|
|
2022-02-27 13:18:57 +01:00
|
|
|
|
def ge_faucet_gift(i, result, chain_spec, conn, block, tx):
|
2022-01-31 00:20:46 +01:00
|
|
|
|
|
|
|
|
|
c = Faucet(chain_spec)
|
|
|
|
|
o = c.token_amount(tx.inputs[0], sender_address=ZERO_ADDRESS, height=block.number)
|
|
|
|
|
r = conn.do(o)
|
|
|
|
|
v = c.parse_token_amount(r)
|
|
|
|
|
logg.info('retrieved token amount {} at block height {} for faucet {}'.format(v, block.number, tx.inputs[0]))
|
|
|
|
|
|
|
|
|
|
token = faucet_token_cache.get(tx.inputs[0])
|
|
|
|
|
if token == None:
|
|
|
|
|
o = c.token(tx.inputs[0], sender_address=ZERO_ADDRESS)
|
|
|
|
|
r = conn.do(o)
|
|
|
|
|
token = c.parse_token(r)
|
|
|
|
|
token = uniform(token)
|
|
|
|
|
faucet_token_cache[tx.inputs[0]] = token
|
|
|
|
|
logg.info('found token {} for faucet {}'.format(v, token, tx.inputs[0]))
|
|
|
|
|
|
|
|
|
|
token_symbol = token_cache.get(token)
|
|
|
|
|
if token_symbol == None:
|
|
|
|
|
c = ERC20(chain_spec)
|
|
|
|
|
o = c.symbol(token, sender_address=ZERO_ADDRESS)
|
|
|
|
|
r = conn.do(o)
|
|
|
|
|
token_symbol = c.parse_symbol(r)
|
|
|
|
|
token_cache[token] = token_symbol
|
|
|
|
|
logg.info('resolved token {} to symbol'.format(v, block.number, tx.inputs[0]))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
o = Faucet.parse_give_to_request(tx.payload)
|
|
|
|
|
s = '{} GE faucet was triggered for {} {} by {} on block {} tx {}'.format(i, v, token_symbol, o[0], block.number, tx.index)
|
2022-02-27 13:18:57 +01:00
|
|
|
|
result.set(s)
|
2022-01-30 21:07:37 +01:00
|
|
|
|
except RequestMismatchException as e:
|
2022-02-27 13:18:57 +01:00
|
|
|
|
return True
|
2022-01-31 00:20:46 +01:00
|
|
|
|
|
2022-02-27 13:18:57 +01:00
|
|
|
|
return False
|
2022-01-31 00:20:46 +01:00
|
|
|
|
|
2022-01-30 21:07:37 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sigmap = {
|
2022-02-27 13:18:57 +01:00
|
|
|
|
'evm:kitabu:5050:sarafu': {
|
2022-01-30 21:07:37 +01:00
|
|
|
|
'0a3b0a4f': account_registry_add,
|
|
|
|
|
},
|
|
|
|
|
}
|
2022-01-31 00:20:46 +01:00
|
|
|
|
|
|
|
|
|
addrmap = {
|
2022-02-27 13:18:57 +01:00
|
|
|
|
'evm:kitabu:5050:sarafu': {
|
2022-01-31 00:20:46 +01:00
|
|
|
|
'd462aadb7251d418e6a9e4f205928f678e1c6b3b': {
|
|
|
|
|
'63e4bff4': ge_faucet_gift,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|