Getting started
This guide will explain how to resolve a SELF NFT + metadata from a SELF username.
Connect the public rpc provider "https://bsc-dataseed1.binance.org/" using Ethers NPM module v.6.6.7. and install this package;
install ethers
npm i ethers
Accesing Metadata
Now we are ready to create the script to get the address of the desired chain.
getMetadata.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: "tokenURI",
outputs: [{ internalType: "string", name: "", type: "string" }],
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);
}
//helper
export function createGatewayLink(ipfsLink) {
const ipfsPrefix = "ipfs://";
const gatewayPrefix = "https://nftstorage.link/ipfs/";
if (ipfsLink.startsWith(ipfsPrefix)) {
const cid = ipfsLink.substring(ipfsPrefix.length);
const gatewayLink = gatewayPrefix + cid;
return gatewayLink;
}
// If the input doesn't start with "ipfs://", return null or throw an error as desired
return undefined;
}
async function rn(_n) {
//Get token uri
const tokenURI = await rc(c, a, "tokenURI", [keccak256(toUtf8Bytes(_n))]);
//Create gatewaylink
const gatewayLink = createGatewayLink(tokenURI);
//fetch metadata
const metadata = await (await fetch(gatewayLink)).json();
//log address
console.log(metadata.foreignAddresses[_c].address);
}
let [name, chain] = process.argv.slice(2);
rn(name, chain);
To run the script input this command:
terminal call
node getMetadata.mjs ricomaverick btc
And the response is this JSON:
Response
bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh
You might encounter an error if the name hasn't been registered yet or if the address for the chain hasn't been added.