deth
Version:
Ethereum node focused on Developer Experience
47 lines (46 loc) • 1.84 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ethers_1 = require("ethers");
// silence ethers warnings: https://github.com/ethers-io/ethers.js/issues/407
ethers_1.ethers.errors.setLogLevel('error');
const debug_1 = __importDefault(require("debug"));
const d = debug_1.default('deth:AbiDecoder');
class AbiDecoder {
constructor(fs) {
this.fs = fs;
// note: this should be modified only by addAbi method
this.ABIs = new Set();
this.iface = new ethers_1.ethers.utils.Interface([...this.ABIs.values()]);
}
loadAbi(path) {
const rawAbi = this.fs.readFile(path);
this.addAbi(JSON.parse(rawAbi));
this.iface = new ethers_1.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_1.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
}
}
exports.AbiDecoder = AbiDecoder;