@hinkal/common 0.1.42 → 0.1.43
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("../../constants/reorg-depths.constants.cjs"),o=require("../../functions/utils/resolve-sync.utils.cjs");require("ethers");require("axios");require("../../constants/chains.constants.cjs");require("../../constants/vite.constants.cjs");require("../../types/circom-data.types.cjs");require("../../types/transactions.types.cjs");require("../../types/curve.types.cjs");require("circomlibjs");const u=require("../../error-handling/logError.cjs");class g{contract;eventConfig;_latestBlockNumber;maxPageSize;isReady=!1;_onNewEvent;isServer;intervalId;inProgress;constructor(e,r,t,s,n){this.contract=e,this.eventConfig=r,this._latestBlockNumber=t,this.maxPageSize=n,this.isServer=s,this.inProgress=!1}get latestBlockNumber(){return this._latestBlockNumber}set onNewEvent(e){this._onNewEvent=e}intervalClear(){this.isReady=!1,this._onNewEvent=void 0,clearInterval(this.intervalId)}async init(){if(this.isReady)throw new Error("Already initialized");this.isReady=!0,await this.retrieveEvents(this._latestBlockNumber+1),this.intervalId=setInterval(async()=>{await this.retrieveEvents(this.latestBlockNumber)},3500)}requireReady(){if(!this.isReady)throw new Error("Not ready")}emitNewEvent(e){this._onNewEvent?.(e)}getLastBlockNumberForEventRequest=async()=>{const e=await this.contract.provider.getBlockNumber();if(!this.isServer)return e;const{chainId:r}=await this.contract.provider.getNetwork();return Math.max(this.latestBlockNumber,e-v.blockReorgDepth[r]+1)};async retrieveEvents(e,r=!1){try{if(this.requireReady(),this.inProgress&&!r)return!1;this.inProgress=!0;const t=await this.getLastBlockNumberForEventRequest();if(t<e)return this.inProgress=!1,!1;const s=o.getSequence(e,t,this.maxPageSize);return await o.resolveSync(s.map(({from:n,to:i})=>async()=>{const c=await this.contract.queryFilter(this.contract.filters[this.eventConfig.name](),n,i);let a=[];c.length>0&&(a=await this.processEventsPage(c),console.log("new events catched",{mappedEvents:a})),this._latestBlockNumber=i,await this.afterEventsAccepted(),a.length>0&&this.emitNewEvent()})),this.inProgress=!1,!0}catch(t){return u.logError("retrieveEvents error:",{err:t}),this.inProgress=!1,!1}}async processEventsPage(e){const r=[];return await o.resolveSync(e.map(t=>async()=>{const{args:s,blockNumber:n}=t;if(
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("../../constants/reorg-depths.constants.cjs"),o=require("../../functions/utils/resolve-sync.utils.cjs");require("ethers");require("axios");require("../../constants/chains.constants.cjs");require("../../constants/vite.constants.cjs");require("../../types/circom-data.types.cjs");require("../../types/transactions.types.cjs");require("../../types/curve.types.cjs");require("circomlibjs");const u=require("../../error-handling/logError.cjs");class g{contract;eventConfig;_latestBlockNumber;maxPageSize;isReady=!1;_onNewEvent;isServer;intervalId;inProgress;constructor(e,r,t,s,n){this.contract=e,this.eventConfig=r,this._latestBlockNumber=t,this.maxPageSize=n,this.isServer=s,this.inProgress=!1}get latestBlockNumber(){return this._latestBlockNumber}set onNewEvent(e){this._onNewEvent=e}intervalClear(){this.isReady=!1,this._onNewEvent=void 0,clearInterval(this.intervalId)}async init(){if(this.isReady)throw new Error("Already initialized");this.isReady=!0,await this.retrieveEvents(this._latestBlockNumber+1),this.intervalId=setInterval(async()=>{await this.retrieveEvents(this.latestBlockNumber)},3500)}requireReady(){if(!this.isReady)throw new Error("Not ready")}emitNewEvent(e){this._onNewEvent?.(e)}getLastBlockNumberForEventRequest=async()=>{const e=await this.contract.provider.getBlockNumber();if(!this.isServer)return e;const{chainId:r}=await this.contract.provider.getNetwork();return Math.max(this.latestBlockNumber,e-v.blockReorgDepth[r]+1)};async retrieveEvents(e,r=!1){try{if(this.requireReady(),this.inProgress&&!r)return!1;this.inProgress=!0;const t=await this.getLastBlockNumberForEventRequest();if(t<e)return this.inProgress=!1,!1;const s=o.getSequence(e,t,this.maxPageSize);return await o.resolveSync(s.map(({from:n,to:i})=>async()=>{const c=await this.contract.queryFilter(this.contract.filters[this.eventConfig.name](),n,i);let a=[];c.length>0&&(a=await this.processEventsPage(c),console.log("new events catched",{mappedEvents:a,events:c})),this._latestBlockNumber=i,await this.afterEventsAccepted(),a.length>0&&this.emitNewEvent()})),this.inProgress=!1,!0}catch(t){return u.logError("retrieveEvents error:",{err:t}),this.inProgress=!1,!1}}async processEventsPage(e){const r=[];return await o.resolveSync(e.map(t=>async()=>{const{args:s,blockNumber:n}=t;if(console.log("args",{args:s}),!s)throw new Error("Wrong event structure");const i=this.mapEvent(s);await this.acceptEvent(i,n)&&r.push(i)})),r}handleEvent=async(...e)=>{const{args:r}=this.eventConfig,{blockNumber:t}=e[e.length-1],s=e.slice(0,r.length),n=r.reduce((a,l,h)=>(a[l]=s[h],a),{});if(!t)throw new Error("Wrong event structure");const i=this.mapEvent(n);if(!await this.acceptEvent(i,t))throw new Error("Failed to retrieve events");this._latestBlockNumber=t,await this.afterEventsAccepted(),this.emitNewEvent(i)}}exports.AbstractEventService=g;
|
|
@@ -70,7 +70,7 @@ class R {
|
|
|
70
70
|
i
|
|
71
71
|
);
|
|
72
72
|
let a = [];
|
|
73
|
-
o.length > 0 && (a = await this.processEventsPage(o), console.log("new events catched", { mappedEvents: a })), this._latestBlockNumber = i, await this.afterEventsAccepted(), a.length > 0 && this.emitNewEvent();
|
|
73
|
+
o.length > 0 && (a = await this.processEventsPage(o), console.log("new events catched", { mappedEvents: a, events: o })), this._latestBlockNumber = i, await this.afterEventsAccepted(), a.length > 0 && this.emitNewEvent();
|
|
74
74
|
})
|
|
75
75
|
), this.inProgress = !1, !0;
|
|
76
76
|
} catch (t) {
|
|
@@ -82,7 +82,7 @@ class R {
|
|
|
82
82
|
return await c(
|
|
83
83
|
e.map((t) => async () => {
|
|
84
84
|
const { args: r, blockNumber: n } = t;
|
|
85
|
-
if (!r)
|
|
85
|
+
if (console.log("args", { args: r }), !r)
|
|
86
86
|
throw new Error("Wrong event structure");
|
|
87
87
|
const i = this.mapEvent(r);
|
|
88
88
|
await this.acceptEvent(i, n) && s.push(i);
|
|
@@ -90,7 +90,7 @@ class R {
|
|
|
90
90
|
), s;
|
|
91
91
|
}
|
|
92
92
|
handleEvent = async (...e) => {
|
|
93
|
-
const { args: s } = this.eventConfig, { blockNumber: t } = e[e.length - 1], r = e.slice(0, s.length), n = s.reduce((a,
|
|
93
|
+
const { args: s } = this.eventConfig, { blockNumber: t } = e[e.length - 1], r = e.slice(0, s.length), n = s.reduce((a, l, h) => (a[l] = r[h], a), {});
|
|
94
94
|
if (!t)
|
|
95
95
|
throw new Error("Wrong event structure");
|
|
96
96
|
const i = this.mapEvent(n);
|