1

I've used buildTransaction() (which works at Contract.transact()) and I can see my transaction has taken place.

(https://ropsten.etherscan.io/tx/0xebcd3e42340688087162bbf4359390de3d77c2b43d8a4683277651b462117a81)

While doing so, I've used the abi and bin contents for calling BuildTransaction, i.e. contract_ = w3.eth.contract(abi=filecontents,bytecode=bincontents)

transaction = {
'to': '0x6c8B815c51D8B84324804167DD6A1a5623407a3F',
'value': 0,
'gas': 4700000,
'gasPrice': w3.eth.gasPrice,
'nonce': w3.eth.getTransactionCount(address)
}

tx = contract_.buildTransaction(transaction).setGreeting('Omid')
signed = w3.eth.account.signTransaction(tx, pk)
final = w3.eth.sendRawTransaction(signed.rawTransaction)

Solidity contract

contract Greeter {
string public greeting;

function Greeter() public {
  greeting = 'Hello';
}

function setGreeting(string _greeting) public {
  greeting = _greeting;
}

function greet() view public returns (string) {
  return greeting;
}

How can I check that the value I've tried setting through setGreeting has been updated in my contract constructor (whose address I haven't mentioned for calling setGreeting above)?

1

I know this is going to sound a little stupid, but it looks like you just copied that smart contract form another source?

contract Greeter {
string public greeting;

function Greeter() public {
  greeting = 'Hello';
}

function setGreeting(string _greeting) public {
  greeting = _greeting;
}

function greet() view public returns (string) {
  return greeting;
}

So within the smart contract and even in remix where you deployed the contract you can view the contract. Just call the greet and/or greeting public getter. Honestly, you don't need both. As for doing it in web3.py, that's a different issue, but should be indicated in the documentation :)

Remix Contract

https://web3py.readthedocs.io/en/stable/contracts.html#contract-deployment-example

Equivalently in web3, the following commands are useful, now you could skip the creation process and just input the address of the deployed contract. You still need the abi of the compiled contract tho.

# Create the contract instance with the newly-deployed address
greeter = w3.eth.contract(
    address=tx_receipt.contractAddress,
    abi=contract_interface['abi'],
)

# Display the default greeting from the contract
print('Default contract greeting: {}'.format(
    greeter.functions.greet().call()
))

In particular,

greeter.functions.greet().call()

will return hello

  • Thanks for the response, yes, it does work with the remix ide. Do you know how do they decide the value of 'to' when calling a function/transaction? Is that something we have to manually declare when making such transactions on web3.py? Please let me know how to do the same on web3.py? – Siddharth Malhotra Dec 12 '18 at 14:42
  • In the example, web3py.readthedocs.io/en/stable/…, it deploys a contract and then stores its abi. In web3, the to field is equvialently the contract address + the method you want to call. – GrandFleet Dec 12 '18 at 16:46
  • please upvote if my answer was helpful :) – GrandFleet Dec 12 '18 at 16:49
  • I'm trying to understand how can I verify my updated value through web3.py, i.e. how do I get the value 'Omid' back? – Siddharth Malhotra Dec 12 '18 at 17:38
  • After the updating is returned, you can call greeter.functions.greet().call() and it will return greeting. – GrandFleet Dec 12 '18 at 17:40

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.