Created
February 10, 2022 18:33
-
-
Save Pzixel/9d94c106c0e88cfe744f8ffa0c0f58b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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