2
votes

I tried using returnString.CallAsync(); to a smart contract function that returns bytes or string but Im getting null. Would you know why this is the case with functions that return string or bytes and not int? I am using nethereum as the .net client for ethereum.

here is my contract

contract A {
    string myString = "someString";

    function A(int multiplier) {
        
    }

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

Here is my .net nethereum code:

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[]>();
            }
1
try unlock using private key....... string privateKey = "your private key"; var account = new Nethereum.Web3.Accounts.Account(privateKey); var web3 = new Web3(account, "GethPath"); your code..... - Jayant Bramhankar

1 Answers

0
votes

When running on local Ganache, this contract works fine (the correct string is returned). No need to mine or to wait.

I've created a example project here https://github.com/StefH/Solidity-Examples/tree/master/SmartContracts/Examples/GetString.

Go to Solidity folder and run npm i and npm run build to generate C# interface and implementation files.

In the ConsoleApp folder there is a example dotnet core console app which outputs like:

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