0

I am trying to call a function which changes the length of a global storage array.

At first I thought that the problem was related to the array-length changing attempt.

But I have finally come to realize that it is related to the way I am calling it via web3.

A similar question is given here, but it does not provide the web3 code which has been used, and so I am unable to make a proper correlation between the answer to it and the problem in my code.

Here is my Python code:

from web3 import Web3
from web3 import RPCProvider
from json import loads

eth = Web3(RPCProvider()).eth
abi = open('Test.abi').read()
bin = open('Test.bin').read()
contract = eth.contract(abi=loads(abi),bytecode=bin)
tx_hash  = contract.deploy(transaction={'from':eth.accounts[0]})
address  = eth.getTransactionReceipt(tx_hash)['contractAddress']
module   = contract(address).call()

print(module.getLength()) # prints 0
module.setLength(1)
print(module.getLength()) # prints 0

And here is my Solidity code:

pragma solidity ^0.4.18;

contract Test {
    uint8[] private arr;

    function Test() public {
    }

    function setLength(uint256 length) public {
        arr.length = length;
    }

    function getLength() public constant returns (uint256) {
        return arr.length;
    }
}

Thank you.

1

module in the question's code will always only make "calls". If you want to change state, you must make a transaction. In web3.py, that looks like:

mycontract = contract(address)
mycontract.transact({... your transaction parameters ...}).setLength(1)

Then, after waiting for the transaction to be mined, getLength() will retrieve the new value.

  • I should use mycontract.call().getLength() though, right? – goodvibration Dec 22 '17 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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