2

I'm new to using Nethereum and I'm trying to call a function owner() in a BSC smart contract, which returns the owner's address. My code:

string url = String.Format("https://api.bscscan.com/api?module=contract&action=getabi&address={0}&apikey={1}", address, ApiKey);
var webRequest = WebRequest.Create(url) as HttpWebRequest;
if (webRequest == null)
{
    return false;
}
webRequest.ContentType = "application/json";
webRequest.UserAgent = "Nothing";

using (var s = webRequest.GetResponse().GetResponseStream())
{
   using (var sr = new StreamReader(s))
   {
       string contractABIstr = sr.ReadToEnd();
       JObject contractABI = JObject.Parse(contractABIstr);
       if (contractABI.SelectToken("status").ToString() == "0")
       {
           // Handle error...
       }
       string contractResults = (string)contractABI.SelectToken("result");

       var web3 = new Web3();
       var contract = web3.Eth.GetContract(contractResults, address);
       Nethereum.Contracts.Function function = contract.GetFunction("owner");

       string owner = function.GetData(); // <--- The result which is coming out wrong
    }
}

I am expection owner to be a wallet address. However, regardless of the contract address I use, my code always returns a string "0x8da5cb5b" (clearly too short to be an address), which I notice happens to be the value of a property Sha3Signature in the function.FunctionBuilder.FunctionABI. Does anyone know what I'm doing wrong here?

T. Baer
  • 69
  • 14

1 Answers1

0

I would try to call the function of the contract via CallAsync method:

var owner = await function.CallAsync<string>();

You can also pass parameters if needed. A good overview is given in the Quick introduction to smart contracts integration with Nethereum

Frank Zielen
  • 67
  • 10