0

Using Brownie, Python in VSCode:

I am trying to combine getNumbers and findNumbers into findNumbers2.

But findNumbers2 returned a transaction id rather than the result data.

Any suggestions?

Solidity code:

uint[] public numbers;

function getNumbers() public view returns (uint[] memory) {
    return numbers;
}

function findNumbers(uint abc) public {
    delete numbers;
    for (uint i = 0; i < 5; i++) {
        numbers.push(abc + i);
    }
}

function findNumbers2(uint abc) public returns (uint[] memory) {
    delete numbers;
    for (uint i = 0; i < 5; i++) {
        numbers.push(abc + i);
    }
    return numbers;
}

Python code:

emContract.findNumbers(
    123,
    {"from": account},
)
print(f"getNumbers {emContract.getNumbers()}")

result = emContract.findNumbers2(
    123,
    {"from": account},
)
print(f"findNumbers2 result {result}")

Output:

Transaction sent: 0x7a0ab0d824d073c880f1062b0426521a7a2d522d6e056051af3eed71316bf67b
  Gas price: 20.0 gwei   Gas limit: 172342   Nonce: 162
  ContractsWithEm.findNumbers confirmed   Block: 163   Gas used: 156675 (90.91%)

getNumbers (123, 124, 125, 126, 127)
Transaction sent: 0x4d3765fddd27c5cfc17c762ac2ccc4308641aa98052befb5b67126f1c4a05770
  Gas price: 20.0 gwei   Gas limit: 68288   Nonce: 163
  ContractsWithEm.findNumbers2 confirmed   Block: 164   Gas used: 45280 (66.31%)

findNumbers2 result <Transaction '0x4d3765fddd27c5cfc17c762ac2ccc4308641aa98052befb5b67126f1c4a05770'>

1 Answer 1

3

When you call a non-view function that modifies the state of the contract, a transaction is sent to the blockchain network.Then you cannot directly retrieve the return value from the function call. Instead, you need to wait for the transaction to be mined and then retrieve the updated value from the contract state by calling a view function that returns the value you're interested in.

To do this in Python, you can use the web3.py library to interact with the Ethereum blockchain. You can wait for the transaction to be mined using the wait_for_transaction_receipt method, and then retrieve the updated value using a separate function call that reads the state of the contract. Something like this:

result = emContract.findNumbers2(
    123,
    {"from": account},
)
tx_receipt = w3.eth.wait_for_transaction_receipt(result)
numbers = emContract.getNumbers()
print(f"findNumbers2 result {numbers}")

UPDATE

To use the w3.eth functionality in Python, you first need to install the web3.py library. You can install web3.py using pip, the Python package manager. Here are the steps to install web3.py:

  • Open a terminal or command prompt on your computer.
  • Type pip install web3 and press Enter.
  • Wait for the installation to complete.

Once you have installed web3.py, you can import it in your Python script and use it to interact with the Ethereum blockchain. Here is an example of how to import web3.py and connect to a local Ethereum node:

from web3 import Web3

# Connect to a local Ethereum node
w3 = Web3(Web3.HTTPProvider('http://localhost:8545'))

Note that you will need to replace 'http://localhost:8545' with the URL of your Ethereum node if you are connecting to a remote node. You can then use w3.eth to interact with the Ethereum blockchain and wait for transaction receipts.

Hope this helps!

3
  • I will upvote if you can tell me how to install w3.eth part!
    – EmilyJ
    Mar 30, 2023 at 1:43
  • 1
    I update the answer, hope it helps! Mar 30, 2023 at 3:01
  • Thanks for a thorough answer!
    – EmilyJ
    Mar 31, 2023 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.