erc20-faucet

ERC20 token faucet
Log | Files | Refs

commit 59c1460b2c9a0f176ef63281efa014a9d1caa953
parent 3e2629eba7e6d506cec1fdde262282777335d529
Author: nolash <dev@holbrook.no>
Date:   Sun,  2 May 2021 11:57:52 +0200

Repair hex parse in method lookup

Diffstat:
Mpython/erc20_faucet/faucet.py | 2++
Mpython/erc20_faucet/interface.py | 52+++++++++++++++++++++++++++++++++++++++++++++++++++-
Mpython/setup.cfg | 2+-
Mpython/tests/test_basic.py | 10++++++++++
4 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/python/erc20_faucet/faucet.py b/python/erc20_faucet/faucet.py @@ -76,6 +76,8 @@ class SingleShotFaucet(Faucet): # TODO: allow multiple overriders def constructor(self, sender_address, token, store, accounts_index, overrider): + if accounts_index == None: + accounts_index = ZERO_ADDRESS code = SingleShotFaucet.bytecode() enc = ABIContractEncoder() enc.uint256(0x80) diff --git a/python/erc20_faucet/interface.py b/python/erc20_faucet/interface.py @@ -1,7 +1,9 @@ # standard imports import logging +import copy # external imports +import sha3 from chainlib.eth.tx import TxFactory from chainlib.eth.constant import ZERO_ADDRESS from chainlib.eth.contract import ( @@ -13,13 +15,58 @@ from chainlib.eth.tx import ( TxFormat, ) from chainlib.jsonrpc import jsonrpc_template -from hexathon import add_0x +from hexathon import ( + add_0x, + strip_0x, + ) +from potaahto.symbols import snake_and_camel_s logg = logging.getLogger().getChild(__name__) class Faucet(TxFactory): + __signatures = { + 'give_to': 'giveTo(address)', + 'token_amount': 'tokenAmount()', + 'token': 'token()', + } + __signatures_reverse = None + + + @classmethod + def method_for(self, b): + if type(b).__name__ == 'str': + b = bytes.fromhex(strip_0x(b)) + if len(b) < 4: + return None + return self.__signatures_reverse.get(b[:4]) + + + @classmethod + def signature_for(self, s): + return self.__signatures.get(s) + + + @classmethod + def build_signatures(cls): + if cls.__signatures_reverse != None: + return + cls.__signatures_reverse = {} + + for (k, v) in copy.copy(Faucet.__signatures).items(): + (x, y) = snake_and_camel_s(k) + h = sha3.keccak_256() + h.update(v.encode('utf-8')) + z = h.digest() + sig = z[:4] + Faucet.__signatures[x] = sig + Faucet.__signatures[y] = sig + Faucet.__signatures_reverse[sig] = x + + return True + + def give_to(self, contract_address, sender_address, beneficiary_address, tx_format=TxFormat.JSONRPC): enc = ABIContractEncoder() enc.method('giveTo') @@ -100,3 +147,6 @@ class Faucet(TxFactory): @classmethod def parse_token_amount(self, v): return abi_decode_single(ABIContractType.UINT256, v) + + +Faucet.build_signatures() diff --git a/python/setup.cfg b/python/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = erc20-faucet -version = 0.2.1a1 +version = 0.2.1a2 description = ERC20 token faucet author = Louis Holbrook author_email = dev@holbrook.no diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py @@ -114,5 +114,15 @@ class TestFaucet(EthTesterCase): self.assertEqual(amount, 1024) + def test_signatures(self): + snake = Faucet(self.chain_spec).signature_for('give_to') + camel = Faucet(self.chain_spec).signature_for('giveTo') + self.assertEqual(snake, camel) + method = Faucet(self.chain_spec).method_for(snake) + self.assertEqual(method, 'give_to') + hx = snake.hex().ljust(64+8, 'f') + method = Faucet(self.chain_spec).method_for(hx) + self.assertEqual(method, 'give_to') + if __name__ == '__main__': unittest.main()