c# - Nethereum 中的 functioncall.CallAsync<string>() 返回 null

标签 c# ethereum

我尝试使用 returnString.CallAsync();到返回字节或字符串但我得到空值的智能合约函数。您知道为什么返回字符串或字节而不是 int 的函数会出现这种情况吗?我正在使用 nethereum 作为 ethereum 的 .net 客户端。

这是我的契约(Contract)

contract A {
    string myString = "someString";

    function A(int multiplier) {
        
    }

    function getMyString() returns (bytes) {
        bytes memory b3 = bytes(myString);
        return b3;
    }
}

这是我的 .net nethereum 代码:​​

var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var password = "password";
            var abi = @"[{""constant"":false,""inputs"":[],""name"":""getMyString"",""outputs"":[{""name"":"""",""type"":""bytes""}],""payable"":false,""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""payable"":false,""type"":""constructor""}]";
            var byteCode =
                "0x60a0604052600a60608190527f736f6d65537472696e6700000000000000000000000000000000000000000000608090815261003e9160009190610060565b50341561004757fe5b6040516020806102a383398101604052515b5b50610100565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a157805160ff19168380011785556100ce565b828001600101855582156100ce579182015b828111156100ce5782518255916020019190600101906100b3565b5b506100db9291506100df565b5090565b6100fd91905b808211156100db57600081556001016100e5565b5090565b90565b6101948061010f6000396000f300606060405263ffffffff60e060020a6000350416637e7b8a968114610021575bfe5b341561002957fe5b6100316100b1565b604080516020808252835181830152835191928392908301918501908083838215610077575b80518252602083111561007757601f199092019160209182019101610057565b505050905090810190601f1680156100a35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100b9610156565b6100c1610156565b6000805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156101475780601f1061011c57610100808354040283529160200191610147565b820191906000526020600020905b81548152906001019060200180831161012a57829003601f168201915b505050505090508091505b5090565b604080516020810190915260008152905600a165627a7a72305820eb300cf43a9ba95dde6cd63161d8229e787f1e4daf4f549c303011176546f2ba0029";


            var web3 = new Web3.Web3();
            var unlockAccountResult =
                await web3.Personal.UnlockAccount.SendRequestAsync(senderAddress, password, 120);

            var transactionHash =
                await web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, 1);

            var mineResult = await web3.Miner.Start.SendRequestAsync(6);


            var receipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionHash);

            while (receipt == null)
            {
                Thread.Sleep(5000);
                receipt = await   web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionHash);
            }

            mineResult = await web3.Miner.Stop.SendRequestAsync();

            var contractAddress = receipt.ContractAddress;

            var contract = web3.Eth.GetContract(abi, contractAddress);

            var returnString = contract.GetFunction("getMyString");

            byte[] result2 = await returnString.CallAsync<byte[]>();

            while (result2 == null)
            {
                Thread.Sleep(5000);
                result2 = await returnString.CallAsync<byte[]>();
            }

最佳答案

当在本地 Ganache 上运行时,这个合约工作正常(返回正确的字符串)。无需挖矿或等待。

我在这里创建了一个示例项目 https://github.com/StefH/Solidity-Examples/tree/master/SmartContracts/Examples/GetString .

进入Solidity文件夹,运行npm inpm run build生成C#接口(interface)和实现文件。

ConsoleApp 文件夹中有一个示例 dotnet 核心控制台应用程序,其输出如下:

Blockchain - Ganache - ConsoleApp
________________________________________________________________________________
DeployContractAsync
Call GetMyStringCallAsync
result = `someString`
________________________________________________________________________________

关于c# - Nethereum 中的 functioncall.CallAsync<string>() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44039721/

相关文章:

c# - 如果初始化失败,如何将 IDisposable 对象传递给在 C# 中处理的基本构造函数参数?

c# - 将字符串格式化为具有负货币的货币,例如 $(10.00)

javascript - Ethereum web3 sendSignedTransaction : Insufficient funds. 该帐户...没有足够的资金。需要 750000000000000 并得到:0

ethereum - Solidity 中的字符串数组

javascript - 在 Azure Function 上使用 web3.js 调用以太坊

ethereum - 使用 web3j 发送 ERC20 代币始终处于挂起状态

c# - 在 CodeObjectCreateExpression 中分配对象的属性值

c# - 为什么在检查变量是否为枚举值时不能使用 'as'?

blockchain - 如何从浏览器创建一个新的以太坊节点?

c# - 影子的目的是什么?