[Ethereum] How to call a contract function using Nethereum

c#contract-invocationnethereum

I would like to call a contract function using Nethereum C#.

Here is the contract in Etherscan (https://etherscan.io/token/0x8c9b261faef3b3c2e64ab5e58e04615f8c788099#readContract)

I want to call the function 24 "getCollectibleDetails".

Here is the code that I did so far, but it isn't working.

var web3 = new Web3("https://mainnet.infura.io");
var contract = web3.Eth.GetContract(abi, contractAddress);
var function = contract.GetFunction("getCollectibleDetails");
var parameters = new object[]
{
    new
    {
        Data = 157517,
        To = "0x8c9b261faef3b3c2e64ab5e58e04615f8c788099"
    }
};

var result = function.GetData(parameters);

Anyone has faced this?

Thanks in advance for any help.

Best Answer

These three lines are correct

var web3 = new Web3("https://mainnet.infura.io");
var contract = web3.Eth.GetContract(abi, contractAddress);
var function = contract.GetFunction("getCollectibleDetails");

I understand what you were trying to do here but there is a better way to get this:

var parameters = new object[]
{
    new
    {
        Data = 157517,
        To = "0x8c9b261faef3b3c2e64ab5e58e04615f8c788099"
    }
};

Lets assume getCollectibleDetails function accepts one parameter "address" and returns a string as response:

// Define the address
string address = "0xb621...";

// Create an array of objects for the param
object[] params = new object[1]{ address };

// Call the function and pass in the params 
var result = function.CallAsync<string>(params);
Related Topic