如何使用C# Nethereum调用智能合约的write函数?

发布于 2025-01-10 17:05:07 字数 1285 浏览 1 评论 0原文

每个人。 我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明天过后 2025-01-17 17:05:07

SendTransactionAsync 方法应接收 TransactionInput 对象;您需要在发送交易之前创建TransactionInput,并定义在区块链中写入所需支付的gas量。

//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。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文