Skip to content

Instantly share code, notes, and snippets.

@Pzixel
Created February 10, 2022 18:33
Show Gist options
  • Save Pzixel/9d94c106c0e88cfe744f8ffa0c0f58b4 to your computer and use it in GitHub Desktop.
Save Pzixel/9d94c106c0e88cfe744f8ffa0c0f58b4 to your computer and use it in GitHub Desktop.
export const decodeArg = (web3: Web3, hexString: string, solidityType: string, offsetInBytes: number, lengthInBytes: number): any => {
if (lengthInBytes > 32) {
throw new Error('Cannot decode types >32 bytes length');
}
const zxoffset = hexString.startsWith('0x') ? 2 : 0;
const zeroedLength = 32 - lengthInBytes;
return web3.eth.abi.decodeParameter(solidityType, '00'.repeat(zeroedLength) + hexString.slice(zxoffset + offsetInBytes * 2, zxoffset + (offsetInBytes + lengthInBytes) * 2));
};
export const decodePackedArgs = (web3: Web3, hexString: string, typeMetadatas: [string, number][]) => {
const result = [];
let offset = 0;
for (let [name, size] of typeMetadatas) {
result.push(decodeArg(web3, hexString, name, offset, size));
offset += size;
}
return result;
}
const [price, tick, ...] = decodePackedArgs(web3, value,
[['uint160', 20], ['int24', 3], ...]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment