Skip to content

Commit

Permalink
feat: allow hex address as contract function input. Resolves #52
Browse files Browse the repository at this point in the history
  • Loading branch information
darwintree committed Dec 5, 2024
1 parent e771281 commit 7f6be5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 5 additions & 6 deletions conflux_web3/_utils/abi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
build_non_strict_registry,
AddressEncoder,
)
from cfx_address import (
Base32Address,
validate_base32
from cfx_address.utils import (
normalize_to
)
from cfx_utils.exceptions import (
InvalidBase32Address
Expand All @@ -26,16 +25,16 @@

class Base32AddressEncoder(AddressEncoder):

encode_fn = lambda self, address: AddressEncoder.encode_fn(Base32Address(address).hex_address)
encode_fn = lambda self, address: AddressEncoder.encode_fn(normalize_to(address, None))

@classmethod
def validate_value(cls, value: Any) -> None:
if is_cns_name(value):
return
try:
validate_base32(value)
normalize_to(value, None)
except InvalidBase32Address:
raise EncodingError(InvalidBase32Address)
raise EncodingError(f"Not a valid Base32 address nor hex address: {value}")

class CfxAddressDecoder(AddressDecoder):
decode_fn = lambda x: x
Expand Down
10 changes: 10 additions & 0 deletions tests/transaction/contract/test_encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from conflux_web3 import Web3


def test_encode_function(w3: Web3):
contract = w3.cfx.contract(name="ERC20", with_deployment_info=False)("0x8888888888888888888888888888888888888888")
account = w3.cfx.account.create()

assert contract.functions.transfer(account.base32_address, 1000000000000000000)._encode_transaction_data() == contract.functions.transfer(account.hex_address, 1000000000000000000)._encode_transaction_data()


0 comments on commit 7f6be5c

Please sign in to comment.