Skip to content

Instantly share code, notes, and snippets.

@tcrowe
Last active April 20, 2022 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcrowe/9848b8bd2f083230087ec455723421e0 to your computer and use it in GitHub Desktop.
Save tcrowe/9848b8bd2f083230087ec455723421e0 to your computer and use it in GitHub Desktop.
ethersjs abi encode and decode
let ethers = require('ethers')
let abi = new ethers.utils.AbiCoder()
let types = ['address[][]', 'address[][]', 'address[][]']
let params = [[
[
'0x407d73d8a49eeb85d32cf465507dd71d507100c1',
'0x407d73d8a49eeb85d32cf465507dd71d507100c2',
'0x407d73d8a49eeb85d32cf465507dd71d507100c2'
],
[
'0x407d73d8a49eeb85d32cf465507dd71d507100c3',
'0x407d73d8a49eeb85d32cf465507dd71d507100c4'
]
], [
[
'0x407d73d8a49eeb85d32cf465507dd71d507100c1',
'0x407d73d8a49eeb85d32cf465507dd71d507100c2'
],
[
'0x407d73d8a49eeb85d32cf465507dd71d507100c3',
'0x407d73d8a49eeb85d32cf465507dd71d507100c4'
]
], [
[
'0x407d73d8a49eeb85d32cf465507dd71d507100c1',
'0x407d73d8a49eeb85d32cf465507dd71d507100c2'
],
[
'0x407d73d8a49eeb85d32cf465507dd71d507100c3',
'0x407d73d8a49eeb85d32cf465507dd71d507100c4'
]
]]
let encoded = abi.encode(types, params)
//
// show the output in a column of 64 bytes per row
//
console.log('encoded:')
console.log('-------')
encoded = encoded.replace('0x', '')
let buf = ''
for (let i = 0; i < encoded.length; i += 1) {
if (i > 0 && i % 64 === 0) {
console.log(buf)
buf = ''
}
buf += encoded[i]
}
if (buf.length > 0) {
console.log(buf)
}
// now decode it
console.log()
console.log('decoded:')
console.log('-------')
console.log(abi.decode(types, '0x' + encoded))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment