Reverse Resolution 예제 코드

Constants

const JSON_RPC_PROVIDER = "" // JSON-RPC provider url
const REVERSE_RECORDS_ADDRESS = "0x87f4483E4157a6592dd1d1546f145B5EE22c790a";
const REVERSE_RECORDS_ABI = [
  {
    type: "function",
    name: "getName",
    stateMutability: "view",
    inputs: [
      {
        internalType: "address",
        name: "addr",
        type: "address",
      },
    ],
    outputs: [
      {
        internalType: "string",
        name: "",
        type: "string",
      },
    ],
  },
];

ethers.js

import ethers from "ethers";

async function domainOrAddressEthers(address) {
  const provider = new ethers.providers.JsonRpcProvider(JSON_RPC_PROVIDER);
  const reverseRecords = new ethers.Contract(REVERSE_RECORDS_ADDRESS, REVERSE_RECORDS_ABI, provider);
  const domain = await reverseRecords.getName(address);
  if (domain === "") {
    return address;
  } else {
    return domain;
  }
}

web3.js

import Web3 from "web3";

async function domainOrAddressWeb3(address) {
  const web3 = new Web3(JSON_RPC_PROVIDER);
  const reverseRecords = new web3.eth.Contract(REVERSE_RECORDS_ABI, REVERSE_RECORDS_ADDRESS);
  const domain = await reverseRecords.methods.getName(address).call();
  if (domain === "") {
    return address;
  } else {
    return domain;
  }
}

caver-js

import Caver from "caver-js";

async function domainOrAddressCaver(address) {
  const caver = new Caver(JSON_RPC_PROVIDER);
  const reverseRecords = new caver.klay.Contract(REVERSE_RECORDS_ABI, REVERSE_RECORDS_ADDRESS);
  const domain = await reverseRecords.methods.getName(address).call();
  if (domain === "") {
    return address;
  } else {
    return domain;
  }
}

테스트용 주소 및 도메인

Reverse resolution 성공 시에는 아래의 주소를 받았을 때 올바르게 도메인을 반환해야 합니다.

  • 주소: 0x0000ac03932ff48ee30209774e3f10fb0ac522e9

  • 도메인: kns.klay

Last updated