@kapa123456789/sdk 0.0.20 → 0.0.24
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/common/src/data-structures/Hinkal/Hinkal.cjs +1 -1
- package/common/src/data-structures/Hinkal/Hinkal.d.ts +0 -4
- package/common/src/data-structures/Hinkal/Hinkal.mjs +2 -10
- package/common/src/data-structures/Hinkal/IHinkal.d.ts +0 -3
- package/common/src/data-structures/Hinkal/index.d.ts +0 -1
- package/common/src/data-structures/IndexedDB/index.cjs +1 -1
- package/common/src/data-structures/IndexedDB/index.d.ts +0 -1
- package/common/src/data-structures/IndexedDB/index.mjs +0 -1
- package/common/src/data-structures/IndexedDB/prices-db.cjs +1 -1
- package/common/src/data-structures/IndexedDB/prices-db.mjs +17 -18
- package/common/src/data-structures/IndexedDB/private-balances-db.cjs +1 -1
- package/common/src/data-structures/IndexedDB/private-balances-db.mjs +11 -10
- package/common/src/functions/web3/events/balanceChangedHandler.d.ts +1 -1
- package/common/src/types/balances.types.d.ts +0 -22
- package/common/src/webworker/package.json +1 -1
- package/common/src/webworker/snarkjsWorkerNode.cjs +11 -1
- package/common/src/webworker/utxoWorkerNode.cjs +1 -1
- package/common/src/webworker/workerProxy-DPSmXOIl.js +122 -0
- package/common/src/webworker/zkProofWorkerNode.cjs +1 -1
- package/package.json +1 -1
- package/services/Hinkal.cjs +1 -1
- package/services/Hinkal.d.ts +3 -3
- package/services/Hinkal.mjs +7 -9
- package/types/IHinkal.d.ts +3 -4
- package/types/balances.types.d.ts +1 -1
- package/common/src/data-structures/Hinkal/HinkalPrivateBalanceController.cjs +0 -1
- package/common/src/data-structures/Hinkal/HinkalPrivateBalanceController.d.ts +0 -19
- package/common/src/data-structures/Hinkal/HinkalPrivateBalanceController.mjs +0 -239
- package/common/src/data-structures/IndexedDB/key-val-store.cjs +0 -1
- package/common/src/data-structures/IndexedDB/key-val-store.d.ts +0 -6
- package/common/src/data-structures/IndexedDB/key-val-store.mjs +0 -41
- package/common/src/webworker/logError-hakeEPz8.js +0 -121
- package/common/src/webworker/workerProxy-36ykh-KW.js +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=require(`./workerProxy-
|
|
1
|
+
const e=require(`./workerProxy-DPSmXOIl.js`),t=require(`./logError-COJpTdjC.js`);let n=require(`worker_threads`),r=require(`buffer`);var i=e.x(e.g());Object.values({preHookContract:`0x0000000000000000000000000000000000000000`,hookContract:`0x0000000000000000000000000000000000000000`,preHookMetadata:`0x00`,postHookMetadata:`0x00`});var a=(e,t)=>{let n=[];for(let r=0;r<e.length;r+=1){n.push([]);for(let i=0;i<e[r].length;i+=1)n[r][i]=t.getSiblingHashesForVerification(BigInt(e[r][i].getCommitment())).map(e=>e.toString())}let r=[];for(let n=0;n<e.length;n+=1){r.push([]);for(let i=0;i<e[n].length;i+=1)r[n][i]=t.getSiblingSides(BigInt(e[n][i].getCommitment())).map(e=>e.toString())}return{inCommitmentSiblings:n,inCommitmentSiblingSides:r}},o=(e,t)=>({accessTokenSiblings:t.getSiblingHashesForVerification(BigInt(e)).map(e=>e.toString()),accessTokenSiblingSides:t.getSiblingSides(BigInt(e)).map(e=>e.toString())}),s=(e,t)=>o(e,t),c=function(e){return e[e.CALC_COMMITMENTS_SIBLING_AND_SIDES=0]=`CALC_COMMITMENTS_SIBLING_AND_SIDES`,e[e.CALC_ACCESS_TOKEN_SIBLING_AND_SIDES=1]=`CALC_ACCESS_TOKEN_SIBLING_AND_SIDES`,e[e.BUILD_IN_NULLIFIERS=2]=`BUILD_IN_NULLIFIERS`,e}({}),l=class extends Error{constructor(){super(`Merkle tree is incomplete`),this.name=`MerkleTreeIncompleteError`}},u=class e{tree;count;index;reverseTree;static createWithData(t,n,r,i,a,o,s){return new e(a,o,s,t,n,r,i)}static create(n,r,i=t.c){return new e(n,i,r)}createReverseTree(){let e=new Map;for(let t=this.getStartIndex();t<this.getStartIndex()+this.count;t+=1n){let n=this.tree.get(t);n!==void 0&&e.set(n,t)}return e}constructor(e,t,n,r,i,a,o){this.hashFunction=e,this.levels=t,this.defaultNodeValue=n,this.tree=r??new Map,this.count=o??0n,this.index=a??2n**(t-1n),i?this.reverseTree=i:this.reverseTree=this.createReverseTree()}getStartIndex(){return 2n**(this.levels-1n)}logarithm2(e){let t=0n;for(;2n**t<e;)t+=1n;return t}bigIntMax(e,t){return e>t?e:t}forceInsert(e,t){if(t<this.getStartIndex())throw RangeError();this.tree.set(t,e),this.reverseTree.set(e,t);let n=this.index-this.getStartIndex(),r=this.logarithm2(n),i=t;for(let e=1n;e<=r;e+=1n){i/=2n;let e=this.hashFunction(this.tree.get(i*2n)||this.defaultNodeValue,this.tree.get(i*2n+1n)||this.defaultNodeValue);this.tree.set(i,e)}}insert(e,n){let r=!1;this.tree.has(n)&&(r=!0,t.t(`Editing commitment index: ${n} `)),r||(this.count+=1n),this.index=this.bigIntMax(this.index,n+1n),this.forceInsert(e,n)}remove(e){this.forceInsert(this.defaultNodeValue,e)}completenessCheck(){if(this.count!==this.index-this.getStartIndex())throw new l}getRootHash(){this.completenessCheck();for(let e=1n;e<2n**this.levels;e*=2n)if(this.tree.get(e))return this.tree.get(e);return this.defaultNodeValue}getMerkleData(){return this.completenessCheck(),new Map(this.tree)}getValue(e){return this.tree.get(e)}getSiblingIndex(e){return e===1n?1n:e%2n==1n?e-1n:e+1n}getSiblingHashesForVerification(e){this.completenessCheck();let t=this.reverseTree.get(e);if(t===void 0)return Array(25).fill(0n);let n=[];for(;t!==0n;)n.push(this.tree.get(this.getSiblingIndex(t))||this.defaultNodeValue),t/=2n;return n.slice(0,25)}getSiblingSides(e){this.completenessCheck();let t=this.reverseTree.get(e);if(t===void 0)return Array(25).fill(0n);let n=[];for(;t!==0n;){let e=t%2n==0n?0n:1n;n.push(e),t/=2n}return n.slice(0,25)}toJSON(){let{tree:e,reverseTree:t,count:n,index:r}=this;return{tree:Object.fromEntries(e),reverseTree:Object.fromEntries(t),count:n.toString(),index:r.toString()}}toJSONLite(){let{tree:e,count:t,index:n}=this;return{tree:Object.fromEntries(e),count:t.toString(),index:n.toString()}}clone(){return e.createWithData(new Map(this.tree),new Map(this.reverseTree),this.index,this.count,this.hashFunction,this.levels,this.defaultNodeValue)}getIndex(){return this.index}},d=e=>new Map(Object.entries(e).map(([e,t])=>[BigInt(e),BigInt(t)])),f=e=>{let n=d(e.tree),r=e.reverseTree?d(e.reverseTree):void 0;return u.createWithData(n,r,BigInt(e.index),BigInt(e.count),t.d,t.c,0n)},p=new e.t,m=e=>{let{inputUtxosSerialized:n,merkleTreeSerialized:r}=e.data,i=f(r),{inCommitmentSiblings:o,inCommitmentSiblingSides:s}=a(n.map(e=>e.map(e=>new t.i(e))),i);p.postMessageToMainThread({inCommitmentSiblings:o,inCommitmentSiblingSides:s})},h=e=>{let{accessKey:t,merkleTreeAccessTokenSerialized:n}=e.data,{accessTokenSiblings:r,accessTokenSiblingSides:i}=s(t,f(n));p.postMessageToMainThread({accessTokenSiblings:r,accessTokenSiblingSides:i})},g=e=>{let{inputUtxosSerialized:n}=e.data,r=n.map(e=>e.map(e=>new t.i(e))).map(e=>e.map(e=>e.amount===0n?`0`:e.getNullifier()));p.postMessageToMainThread(r)},_=async e=>{try{await t.u();let{type:n}=e.payload;switch(n){case c.CALC_COMMITMENTS_SIBLING_AND_SIDES:m(e.payload);return;case c.CALC_ACCESS_TOKEN_SIBLING_AND_SIDES:h(e.payload);return;case c.BUILD_IN_NULLIFIERS:g(e.payload);return;default:throw Error(`Unknown worker message type ${n}`)}}catch(e){console.error(e),p.postErrorToMainThread(e)}};globalThis.process=i.default,globalThis.Buffer=r.Buffer,n.parentPort.on(`message`,async e=>{await _(e)});
|
package/package.json
CHANGED
package/services/Hinkal.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=require(`../common/src/constants/chains.constants.cjs`),t=require(`../common/src/types/hinkal.types.cjs`);require(`../common/src/types/index.cjs`);const n=require(`../common/src/data-structures/Hinkal/Hinkal.cjs`);var
|
|
1
|
+
const e=require(`../common/src/constants/chains.constants.cjs`),t=require(`../common/src/types/hinkal.types.cjs`);require(`../common/src/types/index.cjs`);const n=require(`../common/src/functions/web3/events/balanceChangedHandler.cjs`),r=require(`../common/src/data-structures/Hinkal/Hinkal.cjs`);var i=class{hinkalCommon;constructor(e){this.hinkalCommon=new r.Hinkal(e)}getProviderAdapter(e){return this.hinkalCommon.getProviderAdapter(e)}initProviderAdapter(e,t){return this.hinkalCommon.initProviderAdapter(e,t)}initUserKeys(e=t.LoginMessageMode.PROTOCOL){return this.hinkalCommon.initUserKeys(e)}initUserKeysWithSignature(e){return this.hinkalCommon.initUserKeysWithSignature(e)}initUserKeysFromSeedPhrases(e){return this.hinkalCommon.initUserKeysFromSeedPhrases(e)}resetProviderAdapters(){return this.hinkalCommon.resetProviderAdapters()}disconnectFromConnector(){return this.hinkalCommon.disconnectFromConnector()}signMessage(e){return this.hinkalCommon.signMessage(e)}signTypedData(e,t,n){return this.hinkalCommon.signTypedData(e,t,n)}getSupportedChains(){return this.hinkalCommon.getSupportedChains()}isSelectedNetworkSupported(e){return this.hinkalCommon.isSelectedNetworkSupported(e)}switchNetwork(t){let n=e.networkRegistry[t];if(!n)throw Error(`Network with chainId ${t} not supported`);return this.hinkalCommon.switchNetwork(n)}resetMerkle(e){return this.hinkalCommon.resetMerkle(e)}resetMerkleTreesIfNecessary(e){return this.hinkalCommon.resetMerkleTreesIfNecessary(e)}onBalanceRefresh(e){return n.subscribeToBalanceChange(e)}getTotalBalance(e,t=!1,n=!1){return this.hinkalCommon.getTotalBalance(e,void 0,void 0,t,n)}getStuckShieldedBalances(e,t=!1,n=!1){return this.hinkalCommon.getStuckShieldedBalances(e,void 0,void 0,t,n)}monitorConnectedAddress(e){return this.hinkalCommon.monitorConnectedAddress(e)}getSupportedPassportLinks(){return this.hinkalCommon.getSupportedPassportLinks()}checkAccessToken(e,t){return this.hinkalCommon.checkAccessToken(e,t)}waitForTransaction(e,t,n){return this.hinkalCommon.waitForTransaction(e,t,n)}getSolanaPublicKey(){return this.hinkalCommon.getSolanaPublicKey()}getEthereumAddress(){return this.hinkalCommon.getEthereumAddress()}getEthereumAddressByChain(e){return this.hinkalCommon.getEthereumAddressByChain(e)}getRecipientInfo(){return this.hinkalCommon.getRecipientInfo()}getShieldedPublicKey(){return this.hinkalCommon.userKeys.getShieldedPublicKey()}prooflessDeposit(e,t,n){return this.hinkalCommon.prooflessDeposit(e,t,n)}deposit(e,t,n,r){return this.hinkalCommon.deposit(e,t,n,r)}depositSolana(e,t){return this.hinkalCommon.depositSolana(e,t)}depositForOther(e,t,n,r,i){return this.hinkalCommon.depositForOther(e,t,n,r,i)}depositSolanaForOther(e,t,n){return this.hinkalCommon.depositSolanaForOther(e,t,n)}depositAndWithdraw(e,t,n,r,i,a){return this.hinkalCommon.depositAndWithdraw(e,t,n,r,i,void 0,void 0,void 0,a)}depositAndBridge(e,t,n,r,i){return this.hinkalCommon.depositAndBridge(e,t,n,r,void 0,void 0,void 0,i)}claimUtxo(e,t,n){return this.hinkalCommon.claimUtxo(e,t,n)}transfer(e,t,n,r,i){return this.hinkalCommon.transfer(e,t,n,r,i)}withdraw(e,t,n,r,i,a){return this.hinkalCommon.withdraw(e,t,n,r,i,a)}withdrawStuckUtxos(e,t){return this.hinkalCommon.withdrawStuckUtxos(e,t)}swap(e,t,n,r,i,a){return this.hinkalCommon.swap(e,t,n,r,i,a)}actionPrivateWallet(e,t,n,r,i,a,o,s,c){return this.hinkalCommon.actionPrivateWallet(e,t,n,r,i,a,o,s,c)}proxySwap(e,t,n,r,i,a,o,s){return this.hinkalCommon.proxySwap(e,t,n,r,i,a,o,s)}proxyToPrivate(e,t,n,r,i,a){return this.hinkalCommon.proxyToPrivate(e,t,n,r,i,a)}proxySend(e,t,n,r,i,a){return this.hinkalCommon.proxySend(e,t,n,r,i,a)}};exports.Hinkal=i;
|
package/services/Hinkal.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { BridgeRecipient, ERC20Token, ExternalActionId, FeeStructure, HinkalConfig, LoginMessageMode, StealthAddressStructure, SubAccount, TokenBalance, TokenChanges
|
|
1
|
+
import { BridgeRecipient, ERC20Token, ExternalActionId, FeeStructure, HinkalConfig, LoginMessageMode, StealthAddressStructure, SubAccount, TokenBalance, TokenChanges } from '../common/src/types';
|
|
2
2
|
import { ethers, TypedDataDomain, TypedDataField } from 'ethers';
|
|
3
3
|
import { Utxo } from '../common/src/data-structures/utxo/Utxo';
|
|
4
4
|
import { PublicKey } from '@solana/web3.js';
|
|
5
5
|
import { IHinkal, IProviderAdapter } from '../types';
|
|
6
|
+
import { PrivateBalanceUpdateEventType } from '../types/balances.types';
|
|
6
7
|
export declare class Hinkal<ConnectorType> implements IHinkal<ConnectorType> {
|
|
7
8
|
private readonly hinkalCommon;
|
|
8
9
|
constructor(hinkalConfig?: HinkalConfig);
|
|
@@ -20,8 +21,7 @@ export declare class Hinkal<ConnectorType> implements IHinkal<ConnectorType> {
|
|
|
20
21
|
switchNetwork(chainId: number): Promise<unknown>;
|
|
21
22
|
resetMerkle(chainIdsToReset?: number[]): Promise<void>;
|
|
22
23
|
resetMerkleTreesIfNecessary(chainIdsToCheck?: number[]): Promise<void>;
|
|
23
|
-
|
|
24
|
-
onPrivateBalancesWithUSDChange(callback: (state: PrivateBalancesState) => void): () => void;
|
|
24
|
+
onBalanceRefresh(callback: (detail: PrivateBalanceUpdateEventType) => void | Promise<void>): () => void;
|
|
25
25
|
getTotalBalance(chainId: number, resetCacheBefore?: boolean, updateTokensListBefore?: boolean): Promise<TokenBalance[]>;
|
|
26
26
|
getStuckShieldedBalances(chainId: number, resetCacheBefore?: boolean, updateTokensListBefore?: boolean): Promise<TokenBalance[]>;
|
|
27
27
|
monitorConnectedAddress(chainId: number): Promise<void>;
|
package/services/Hinkal.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { networkRegistry as e } from "../common/src/constants/chains.constants.mjs";
|
|
2
2
|
import { LoginMessageMode as t } from "../common/src/types/hinkal.types.mjs";
|
|
3
3
|
import "../common/src/types/index.mjs";
|
|
4
|
-
import {
|
|
4
|
+
import { subscribeToBalanceChange as n } from "../common/src/functions/web3/events/balanceChangedHandler.mjs";
|
|
5
|
+
import { Hinkal as r } from "../common/src/data-structures/Hinkal/Hinkal.mjs";
|
|
5
6
|
//#region libs/shared/sdk/src/services/Hinkal.ts
|
|
6
|
-
var
|
|
7
|
+
var i = class {
|
|
7
8
|
hinkalCommon;
|
|
8
9
|
constructor(e) {
|
|
9
|
-
this.hinkalCommon = new
|
|
10
|
+
this.hinkalCommon = new r(e);
|
|
10
11
|
}
|
|
11
12
|
getProviderAdapter(e) {
|
|
12
13
|
return this.hinkalCommon.getProviderAdapter(e);
|
|
@@ -52,11 +53,8 @@ var r = class {
|
|
|
52
53
|
resetMerkleTreesIfNecessary(e) {
|
|
53
54
|
return this.hinkalCommon.resetMerkleTreesIfNecessary(e);
|
|
54
55
|
}
|
|
55
|
-
|
|
56
|
-
return
|
|
57
|
-
}
|
|
58
|
-
onPrivateBalancesWithUSDChange(e) {
|
|
59
|
-
return this.hinkalCommon.onPrivateBalancesWithUSDChange(e);
|
|
56
|
+
onBalanceRefresh(e) {
|
|
57
|
+
return n(e);
|
|
60
58
|
}
|
|
61
59
|
getTotalBalance(e, t = !1, n = !1) {
|
|
62
60
|
return this.hinkalCommon.getTotalBalance(e, void 0, void 0, t, n);
|
|
@@ -141,4 +139,4 @@ var r = class {
|
|
|
141
139
|
}
|
|
142
140
|
};
|
|
143
141
|
//#endregion
|
|
144
|
-
export {
|
|
142
|
+
export { i as Hinkal };
|
package/types/IHinkal.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BridgeRecipient, ERC20Token, ExternalActionId, FeeStructure, LoginMessageMode, StealthAddressStructure, SubAccount, TokenBalance, TokenChanges } from '../common/src/types';
|
|
2
2
|
import { ethers, TypedDataDomain, TypedDataField } from 'ethers';
|
|
3
3
|
import { Utxo } from '../common/src/data-structures/utxo/Utxo';
|
|
4
4
|
import { PublicKey } from '@solana/web3.js';
|
|
5
5
|
import { IProviderAdapter } from './IProviderAdapter';
|
|
6
|
+
import { PrivateBalanceUpdateEventType } from './balances.types';
|
|
6
7
|
export interface IHinkal<ConnectorType = unknown> {
|
|
7
8
|
getProviderAdapter(chainId?: number): IProviderAdapter;
|
|
8
9
|
initProviderAdapter(connector: ConnectorType, providerAdapter: IProviderAdapter<ConnectorType>): Promise<void>;
|
|
@@ -18,9 +19,7 @@ export interface IHinkal<ConnectorType = unknown> {
|
|
|
18
19
|
switchNetwork(chainId: number): Promise<unknown>;
|
|
19
20
|
resetMerkle(chainIdsToReset?: number[]): Promise<void>;
|
|
20
21
|
resetMerkleTreesIfNecessary(chainIdsToCheck?: number[]): Promise<void>;
|
|
21
|
-
|
|
22
|
-
/** Returns unsubscribe. Multiple listeners supported. */
|
|
23
|
-
onPrivateBalancesWithUSDChange(callback: (state: PrivateBalancesState) => void): () => void;
|
|
22
|
+
onBalanceRefresh(callback: (detail: PrivateBalanceUpdateEventType) => void | Promise<void>): () => void;
|
|
24
23
|
getTotalBalance(chainId: number, resetCacheBefore?: boolean, updateTokensListBefore?: boolean): Promise<TokenBalance[]>;
|
|
25
24
|
getStuckShieldedBalances(chainId: number, resetCacheBefore?: boolean, updateTokensListBefore?: boolean): Promise<TokenBalance[]>;
|
|
26
25
|
monitorConnectedAddress(chainId: number): Promise<void>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { PrivateBalanceUpdateEventType as CommonPrivateBalanceUpdateEventType } from '../common/src/types';
|
|
2
2
|
export { PrivateBalanceUpdateType } from '../common/src/types';
|
|
3
|
-
export type {
|
|
3
|
+
export type { TokenBalance } from '../common/src/types';
|
|
4
4
|
export type PrivateBalanceUpdateEventType = Omit<CommonPrivateBalanceUpdateEventType, 'balanceChangeState'>;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const e=require(`../../types/balances.types.cjs`),t=require(`../../functions/web3/etherFunctions.cjs`),n=require(`../../functions/utils/upToDateState.cjs`),r=require(`../../error-handling/logger.cjs`),i=require(`../../functions/utils/string.utils.cjs`),a=require(`../../functions/utils/token-check.utils.cjs`),o=require(`../../functions/web3/events/balanceChangedHandler.cjs`),s=require(`../IndexedDB/private-balances-db.cjs`),c=require(`../IndexedDB/prices-db.cjs`);var l=(e,t)=>{switch(t.type){case`SET_BALANCES`:{let n=e[t.chainId];if(t.balances===void 0){let n={...e};return delete n[t.chainId],Object.keys(n).length>0?n:{}}if(n===void 0)return{...e,[t.chainId]:t.balances};let r=t.balances,i=r.filter(e=>!n.some(t=>a.isSameERC20Token(e.token,t.token))),o=n.map(e=>({...e,balance:r.find(t=>a.isSameERC20Token(t.token,e.token))?.balance??0n})).filter(e=>e.balance>0n);return o=[...o,...i],{...e,[t.chainId]:o}}case`SET_USD_BALANCES`:{let n=e[t.chainId];if(n===void 0||t.balances===void 0)return e;let r=t.balances,i=n.map(e=>({...e,usdValue:r.find(t=>a.isSameERC20Token(t.token,e.token))?.usdValue??0}));return{...e,[t.chainId]:i}}case`CLEAR_BALANCES`:{if(t.chainId===void 0)return{};let n={...e};return delete n[t.chainId],Object.keys(n).length>0?n:{}}case`SET_LOADING`:{let n=e[t.chainId];if(n===void 0)return e;if(n.length===0&&t.newValue)return{...e,[t.chainId]:void 0};let r=n.map(e=>({...e,isBalanceLoading:t.newValue}));return{...e,[t.chainId]:r}}case`SET_USD_LOADING`:{let n=e[t.chainId];if(n===void 0)return e;let r=n.map(e=>({...e,isUsdValueLoading:t.newValue}));return{...e,[t.chainId]:r}}default:return e}},u=class{privateBalancesState={};privateBalancesListeners=new Set;constructor(e){this.hinkal=e,o.subscribeToBalanceChange(e=>{this.applyRefreshFromBalanceChangeEvent(e).catch(e=>{r.Logger.error(`Private balance refresh from balance change failed:`,e)})})}getSnapshot(){return this.privateBalancesState}onPrivateBalancesWithUSDChange(e){return this.privateBalancesListeners.add(e),()=>this.privateBalancesListeners.delete(e)}async applyRefreshFromBalanceChangeEvent(t){if(!t){await this.refreshInternal(e.PrivateBalanceUpdateType.FreshWithoutLoader);return}let{updateType:n,balanceChangeState:r,chainIdToUpdate:i,resetCacheBefore:a,updateTokensListBefore:o}=t;await this.refreshInternal(n,r,a,o,i)}dispatch(e){this.privateBalancesState=l(this.privateBalancesState,e);let t=this.privateBalancesState;this.privateBalancesListeners.forEach(e=>e(t))}async getAddressAndChain(e){return{ethAdd:e?.ethereumAddress??await this.hinkal.getEthereumAddress(),userKeys:e?.userKeys??this.hinkal.userKeys}}async hasCache(e,t){return await s.privateBalancesStorage.getBalancesByWallet(e,t)!==void 0}async getBalancesWithUsdValues(e,n){try{if(!n)return[];let r=e.map(e=>e.token.erc20TokenAddress);if(!r.length)return[];let i=await c.pricesStorage.getAndUpdatePrices(n,r),a=new Map(r.map((e,t)=>[e,i[t]]));return[...e.map(e=>{try{let n=e.token.erc20TokenAddress,r=a.get(n)??0,i=e.balance,o=Number(t.getAmountInToken(e.token,i))*r;return{...e,usdValue:o}}catch{return{...e,usdValue:0}}})]}catch{return r.Logger.log(`Error fetching private token balances`),[]}}handleCached=async(e,a)=>{try{await n.checkAddressNotUpdated(this.hinkal,e),this.dispatch({type:`SET_BALANCES`,chainId:a,balances:void 0});let r=await s.privateBalancesStorage.getBalancesByWallet(e,a),o=await c.pricesStorage.getPrices();if(await n.checkAddressNotUpdated(this.hinkal,e),!r||(this.dispatch({type:`SET_BALANCES`,chainId:a,balances:r}),!o))return;let l=r.map(e=>{let n=i.getTokenStateKey(e.token.erc20TokenAddress,a);return{...e,usdValue:o[n]?Number(t.getAmountInToken(e.token,e.balance))*o[n]:0}});this.dispatch({type:`SET_USD_BALANCES`,chainId:a,balances:l})}catch(e){r.Logger.error(`Error refreshing cached private balance:`,e)}};handleFresh=async(e,t,i,a,o,c)=>{let l=o;try{await n.checkAddressNotUpdated(this.hinkal,e);let r=await this.hasCache(e,t);await n.checkAddressNotUpdated(this.hinkal,e),l=o||!r,l&&this.dispatch({type:`SET_LOADING`,chainId:t,newValue:!0}),await n.checkAddressNotUpdated(this.hinkal,e),a&&await this.hinkal.resetMerkle([t]);let u=await this.hinkal.getTotalBalance(t,i,e,a,c);await n.checkAddressNotUpdated(this.hinkal,e);let d=u.filter(e=>e.balance>0n);this.dispatch({type:`SET_BALANCES`,chainId:t,balances:d}),await s.privateBalancesStorage.setBalances(e,t,d),await n.checkAddressNotUpdated(this.hinkal,e),l&&(this.dispatch({type:`SET_USD_LOADING`,chainId:t,newValue:!0}),this.dispatch({type:`SET_LOADING`,chainId:t,newValue:!1})),await n.checkAddressNotUpdated(this.hinkal,e);let f=await this.getBalancesWithUsdValues(d,t);await n.checkAddressNotUpdated(this.hinkal,e),f.length>0&&this.dispatch({type:`SET_USD_BALANCES`,chainId:t,balances:f})}catch(e){r.Logger.error(`Error refreshing private balance:`,e)}finally{l&&(this.dispatch({type:`SET_LOADING`,chainId:t,newValue:!1}),this.dispatch({type:`SET_USD_LOADING`,chainId:t,newValue:!1}))}};getSupportedChainsToUpdate(e){let t=this.hinkal.getSupportedChains();return e?t.includes(e)?[e]:[]:t}async refreshInternal(t=e.PrivateBalanceUpdateType.FreshWithoutLoader,n,r=!1,i=!1,a){let{ethAdd:o,userKeys:s}=await this.getAddressAndChain(n);!o||!s?.getShieldedPublicKey()||await Promise.all(this.getSupportedChainsToUpdate(a).map(async n=>{switch(t){case e.PrivateBalanceUpdateType.OnlyCache:await this.handleCached(o,n);break;case e.PrivateBalanceUpdateType.Fresh:await this.handleFresh(o,n,s,r,!0,i);break;case e.PrivateBalanceUpdateType.FreshWithoutLoader:await this.handleFresh(o,n,s,r,!1,i);break;default:break}}))}};exports.HinkalPrivateBalanceController=u;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { PrivateBalancesState } from '../../types/balances.types';
|
|
2
|
-
import { IHinkal } from './IHinkal';
|
|
3
|
-
export declare class HinkalPrivateBalanceController {
|
|
4
|
-
private readonly hinkal;
|
|
5
|
-
private privateBalancesState;
|
|
6
|
-
private readonly privateBalancesListeners;
|
|
7
|
-
constructor(hinkal: IHinkal);
|
|
8
|
-
getSnapshot(): PrivateBalancesState;
|
|
9
|
-
onPrivateBalancesWithUSDChange(callback: (state: PrivateBalancesState) => void): () => void;
|
|
10
|
-
private applyRefreshFromBalanceChangeEvent;
|
|
11
|
-
private dispatch;
|
|
12
|
-
private getAddressAndChain;
|
|
13
|
-
private hasCache;
|
|
14
|
-
private getBalancesWithUsdValues;
|
|
15
|
-
private handleCached;
|
|
16
|
-
private handleFresh;
|
|
17
|
-
private getSupportedChainsToUpdate;
|
|
18
|
-
private refreshInternal;
|
|
19
|
-
}
|
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
import { PrivateBalanceUpdateType as e } from "../../types/balances.types.mjs";
|
|
2
|
-
import { getAmountInToken as t } from "../../functions/web3/etherFunctions.mjs";
|
|
3
|
-
import { checkAddressNotUpdated as n } from "../../functions/utils/upToDateState.mjs";
|
|
4
|
-
import { Logger as r } from "../../error-handling/logger.mjs";
|
|
5
|
-
import { getTokenStateKey as i } from "../../functions/utils/string.utils.mjs";
|
|
6
|
-
import { isSameERC20Token as a } from "../../functions/utils/token-check.utils.mjs";
|
|
7
|
-
import { subscribeToBalanceChange as o } from "../../functions/web3/events/balanceChangedHandler.mjs";
|
|
8
|
-
import { privateBalancesStorage as s } from "../IndexedDB/private-balances-db.mjs";
|
|
9
|
-
import { pricesStorage as c } from "../IndexedDB/prices-db.mjs";
|
|
10
|
-
//#region libs/shared/common/src/data-structures/Hinkal/HinkalPrivateBalanceController.ts
|
|
11
|
-
var l = (e, t) => {
|
|
12
|
-
switch (t.type) {
|
|
13
|
-
case "SET_BALANCES": {
|
|
14
|
-
let n = e[t.chainId];
|
|
15
|
-
if (t.balances === void 0) {
|
|
16
|
-
let n = { ...e };
|
|
17
|
-
return delete n[t.chainId], Object.keys(n).length > 0 ? n : {};
|
|
18
|
-
}
|
|
19
|
-
if (n === void 0) return {
|
|
20
|
-
...e,
|
|
21
|
-
[t.chainId]: t.balances
|
|
22
|
-
};
|
|
23
|
-
let r = t.balances, i = r.filter((e) => !n.some((t) => a(e.token, t.token))), o = n.map((e) => ({
|
|
24
|
-
...e,
|
|
25
|
-
balance: r.find((t) => a(t.token, e.token))?.balance ?? 0n
|
|
26
|
-
})).filter((e) => e.balance > 0n);
|
|
27
|
-
return o = [...o, ...i], {
|
|
28
|
-
...e,
|
|
29
|
-
[t.chainId]: o
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
case "SET_USD_BALANCES": {
|
|
33
|
-
let n = e[t.chainId];
|
|
34
|
-
if (n === void 0 || t.balances === void 0) return e;
|
|
35
|
-
let r = t.balances, i = n.map((e) => ({
|
|
36
|
-
...e,
|
|
37
|
-
usdValue: r.find((t) => a(t.token, e.token))?.usdValue ?? 0
|
|
38
|
-
}));
|
|
39
|
-
return {
|
|
40
|
-
...e,
|
|
41
|
-
[t.chainId]: i
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
case "CLEAR_BALANCES": {
|
|
45
|
-
if (t.chainId === void 0) return {};
|
|
46
|
-
let n = { ...e };
|
|
47
|
-
return delete n[t.chainId], Object.keys(n).length > 0 ? n : {};
|
|
48
|
-
}
|
|
49
|
-
case "SET_LOADING": {
|
|
50
|
-
let n = e[t.chainId];
|
|
51
|
-
if (n === void 0) return e;
|
|
52
|
-
if (n.length === 0 && t.newValue) return {
|
|
53
|
-
...e,
|
|
54
|
-
[t.chainId]: void 0
|
|
55
|
-
};
|
|
56
|
-
let r = n.map((e) => ({
|
|
57
|
-
...e,
|
|
58
|
-
isBalanceLoading: t.newValue
|
|
59
|
-
}));
|
|
60
|
-
return {
|
|
61
|
-
...e,
|
|
62
|
-
[t.chainId]: r
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
case "SET_USD_LOADING": {
|
|
66
|
-
let n = e[t.chainId];
|
|
67
|
-
if (n === void 0) return e;
|
|
68
|
-
let r = n.map((e) => ({
|
|
69
|
-
...e,
|
|
70
|
-
isUsdValueLoading: t.newValue
|
|
71
|
-
}));
|
|
72
|
-
return {
|
|
73
|
-
...e,
|
|
74
|
-
[t.chainId]: r
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
default: return e;
|
|
78
|
-
}
|
|
79
|
-
}, u = class {
|
|
80
|
-
privateBalancesState = {};
|
|
81
|
-
privateBalancesListeners = /* @__PURE__ */ new Set();
|
|
82
|
-
constructor(e) {
|
|
83
|
-
this.hinkal = e, o((e) => {
|
|
84
|
-
this.applyRefreshFromBalanceChangeEvent(e).catch((e) => {
|
|
85
|
-
r.error("Private balance refresh from balance change failed:", e);
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
getSnapshot() {
|
|
90
|
-
return this.privateBalancesState;
|
|
91
|
-
}
|
|
92
|
-
onPrivateBalancesWithUSDChange(e) {
|
|
93
|
-
return this.privateBalancesListeners.add(e), () => this.privateBalancesListeners.delete(e);
|
|
94
|
-
}
|
|
95
|
-
async applyRefreshFromBalanceChangeEvent(t) {
|
|
96
|
-
if (!t) {
|
|
97
|
-
await this.refreshInternal(e.FreshWithoutLoader);
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
let { updateType: n, balanceChangeState: r, chainIdToUpdate: i, resetCacheBefore: a, updateTokensListBefore: o } = t;
|
|
101
|
-
await this.refreshInternal(n, r, a, o, i);
|
|
102
|
-
}
|
|
103
|
-
dispatch(e) {
|
|
104
|
-
this.privateBalancesState = l(this.privateBalancesState, e);
|
|
105
|
-
let t = this.privateBalancesState;
|
|
106
|
-
this.privateBalancesListeners.forEach((e) => e(t));
|
|
107
|
-
}
|
|
108
|
-
async getAddressAndChain(e) {
|
|
109
|
-
return {
|
|
110
|
-
ethAdd: e?.ethereumAddress ?? await this.hinkal.getEthereumAddress(),
|
|
111
|
-
userKeys: e?.userKeys ?? this.hinkal.userKeys
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
async hasCache(e, t) {
|
|
115
|
-
return await s.getBalancesByWallet(e, t) !== void 0;
|
|
116
|
-
}
|
|
117
|
-
async getBalancesWithUsdValues(e, n) {
|
|
118
|
-
try {
|
|
119
|
-
if (!n) return [];
|
|
120
|
-
let r = e.map((e) => e.token.erc20TokenAddress);
|
|
121
|
-
if (!r.length) return [];
|
|
122
|
-
let i = await c.getAndUpdatePrices(n, r), a = new Map(r.map((e, t) => [e, i[t]]));
|
|
123
|
-
return [...e.map((e) => {
|
|
124
|
-
try {
|
|
125
|
-
let n = e.token.erc20TokenAddress, r = a.get(n) ?? 0, i = e.balance, o = Number(t(e.token, i)) * r;
|
|
126
|
-
return {
|
|
127
|
-
...e,
|
|
128
|
-
usdValue: o
|
|
129
|
-
};
|
|
130
|
-
} catch {
|
|
131
|
-
return {
|
|
132
|
-
...e,
|
|
133
|
-
usdValue: 0
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
})];
|
|
137
|
-
} catch {
|
|
138
|
-
return r.log("Error fetching private token balances"), [];
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
handleCached = async (e, a) => {
|
|
142
|
-
try {
|
|
143
|
-
await n(this.hinkal, e), this.dispatch({
|
|
144
|
-
type: "SET_BALANCES",
|
|
145
|
-
chainId: a,
|
|
146
|
-
balances: void 0
|
|
147
|
-
});
|
|
148
|
-
let r = await s.getBalancesByWallet(e, a), o = await c.getPrices();
|
|
149
|
-
if (await n(this.hinkal, e), !r || (this.dispatch({
|
|
150
|
-
type: "SET_BALANCES",
|
|
151
|
-
chainId: a,
|
|
152
|
-
balances: r
|
|
153
|
-
}), !o)) return;
|
|
154
|
-
let l = r.map((e) => {
|
|
155
|
-
let n = i(e.token.erc20TokenAddress, a);
|
|
156
|
-
return {
|
|
157
|
-
...e,
|
|
158
|
-
usdValue: o[n] ? Number(t(e.token, e.balance)) * o[n] : 0
|
|
159
|
-
};
|
|
160
|
-
});
|
|
161
|
-
this.dispatch({
|
|
162
|
-
type: "SET_USD_BALANCES",
|
|
163
|
-
chainId: a,
|
|
164
|
-
balances: l
|
|
165
|
-
});
|
|
166
|
-
} catch (e) {
|
|
167
|
-
r.error("Error refreshing cached private balance:", e);
|
|
168
|
-
}
|
|
169
|
-
};
|
|
170
|
-
handleFresh = async (e, t, i, a, o, c) => {
|
|
171
|
-
let l = o;
|
|
172
|
-
try {
|
|
173
|
-
await n(this.hinkal, e);
|
|
174
|
-
let r = await this.hasCache(e, t);
|
|
175
|
-
await n(this.hinkal, e), l = o || !r, l && this.dispatch({
|
|
176
|
-
type: "SET_LOADING",
|
|
177
|
-
chainId: t,
|
|
178
|
-
newValue: !0
|
|
179
|
-
}), await n(this.hinkal, e), a && await this.hinkal.resetMerkle([t]);
|
|
180
|
-
let u = await this.hinkal.getTotalBalance(t, i, e, a, c);
|
|
181
|
-
await n(this.hinkal, e);
|
|
182
|
-
let d = u.filter((e) => e.balance > 0n);
|
|
183
|
-
this.dispatch({
|
|
184
|
-
type: "SET_BALANCES",
|
|
185
|
-
chainId: t,
|
|
186
|
-
balances: d
|
|
187
|
-
}), await s.setBalances(e, t, d), await n(this.hinkal, e), l && (this.dispatch({
|
|
188
|
-
type: "SET_USD_LOADING",
|
|
189
|
-
chainId: t,
|
|
190
|
-
newValue: !0
|
|
191
|
-
}), this.dispatch({
|
|
192
|
-
type: "SET_LOADING",
|
|
193
|
-
chainId: t,
|
|
194
|
-
newValue: !1
|
|
195
|
-
})), await n(this.hinkal, e);
|
|
196
|
-
let f = await this.getBalancesWithUsdValues(d, t);
|
|
197
|
-
await n(this.hinkal, e), f.length > 0 && this.dispatch({
|
|
198
|
-
type: "SET_USD_BALANCES",
|
|
199
|
-
chainId: t,
|
|
200
|
-
balances: f
|
|
201
|
-
});
|
|
202
|
-
} catch (e) {
|
|
203
|
-
r.error("Error refreshing private balance:", e);
|
|
204
|
-
} finally {
|
|
205
|
-
l && (this.dispatch({
|
|
206
|
-
type: "SET_LOADING",
|
|
207
|
-
chainId: t,
|
|
208
|
-
newValue: !1
|
|
209
|
-
}), this.dispatch({
|
|
210
|
-
type: "SET_USD_LOADING",
|
|
211
|
-
chainId: t,
|
|
212
|
-
newValue: !1
|
|
213
|
-
}));
|
|
214
|
-
}
|
|
215
|
-
};
|
|
216
|
-
getSupportedChainsToUpdate(e) {
|
|
217
|
-
let t = this.hinkal.getSupportedChains();
|
|
218
|
-
return e ? t.includes(e) ? [e] : [] : t;
|
|
219
|
-
}
|
|
220
|
-
async refreshInternal(t = e.FreshWithoutLoader, n, r = !1, i = !1, a) {
|
|
221
|
-
let { ethAdd: o, userKeys: s } = await this.getAddressAndChain(n);
|
|
222
|
-
!o || !s?.getShieldedPublicKey() || await Promise.all(this.getSupportedChainsToUpdate(a).map(async (n) => {
|
|
223
|
-
switch (t) {
|
|
224
|
-
case e.OnlyCache:
|
|
225
|
-
await this.handleCached(o, n);
|
|
226
|
-
break;
|
|
227
|
-
case e.Fresh:
|
|
228
|
-
await this.handleFresh(o, n, s, r, !0, i);
|
|
229
|
-
break;
|
|
230
|
-
case e.FreshWithoutLoader:
|
|
231
|
-
await this.handleFresh(o, n, s, r, !1, i);
|
|
232
|
-
break;
|
|
233
|
-
default: break;
|
|
234
|
-
}
|
|
235
|
-
}));
|
|
236
|
-
}
|
|
237
|
-
};
|
|
238
|
-
//#endregion
|
|
239
|
-
export { u as HinkalPrivateBalanceController };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const e=require(`../../functions/utils/serialize.utils.cjs`),t=require(`../cacheDevices/FileCacheDevice.cjs`);var n=()=>typeof globalThis<`u`&&`indexedDB`in globalThis&&globalThis.indexedDB!=null,r=class{idbPromise=null;idb(){return this.idbPromise||=import(`idb-keyval`),this.idbPromise}async get(e){let{get:t}=await this.idb();return t(e)}async set(e,t){let{set:n}=await this.idb();await n(e,t)}async update(e,t){let{update:n}=await this.idb(),r;return await n(e,e=>(r=t(e),r)),r}},i=class{device;constructor(){let e=require(`path`);this.device=new t.FileCacheDevice(!0,e.resolve(process.cwd(),`hinkalKeyValCache.json`))}async get(t){let n=this.device.get(t);if(n)return e.CustomJSONParse(n)}async set(t,n){this.device.set(t,e.CustomJSONStringify(n))}async update(e,t){let n=t(await this.get(e));return await this.set(e,n),n}},a=n()?new r:new i;exports.keyValStore=a;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { __require as e } from "../../../../_virtual/_rolldown/runtime.mjs";
|
|
2
|
-
import { CustomJSONParse as t, CustomJSONStringify as n } from "../../functions/utils/serialize.utils.mjs";
|
|
3
|
-
import { FileCacheDevice as r } from "../cacheDevices/FileCacheDevice.mjs";
|
|
4
|
-
//#region libs/shared/common/src/data-structures/IndexedDB/key-val-store.ts
|
|
5
|
-
var i = () => typeof globalThis < "u" && "indexedDB" in globalThis && globalThis.indexedDB != null, a = class {
|
|
6
|
-
idbPromise = null;
|
|
7
|
-
idb() {
|
|
8
|
-
return this.idbPromise ||= import("idb-keyval"), this.idbPromise;
|
|
9
|
-
}
|
|
10
|
-
async get(e) {
|
|
11
|
-
let { get: t } = await this.idb();
|
|
12
|
-
return t(e);
|
|
13
|
-
}
|
|
14
|
-
async set(e, t) {
|
|
15
|
-
let { set: n } = await this.idb();
|
|
16
|
-
await n(e, t);
|
|
17
|
-
}
|
|
18
|
-
async update(e, t) {
|
|
19
|
-
let { update: n } = await this.idb(), r;
|
|
20
|
-
return await n(e, (e) => (r = t(e), r)), r;
|
|
21
|
-
}
|
|
22
|
-
}, o = class {
|
|
23
|
-
device;
|
|
24
|
-
constructor() {
|
|
25
|
-
let t = e("path");
|
|
26
|
-
this.device = new r(!0, t.resolve(process.cwd(), "hinkalKeyValCache.json"));
|
|
27
|
-
}
|
|
28
|
-
async get(e) {
|
|
29
|
-
let n = this.device.get(e);
|
|
30
|
-
if (n) return t(n);
|
|
31
|
-
}
|
|
32
|
-
async set(e, t) {
|
|
33
|
-
this.device.set(e, n(t));
|
|
34
|
-
}
|
|
35
|
-
async update(e, t) {
|
|
36
|
-
let n = t(await this.get(e));
|
|
37
|
-
return await this.set(e, n), n;
|
|
38
|
-
}
|
|
39
|
-
}, s = i() ? new a() : new o();
|
|
40
|
-
//#endregion
|
|
41
|
-
export { s as keyValStore };
|