简体   繁体   English

使用 Nethereum 调用 BSC 合约 function 不断产生相同(不正确)的结果

[英]Calling BSC contract function with Nethereum keeps producing the same (incorrect) result

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.我是 Nethereum 的新手,我正在尝试在 BSC 智能合约中调用 function owner() ,它会返回所有者的地址。 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.我期望owner是一个钱包地址。 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 .但是,无论我使用的合约地址如何,我的代码总是返回一个字符串"0x8da5cb5b" (显然太短而不能作为地址),我注意到它恰好是 function.FunctionBuilder.FunctionABI 中的属性Sha3Signaturefunction.FunctionBuilder.FunctionABI Does anyone know what I'm doing wrong here?有谁知道我在这里做错了什么?

I would try to call the function of the contract via CallAsync method:我会尝试通过CallAsync方法调用合同的 function:

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 Quick introduction to smart contracts integration with Nethereum中给出了一个很好的概述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM