0

I was experimenting with this simple contract on a test network:

contract HelloWorld {

  string public message;

  constructor(string memory initMessage) public {
      message = initMessage;
  }

  function update(string memory newMessage) public {
      message = newMessage;
  }
}

The corresponding abi that is created lists message as a function

...
{ 
inputs: [],
name: "message",
outputs: [{
    internalType: "string",
    name: "",
    type: "string"
    }],
stateMutability: "view",
type: "function"
}
....

Why is message listed as function? It seems pointless. If one instantiates this contract and calls mySimpleContract.message() nothing happens, it seems to generate an empty transaction. Why not have the abi define message as type "variable" instead of function?

0

For every public variable in your contract, Solc generates a getter function of the same name.

So what you see in the ABI is in fact a representation of:

function message() public view returns (string);
3
  • I see... if I may add a follow up question, why does calling mySimpleContract.message() from an ethereum client return a transaction hash, instead of simply the value of the string?
    – ttb
    Jul 16 '20 at 8:18
  • @ttb: Because it's a non-constant function (neither pure nor view). In other words, it changes the state of the contract (and/or other contracts). As such, you need to execute it in a transaction. The transaction may still be in progress as far as your off-chain script is concerned, so you get a transaction-hash, which you can use in order to follow the status of that transaction. Jul 16 '20 at 8:45
  • 1
    The return-value of that function is valid and useful only when you call it on-chain (i.e., from some other function in this contract or in another contract), because in your contracts everything is guaranteed to be executed sequentially (i.e., until completion). Jul 16 '20 at 8:46

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.