Ethers.js – How to Use Ethers ENS setABI

abiensethers.js

I'm having trouble getting my ABI data registered with my ENS record. I am unable to find any examples which include ethers.

I have tried using the artifact's abi object, JSON.stringify(abi), various formats of the ethers.utils.Interface object. They all fail arrayifying.

code

const hre = require('hardhat');
const ethers = require('ethers');
const namehash = require('eth-ens-namehash');
const [owner] = await hre.getSigners();
const resolver = hre.ethers.getContractAt("PublicResolver", "0x...", owner);
const artifact = hre.artifacts.readArtifact("Contract");
const node = namehash.hash('name.sub.foo.eth');
resolver['setABI(bytes32,uint256,bytes)'](node, 0x1, artifact.abi);

error

$ npx hardhat run scripts/abi.js
Uncaught:
Error: invalid arrayify value (argument="value", value=[...], code=INVALID_ARGUMENT, version=bytes/5.5.0)
    at BytesCoder.DynamicBytesCoder.encode (.../node_modules/@ethersproject/abi/src.ts/coders/bytes.ts:17:25)
    at arrayify (.../node_modules/@ethersproject/bytes/src.ts/index.ts:138:19)
    at Logger.throwArgumentError (.../node_modules/@ethersproject/logger/src.ts/index.ts:241:21)
    at Logger.throwError (.../node_modules/@ethersproject/logger/src.ts/index.ts:237:20)
    at Logger.makeError (.../node_modules/@ethersproject/logger/src.ts/index.ts:225:28) {
  reason: 'invalid arrayify value',
  code: 'INVALID_ARGUMENT',
  argument: 'value',
  value: [...]

Best Answer

Turns out I was missing a few things

  1. Bytes must be converted from strings first
  2. Interfaces are required, when exporting ABIs
const hre = require('hardhat');
const ethers = require('ethers');
const namehash = require('eth-ens-namehash');
const [owner] = await hre.getSigners();
const resolver = hre.ethers.getContractAt("PublicResolver", "0x...", owner);
artifact = hre.artifacts.readArtifact("Contract");
iface = new ethers.utils.Interface(artifact.abi);
bytes =  ethers.utils.toUTF8Bytes(iface.format(ethers.utils.FormatTypes.json);
node = namehash.hash('name.sub.foo.eth');
resolver['setABI(bytes32,uint256,bytes)'](node, 0x1, bytes);

Related Topic