deth
Version:
Ethereum node focused on Developer Experience
41 lines (40 loc) • 1.52 kB
JavaScript
import { ethers } from 'ethers';
// silence ethers warnings: https://github.com/ethers-io/ethers.js/issues/407
ethers.errors.setLogLevel('error');
import debug from 'debug';
const d = debug('deth:AbiDecoder');
export class AbiDecoder {
constructor(fs) {
this.fs = fs;
// note: this should be modified only by addAbi method
this.ABIs = new Set();
this.iface = new ethers.utils.Interface([...this.ABIs.values()]);
}
loadAbi(path) {
const rawAbi = this.fs.readFile(path);
this.addAbi(JSON.parse(rawAbi));
this.iface = new ethers.utils.Interface([...this.ABIs.values()]);
}
loadAbis(glob, basePath) {
const abiPaths = this.fs.findFiles(glob, basePath);
d(`Loading (${abiPaths.length}) matched by ${glob} from ${basePath}`);
const rawAbis = abiPaths.map(p => this.fs.readFile(p));
for (const rawAbi of rawAbis) {
this.addAbi(JSON.parse(rawAbi));
}
this.iface = new ethers.utils.Interface([...this.ABIs.values()]);
}
addAbi(abi) {
for (const a of abi) {
this.ABIs.add(a);
}
}
decodeLog(log) {
var _a;
return _a = this.iface.parseLog(log), (_a !== null && _a !== void 0 ? _a : undefined); // note: ethers returns null by default
}
decodeCalldata(data) {
var _a;
return _a = this.iface.parseTransaction({ data }), (_a !== null && _a !== void 0 ? _a : undefined); // note: ethers returns null by default
}
}