I am using Nethereum to call a smart contract function in Ropsten. I am also using Infura.

The function required the contract owner's permission to execute. This is my code:

var account = new Account(ownerPrivateKey)
var web3 = new Web3(account, $"https://ropsten.infura.io/v3/{infuraId}");
var contract = web3.Eth.GetContract(abi, contractAddress);
var function = contract.GetFunction("myFunction");
var result = await function .CallAsync<dynamic>(address, true);

I got error: Nethereum.JsonRpc.Client.RpcResponseException: execution reverted: Ownable: caller is not the owner. I think I need to sign the transaction before sending it but I don't know how. I don't know whether I should use CallAsync or SendTransactionAsync because the function writes data down, not reading anything. I tried SendTransactionAsync but it doesn't seem to work with Infura. Anyone have experience with this, please help me. I am new to this.

My contract function:

function myFunction(address account, bool flag) internal {
    _address[account] = flag;
  • In what language exactly is $"https://ropsten.infura.io/v3/{infuraId}"? – goodvibration Jan 4 at 8:22
  • Sorry I didn't mention the language. I am using C#. I assumed Nethereum is written for C#. – Anh Vũ Nguyễn Jan 4 at 8:29
  • "I think I need to sign the transaction before sending it": The error execution reverted: Ownable: caller is not the owner implies that this is not the problem. Sounds like you can fix this error be changing the value of ownerPrivateKey to be that of the account used for deploying this contract. – goodvibration Jan 4 at 8:33
  • Or if you've already transferred the ownership of this contract, then the private key of the account which you've transferred the ownership to. – goodvibration Jan 4 at 8:37
  • I logged in Metamask and checked the private key I am using is of the contract owner. – Anh Vũ Nguyễn Jan 4 at 8:54

It seems that your function checks is the valid owner, hence you have a revert error message Nethereum.JsonRpc.Client.RpcResponseException: execution reverted: Ownable: caller is not the owner. This is the output after making a CallAsync and getting the solidity revert message.

If you want to change the state of the contract (mainly write to the contract / ethereum) you need to use SendTransactionAsync instead of a CallAsync

Back to the first issue, as you are working "untyped", (interacting using strings and not specific contract definitions), you need to set the from parameter to your account when making a CallAsync. You can use the account.Address for this. Mainly when doing a call / query to the smart contract, setting the from value in the CallAsync will make that the msg.sender in solidity as in a transaction call.

Check this example http://playground.nethereum.com/csharp/id/1045 (very big example not fit for here) on how to interact that way.

In the long run I recommend using the code generator with typed contract definitions, it is much easier in the long run (also gives you support for structs, generate your events, and multiple output, auto estimate gas, etc)

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.