1

Everyone. I deployed this smart contract on Avalanche Testnet.

contract Storage {

uint256 number;

/**
 * @dev Store value in variable
 * @param num value to store
 */
function store(uint256 num) public {
    number = num;
}

/**
 * @dev Return value 
 * @return value of 'number'
 */
function retrieve() public view returns (uint256){
    return number;
}

}

I'm trying to call write function("store" in this contract) using Nethereum.

        Task<BigInteger> retrieveFunction = tmpContract.GetFunction("retrieve").CallAsync<BigInteger>();
        retrieveFunction.Wait();
        int result1 = (int)retrieveFunction.Result;


        //Prompts for the account address.
        Console.Write("Current stored amount: {0}\n", result1);
        string accountAddress = "0xa40e61095202Afe72dFfc4Aae70bc631429293B2";
                    
        BigInteger value = 450000;
        try
        {
            
            Task<string> storeFunction = tmpContract.GetFunction("store").SendTransactionAsync(accountAddress, value);
            storeFunction.Wait();
            Console.WriteLine("Succesfully Stored!");
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e.Message);
        }

As a result, retrieve function is working well but in store function side, occurs errors.

LiYiChong
  • 11
  • 2
  • you should call yourself 'expert', as you're doing something so unsual and new. – Lei Yang Feb 28 '22 at 12:24
  • 2
    Please edit your question to show all the code and the results as *text* instead of as images. Also note that if you log the complete exception rather than just the message, you may well see more information, e.g. inner exceptions. – Jon Skeet Feb 28 '22 at 12:48

1 Answers1

0

The SendTransactionAsync method should receive a TransactionInput object; you need to create the TransactionInput before sending the transaction, and define the amount of gas to pay for writing in the blockchain.

//Number to store
var n = new Nethereum.Hex.HexTypes.HexBigInteger(450000);

//  Create the transaction input
var transactionInput = yourFunction.CreateTransactionInput(account.Address, new Nethereum.Hex.HexTypes.HexBigInteger(5000000), null, null, n);

// Send the transaction
var transactionHash = await web3.Eth.TransactionManager.SendTransactionAsync(transactionInput);

yourFunction should be the instance of GetFunction("store").

The gas in the example above is 5000000.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Farceg
  • 1
  • 2