ApeWorX / ApeWorX/web3.py

Proposal: Simplify creating function call transactions

Open
#1,967 0 comments 2 reactions 0 assignees View on GitHub
priority: p2 normal
Dominant language
Python
Stars
5.5k
Forks
1.7k
Avg merge
3d 10h
Merged PRs (30d)
2

Description

* 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](https://infura.io/)), 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:
```python
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](https://web3py.readthedocs.io/en/stable/contracts.html#contract-deployment-example) using the transact funciton:

```python
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](https://github.com/eth-brownie/brownie) has made life even easier. You're able to make a transaction on a function like so:

```python
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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.