From 58b577b3cb80b6f0b1f75032e4ed69aa249bb736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Traian-Florin=20=C8=98erb=C4=83nu=C8=9B=C4=83?= Date: Thu, 19 Dec 2024 13:56:55 +0200 Subject: [PATCH] Fix checkstyle/formatting errors --- pykwasm/src/pykwasm/call.py | 44 ++++++++++++++------------ pykwasm/src/pykwasm/deploy_contract.py | 2 +- pykwasm/src/pykwasm/fund_acct.py | 13 ++++---- pykwasm/src/pykwasm/mkacct.py | 5 ++- tests/ulm/erc20/erc20_test.sh | 19 +++++++++-- 5 files changed, 51 insertions(+), 32 deletions(-) diff --git a/pykwasm/src/pykwasm/call.py b/pykwasm/src/pykwasm/call.py index dbeeb6ce6..b853cc6ac 100644 --- a/pykwasm/src/pykwasm/call.py +++ b/pykwasm/src/pykwasm/call.py @@ -1,11 +1,11 @@ #!/usr/bin/python3 -from requests.exceptions import ConnectionError import sys from pathlib import Path from eth_account import Account +from requests.exceptions import ConnectionError from web3 import Web3 -from web3.exceptions import BadFunctionCallOutput, Web3RPCError, ABIFunctionNotFound +from web3.exceptions import BadFunctionCallOutput, Web3RPCError from web3.middleware import SignAndSendRawMiddlewareBuilder ABI_MAP = { @@ -46,7 +46,7 @@ 'name': 'allowance', 'inputs': [{'name': 'owner', 'type': 'address'}, {'name': 'spender', 'type': 'address'}], 'outputs': [{'name': '', 'type': 'uint256'}], - 'stateMutability': 'view' + 'stateMutability': 'view', }, { 'type': 'function', @@ -61,11 +61,14 @@ def parse_arg(param_ty, arg): match param_ty: case 'uint256': - try: return int(arg) - except ValueError: pass - try: return int(arg, 16) + try: + return int(arg) except ValueError: - raise ValueError(f'Failed to parse numeric argument {arg}') + pass + try: + return int(arg, 16) + except ValueError as err: + raise ValueError(f'Failed to parse numeric argument {arg}') from err case 'address': assert Web3.is_address(arg) return arg @@ -78,7 +81,7 @@ def parse_params(abi, method, args): if ty == 'function' and name == method: if len(inputs) != len(args): raise ValueError('call to method {method} with {inputs} has incorrect parameters {params}') - for param, arg in zip(inputs, args): + for param, arg in zip(inputs, args, strict=True): parsed_args.append(parse_arg(param['type'], arg)) break else: @@ -88,14 +91,14 @@ def parse_params(abi, method, args): def run_method(w3, contract, sender, eth, method, params): func = contract.functions[method](*params) - viewLike = func.abi.get('stateMutability', 'nonpayable') in {'view','pure'} + view_like = func.abi.get('stateMutability', 'nonpayable') in {'view', 'pure'} try: - if viewLike: - resultOrReceipt = func.call() + if view_like: + result_or_receipt = func.call() else: tx_hash = func.transact({'from': sender.address, 'value': eth}) - resultOrReceipt = w3.eth.wait_for_transaction_receipt(tx_hash) - except (ConnectionError, ConnectionRefusedError, BadFunctionCallOutput, Web3RPCError) as e: + result_or_receipt = w3.eth.wait_for_transaction_receipt(tx_hash) + except (ConnectionError, BadFunctionCallOutput, Web3RPCError) as e: if isinstance(e, (ConnectionError, ConnectionRefusedError)): msg = f'Failed to connect to node: {e.message}' elif isinstance(e, BadFunctionCallOutput): @@ -105,7 +108,7 @@ def run_method(w3, contract, sender, eth, method, params): print(msg, file=sys.stderr) sys.exit(1) - return (viewLike, resultOrReceipt) + return (view_like, result_or_receipt) USAGE = 'call.py [param...]' @@ -130,7 +133,7 @@ def main(): # validate method try: contract.functions[method] - except BaseException: + except BaseException: # noqa: B036 print(f'Invalid method {method} for {abi_name} contract ABI', file=sys.stderr) sys.exit(1) # get sender @@ -142,18 +145,17 @@ def main(): params = parse_params(abi, method, params) eth = int(eth) # run method - (viewLike, resultOrReceipt) = run_method(w3, contract, sender, eth, method, params) + (view_like, result_or_receipt) = run_method(w3, contract, sender, eth, method, params) # handle result - if viewLike: - print(resultOrReceipt) + if view_like: + print(result_or_receipt) else: # return exit code based on status which is 1 for confirmed and 0 for reverted - success = bool(resultOrReceipt['status']) + success = bool(result_or_receipt['status']) if not success: - print(resultOrReceipt, file=sys.stderr) + print(result_or_receipt, file=sys.stderr) sys.exit(int(not success)) - if __name__ == '__main__': main() diff --git a/pykwasm/src/pykwasm/deploy_contract.py b/pykwasm/src/pykwasm/deploy_contract.py index 9869d805e..180be508e 100644 --- a/pykwasm/src/pykwasm/deploy_contract.py +++ b/pykwasm/src/pykwasm/deploy_contract.py @@ -26,7 +26,7 @@ def deploy_contract(w3, sender, contract_hex): try: deploy_tx_hash = w3.eth.send_transaction(deploy_token_tx) deploy_tx_receipt = w3.eth.wait_for_transaction_receipt(deploy_tx_hash) - except (ConnectionError, ConnectionRefusedError): + except ConnectionError: print('Failed to connect to node', file=sys.stderr) sys.exit(1) except Web3RPCError as e: diff --git a/pykwasm/src/pykwasm/fund_acct.py b/pykwasm/src/pykwasm/fund_acct.py index cead93615..9bc6d652d 100644 --- a/pykwasm/src/pykwasm/fund_acct.py +++ b/pykwasm/src/pykwasm/fund_acct.py @@ -1,21 +1,20 @@ #!/usr/bin/python3 import sys -from requests.exceptions import ConnectionError from pathlib import Path from eth_account import Account +from requests.exceptions import ConnectionError from web3 import Web3 -from web3.middleware import SignAndSendRawMiddlewareBuilder + +# from web3.middleware import SignAndSendRawMiddlewareBuilder def fund_acct(w3, addr): try: - fund_tx_hash = w3.eth.send_transaction( - {'from': w3.eth.accounts[0], 'to': addr, 'value': 1000000000000000000} - ) + fund_tx_hash = w3.eth.send_transaction({'from': w3.eth.accounts[0], 'to': addr, 'value': 1000000000000000000}) fund_tx_receipt = w3.eth.wait_for_transaction_receipt(fund_tx_hash) - except (ConnectionError, ConnectionRefusedError): - print("Failed to connect to node", file=sys.stderr) + except ConnectionError: + print('Failed to connect to node', file=sys.stderr) sys.exit(1) return fund_tx_receipt diff --git a/pykwasm/src/pykwasm/mkacct.py b/pykwasm/src/pykwasm/mkacct.py index d21128843..b2863876e 100644 --- a/pykwasm/src/pykwasm/mkacct.py +++ b/pykwasm/src/pykwasm/mkacct.py @@ -1,11 +1,14 @@ from web3 import Web3 + def mkaddr(): w3 = Web3() return w3.to_hex(w3.eth.account.create().key) + def main(): print(mkaddr()) -if __name__ == "__main__": + +if __name__ == '__main__': main() diff --git a/tests/ulm/erc20/erc20_test.sh b/tests/ulm/erc20/erc20_test.sh index 82e1ac612..1357fe1d1 100755 --- a/tests/ulm/erc20/erc20_test.sh +++ b/tests/ulm/erc20/erc20_test.sh @@ -14,21 +14,36 @@ fi # generate some accounts a1=$(mkacct) +echo "Account1: $a1" + a2=$(mkacct) +echo "Account2: $a2" + a3=$(mkacct) +echo "Account3: $a3" + # fund accounts +echo "Funding account 1" fund /dev/stdin <<< $a1 + +echo "Funding account 2" fund /dev/stdin <<< $a2 + +echo "Funding account 3" fund /dev/stdin <<< $a3 # deploy contract +echo "Deploying contract" contract=$(deploy build/erc20/erc20.bin http://localhost:8545 /dev/stdin <<< $a1) +echo "Contract deployed. Contract address: $contract" # check decimals +echo "Calling decimals on Account 1" decimals=$(call http://localhost:8545 erc20 $contract /dev/stdin 0 decimals <<< $a1) +echo "Called decimals on Account 1. Result: $decimals" # check total supply +echo "Calling supply on Account 1" supply=$(call http://localhost:8545 erc20 $contract /dev/stdin 0 totalSupply <<< $a1) - - +echo "Called supply on Account 1. Result: $supply"