@fractional-company/common
Version:
Tessera constants
67 lines (66 loc) • 2.25 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalEventBuilder = void 0;
const InternalEvent_1 = require("./InternalEvent");
const NULL_ADDRESS = "0x0000000000000000000000000000000000000000";
class InternalEventBuilder {
constructor(eventName, log) {
if (!eventName) {
throw new Error("Event Name Missing");
}
this.eventName = eventName;
if (log) {
this.transactionHash = log.transactionHash.toLowerCase();
this.blockNumber = log.blockNumber;
}
this.fromAddress = NULL_ADDRESS;
this.date = new Date();
}
setDate(date) {
this.date = date;
return this;
}
setTransactionHash(hash) {
this.transactionHash = hash.toLowerCase();
return this;
}
setBlockNumber(blockNumber) {
this.blockNumber = blockNumber;
return this;
}
setFromAddress(fromAddress) {
this.fromAddress = fromAddress;
return this;
}
validate(params, validateContext = false) {
// validate Params
if (validateContext) {
params = params.concat(["date", "transactionHash", "fromAddress", "blockNumber"]);
}
for (let i = 0; i < params.length; i++) {
const key = params[i];
// @ts-ignore
const value = this.hasOwnProperty(key) && !(this[key] === undefined || this[key] === null);
if (!value) {
throw new Error(`[${this.eventName}] Param ${key} does not exists`);
}
}
return true;
}
resolveInternalEvent(body) {
body = body || this.getEventBody(this.eventName);
return new InternalEvent_1.InternalEvent(this.eventName, body);
}
getContext() {
if (!this.fromAddress || !this.date) {
throw new Error("Cannot build context - missing data");
}
return {
fromAddress: this.fromAddress,
date: this.date,
transactionHash: this.transactionHash,
blockNumber: this.blockNumber,
};
}
}
exports.InternalEventBuilder = InternalEventBuilder;