[Ethereum] Nethereum contract function call throwing exception on return: hex string has length 64, want 40 for common.Address

c#nethereumrpc

Both a call returning and address and a call returning an int256 fail with the same error:

Nethereum.JsonRpc.Client.RpcResponseException
  HResult=0x80131500
  Message=invalid argument 0: hex string has length 64, want 40 for common.Address
  Source=Nethereum.Portable
  StackTrace:
   at Nethereum.JsonRpc.Client.RpcClient.HandleRpcError(RpcResponseMessage response) in H:\Projects\BlockChain\Ethereum\Oricals\Nethereum-master\src\Nethereum.JsonRpc.RpcClient\RpcClient.cs:line 67

The C# code is:

using System;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Contracts;
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.Eth.DTOs; //using Nethereum.Geth;


namespace Test08
{
    class Program
    {
        class AccountClass
        {
            string privateKey; // = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            string senderAddress; // = "0x12890d2cce102216644c59daE5baed380d84830c";
            string password;
            Account account; // = new Account(privateKey);

            public string getPrivateKey() { return (privateKey); }
            public string getSenderAddress() { return (senderAddress); }
            public string getPassword() { return (password); }
            public Account getAccount() { return (account); }


            public void setPrivateKey(string inPrivateKey) { privateKey = inPrivateKey; }
            public void setSenderAddress(string inSenderAddress) { senderAddress = inSenderAddress; }
            public void setPassword(string inPassword) { password = inPassword; }

            public void setAccount(Account inAccount) { account = inAccount; }

            public AccountClass(string inPrivateKey, string inSenderAddress, string inPassword, Account inAccount)
            {
                privateKey = inPrivateKey;
                senderAddress = inSenderAddress;
                password = inPassword;
                account = inAccount;
            }
        };


        class SyncFunction : Nethereum.Contracts.Function
        {
            public SyncFunction(Contract contract, FunctionBuilder functionBuilder) : base(contract, functionBuilder)
            {

            }

            public async Task<TransactionReceipt> SendTransactionAndWaitForReceiptSync(string from, CancellationTokenSource receiptRequestCancellationToken = null, params object[] functionInput)
            {
                return (await SendTransactionAndWaitForReceiptAsync(from, receiptRequestCancellationToken, functionInput));
            }

            public async Task<TransactionReceipt> SendTransactionAndWaitForReceiptSync(TransactionInput input, CancellationTokenSource receiptRequestCancellationToken = null, params object[] functionInput)
            {
                return (await SendTransactionAndWaitForReceiptAsync(input, receiptRequestCancellationToken, functionInput));
            }

        }

        public static async Task<uint> RetrieveInt(Contract contract, string FunctionName)
        {
            var FunctionAddress = contract.GetFunction(FunctionName);

            Console.WriteLine("Calling " + FunctionName + ":");

            var result2 = await FunctionAddress.CallAsync<uint>();

            Console.WriteLine(FunctionName + " Returned: " + result2);

            return (result2);
        }

        public static async Task<string> RetrieveAddress(Contract contract, string FunctionName)
        {
            var FunctionAddress = contract.GetFunction(FunctionName);

            Console.WriteLine("Calling " + FunctionName + ":");

            var result2 = await FunctionAddress.CallAsync<string>();

            Console.WriteLine(FunctionName + " Returned: " + result2);

            return (result2.ToString());
        }



        static async Task Main(string[] args)
        {
            AccountClass Tester = new AccountClass("",
                                            "0x0795e077E62Ae7565d589713b4c6D547f5185FC7",
                                            "PrivateTest001",
                                            null);


            string Test08Address = "0x9dB5E63F4319Ec4B2c0403C0bDeA43D864D94FC0";
            string Test08ABI = File.ReadAllText("Test08ABI.txt");
            string Test08BIN = File.ReadAllText("Test08BIN.txt");
            string Test08ByteCode = File.ReadAllText("Test08ByteCode.txt");


            var BasicWeb3 = new Web3();

            var client = BasicWeb3.Client;

            var web3 = new Nethereum.Geth.Web3Geth(client);

            var unlockAccountResult = await web3.Personal.UnlockAccount.SendRequestAsync(Tester.getSenderAddress(), Tester.getPassword(), 120);
            Debug.Assert(unlockAccountResult);

            try
            {
                var mineResult = await web3.Miner.Start.SendRequestAsync(6);
            }
            catch (Exception e)
            {
                Console.WriteLine("Miner Start reported a Failure: " + e.Message);
                Console.WriteLine();
            }

            HexBigInteger gas = new HexBigInteger(4000000);

            Test08Address = await web3.Eth.DeployContract.SendRequestAsync(Test08ABI, Test08BIN, Tester.getSenderAddress(), gas);


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

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

            //mineResult = await web3.Miner.Stop.SendRequestAsync();
            try
            {
                var mineResult = await web3.Miner.Stop.SendRequestAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine("Miner Stop reported a Failure: " + e.Message);
                Console.WriteLine();
            }

            var Test08ContractAddress = Test08Address;

            var contract = web3.Eth.GetContract(Test08ABI, Test08ContractAddress);

            // test of get int
            uint result;

            result = await RetrieveInt(contract, "getTestAFee"); // retrieve value and display to console

            result = await RetrieveInt(contract, "getTestBFee"); // retrieve value and display to console

            result = await RetrieveInt(contract, "getTestcFee"); // retrieve value and display to console

            result = await RetrieveInt(contract, "getTesterFee"); // retrieve value and display to console

            string AddressResult;

            AddressResult = await RetrieveAddress(contract, "getTester"); // retrieve value and display to console


            Console.WriteLine("Test08 Contract Calls complete.");
        }
    }
}

The solidity contract code:

pragma solidity ^0.4.17;


contract Test08 {

    address Creator;  // Creating address
    address TesterAddress;  // Owner of this Test

    uint TesterFee;
    uint TestAFee;
    uint TestBFee;
    uint TestcFee;
    uint TestCount;

    address TokenAddress;

//    function Test08(uint inTesterFee, uint inTestAFee, uint inTestBFee, uint inTestcFee, address token) public {
    function Test08() public {

        TesterAddress = msg.sender;
        TesterFee = 5; // inTesterFee;
        TestAFee = 6; // inTestAFee;
        TestBFee = 7; // inTestBFee;
        TestcFee = 8; // inTestcFee;
        Creator = msg.sender;
        TokenAddress = 1234; // token;
        TestCount = 0;
    }


  /* The function without name is the default function that is called whenever anyone sends funds to a contract */
    function () public payable {
    }


    function getTesterFee() public view returns (uint) {return(TesterFee);}
    function getTestAFee() public view returns (uint) {return(TestAFee);}
    function getTestBFee() public view returns (uint) {return(TestBFee);}
    function getTestcFee() public view returns (uint) {return(TestcFee);}
    function getTester() public view returns (address) {return(Creator);}

    function setTesterFee(uint inTesterFee) public {
        if (msg.sender == Creator) {TesterFee = inTesterFee;}
    }

    function setTestAFee(uint inTestAFee) public {
        if (msg.sender == Creator) {TestAFee = inTestAFee;}
    }

    function setTestBFee(uint inTestBFee) public {
        if (msg.sender == Creator) {TestBFee = inTestBFee;}
    }

    function setTestcFee(uint inTestcFee) public {
        if (msg.sender == Creator) {TestcFee = inTestcFee;}
    }

}

How do I get my return values?

Best Answer

I think the issue is that Test08Address holds the transaction hash, and thus so does Test08ContractAddress. You need to get the address from the transaction receipt and use that to make your calls.

Related Topic