简体   繁体   English

Nethereum 调用币安智能链合约 function like balanceOf 或通过 Blazor WebAssembly 转账

[英]Nethereum Call Binance Smart Chain contract function like balanceOf or transfer via Blazor WebAssembly

I'm developing a web application with Blazor WebAssembly.我正在使用 Blazor WebAssembly 开发一个 web 应用程序。 I want to call some functions in my token's contract.我想在我的代币合约中调用一些函数。 I've installed Nethereum.Web nuget package to my project.我已经将 Nethereum.Web nuget package 安装到我的项目中。 But I don't want to call a contract from Ethereum Mai.net, it has to be Binance Smart Chain.但是我不想从以太坊 Mai.net 调用合约,它必须是 Binance Smart Chain。 Can anyone help me?谁能帮我?

You can use Nethereum.Web3 Nuget package for both, Ethereum and Binance Smart Chain (BSC).您可以将 Nethereum.Web3 Nuget package 用于以太坊和币安智能链 (BSC)。

Enclosed you find a C# example connecting to BSC via GetBlock .随函附上一个 C# 通过GetBlock连接到 BSC 的示例。 You can easily setup a free node with GetBlock and obtain your API key with associated endpoint within a few clicks.您可以使用 GetBlock 轻松设置一个免费节点,只需点击几下即可获得带有关联端点的 API 密钥。

In the code snippet the Chromia token contract is used and we request the balance of a sample account.在代码片段中,使用了 Chromia 代币合约,我们请求示例账户的余额。 You just need to replace code with your contract of choice.您只需要用您选择的合约替换代码。

using System;
using System.Threading.Tasks;
using System.Numerics;
using Nethereum.Web3;

namespace BSC
{
    class Example
    {
        static async Task Main(string[] args)
        {
            // Connect to BSC node
            // Replace XX-XXX-XX with your personal API key (when also using GetBlock)
            var web3 = new Web3("https://bsc.getblock.io/mainnet/?api_key=XX-XXX-XX");

            // Get contract by providing ABI and address of contract
            // Here Chromia token with ABI for balanceOf function only is used for example
            string abi = @"[{""inputs"":[{""internalType"":""address"",""name"":""account"",""type"":""address""}],""name"":""balanceOf"",""outputs"":[{""internalType"":""uint256"",""name"":"""",""type"":""uint256""}],""stateMutability"":""view"",""type"":""function""}]";
            string contractaddress = "0xf9CeC8d50f6c8ad3Fb6dcCEC577e05aA32B224FE";
            var contract = web3.Eth.GetContract(abi, contractaddress);

            // Call function of contract
            // Here balance of a random address is requested for example
            var function = contract.GetFunction("balanceOf");
            string address = "0x8A2279d4A90B6fe1C4B30fa660cC9f926797bAA2";
            BigInteger balance = await function.CallAsync<BigInteger>(address);

            Console.WriteLine("Balance: " + balance);
        }
    }
}

Yes.是的。 You just need to connect to a Binance Smart Chain node instead of Ethereum node.您只需要连接到 Binance Smart Chain 节点而不是 Ethereum 节点。 All the other code is just the same.所有其他代码都是一样的。

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

相关问题 如何使用nethereum运行此智能合约功能以返回结果? - How do I run this smart contract function using nethereum to return a result? 使用 Nethereum 调用 BSC 合约 function 不断产生相同(不正确)的结果 - Calling BSC contract function with Nethereum keeps producing the same (incorrect) result 将交易发送到已部署的智能合约需要很长时间。 (Nethereum + Unity) - It takes too long to send transaction to the smart contract which is already deployed. (Nethereum + Unity) Blazor Webassembly API Call Children 无法评估 - Blazor Webassembly API Call Children could not be evaluated 使用 Nethereum 订阅合约事件 - Subscribe to contract events using Nethereum 如何加载 ResponseCompressionDefaults function(Blazor WebAssembly SignalR) - how to load ResponseCompressionDefaults function (Blazor WebAssembly SignalR) Blazor WebAssembly 可以直接调用另一个 WASM 模块吗? - Can Blazor WebAssembly call another WASM module directly? 通过 WebAssembly 在 JavaScript 中调用 C# 方法 - Call C# Method in JavaScript via WebAssembly Nethereum C#自动以太坊转移 - Nethereum C# automated ether transfer Blazor WebAssembly - trackBy 等效项 - Blazor WebAssembly - trackBy equivalent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM