@hinkal/common 0.1.47 → 0.1.48
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.
- package/data-structures/Hinkal/resetMerkleTrees.cjs +1 -1
- package/data-structures/Hinkal/resetMerkleTrees.mjs +8 -8
- package/data-structures/event-service/AbstractEventService.cjs +1 -1
- package/data-structures/event-service/AbstractEventService.d.ts +1 -0
- package/data-structures/event-service/AbstractEventService.mjs +15 -22
- package/data-structures/snapshot/ClientCommitmentsSnapshotService.cjs +1 -1
- package/data-structures/snapshot/ClientCommitmentsSnapshotService.d.ts +1 -1
- package/data-structures/snapshot/ClientCommitmentsSnapshotService.mjs +7 -7
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("../../constants/chains.constants.cjs"),n=require("../../functions/web3/events/balanceChangedHandler.cjs");require("../../types/circom-data.types.cjs");const r=require("../../types/ethereum-network.types.cjs");require("../../types/transactions.types.cjs");require("../../types/curve.types.cjs");const c=require("../snapshot/ClientAccessTokenSnapshotService.cjs"),i=require("../snapshot/ClientCommitmentsSnapshotService.cjs"),o=require("../snapshot/ClientNullifierSnapshotService.cjs"),a=async e=>{const{maxPageSize:t}=s.networkRegistry[e.getCurrentChainId()];await e.getEthereumAddress(),e.commitmentsSnapshotService?.intervalClear(),e.commitmentsSnapshotService=new i.ClientCommitmentsSnapshotService(e.getContractWithFetcher(r.ContractType.HinkalContract),t),e.nullifierSnapshotService?.intervalClear(),e.nullifierSnapshotService=new o.ClientNullifierSnapshotService(e.getContractWithFetcher(r.ContractType.HinkalContract),t),e.accessTokenSnapshotService?.intervalClear(),e.accessTokenSnapshotService=new c.ClientAccessTokenSnapshotService(e.getContractWithFetcher(r.ContractType.CrossChainAccessTokenContract),t),await Promise.all([e.commitmentsSnapshotService.init(),e.nullifierSnapshotService.init(),e.accessTokenSnapshotService.init()]),e.merkleTreeHinkal=e.commitmentsSnapshotService.merkleTree,e.merkleTreeAccessToken=e.accessTokenSnapshotService.merkleTree,e.nullifiers=e.nullifierSnapshotService.nullifiers,e.encryptedOutputs=e.commitmentsSnapshotService.encryptedOutputs,e.commitmentsSnapshotService.onNewEvent=n.balanceChangedHandler,e.nullifierSnapshotService.onNewEvent=n.balanceChangedHandler};exports.resetMerkleTrees=a;
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { networkRegistry as
|
|
1
|
+
import { networkRegistry as c } from "../../constants/chains.constants.mjs";
|
|
2
2
|
import { balanceChangedHandler as o } from "../../functions/web3/events/balanceChangedHandler.mjs";
|
|
3
3
|
import "../../types/circom-data.types.mjs";
|
|
4
4
|
import { ContractType as r } from "../../types/ethereum-network.types.mjs";
|
|
5
5
|
import "../../types/transactions.types.mjs";
|
|
6
6
|
import "../../types/curve.types.mjs";
|
|
7
|
-
import { ClientAccessTokenSnapshotService as
|
|
8
|
-
import { ClientCommitmentsSnapshotService as
|
|
9
|
-
import { ClientNullifierSnapshotService as
|
|
7
|
+
import { ClientAccessTokenSnapshotService as n } from "../snapshot/ClientAccessTokenSnapshotService.mjs";
|
|
8
|
+
import { ClientCommitmentsSnapshotService as s } from "../snapshot/ClientCommitmentsSnapshotService.mjs";
|
|
9
|
+
import { ClientNullifierSnapshotService as i } from "../snapshot/ClientNullifierSnapshotService.mjs";
|
|
10
10
|
const T = async (e) => {
|
|
11
|
-
const { maxPageSize: t } =
|
|
12
|
-
e.commitmentsSnapshotService?.intervalClear(), e.commitmentsSnapshotService = new
|
|
11
|
+
const { maxPageSize: t } = c[e.getCurrentChainId()];
|
|
12
|
+
await e.getEthereumAddress(), e.commitmentsSnapshotService?.intervalClear(), e.commitmentsSnapshotService = new s(
|
|
13
13
|
e.getContractWithFetcher(r.HinkalContract),
|
|
14
14
|
t
|
|
15
|
-
), e.nullifierSnapshotService?.intervalClear(), e.nullifierSnapshotService = new
|
|
15
|
+
), e.nullifierSnapshotService?.intervalClear(), e.nullifierSnapshotService = new i(
|
|
16
16
|
e.getContractWithFetcher(r.HinkalContract),
|
|
17
17
|
t
|
|
18
|
-
), e.accessTokenSnapshotService?.intervalClear(), e.accessTokenSnapshotService = new
|
|
18
|
+
), e.accessTokenSnapshotService?.intervalClear(), e.accessTokenSnapshotService = new n(
|
|
19
19
|
e.getContractWithFetcher(r.CrossChainAccessTokenContract),
|
|
20
20
|
t
|
|
21
21
|
), await Promise.all([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("../../constants/reorg-depths.constants.cjs"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("../../constants/reorg-depths.constants.cjs"),c=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;ethAddress;constructor(e,s,t,r,n){this.contract=e,this.eventConfig=s,this._latestBlockNumber=t,this.maxPageSize=n,this.isServer=r,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:s}=await this.contract.provider.getNetwork();return Math.max(this.latestBlockNumber,e-v.blockReorgDepth[s]+1)};async retrieveEvents(e,s=!1){try{if(this.requireReady(),this.inProgress&&!s)return!1;this.inProgress=!0;const t=await this.getLastBlockNumberForEventRequest();if(t<e)return this.inProgress=!1,!1;const r=c.getSequence(e,t,this.maxPageSize);return await c.resolveSync(r.map(({from:n,to:i})=>async()=>{const o=await this.contract.queryFilter(this.contract.filters[this.eventConfig.name](),n,i);let a=[];o.length>0&&(a=await this.processEventsPage(o),this.eventConfig.name==="NewCommitment"&&console.log("new events catched",this.ethAddress,{mappedEvents:a,events:o})),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 s=[];return await c.resolveSync(e.map(t=>async()=>{const{args:r,blockNumber:n}=t;if(this.eventConfig.name==="NewCommitment"&&console.log("args",this.ethAddress,{args:r}),!r)throw new Error("Wrong event structure");const i=this.mapEvent(r);await this.acceptEvent(i,n)&&s.push(i)})),s}handleEvent=async(...e)=>{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),{});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;
|
|
@@ -14,6 +14,7 @@ export declare abstract class AbstractEventService<EventType, SerializedEventTyp
|
|
|
14
14
|
protected isServer: boolean;
|
|
15
15
|
protected intervalId?: any;
|
|
16
16
|
protected inProgress: boolean;
|
|
17
|
+
protected ethAddress: string;
|
|
17
18
|
protected constructor(contract: ethers.Contract, eventConfig: EventConfig, initialBlockNumber: number, isServer: boolean, maxPageSize?: number);
|
|
18
19
|
get latestBlockNumber(): number;
|
|
19
20
|
set onNewEvent(value: NewEventListener<EventType>);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { blockReorgDepth as v } from "../../constants/reorg-depths.constants.mjs";
|
|
2
|
-
import { getSequence as m, resolveSync as
|
|
2
|
+
import { getSequence as m, resolveSync as c } from "../../functions/utils/resolve-sync.utils.mjs";
|
|
3
3
|
import "ethers";
|
|
4
4
|
import "axios";
|
|
5
5
|
import "../../constants/chains.constants.mjs";
|
|
@@ -20,8 +20,9 @@ class C {
|
|
|
20
20
|
intervalId;
|
|
21
21
|
inProgress;
|
|
22
22
|
// to remove race conditions
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
ethAddress;
|
|
24
|
+
constructor(e, s, t, r, n) {
|
|
25
|
+
this.contract = e, this.eventConfig = s, this._latestBlockNumber = t, this.maxPageSize = n, this.isServer = r, this.inProgress = !1;
|
|
25
26
|
}
|
|
26
27
|
get latestBlockNumber() {
|
|
27
28
|
return this._latestBlockNumber;
|
|
@@ -62,19 +63,15 @@ class C {
|
|
|
62
63
|
if (t < e)
|
|
63
64
|
return this.inProgress = !1, !1;
|
|
64
65
|
const r = m(e, t, this.maxPageSize);
|
|
65
|
-
return await
|
|
66
|
-
r.map(({ from:
|
|
66
|
+
return await c(
|
|
67
|
+
r.map(({ from: n, to: i }) => async () => {
|
|
67
68
|
const o = await this.contract.queryFilter(
|
|
68
69
|
this.contract.filters[this.eventConfig.name](),
|
|
69
|
-
|
|
70
|
+
n,
|
|
70
71
|
i
|
|
71
72
|
);
|
|
72
|
-
let
|
|
73
|
-
|
|
74
|
-
const c = await this.contract.signer.getAddress();
|
|
75
|
-
console.log("new events catched", c, { mappedEvents: n, events: o });
|
|
76
|
-
}
|
|
77
|
-
this._latestBlockNumber = i, await this.afterEventsAccepted(), n.length > 0 && this.emitNewEvent();
|
|
73
|
+
let a = [];
|
|
74
|
+
o.length > 0 && (a = await this.processEventsPage(o), this.eventConfig.name === "NewCommitment" && console.log("new events catched", this.ethAddress, { mappedEvents: a, events: o })), this._latestBlockNumber = i, await this.afterEventsAccepted(), a.length > 0 && this.emitNewEvent();
|
|
78
75
|
})
|
|
79
76
|
), this.inProgress = !1, !0;
|
|
80
77
|
} catch (t) {
|
|
@@ -83,25 +80,21 @@ class C {
|
|
|
83
80
|
}
|
|
84
81
|
async processEventsPage(e) {
|
|
85
82
|
const s = [];
|
|
86
|
-
return await
|
|
83
|
+
return await c(
|
|
87
84
|
e.map((t) => async () => {
|
|
88
|
-
const { args: r, blockNumber:
|
|
89
|
-
if (this.eventConfig.name === "NewCommitment")
|
|
90
|
-
const n = await this.contract.signer.getAddress();
|
|
91
|
-
console.log("args", n, { args: r });
|
|
92
|
-
}
|
|
93
|
-
if (!r)
|
|
85
|
+
const { args: r, blockNumber: n } = t;
|
|
86
|
+
if (this.eventConfig.name === "NewCommitment" && console.log("args", this.ethAddress, { args: r }), !r)
|
|
94
87
|
throw new Error("Wrong event structure");
|
|
95
88
|
const i = this.mapEvent(r);
|
|
96
|
-
await this.acceptEvent(i,
|
|
89
|
+
await this.acceptEvent(i, n) && s.push(i);
|
|
97
90
|
})
|
|
98
91
|
), s;
|
|
99
92
|
}
|
|
100
93
|
handleEvent = async (...e) => {
|
|
101
|
-
const { args: s } = this.eventConfig, { blockNumber: t } = e[e.length - 1], r = e.slice(0, s.length),
|
|
94
|
+
const { args: s } = this.eventConfig, { blockNumber: t } = e[e.length - 1], r = e.slice(0, s.length), n = s.reduce((a, h, l) => (a[h] = r[l], a), {});
|
|
102
95
|
if (!t)
|
|
103
96
|
throw new Error("Wrong event structure");
|
|
104
|
-
const i = this.mapEvent(
|
|
97
|
+
const i = this.mapEvent(n);
|
|
105
98
|
if (!await this.acceptEvent(i, t))
|
|
106
99
|
throw new Error("Failed to retrieve events");
|
|
107
100
|
this._latestBlockNumber = t, await this.afterEventsAccepted(), this.emitNewEvent(i);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("circomlibjs");require("ethers");require("../../constants/vite.constants.cjs");require("../../constants/reorg-depths.constants.cjs");require("axios");require("../../constants/chains.constants.cjs");require("../../types/circom-data.types.cjs");require("../../types/transactions.types.cjs");require("../../types/curve.types.cjs");const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("circomlibjs");require("ethers");require("../../constants/vite.constants.cjs");require("../../constants/reorg-depths.constants.cjs");require("axios");require("../../constants/chains.constants.cjs");require("../../types/circom-data.types.cjs");require("../../types/transactions.types.cjs");require("../../types/curve.types.cjs");const s=require("../../API/fetchCommitmentsCache.cjs"),i=require("../event-service/AbstractCommitmentsSnapshotService.cjs");class n extends i.AbstractCommitmentsSnapshotService{constructor(t,e,r){super(t,0,!1,e),this.ethAddress=r}async fetchSnapshot(){const{chainId:t}=await this.contract.provider.getNetwork(),e=await s.fetchCommitmentsSnapshot(t);if(e.hinkalAddress!==this.contract.address||e.chainId!==t)throw Error("Commitment Snapshot: incorrect contract or chain id");return{latestBlockNumber:e.latestBlockNumber,merkleTree:e.merkleTree,encryptedOutputs:e.encryptedOutputs}}persistSnapshot(t){return Promise.resolve()}}exports.ClientCommitmentsSnapshotService=n;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AbstractCommitmentsSnapshotService, CommitmentsSerializedSnapshot } from '../event-service';
|
|
2
2
|
import { Contract } from 'ethers';
|
|
3
3
|
export declare class ClientCommitmentsSnapshotService extends AbstractCommitmentsSnapshotService {
|
|
4
|
-
constructor(contract: Contract, maxPageSize?: number);
|
|
4
|
+
constructor(contract: Contract, maxPageSize?: number, ethAddress?: string);
|
|
5
5
|
protected fetchSnapshot(): Promise<CommitmentsSerializedSnapshot>;
|
|
6
6
|
protected persistSnapshot(_serializedSnapshot: CommitmentsSerializedSnapshot): Promise<unknown>;
|
|
7
7
|
}
|
|
@@ -7,14 +7,14 @@ import "../../constants/chains.constants.mjs";
|
|
|
7
7
|
import "../../types/circom-data.types.mjs";
|
|
8
8
|
import "../../types/transactions.types.mjs";
|
|
9
9
|
import "../../types/curve.types.mjs";
|
|
10
|
-
import { fetchCommitmentsSnapshot as
|
|
11
|
-
import { AbstractCommitmentsSnapshotService as
|
|
12
|
-
class
|
|
13
|
-
constructor(r, t) {
|
|
14
|
-
super(r, 0, !1, t);
|
|
10
|
+
import { fetchCommitmentsSnapshot as o } from "../../API/fetchCommitmentsCache.mjs";
|
|
11
|
+
import { AbstractCommitmentsSnapshotService as s } from "../event-service/AbstractCommitmentsSnapshotService.mjs";
|
|
12
|
+
class k extends s {
|
|
13
|
+
constructor(r, t, e) {
|
|
14
|
+
super(r, 0, !1, t), this.ethAddress = e;
|
|
15
15
|
}
|
|
16
16
|
async fetchSnapshot() {
|
|
17
|
-
const { chainId: r } = await this.contract.provider.getNetwork(), t = await
|
|
17
|
+
const { chainId: r } = await this.contract.provider.getNetwork(), t = await o(r);
|
|
18
18
|
if (t.hinkalAddress !== this.contract.address || t.chainId !== r)
|
|
19
19
|
throw Error("Commitment Snapshot: incorrect contract or chain id");
|
|
20
20
|
return {
|
|
@@ -28,5 +28,5 @@ class f extends o {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
export {
|
|
31
|
-
|
|
31
|
+
k as ClientCommitmentsSnapshotService
|
|
32
32
|
};
|