0

I have the following contract method:

    function setTokenURI(uint256 tokenid, string memory uri) public {
        require(bytes(uri).length != 0, "Cannot set an empty URI");
        _setTokenURI(tokenid, uri);
    }

and when I call it from my web3py script:

contract.functions.setTokenURI(token, "https://gateway.pinata.cloud/ipfs/2jh2kl3j4h23jk4h2").call()

I don't get any errors, but the URI is not set. I run other functions against the contract with the web3py interface and they all work as expected.

Why would this call not update the NFT tokenURI?

1
  • Writing functions on blockchain requires gas, are you sending gas cost ? Commented Jul 11, 2021 at 8:42

1 Answer 1

1

tl;dr: instead of using contract.functions.setURI(...).call(), use contract.functions.setURI.transact()

There's a big difference between how web3 libraries call a read-only function and how they call a state-changing function. For the former, no value needs to be sent, nothing needs to be signed with a private key. When state is being changed, though, a transaction is made. Changing the state requires sending ether over the network to pay for gas, which requires forming a transaction signed by a private key.

These two ways of interacting with the chain are reflected by the two different suffixes you can use with web3.py: call() and transact(). call() is used for reading data from the chain (view or pure functions), whereas transact() is used for state-changing functions.

Depending on your web3.py setup, you may need to add details to your transact() call. transact() can take an object as an argument in which you can specify what address the tx should originate from, the amount of ether being sent, etc. The docs for transact() are here, and the docs for what you can put in the object arg are here (the object follows the format of a transaction object, which is outlined there).

Hope that helps!

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.