Endpoints
In this guide, we will look at how to work with endpoints when querying the SELF API. A decentralized application (dApp) consistently requires data from the blockchain to complete user requests such as sending transactions, retrieving block data, or evaluating the state of the blockchain. Nodes enable this function through Remote Procedure Calls (RPC) which connect dApps to the blockchain. RPC nodes enable web3 applications to interact with the blockchain and access web3 state variables, stored in Smart Contracts.
When a dApp (the client) needs information from a blockchain or wants to relay a new transaction, it uses an RPC to send a request to the RPC endpoint. The RPC node (the server) with the endpoint then receives that request and returns the desired information. For the sake of understanding, we distinguish two types of endpoints public and private, however there are more. You can even run your own node.
Public RPC endpoint
Public RPC Endpoints are shared, rate-limited resources which run on RPC nodes available for any client to make requests to. Blockchains offer public RPC endpoints because any connected node should be able to send and receive data from the blockchain, or in web3 language; should make a transaction and change state.
Public endpoints are free and ready to use at any time but are often rate-limited. Further, public RPC endpoints have no customer support, lack active developer infrastructure, and do not scale to the demands of running dApps.
This is a selection of Public Endpoint for Binance Smart Chain (Mainnet BSC ChainID: 56 )
- https://bsc-dataseed1.binance.org/
- https://bsc-dataseed2.binance.org/
- https://bsc-dataseed1.defibit.io/
- https://bsc-dataseed2.defibit.io/
- https://bsc-dataseed1.ninicoin.io/
- https://bsc-dataseed2.ninicoin.io/
Private RPC endpoint
Private RPC endpoints benefit from a fast and consistent RPC service. Private RPC endpoints often require explicit service-level agreements (SLAs), guaranteeing performant services.
This is a selection of popular blockchain node providers that offer an effective platform for web3 development;
- https://moralis.io
- https://www.quicknode.com/
- https://www.infura.io
- https://www.alchemy.com/
- https://getblock.io
Example connection
In this example, we connect the public rpc provider "https://bsc-dataseed1.binance.org/" using Ethers NPM module v.6.6.7. Install this package before getting started;
install ethers
npm i ethers
The ethers.js library aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem. Ethers is used to create dApps, other tools and simple scripts that require reading and writing to the blockchain. For more info: https://docs.ethers.org/v6/
With Ethers installed we can create this sample javascript script.
Please note we have used .mjs as extension in order to not have to use a deprecated version.
Since Node.js was created, the ECMAScript module system (which uses import and export) has become standard and Node.js has added support for it.
Node.js will treat .cjs files as CommonJS modules and .mjs files as ECMAScript modules.
Basic .js files will be handled in the default mode for the project, (like as argumented in package.json)
getwallet.mjs
// requirements
import { ethers, keccak256, toUtf8Bytes } from "ethers";
// endpoint abi contract
const e = "https://bsc-dataseed1.binance.org/";
const a = [{"inputs": [{ "internalType": "uint256", "name": "tokenId", "type": "uint256" }],"name": "ownerOf","outputs": [{ "internalType": "address", "name": "", "type": "address" }],"stateMutability": "view","type": "function"}];
const c = "0x125Bb13F77f3565d421bD22e92aaFfC795D97a72";
// getProvider
function gp() {return new ethers.JsonRpcProvider(e);}
// readContract
async function rc( _c, _a, _f, _p=[]) {
const p = gp();
const c = new ethers.Contract(_c, _a, p);
return await c[_f](..._p);
}
// resolveName
async function rn(_n) {console.log( await rc(c,abi,"ownerOf",[keccak256(toUtf8Bytes(_n))]));}
rn(process.argv.slice(2)[0]);
This basic call will call the 'ownerOf' routine of the smart contract as deployed on BSC (Binance Smart Chain).
terminal call
node getwallet.mjs superman
For detailed information have a look at the 'read contract section' of the contract at https://bscscan.com/address/0x125Bb13F77f3565d421bD22e92aaFfC795D97a72#readContract
And the response from the public endpoint should be as follows;
response
0x1837FEba56517a94ecF905E6d4AB33902Df11B30
Now you can experiment with the above sample script. By looking at the readme section of the smart contract, you are now able to change the function call.
Change the ABI as well as your function call may not be recognized!