Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatBytes32String not working in ethers v6

const ethers =  require('ethers');

async function createBytes (args) {
   const name = args[0];
   const bytes = ethers.utils.formatBytes32String(name);
   console.log('name: ',bytes)
}

createBytes(process.argv.slice(2));

enter image description here

What's wrong in it and i can't understand this error. I am trying to run js file through a powershell

like image 645
hp0514 Avatar asked Jun 14 '26 01:06

hp0514


1 Answers

You are using the old v5 syntax.

formatBytes32String has been renamed to encodeBytes32String in v6:

// v5:
bytes32 = ethers.utils.formatBytes32String(text)
text = ethers.utils.parseBytes32String(bytes32)

// v6:
bytes32 = ethers.encodeBytes32String(text)
text = ethers.decodeBytes32String(bytes32)

Change your code to the following:

const ethers =  require('ethers');

 async function createBytes (args) {
    const name = args[0];
    const bytes = ethers.encodeBytes32String(name);
    console.log('name: ',bytes)
}

createBytes(process.argv.slice(2));
name:  0x736f6d657468696e670000000000000000000000000000000000000000000000

For more info, check the 'Utilities' documentation page

like image 195
0stone0 Avatar answered Jun 15 '26 13:06

0stone0