chainlib

Generic blockchain access library and tooling
Log | Files | Refs | README | LICENSE

commit b4531c22ccbc37e3200dc2be0127f06cd22ddfc2
parent 6e44b903edf1a58c15f2f5719bf01d20b344e948
Author: nolash <dev@holbrook.no>
Date:   Mon, 15 Feb 2021 18:59:55 +0100

Add missing encoding lib file

Diffstat:
Achainlib/eth/encoding.py | 20++++++++++++++++++++
Mchainlib/eth/erc20.py | 9++++-----
2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/chainlib/eth/encoding.py b/chainlib/eth/encoding.py @@ -0,0 +1,20 @@ +from eth_abi import ( + encode_single as __encode_single, + decode_single as __decode_single, + ) + +def abi_encode(signature, *args): + return __encode_single(signature, *args) + + +def abi_encode_hex(signature, *args): + return __encode_single(signature, *args).hex() + + +def abi_decode(signature, *args): + return __decode_single(signature, *args) + + +def abi_decode_hex(signature, *args): + return __decode_single(signature, *args).hex() + diff --git a/chainlib/eth/erc20.py b/chainlib/eth/erc20.py @@ -1,7 +1,6 @@ # third-party imports import sha3 from hexathon import add_0x -from eth_abi import encode_single from crypto_dev_signer.eth.transaction import EIP155Transaction # local imports @@ -12,6 +11,7 @@ from chainlib.hash import ( from .constant import ZERO_ADDRESS from .rpc import jsonrpc_template from .tx import TxFactory +from .encoding import abi_encode # TODO: move to cic-contracts @@ -21,14 +21,13 @@ erc20_transfer_signature = keccak256_string_to_hex('transfer(address,uint256)')[ class ERC20TxFactory(TxFactory): - def erc20_balance(self, contract_address, address, sender_address=ZERO_ADDRESS): o = jsonrpc_template() o['method'] = 'eth_call' data = erc20_balance_signature - data += encode_single('address', address).hex() + data += abi_encode('address', address).hex() data = add_0x(data) tx = self.template(sender_address, contract_address) tx = self.set_code(tx, data) @@ -50,8 +49,8 @@ class ERC20TxFactory(TxFactory): def erc20_transfer(self, contract_address, sender_address, recipient_address, value): data = erc20_transfer_signature - data += encode_single('address', recipient_address).hex() - data += encode_single('uint256', value).hex() + data += abi_encode('address', recipient_address).hex() + data += abi_encode('uint256', value).hex() data = add_0x(data) tx = self.template(sender_address, contract_address) tx = self.set_code(tx, data)