Ethers/6/Utilities

/icons/calendar.svg

Last update

Aug 25, 2024

This codemod does tranformation based on changes in the utils in ethers.js

Example

Byte32 string helpers

Before

bytes32 = ethers.utils.formatBytes32String(text);
text = ethers.utils.parseBytes32String(bytes32);

After

bytes32 = ethers.encodeBytes32String(text);
text = ethers.decodeBytes32String(bytes32);

Constants

Before

ethers.constants.AddressZero;
ethers.constants.HashZero;

After

ethers.ZeroAddress;
ethers.ZeroHash;

Data Manipulation

Before

slice = ethers.utils.hexDataSlice(value, start, end);
padded = ethers.utils.hexZeroPad(value, length);
hex = hexlify(35);

After

slice = ethers.dataSlice(value, start, end);
padded = ethers.zeroPadValue(value, length);
// v6; converting numbers to hexstrings
hex = toBeHex(35);

Deafult AbiCoder

Before

// In v5, it is a property of AbiCoder
coder = AbiCoder.defaultAbiCoder;

After

// In v6, it is a static function on AbiCoder
coder = AbiCoder.defaultAbiCoder();

Hex Conversion

Before

hex = ethers.utils.hexValue(value);
array = ethers.utils.arrayify(value);

After

hex = ethers.toQuantity(value);
array = ethers.getBytes(value);

Solidity non-standard packed

Before

ethers.utils.solidityPack(types, values);
ethers.utils.solidityKeccak256(types, values);
ethers.utils.soliditySha256(types, values);

After

ethers.solidityPacked(types, values);
ethers.solidityPackedKeccak256(types, values);
ethers.solidityPackedSha256(types, values);

Property Manipulation

Before

ethers.utils.defineReadOnly(obj, 'name', value);

After

ethers.defineProperties(obj, { name: value });

Above were some transformation, that were done in utilities section of ethers, now below are some cleanups and breaking changes. PS: These changes are not done using codemod.

Fetching content

Basic Fetch with Json

Before

data = await ethers.utils.fetchJson(url, json, processFunc);

After

const req = new ethers.FetchRequest(url);
req.body = json;
req.processFunc = processFunc;
const resp = await req.send();
data = resp.bodyJson; // or resp.bodyText / resp.body depending on the desired format

Fetch with Connection Overrides:

Before

req = {
url: url,
user: "username",
password: "password"
// etc. properties have FetchRequest equivalents
};
data = await ethers.utils.fetchJson(req, json, processFunc);

After

const req = new ethers.FetchRequest(url);
req.setCredentials("username", "password");
req.body = json;
req.processFunc = processFunc;
const resp = await req.send();
data = resp.bodyJson; // or resp.bodyText / resp.body depending on the desired format

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now