Resolve $SELF Names to Cross-Chain Addresses Using ethers.js
This page provides the integration steps to resolve $SELF names to cross-chain addresses using ethers.js. For a complete list of supported chains and their integration keys, see the Supported Chains page.
Prerequisites
Ensure you have Node.js installed in your environment.
For the latest contract addresses and ABIs, please refer to our Contract Addresses and Contract ABIs pages.
The cross-chain resolution uses SELF metadata to store and retrieve addresses for different networks. For detailed information about the metadata format, see the Metadata Structure documentation.
Integration Steps
Project Initialization
- Initialize a new Node.js project in your desired directory:
npm init -y
- Install ethers.js:
npm i ethers@5.7.2
- Update
package.json
to enable ES6 import/export:"type": "module",
- Create a file named
index.js
. Ensure your project structure is as follows:- node_modules/ - package.json - package-lock.json - index.js
Complete Code Example
-
Here's the complete script that you can copy and use:
import { ethers } from "ethers"; import { keccak256, toUtf8Bytes } from "ethers/lib/utils.js"; // You can find the complete ABI in our Contract ABIs page: /contract-abis const selfNftAbi = [ { inputs: [ { internalType: "uint256", name: "tokenId", type: "uint256", }, ], name: "tokenURI", outputs: [ { internalType: "string", name: "", type: "string", }, ], stateMutability: "view", type: "function", }, ]; const SELF_NFT_ADDRESS = "0x125Bb13F77f3565d421bD22e92aaFfC795D97a72"; const BSC_RPC_URL = "https://bsc-dataseed1.binance.org/"; const NAME_TO_RESOLVE = "ricomaverick"; const CHAIN_TO_RESOLVE = "eth"; // Change this to the chain you want to resolve const provider = new ethers.providers.JsonRpcProvider(BSC_RPC_URL); async function main() { const hashedName = BigInt(keccak256(toUtf8Bytes(NAME_TO_RESOLVE))).toString(); const contract = new ethers.Contract(SELF_NFT_ADDRESS, selfNftAbi, provider); // Get token URI const tokenUri = await contract.tokenURI(hashedName); console.log("Token URI:", tokenUri); // Convert IPFS URI to HTTP URL using a public gateway const ipfsGateway = "https://ipfs.io/ipfs/"; const httpUrl = tokenUri.replace("ipfs://", ipfsGateway); // Fetch metadata const response = await fetch(httpUrl); const metadata = await response.json(); console.log("Metadata retrieved successfully"); // Get cross-chain address const chainAddress = metadata.foreignAddresses?.[CHAIN_TO_RESOLVE]?.address; if (chainAddress) { console.log( `Resolved ${NAME_TO_RESOLVE} to ${CHAIN_TO_RESOLVE} address: ${chainAddress}` ); } else { console.log(`No ${CHAIN_TO_RESOLVE} address found for ${NAME_TO_RESOLVE}`); } } main().catch((error) => { console.error(error); process.exitCode = 1; });
Running the Script
-
Execute the script using Node.js:
node index.js
Make sure you have:
- Installed all dependencies (
npm install
) - Updated the
NAME_TO_RESOLVE
constant with the name you want to look up - Set the
CHAIN_TO_RESOLVE
constant to your desired chain
Output
-
The script outputs the resolved address for the specified chain:
Token URI: ipfs://bafkreibxwvukmiliv7ckx24s2zrn3aevbxvhqjfm24rti23oo2ne46aj7u Metadata retrieved successfully Resolved ricomaverick to solana address: 5FHwkrdxkN8yPxmVZc1R6BkVoXHtGwsXHvi6Xo6JuXrX