Skip to content

Proposal: Simplify creating function call transactions #1967

@PatrickAlphaC

Description

@PatrickAlphaC
  • Version: 5.18.0
  • Python: 3.8
  • OS: osx
  • pip freeze output
web3==5.18.0

Abstract

Right now, if you use a wallet address that is different from your node (such as a metamask wallet, and a web3 client like infura), you have to follow a long buildtransaction process in order to send a transaction.

For example, calling the approve function on an erc20 on the kovan network:

from web3 import WEB3
w3 = Web3(Web3.HTTPProvider(os.getenv('KOVAN_RPC_URL')))

erc20 = w3.eth.contract(address=erc20_address, abi=erc20_abi)
function_call = erc20.functions.approve(spender, amount)
nonce = w3.eth.getTransactionCount(my_address)
transaction = function_call.buildTransaction({'chainId': 42,
                                                  'from': my_address,
                                                  'nonce': nonce})
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=PRIVATE_KEY)
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
w3.eth.wait_for_transaction_receipt(tx_hash)

However, if my web3 connection is also my wallet, it looks like I can do something like this using the transact funciton:

erc20 = w3.eth.contract(address=erc20_address, abi=erc20_abi)
w3.eth.default_account = w3.eth.accounts[0]
tx_hash = erc20.functions.approve(spender, amount).transact()
w3.eth.wait_for_transaction_receipt(tx_hash)

This seems much nicer.
Brownie has made life even easier. You're able to make a transaction on a function like so:

from brownie import interface, accounts

acct = accounts.add(PRIVATE_KEY)
erc20 = interface.IERC20(ERC20_ADDRESS)
tx_hash = erc20.approve(spender, amount, {'from': acct})
tx_hash.wait(1)

Which feels soooo clean. It is able to tell whether or not the function call is going to do a state change or not. If we could have something similar for web3.py in general, I think it would greatly benefit overall developer experience.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions