Skip to content

Commit

Permalink
Fix checkstyle/formatting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
traiansf committed Dec 19, 2024
1 parent b7401c5 commit 58b577b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 32 deletions.
44 changes: 23 additions & 21 deletions pykwasm/src/pykwasm/call.py
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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):
Expand All @@ -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 <node_url> <contract_abi> <contract_address_lit_or_file> <sender_private_key_file> <eth> <method> [param...]'
Expand All @@ -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
Expand All @@ -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()
2 changes: 1 addition & 1 deletion pykwasm/src/pykwasm/deploy_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 6 additions & 7 deletions pykwasm/src/pykwasm/fund_acct.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 4 additions & 1 deletion pykwasm/src/pykwasm/mkacct.py
Original file line number Diff line number Diff line change
@@ -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()
19 changes: 17 additions & 2 deletions tests/ulm/erc20/erc20_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 58b577b

Please sign in to comment.