Resolve $SELF Names to EVM Addresses Using ethers.js
This page provides the integration steps to resolve $SELF names to Ethereum Virtual Machine (EVM) addresses using ethers.js.
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.
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"; const selfNftAbi = [ { "inputs": [{"internalType": "uint256", "name": "tokenId", "type": "uint256"}], "name": "ownerOf", "outputs": [{"internalType": "address", "name": "", "type": "address"}], "stateMutability": "view", "type": "function" } ]; const SELF_NFT_ADDRESS = "0x125Bb13F77f3565d421bD22e92aaFfC795D97a72"; const BSC_RPC_URL = "https://bsc-dataseed1.binance.org/"; const NAME_TO_RESOLVE = "ricomaverick"; 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); const resolvedName = await contract.ownerOf(hashedName); console.log("Resolved Name:", resolvedName); } 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:
- Updated the
NAME_TO_RESOLVE
constant with the name you want to look up - Installed all dependencies (
npm install
)
Output
-
The script outputs the resolved EVM address for the provided name:
Resolved Name: 0x383D8ee00c3Ea31dd4eb6e5eCf649Bc9C08D8e31