diff --git a/apps/cic-eth/cic_eth/runnable/daemons/filters/token.py b/apps/cic-eth/cic_eth/runnable/daemons/filters/token.py index 6c8ace88..b447332b 100644 --- a/apps/cic-eth/cic_eth/runnable/daemons/filters/token.py +++ b/apps/cic-eth/cic_eth/runnable/daemons/filters/token.py @@ -1,20 +1,32 @@ +# standard imports +import logging + # external imports from eth_erc20 import ERC20 from chainlib.eth.contract import ( ABIContractEncoder, ABIContractType, ) +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.eth.address import is_same_address +from cic_eth_registry import CICRegistry +from cic_eth_registry.erc20 import ERC20Token +from eth_token_index import TokenUniqueSymbolIndex import celery # local imports from .base import SyncFilter +#logg = logging.getLogger(__name__) +logg = logging.getLogger() + class TokenFilter(SyncFilter): - def __init__(self, chain_spec, queue): + def __init__(self, chain_spec, queue, call_address=ZERO_ADDRESS): self.queue = queue self.chain_spec = chain_spec + self.caller_address = call_address def filter(self, conn, block, tx, db_session=None): @@ -25,7 +37,15 @@ class TokenFilter(SyncFilter): r = ERC20.parse_transfer_request(tx.payload) except RequestMismatchException: return (None, None) - + + token_address = tx.inputs[0] + token = ERC20Token(self.chain_spec, conn, token_address) + + registry = CICRegistry(self.chain_spec, conn) + r = registry.by_name(token.symbol, sender_address=self.caller_address) + if is_same_address(r, ZERO_ADDRESS): + return None + enc = ABIContractEncoder() enc.method('transfer') method = enc.get() @@ -33,7 +53,7 @@ class TokenFilter(SyncFilter): s = celery.signature( 'cic_eth.eth.gas.apply_gas_value_cache', [ - tx.inputs[0], + token_address, method, tx.gas_used, tx.hash, diff --git a/apps/cic-eth/tests/filters/test_token_filter.py b/apps/cic-eth/tests/filters/test_token_filter.py index 10cb8a42..04824dfa 100644 --- a/apps/cic-eth/tests/filters/test_token_filter.py +++ b/apps/cic-eth/tests/filters/test_token_filter.py @@ -20,6 +20,7 @@ from chainlib.eth.block import ( ) from chainlib.eth.contract import ABIContractEncoder from hexathon import strip_0x +from eth_token_index import TokenUniqueSymbolIndex # local imports from cic_eth.runnable.daemons.filters.token import TokenFilter @@ -32,10 +33,14 @@ def test_filter_gas( init_database, eth_rpc, eth_signer, + contract_roles, agent_roles, token_roles, foo_token, - celery_session_worker, + token_registry, + register_lookups, + celery_worker, + cic_registry, ): rpc = RPCConnection.connect(default_chain_spec, 'default') @@ -49,7 +54,7 @@ def test_filter_gas( rcpt = eth_rpc.do(o) assert rcpt['status'] == 1 - fltr = TokenFilter(default_chain_spec, queue=None) + fltr = TokenFilter(default_chain_spec, queue=None, call_address=agent_roles['ALICE']) o = block_latest() r = eth_rpc.do(o) @@ -62,6 +67,17 @@ def test_filter_gas( tx_src = unpack(tx_signed_raw_bytes, default_chain_spec) tx = Tx(tx_src, block=block) tx.apply_receipt(rcpt) + t = fltr.filter(eth_rpc, block, tx, db_session=init_database) + assert t == None + + nonce_oracle = RPCNonceOracle(contract_roles['CONTRACT_DEPLOYER'], eth_rpc) + c = TokenUniqueSymbolIndex(default_chain_spec, signer=eth_signer, nonce_oracle=nonce_oracle) + (tx_hash_hex_register, o) = c.register(token_registry, contract_roles['CONTRACT_DEPLOYER'], foo_token) + eth_rpc.do(o) + o = receipt(tx_hash_hex) + r = eth_rpc.do(o) + assert r['status'] == 1 + t = fltr.filter(eth_rpc, block, tx, db_session=init_database) r = t.get_leaf() assert t.successful()