每个人。我在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;
}
}
我正在尝试使用Nethereum调用write函数(在这个合约中为"store"(。
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);
}
因此,检索函数运行良好,但在存储函数方面,会出现错误。
SendTransactionAsync
方法应该接收一个TransactionInput
对象;在发送交易之前,您需要创建TransactionInput
,并定义在区块链中写入所需的气体量。
//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
应该是GetFunction("store")
的实例。
上述示例中的气体为5000000。