如何使用C# Nethereum调用智能合约的write函数?
每个人。 我在 Avalanche 测试网上部署了这个智能合约。
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);
}
因此,检索功能运行良好,但在存储功能方面出现错误。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SendTransactionAsync
方法应接收TransactionInput
对象;您需要在发送交易之前创建TransactionInput
,并定义在区块链中写入所需支付的gas量。yourFunction
应该是GetFunction("store")
的实例。上例中的气体为 5000000。
The
SendTransactionAsync
method should receive aTransactionInput
object; you need to create theTransactionInput
before sending the transaction, and define the amount of gas to pay for writing in the blockchain.yourFunction
should be the instance ofGetFunction("store")
.The gas in the example above is 5000000.