[英]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.