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

时间:2017-05-18 06:02:43

标签: c# ethereum

我尝试使用returnString.CallAsync();到一个智能契约函数返回字节或字符串,但我得到null。你知道为什么这是返回字符串或字节而不是int的函数的情况?我使用nethereum作为以太坊的.net客户端。

这是我的合同

&#13;
&#13;
contract A {
    string myString = "someString";

    function A(int multiplier) {
        
    }

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

这是我的.net nethereum代码:

&#13;
&#13;
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[]>();
            }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在本地Ganache上运行时,此合同正常(返回正确的字符串)。无需挖掘或等待。

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

转到Solidity文件夹并运行npm inpm run build以生成C#界面和实施文件。

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

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