47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
|
|
//@ts-ignore
|
|
import { keccak256, _jsonInterfaceMethodToString, AbiInput } from "web3-utils";
|
|
import web3abi from 'web3-eth-abi';
|
|
import { IEventCfg } from "interface/IEventCfg";
|
|
|
|
export const getTopics = (cfg: IEventCfg) => {
|
|
// let abi = cfg.abi
|
|
// let topic = `${abi.name}(`
|
|
// for (let item of abi.inputs) {
|
|
// topic += item.type + ','
|
|
// }
|
|
// topic = topic.slice(0, -1)
|
|
// topic += ')'
|
|
return keccak256(_jsonInterfaceMethodToString(cfg.abi))
|
|
}
|
|
|
|
export const decodeEvent = (cfg: IEventCfg, eventData: {data: string, topics: string[]}) => {
|
|
const abiInputs = cfg.abi.inputs;
|
|
let result = web3abi.decodeLog(abiInputs, eventData.data, eventData.topics.slice(1));
|
|
let decodedData: any = {}
|
|
for (let i = 0; i < abiInputs.length; i++) {
|
|
const input: AbiInput = abiInputs[i];
|
|
if (input.type === 'tuple[]') {
|
|
// @ts-ignore
|
|
decodedData[input.name] = result[i].map(item => {
|
|
let itemData = {}
|
|
for (let j = 0; j < input.components.length; j++) {
|
|
const component = input.components[j];
|
|
itemData[component.name] = item[j]
|
|
}
|
|
return itemData
|
|
})
|
|
} else if (input.type === 'tuple') {
|
|
let itemData = {}
|
|
for (let j = 0; j < input.components.length; j++) {
|
|
const component = input.components[j];
|
|
itemData[component.name] = result[i][j]
|
|
}
|
|
decodedData[input.name] = itemData
|
|
} else {
|
|
decodedData[input.name] = result[i]
|
|
}
|
|
}
|
|
return decodedData
|
|
}
|