@sabaaa1/common 0.0.1
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/.env.example +5 -0
- package/.eslintrc.json +18 -0
- package/README.md +164 -0
- package/copyPackageJson.js +81 -0
- package/handleCDNWorkers.js +92 -0
- package/jest.config.ts +10 -0
- package/package.json +101 -0
- package/polyfillWorker.ts +15 -0
- package/project.json +32 -0
- package/src/API/API.ts +203 -0
- package/src/API/admin-calls.ts +8 -0
- package/src/API/ai-calls.ts +48 -0
- package/src/API/brotherhoodCalls.ts +184 -0
- package/src/API/callLifiAPI.ts +41 -0
- package/src/API/callMonitor.ts +9 -0
- package/src/API/callOdosAPI.ts +89 -0
- package/src/API/callOneInchAPI.ts +47 -0
- package/src/API/callRelayer.ts +23 -0
- package/src/API/checkRisk.ts +7 -0
- package/src/API/duneAPI.ts +26 -0
- package/src/API/enclaveCalls.ts +23 -0
- package/src/API/fetchCommitmentsCache.ts +63 -0
- package/src/API/generate-trading-points.ts +77 -0
- package/src/API/get-token-data.ts +12 -0
- package/src/API/getBridgeProtocols.ts +16 -0
- package/src/API/getCoingeckoPrice.ts +59 -0
- package/src/API/getGasEstimates.ts +30 -0
- package/src/API/getLifiStatus.ts +35 -0
- package/src/API/getRelayerURL.ts +27 -0
- package/src/API/getServerURL.ts +116 -0
- package/src/API/getTokenPrice.ts +39 -0
- package/src/API/getWebsiteURL.ts +58 -0
- package/src/API/index.ts +19 -0
- package/src/API/is-tx-stateless.ts +32 -0
- package/src/API/kycCalls.ts +81 -0
- package/src/API/leaderboardCalls.ts +32 -0
- package/src/API/mysteryBoxesCalls.ts +141 -0
- package/src/API/passwordCalls.ts +34 -0
- package/src/API/proxyAccountsCalls.ts +40 -0
- package/src/API/referralsCalls.ts +52 -0
- package/src/API/relayCalls.ts +14 -0
- package/src/API/restoreSnapshots.ts +7 -0
- package/src/API/rewardsUserCalls.ts +124 -0
- package/src/API/send-telegram-callback.ts +17 -0
- package/src/API/simulate-batch-tx.ts +19 -0
- package/src/API/tenderly.api.ts +96 -0
- package/src/API/token-calls.ts +19 -0
- package/src/API/weeklyAnalyticCalls.ts +43 -0
- package/src/constants/activity.constants.ts +10 -0
- package/src/constants/addresses.constants.ts +74 -0
- package/src/constants/assets.constants.ts +15 -0
- package/src/constants/backend.constants.ts +16 -0
- package/src/constants/balance.constants.ts +1 -0
- package/src/constants/bridging.constants.ts +5 -0
- package/src/constants/chains.constants.ts +194 -0
- package/src/constants/coingecko.constants.ts +67 -0
- package/src/constants/contracts.constants.ts +106 -0
- package/src/constants/conversion.constants.ts +1 -0
- package/src/constants/deploy-data/deploy-data-arbMainnet.json +5685 -0
- package/src/constants/deploy-data/deploy-data-arcTestnet.json +6157 -0
- package/src/constants/deploy-data/deploy-data-avalanche.json +5921 -0
- package/src/constants/deploy-data/deploy-data-base.json +5695 -0
- package/src/constants/deploy-data/deploy-data-bnbMainnet.json +5921 -0
- package/src/constants/deploy-data/deploy-data-ethMainnet.json +5922 -0
- package/src/constants/deploy-data/deploy-data-localhost.json +5921 -0
- package/src/constants/deploy-data/deploy-data-optimism.json +5687 -0
- package/src/constants/deploy-data/deploy-data-polygon.json +5695 -0
- package/src/constants/deploy-data/index.ts +21 -0
- package/src/constants/events.constants.ts +10 -0
- package/src/constants/fees.constants.ts +4 -0
- package/src/constants/function-signatures.constants.ts +6 -0
- package/src/constants/index.ts +30 -0
- package/src/constants/kyc.constants.ts +202 -0
- package/src/constants/lifi.constants.ts +1 -0
- package/src/constants/mediaUrls.constants.ts +25 -0
- package/src/constants/mystery-boxes.constants.ts +402 -0
- package/src/constants/oauthUrls.constants.ts +7 -0
- package/src/constants/permit2.constants.ts +28 -0
- package/src/constants/presale.constants.ts +503 -0
- package/src/constants/protocol.constants.ts +65 -0
- package/src/constants/reorg-depths.constants.ts +13 -0
- package/src/constants/rewards.constants.ts +27 -0
- package/src/constants/save-depths.ts +13 -0
- package/src/constants/server.constants.ts +233 -0
- package/src/constants/tasks.constants.ts +205 -0
- package/src/constants/token-data/ERC20Registry.ts +90 -0
- package/src/constants/token-data/arbMainnetRegistry.json +1841 -0
- package/src/constants/token-data/arbMainnetRegistryFixed.json +1841 -0
- package/src/constants/token-data/arcTestnetRegistry.json +436 -0
- package/src/constants/token-data/avalancheRegistry.json +729 -0
- package/src/constants/token-data/avalancheRegistryFixed.json +729 -0
- package/src/constants/token-data/baseRegistry.json +1134 -0
- package/src/constants/token-data/baseRegistryFixed.json +1134 -0
- package/src/constants/token-data/bnbMainnetRegistry.json +900 -0
- package/src/constants/token-data/bnbMainnetRegistryFixed.json +900 -0
- package/src/constants/token-data/ethMainnetRegistry.json +4298 -0
- package/src/constants/token-data/ethMainnetRegistryFixed.json +4298 -0
- package/src/constants/token-data/index.ts +60 -0
- package/src/constants/token-data/localhostRegistry.json +2784 -0
- package/src/constants/token-data/optimismRegistry.json +701 -0
- package/src/constants/token-data/optimismRegistryFixed.json +701 -0
- package/src/constants/token-data/polygonRegistry.json +2309 -0
- package/src/constants/token-data/polygonRegistryFixed.json +2309 -0
- package/src/constants/token-data/popularTokens.constants.ts +11 -0
- package/src/constants/token-data/tokenPricing.consts.ts +2 -0
- package/src/constants/token.limits.constants.ts +31 -0
- package/src/constants/vite.constants.ts +74 -0
- package/src/constants/wallet.constants.ts +9 -0
- package/src/crypto/babyJub.ts +31 -0
- package/src/crypto/index.ts +3 -0
- package/src/crypto/poseidon.ts +36 -0
- package/src/crypto/preProcessing.ts +20 -0
- package/src/data-structures/ApprovalDBs/EventsPublicApprovalsDB.ts +288 -0
- package/src/data-structures/ApprovalDBs/index.ts +1 -0
- package/src/data-structures/Hinkal/Hinkal.ts +746 -0
- package/src/data-structures/Hinkal/IHinkal.ts +315 -0
- package/src/data-structures/Hinkal/handleAutoDepositBack.ts +138 -0
- package/src/data-structures/Hinkal/hinkalActionFundApproveAndTransact.ts +64 -0
- package/src/data-structures/Hinkal/hinkalActionReceive.ts +65 -0
- package/src/data-structures/Hinkal/hinkalActionStake.ts +152 -0
- package/src/data-structures/Hinkal/hinkalApprove.ts +170 -0
- package/src/data-structures/Hinkal/hinkalCheckTokenRegistry.ts +26 -0
- package/src/data-structures/Hinkal/hinkalDeposit.ts +199 -0
- package/src/data-structures/Hinkal/hinkalGetRecipientInfo.ts +12 -0
- package/src/data-structures/Hinkal/hinkalGetZkMeProvider.ts +31 -0
- package/src/data-structures/Hinkal/hinkalInsideTransact.ts +190 -0
- package/src/data-structures/Hinkal/hinkalMultiSend.ts +26 -0
- package/src/data-structures/Hinkal/hinkalPrivateWallet.ts +198 -0
- package/src/data-structures/Hinkal/hinkalProoflessDeposit.ts +91 -0
- package/src/data-structures/Hinkal/hinkalProxySwap.ts +91 -0
- package/src/data-structures/Hinkal/hinkalSignSubAccount.ts +28 -0
- package/src/data-structures/Hinkal/hinkalSwap.ts +157 -0
- package/src/data-structures/Hinkal/hinkalTransfer.ts +171 -0
- package/src/data-structures/Hinkal/hinkalWithdraw.ts +161 -0
- package/src/data-structures/Hinkal/index.ts +9 -0
- package/src/data-structures/Hinkal/resetMerkleTrees.ts +64 -0
- package/src/data-structures/IndexedDB/activity-db.ts +204 -0
- package/src/data-structures/IndexedDB/balances-db.ts +97 -0
- package/src/data-structures/IndexedDB/contact-db.ts +81 -0
- package/src/data-structures/IndexedDB/index.ts +6 -0
- package/src/data-structures/IndexedDB/prices-db.ts +89 -0
- package/src/data-structures/IndexedDB/private-balances-db.ts +31 -0
- package/src/data-structures/IndexedDB/signatures-db.ts +62 -0
- package/src/data-structures/MultiThreadedUtxoUtils/MultiThreadedUtxoUtils.ts +152 -0
- package/src/data-structures/TokenDBs/AlchemyPublicTokensDB.ts +224 -0
- package/src/data-structures/TokenDBs/ArcPublicTokensDB.ts +198 -0
- package/src/data-structures/TokenDBs/EventsPublicTokensDB.ts +216 -0
- package/src/data-structures/TokenDBs/IPublicTokensDB.ts +7 -0
- package/src/data-structures/TokenDBs/PrivateTokensDB.ts +96 -0
- package/src/data-structures/TokenDBs/PublicTokensDB.ts +49 -0
- package/src/data-structures/TokenDBs/index.ts +4 -0
- package/src/data-structures/TokenDBs/token-visibility-db.ts +154 -0
- package/src/data-structures/ValueCache/ValueCache.ts +29 -0
- package/src/data-structures/ValueCache/index.ts +1 -0
- package/src/data-structures/cacheDevices/AttachableMemoryCacheDevice.ts +33 -0
- package/src/data-structures/cacheDevices/BaseCacheDevice.ts +30 -0
- package/src/data-structures/cacheDevices/FileCacheDevice.ts +59 -0
- package/src/data-structures/cacheDevices/LocalStorageCacheDevice.ts +36 -0
- package/src/data-structures/cacheDevices/index.ts +2 -0
- package/src/data-structures/crypto-keys/decodeUTXO.ts +60 -0
- package/src/data-structures/crypto-keys/encryptDecryptUtxo.ts +74 -0
- package/src/data-structures/crypto-keys/index.ts +4 -0
- package/src/data-structures/crypto-keys/keyUtils.ts +8 -0
- package/src/data-structures/crypto-keys/keys.ts +203 -0
- package/src/data-structures/custom-token-registry/CustomTokenRegistry.ts +49 -0
- package/src/data-structures/event-service/AbstractAccessTokenSnapshotService.ts +142 -0
- package/src/data-structures/event-service/AbstractApprovalsSnapshotService.ts +152 -0
- package/src/data-structures/event-service/AbstractCommitmentsSnapshotService.ts +116 -0
- package/src/data-structures/event-service/AbstractEventService.ts +44 -0
- package/src/data-structures/event-service/AbstractNullifierSnapshotService.ts +76 -0
- package/src/data-structures/event-service/AbstractSnapshotService.ts +63 -0
- package/src/data-structures/event-service/BlockchainEventEmitter.ts +132 -0
- package/src/data-structures/event-service/index.ts +7 -0
- package/src/data-structures/index.ts +18 -0
- package/src/data-structures/merkle-tree/MerkleTree.test.ts +77 -0
- package/src/data-structures/merkle-tree/MerkleTree.ts +253 -0
- package/src/data-structures/merkle-tree/MerkleTreeIncompleteError.ts +6 -0
- package/src/data-structures/merkle-tree/getPatchedAccessTokenMerkleTree.ts +24 -0
- package/src/data-structures/merkle-tree/index.ts +3 -0
- package/src/data-structures/presale/PresaleContractWrapper.ts +80 -0
- package/src/data-structures/presale/index.ts +1 -0
- package/src/data-structures/provider-adapter/IProviderAdapter.ts +43 -0
- package/src/data-structures/snapshot/ClientAccessTokenSnapshotService.ts +41 -0
- package/src/data-structures/snapshot/ClientApprovalsSnapshotService.ts +36 -0
- package/src/data-structures/snapshot/ClientCommitmentsSnapshotService.ts +30 -0
- package/src/data-structures/snapshot/ClientNullifierSnapshotService.ts +28 -0
- package/src/data-structures/snapshot/SnapshotFetcherService.ts +68 -0
- package/src/data-structures/snapshot/index.ts +1 -0
- package/src/data-structures/token-price-fetcher/TokenChecker.ts +45 -0
- package/src/data-structures/token-price-fetcher/index.ts +1 -0
- package/src/data-structures/tor/HttpClient.ts +118 -0
- package/src/data-structures/tor/TorAdapter.ts +94 -0
- package/src/data-structures/tor/TorClient.ts +283 -0
- package/src/data-structures/tor/WebSocketDuplex.ts +32 -0
- package/src/data-structures/tor/index.ts +3 -0
- package/src/data-structures/transactions-manager/history/history.types.ts +14 -0
- package/src/data-structures/transactions-manager/index.ts +1 -0
- package/src/data-structures/utxo/Utxo.ts +189 -0
- package/src/error-handling/customErrors/ErrorWithAmount.ts +8 -0
- package/src/error-handling/customErrors/ErrorWithTx.ts +10 -0
- package/src/error-handling/customErrors/FeeOverTransactionValueError.ts +16 -0
- package/src/error-handling/customErrors/SimulationFailureError.ts +9 -0
- package/src/error-handling/customErrors/customErrors.helpers.ts +39 -0
- package/src/error-handling/customErrors/index.ts +5 -0
- package/src/error-handling/error-codes.constants.ts +214 -0
- package/src/error-handling/get-error.message.ts +149 -0
- package/src/error-handling/handleErrorRestore.ts +25 -0
- package/src/error-handling/index.ts +7 -0
- package/src/error-handling/logError.ts +8 -0
- package/src/error-handling/logger.ts +21 -0
- package/src/error-handling/types.ts +10 -0
- package/src/externalABIs/BUSD.ts +516 -0
- package/src/externalABIs/BabPassport.json +24 -0
- package/src/externalABIs/DAI.ts +331 -0
- package/src/externalABIs/ERC1155.json +316 -0
- package/src/externalABIs/ERC20.json +297 -0
- package/src/externalABIs/ERC20.ts +1 -0
- package/src/externalABIs/ERC721.json +205 -0
- package/src/externalABIs/GalxePassport.json +23 -0
- package/src/externalABIs/IQuoterV2.json +211 -0
- package/src/externalABIs/ISwapRouter.json +568 -0
- package/src/externalABIs/IUniswapV3Factory.json +207 -0
- package/src/externalABIs/IUniswapV3Pool.json +999 -0
- package/src/externalABIs/NodeInterfaceABI.json +1 -0
- package/src/externalABIs/OptimismGasPriceOracle.json +1 -0
- package/src/externalABIs/PoLidoNftAbi.json +307 -0
- package/src/externalABIs/SanctionsList.ts +86 -0
- package/src/externalABIs/USDC.ts +718 -0
- package/src/externalABIs/USDR.ts +343 -0
- package/src/externalABIs/USDR3CRV.ts +539 -0
- package/src/externalABIs/USDT.ts +390 -0
- package/src/externalABIs/UniswapV2PoolAbi.json +66 -0
- package/src/externalABIs/WETH.ts +153 -0
- package/src/externalABIs/amToken.ts +347 -0
- package/src/externalABIs/index.ts +53 -0
- package/src/externalABIs/swapAbi.ts +442 -0
- package/src/externalABIs/transactionsProver.json +45 -0
- package/src/functions/index.ts +16 -0
- package/src/functions/kyc/aiPriseHelper.ts +0 -0
- package/src/functions/kyc/authentoHelper.ts +44 -0
- package/src/functions/kyc/index.ts +3 -0
- package/src/functions/kyc/openDefaultPassportWindow.ts +9 -0
- package/src/functions/kyc/passportHelper.ts +13 -0
- package/src/functions/kyc/zkMeHelper.ts +69 -0
- package/src/functions/pre-transaction/constructAdminData.ts +21 -0
- package/src/functions/pre-transaction/getExternalSwapAddress.ts +30 -0
- package/src/functions/pre-transaction/getFeeStructure.ts +44 -0
- package/src/functions/pre-transaction/getFlatFees.ts +88 -0
- package/src/functions/pre-transaction/getSignatureDataForTransact.ts +16 -0
- package/src/functions/pre-transaction/getVolatileTransferAmount.ts +56 -0
- package/src/functions/pre-transaction/index.ts +7 -0
- package/src/functions/pre-transaction/interaction-to-action.ts +17 -0
- package/src/functions/pre-transaction/merge-with-fee-structure-emporium.ts +50 -0
- package/src/functions/pre-transaction/merge-with-fee-structure.ts +35 -0
- package/src/functions/pre-transaction/outputApprovalDataProcessing.ts +95 -0
- package/src/functions/pre-transaction/outputUtxoProcessing.ts +52 -0
- package/src/functions/pre-transaction/process-gas-estimates.ts +36 -0
- package/src/functions/pre-transaction/shouldPatchAccessTokenMerkleTree.ts +15 -0
- package/src/functions/private-wallet/emporium.helpers.ts +296 -0
- package/src/functions/private-wallet/emporium.swap.helpers.ts +284 -0
- package/src/functions/private-wallet/index.ts +3 -0
- package/src/functions/private-wallet/opProducer.ts +33 -0
- package/src/functions/snarkjs/common.snarkjs.ts +377 -0
- package/src/functions/snarkjs/constant.ts +490 -0
- package/src/functions/snarkjs/constructEmporiumProof.ts +301 -0
- package/src/functions/snarkjs/constructGeneralZkProof.ts +281 -0
- package/src/functions/snarkjs/generateCircomData.ts +81 -0
- package/src/functions/snarkjs/generateZkProof.ts +21 -0
- package/src/functions/snarkjs/generateZkProofEnclave.ts +45 -0
- package/src/functions/snarkjs/generateZkProofSelf.ts +29 -0
- package/src/functions/snarkjs/getOriginalSender.ts +4 -0
- package/src/functions/snarkjs/getZKFiles.ts +29 -0
- package/src/functions/snarkjs/index.ts +4 -0
- package/src/functions/staking/index.ts +78 -0
- package/src/functions/utils/addresses.ts +37 -0
- package/src/functions/utils/amounts.utils.test.ts +91 -0
- package/src/functions/utils/amounts.utils.ts +119 -0
- package/src/functions/utils/arraysMatch.ts +4 -0
- package/src/functions/utils/cacheDevice.utils.ts +21 -0
- package/src/functions/utils/cacheFunctions.ts +154 -0
- package/src/functions/utils/caseInsensitive.utils.ts +10 -0
- package/src/functions/utils/convertEmporiumOpToCallInfo.ts +19 -0
- package/src/functions/utils/convertIntegrationProviderToExternalActionId.ts +14 -0
- package/src/functions/utils/create-provider.ts +11 -0
- package/src/functions/utils/encodeTokenWithId.ts +4 -0
- package/src/functions/utils/encryptInputForEnclave.ts +119 -0
- package/src/functions/utils/enum.utils.ts +30 -0
- package/src/functions/utils/erc20tokenFunctions.ts +110 -0
- package/src/functions/utils/ethers-formatter.utils.ts +32 -0
- package/src/functions/utils/evmNetworkFunctions.ts +9 -0
- package/src/functions/utils/external-action.utils.ts +15 -0
- package/src/functions/utils/fees.utils.ts +26 -0
- package/src/functions/utils/get-hinkal-approvals.ts +36 -0
- package/src/functions/utils/get-signature-header.ts +5 -0
- package/src/functions/utils/getBlockExplorerUrl.ts +14 -0
- package/src/functions/utils/getDataFromTransaction.ts +79 -0
- package/src/functions/utils/getRecipientInfoFromUserKeys.ts +28 -0
- package/src/functions/utils/inLogicMetadata.ts +9 -0
- package/src/functions/utils/index.ts +43 -0
- package/src/functions/utils/involves-permit2-op.ts +17 -0
- package/src/functions/utils/ipfs.ts +74 -0
- package/src/functions/utils/is-valid-url.ts +5 -0
- package/src/functions/utils/lifi.utils.ts +3 -0
- package/src/functions/utils/memoize.utils.ts +23 -0
- package/src/functions/utils/merkleTree.utils.ts +27 -0
- package/src/functions/utils/mutexes.utils.ts +3 -0
- package/src/functions/utils/mystery-box-auction.utils.ts +61 -0
- package/src/functions/utils/networks.utils.ts +6 -0
- package/src/functions/utils/nftTokenFunctions.ts +25 -0
- package/src/functions/utils/nickname.utils.ts +4 -0
- package/src/functions/utils/postToOffscreen.ts +6 -0
- package/src/functions/utils/prepareHinkal.ts +78 -0
- package/src/functions/utils/process.utils.ts +23 -0
- package/src/functions/utils/processUseApprovalUtxoData.ts +26 -0
- package/src/functions/utils/publicBalance.utils.ts +131 -0
- package/src/functions/utils/reloadPage.ts +1 -0
- package/src/functions/utils/replaceAddressInCalldata.ts +6 -0
- package/src/functions/utils/requireEnv.ts +10 -0
- package/src/functions/utils/resolve-sync.utils.ts +40 -0
- package/src/functions/utils/rpc-int-encode.ts +7 -0
- package/src/functions/utils/serialize.utils.ts +12 -0
- package/src/functions/utils/string.utils.ts +92 -0
- package/src/functions/utils/time.utils.ts +80 -0
- package/src/functions/utils/token-check.utils.ts +12 -0
- package/src/functions/utils/trimFieldValues.ts +24 -0
- package/src/functions/utils/upToDateState.ts +12 -0
- package/src/functions/utils/userAgent.ts +26 -0
- package/src/functions/utils/walletBalances.utils.ts +117 -0
- package/src/functions/web3/EIP-712.test.ts +302 -0
- package/src/functions/web3/EIP-712.ts +243 -0
- package/src/functions/web3/etherFunctions.test.ts +129 -0
- package/src/functions/web3/etherFunctions.ts +84 -0
- package/src/functions/web3/events/balanceChangedCustomHandler.ts +13 -0
- package/src/functions/web3/events/balanceChangedHandler.ts +10 -0
- package/src/functions/web3/events/getApprovedBalance.ts +154 -0
- package/src/functions/web3/events/getInputUtxoAndBalance.ts +305 -0
- package/src/functions/web3/events/getInputUtxosEnclave.ts +40 -0
- package/src/functions/web3/events/getShieldedBalance.ts +163 -0
- package/src/functions/web3/events/getTransactionLogEvents.ts +55 -0
- package/src/functions/web3/events/index.ts +7 -0
- package/src/functions/web3/events/web3RetrieveEvents.ts +33 -0
- package/src/functions/web3/functionCalls/accessTokenCalls.ts +27 -0
- package/src/functions/web3/functionCalls/approveToken.ts +180 -0
- package/src/functions/web3/functionCalls/approveTokensToHinkal.ts +21 -0
- package/src/functions/web3/functionCalls/estimateGasRelayer.ts +40 -0
- package/src/functions/web3/functionCalls/getRootHash.ts +7 -0
- package/src/functions/web3/functionCalls/inHinkalApprovalCalls.ts +60 -0
- package/src/functions/web3/functionCalls/relayFunctions.ts +12 -0
- package/src/functions/web3/functionCalls/transactCallDirect.ts +71 -0
- package/src/functions/web3/functionCalls/transactCallRelayer.ts +57 -0
- package/src/functions/web3/getContractMetadata.ts +43 -0
- package/src/functions/web3/getPublicAddressBalance.ts +17 -0
- package/src/functions/web3/getTokenHolder.ts +53 -0
- package/src/functions/web3/index.ts +9 -0
- package/src/functions/web3/lifiAPI.ts +41 -0
- package/src/functions/web3/odosAPI.ts +50 -0
- package/src/functions/web3/oneInchAPI.ts +47 -0
- package/src/functions/web3/runContractFunction.ts +116 -0
- package/src/functions/web3/uniswapAPI.ts +156 -0
- package/src/index.ts +9 -0
- package/src/mutexes/index.ts +1 -0
- package/src/mutexes/mutex.ts +3 -0
- package/src/providers/EthersProviderAdapter.ts +230 -0
- package/src/providers/TorExternalProvider.ts +36 -0
- package/src/providers/WagmiProviderAdapter.ts +311 -0
- package/src/providers/exportProviers.ts +11 -0
- package/src/providers/prepareEthersHinkal.ts +18 -0
- package/src/providers/prepareWagmiHinkal.ts +16 -0
- package/src/types/API.types.ts +23 -0
- package/src/types/ICacheDevice.ts +5 -0
- package/src/types/IMultiThreadedUtxoUtils.ts +16 -0
- package/src/types/RewardUserEnums.ts +12 -0
- package/src/types/TransactionSimulator.types.ts +30 -0
- package/src/types/WeeklyAnalytics.types.ts +44 -0
- package/src/types/activities.types.ts +90 -0
- package/src/types/admin.types.ts +30 -0
- package/src/types/all-points.types.ts +8 -0
- package/src/types/approvals.types.ts +45 -0
- package/src/types/balances.types.ts +20 -0
- package/src/types/big-intable.types.ts +3 -0
- package/src/types/bridging.types.ts +30 -0
- package/src/types/brotherhood-user.types.ts +13 -0
- package/src/types/cache.types.ts +42 -0
- package/src/types/circom-data.types.ts +120 -0
- package/src/types/coingecko.types.ts +216 -0
- package/src/types/commitments.types.ts +39 -0
- package/src/types/confirmation-request.types.ts +10 -0
- package/src/types/contacts.types.ts +6 -0
- package/src/types/crypto.types.ts +8 -0
- package/src/types/duneAPI.types.ts +9 -0
- package/src/types/eip5792.types.ts +31 -0
- package/src/types/ethereum-network.types.ts +70 -0
- package/src/types/external-action.types.ts +19 -0
- package/src/types/fee.types.ts +11 -0
- package/src/types/generatePoints.ts +14 -0
- package/src/types/hinkal.stake.types.ts +12 -0
- package/src/types/hinkal.types.ts +150 -0
- package/src/types/index.ts +50 -0
- package/src/types/kyc.types.ts +49 -0
- package/src/types/merch-order.types.ts +18 -0
- package/src/types/message.types.ts +6 -0
- package/src/types/mystery-boxes.types.ts +50 -0
- package/src/types/new-rewards.type.ts +181 -0
- package/src/types/offscreen.types.ts +8 -0
- package/src/types/presaleReferral.types.ts +4 -0
- package/src/types/proxy.types.ts +11 -0
- package/src/types/referral.types.ts +13 -0
- package/src/types/relayer.types.ts +16 -0
- package/src/types/remote-proof.types.ts +18 -0
- package/src/types/rewards.types.ts +53 -0
- package/src/types/routing.types.ts +7 -0
- package/src/types/sandbox.types.ts +246 -0
- package/src/types/session.types.ts +12 -0
- package/src/types/signature.types.ts +11 -0
- package/src/types/slippage.types.ts +3 -0
- package/src/types/snark.types.ts +43 -0
- package/src/types/tenderly.api.types.ts +200 -0
- package/src/types/token-prices.types.ts +8 -0
- package/src/types/token-with-id.types.ts +4 -0
- package/src/types/token.types.ts +86 -0
- package/src/types/transactions.types.ts +120 -0
- package/src/types/wc.types.ts +83 -0
- package/src/types/with-id.types.ts +1 -0
- package/src/webworker/performTaskWithWorker.ts +50 -0
- package/src/webworker/snarkjsWorker/snarkjs.d.ts +2 -0
- package/src/webworker/snarkjsWorker/snarkjsWorker.types.ts +18 -0
- package/src/webworker/snarkjsWorker/snarkjsWorkerLauncher.ts +19 -0
- package/src/webworker/snarkjsWorker/snarkjsWorkerLogic.ts +90 -0
- package/src/webworker/utxoWorker/utxoWorker.types.ts +41 -0
- package/src/webworker/utxoWorker/utxoWorkerLauncher.ts +19 -0
- package/src/webworker/utxoWorker/utxoWorkerLogic.ts +107 -0
- package/src/webworker/viteWorkerURL.constant.ts +25 -0
- package/src/webworker/worker.registry.ts +11 -0
- package/src/webworker/worker.types.ts +35 -0
- package/src/webworker/workerErrorHandler.ts +10 -0
- package/src/webworker/workerFactory.ts +47 -0
- package/src/webworker/workerProxy.ts +88 -0
- package/src/webworker/zkProofWorker/zkProofWorker.types.ts +52 -0
- package/src/webworker/zkProofWorker/zkProofWorkerLauncher.ts +19 -0
- package/src/webworker/zkProofWorker/zkProofWorkerLogic.ts +84 -0
- package/tsconfig.json +23 -0
- package/tsconfig.lib.json +10 -0
- package/tsconfig.spec.json +15 -0
- package/vite.config.ts +97 -0
- package/vite.config.workers.ts +51 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { get, update } from 'idb-keyval';
|
|
3
|
+
import { API } from '../../API/API';
|
|
4
|
+
import { networkRegistry } from '../../constants/chains.constants';
|
|
5
|
+
import { Logger } from '../../error-handling/logger';
|
|
6
|
+
import { lowerCaseIncludes } from '../../functions/utils/caseInsensitive.utils';
|
|
7
|
+
import { StorageKeys } from '../../types/cache.types';
|
|
8
|
+
import { ERC20Token } from '../../types/token.types';
|
|
9
|
+
import { getStateKey } from '../../functions/utils/string.utils';
|
|
10
|
+
import { zeroAddress } from '../../constants/protocol.constants';
|
|
11
|
+
import { Mutex } from 'async-mutex';
|
|
12
|
+
import { IPublicTokensDB } from './IPublicTokensDB';
|
|
13
|
+
import { isNFTToken } from '../../functions/utils/erc20tokenFunctions';
|
|
14
|
+
import { tokenVisibilityStorage } from './token-visibility-db';
|
|
15
|
+
import { createTorRpcProvider } from '../../functions';
|
|
16
|
+
import { EVENTS_INITIAL_BLOCKS } from '../../constants';
|
|
17
|
+
|
|
18
|
+
export class EventsPublicTokensDB implements IPublicTokensDB {
|
|
19
|
+
private fetchedTokens: Record<string, ERC20Token[]> = {};
|
|
20
|
+
|
|
21
|
+
private updateMutexes: Record<string, Mutex> = {};
|
|
22
|
+
|
|
23
|
+
private readonly transferEventTopic = ethers.utils.id('Transfer(address,address,uint256)');
|
|
24
|
+
|
|
25
|
+
private getMutex(stateKey: string): Mutex {
|
|
26
|
+
if (!this.updateMutexes[stateKey]) {
|
|
27
|
+
this.updateMutexes[stateKey] = new Mutex();
|
|
28
|
+
}
|
|
29
|
+
return this.updateMutexes[stateKey];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public async getPublicTokens(chainId: number, walletAddress: string): Promise<ERC20Token[]> {
|
|
33
|
+
const key = getStateKey(walletAddress, chainId);
|
|
34
|
+
const publicTokens = await get<Record<string, ERC20Token[]>>(StorageKeys.EVENTS_PUBLIC_TOKENS);
|
|
35
|
+
return publicTokens?.[key] || [];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public getFetchedTokens(chainId: number, walletAddress: string): ERC20Token[] {
|
|
39
|
+
const key = getStateKey(walletAddress, chainId);
|
|
40
|
+
return this.fetchedTokens[key] || [];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public async fetchAndUpdatePublicTokens(walletAddress: string, chainId: number) {
|
|
44
|
+
const stateKey = getStateKey(walletAddress, chainId);
|
|
45
|
+
const mutex = this.getMutex(stateKey);
|
|
46
|
+
|
|
47
|
+
await mutex.runExclusive(async () => {
|
|
48
|
+
try {
|
|
49
|
+
const publicTokens = await this.getPublicTokens(chainId, walletAddress);
|
|
50
|
+
const publicTokensAddresses = publicTokens.map((token) => token.erc20TokenAddress);
|
|
51
|
+
|
|
52
|
+
const { toEvents, fromEvents, latestBlock } = await this.getEvents(walletAddress, chainId);
|
|
53
|
+
|
|
54
|
+
const allEvents = [...toEvents, ...fromEvents];
|
|
55
|
+
|
|
56
|
+
const { erc20Tokens } = await this.fetchPublicTokens(allEvents, chainId, publicTokensAddresses);
|
|
57
|
+
|
|
58
|
+
const hiddenTokenAddresses = await tokenVisibilityStorage.hideSpamsAndFetchHiddenTokensAddresses(
|
|
59
|
+
chainId,
|
|
60
|
+
erc20Tokens,
|
|
61
|
+
);
|
|
62
|
+
const nonHiddenErc20Tokens = erc20Tokens.filter(
|
|
63
|
+
(token) => !lowerCaseIncludes(hiddenTokenAddresses, token.erc20TokenAddress), // filter out hidden tokens
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
await update(StorageKeys.EVENTS_PUBLIC_TOKENS, (pubTokens: Record<string, ERC20Token[]> = {}) => {
|
|
67
|
+
const currentTokens = pubTokens[stateKey] || [];
|
|
68
|
+
const currentTokensAddresses = currentTokens.map((t) => t.erc20TokenAddress);
|
|
69
|
+
const newTokens = nonHiddenErc20Tokens.filter(
|
|
70
|
+
(token) => !lowerCaseIncludes(currentTokensAddresses, token.erc20TokenAddress),
|
|
71
|
+
);
|
|
72
|
+
currentTokens.push(...newTokens);
|
|
73
|
+
|
|
74
|
+
const updatedTokensWithIds = this.updateTokenIds(fromEvents, toEvents, currentTokens);
|
|
75
|
+
|
|
76
|
+
const newPublicTokens = {
|
|
77
|
+
...pubTokens,
|
|
78
|
+
[stateKey]: updatedTokensWithIds,
|
|
79
|
+
};
|
|
80
|
+
this.fetchedTokens = newPublicTokens;
|
|
81
|
+
return newPublicTokens;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Only update the last processed block if we successfully fetched events
|
|
85
|
+
if (latestBlock) {
|
|
86
|
+
await this.updateLastProcessedBlock(chainId, walletAddress, latestBlock);
|
|
87
|
+
}
|
|
88
|
+
} catch (error: unknown) {
|
|
89
|
+
Logger.error('public tokens db error', error);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private async getLastProcessedBlock(chainId: number, walletAddress: string): Promise<number> {
|
|
95
|
+
const lastBlocks = (await get<Record<string, number>>(StorageKeys.PUBLIC_TOKENS_LAST_PROCESSED_BLOCKS)) || {};
|
|
96
|
+
const key = getStateKey(walletAddress, chainId);
|
|
97
|
+
return lastBlocks[key] || EVENTS_INITIAL_BLOCKS[chainId];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private async updateLastProcessedBlock(chainId: number, walletAddress: string, blockNumber: number): Promise<void> {
|
|
101
|
+
await update(StorageKeys.PUBLIC_TOKENS_LAST_PROCESSED_BLOCKS, (lastBlocks: Record<string, number> = {}) => {
|
|
102
|
+
const key = getStateKey(walletAddress, chainId);
|
|
103
|
+
return {
|
|
104
|
+
...lastBlocks,
|
|
105
|
+
[key]: blockNumber,
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private async getEvents(walletAddress: string, chainId: number) {
|
|
111
|
+
try {
|
|
112
|
+
const { fetchRpcUrl } = networkRegistry[chainId];
|
|
113
|
+
if (!fetchRpcUrl) throw Error('No fetchRpcUrl in PublicTokensDB');
|
|
114
|
+
const provider = createTorRpcProvider(fetchRpcUrl);
|
|
115
|
+
|
|
116
|
+
const fromBlock = (await this.getLastProcessedBlock(chainId, walletAddress)) + 1;
|
|
117
|
+
const latestBlock = await provider.getBlockNumber();
|
|
118
|
+
|
|
119
|
+
const paddedWalletAddress = ethers.utils.hexZeroPad(walletAddress.toLowerCase(), 32);
|
|
120
|
+
|
|
121
|
+
const filterTo = {
|
|
122
|
+
topics: [this.transferEventTopic, null, paddedWalletAddress],
|
|
123
|
+
fromBlock,
|
|
124
|
+
toBlock: latestBlock,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const filterFrom = {
|
|
128
|
+
topics: [this.transferEventTopic, paddedWalletAddress, null],
|
|
129
|
+
fromBlock,
|
|
130
|
+
toBlock: latestBlock,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const [toEvents, fromEvents] = await Promise.all([provider.getLogs(filterTo), provider.getLogs(filterFrom)]);
|
|
134
|
+
return { toEvents, fromEvents, latestBlock };
|
|
135
|
+
} catch (error) {
|
|
136
|
+
Logger.error('Error fetching events:', error);
|
|
137
|
+
return { toEvents: [], fromEvents: [], latestBlock: undefined };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// First get existed tokenIds from tokens
|
|
142
|
+
// Then add new tokenIds from toEvents (incoming transfers)
|
|
143
|
+
// Then remove tokenIds from fromEvents (outgoing transfers)
|
|
144
|
+
private updateTokenIds(fromEvents: ethers.providers.Log[], toEvents: ethers.providers.Log[], tokens: ERC20Token[]) {
|
|
145
|
+
const tokenIdsByAddress = new Map<string, string[]>();
|
|
146
|
+
|
|
147
|
+
tokens.forEach((token) => {
|
|
148
|
+
if (isNFTToken(token) && token.tokenIds) {
|
|
149
|
+
const address = token.erc20TokenAddress.toLowerCase();
|
|
150
|
+
tokenIdsByAddress.set(address, [...token.tokenIds]);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
toEvents.forEach((event) => {
|
|
155
|
+
if (event.topics.length <= 3) return;
|
|
156
|
+
|
|
157
|
+
const address = event.address.toLowerCase();
|
|
158
|
+
const tokenId = ethers.BigNumber.from(event.topics[3]).toString();
|
|
159
|
+
|
|
160
|
+
if (!tokenIdsByAddress.has(address)) {
|
|
161
|
+
tokenIdsByAddress.set(address, []);
|
|
162
|
+
}
|
|
163
|
+
tokenIdsByAddress.get(address)?.push(tokenId);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
fromEvents.forEach((event) => {
|
|
167
|
+
if (event.topics.length <= 3) return;
|
|
168
|
+
|
|
169
|
+
const address = event.address.toLowerCase();
|
|
170
|
+
const tokenArray = tokenIdsByAddress.get(address);
|
|
171
|
+
|
|
172
|
+
if (tokenArray?.length) {
|
|
173
|
+
const tokenId = ethers.BigNumber.from(event.topics[3]).toString();
|
|
174
|
+
const idx = tokenArray.indexOf(tokenId);
|
|
175
|
+
if (idx !== -1) {
|
|
176
|
+
tokenArray.splice(idx, 1);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
tokens.forEach((token) => {
|
|
182
|
+
if (isNFTToken(token)) {
|
|
183
|
+
const address = token.erc20TokenAddress.toLowerCase();
|
|
184
|
+
const tokenArray = tokenIdsByAddress.get(address);
|
|
185
|
+
token.tokenIds = tokenArray?.length && tokenArray.length > 0 ? tokenArray : undefined;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
return tokens;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private async fetchPublicTokens(
|
|
193
|
+
allEvents: ethers.providers.Log[],
|
|
194
|
+
chainId: number,
|
|
195
|
+
publicTokensAddresses: string[],
|
|
196
|
+
): Promise<{ erc20Tokens: ERC20Token[] }> {
|
|
197
|
+
try {
|
|
198
|
+
const tokenAddresses = [zeroAddress, ...new Set(allEvents.map((event) => event.address))].filter(
|
|
199
|
+
(address) => address && !lowerCaseIncludes(publicTokensAddresses, address),
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
if (!tokenAddresses.length) return { erc20Tokens: [] };
|
|
203
|
+
|
|
204
|
+
const erc20Tokens = (await API.tokensInfoCall(chainId, tokenAddresses)).filter(
|
|
205
|
+
(token): token is ERC20Token => !!token,
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
return { erc20Tokens };
|
|
209
|
+
} catch (error) {
|
|
210
|
+
Logger.error('Error fetching public tokens:', error);
|
|
211
|
+
return { erc20Tokens: [] };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export const eventsPublicTokensDB = new EventsPublicTokensDB();
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ERC20Token } from '../../types/token.types';
|
|
2
|
+
|
|
3
|
+
export interface IPublicTokensDB {
|
|
4
|
+
getPublicTokens(chainId: number, walletAddress: string): Promise<ERC20Token[]>;
|
|
5
|
+
getFetchedTokens(chainId: number, walletAddress: string): ERC20Token[];
|
|
6
|
+
fetchAndUpdatePublicTokens(walletAddress: string, chainId: number): Promise<void>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { get, update } from 'idb-keyval';
|
|
2
|
+
import { API } from '../../API/API';
|
|
3
|
+
import { Logger } from '../../error-handling/logger';
|
|
4
|
+
import { lowerCaseIncludes } from '../../functions/utils/caseInsensitive.utils';
|
|
5
|
+
import { ERC20Token } from '../../types/token.types';
|
|
6
|
+
import { StorageKeys } from '../../types/cache.types';
|
|
7
|
+
import { getStateKey } from '../../functions/utils/string.utils';
|
|
8
|
+
import { tokenVisibilityStorage } from './token-visibility-db';
|
|
9
|
+
|
|
10
|
+
export class PrivateTokensDB {
|
|
11
|
+
private fetchedTokens: Record<string, ERC20Token[]> = {};
|
|
12
|
+
|
|
13
|
+
public initalFetched = false;
|
|
14
|
+
|
|
15
|
+
public async updateFetchedTokens() {
|
|
16
|
+
const privateTokens = await get<Record<string, ERC20Token[]>>(StorageKeys.PRIVATE_TOKENS);
|
|
17
|
+
this.fetchedTokens = privateTokens ?? {};
|
|
18
|
+
this.initalFetched = true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async getPrivateTokens(chainId: number, walletAddress: string): Promise<ERC20Token[]> {
|
|
22
|
+
const key = getStateKey(walletAddress, chainId);
|
|
23
|
+
const privateTokens = await get<Record<string, ERC20Token[]>>(StorageKeys.PRIVATE_TOKENS);
|
|
24
|
+
return privateTokens?.[key] || [];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private async fetchPrivateTokens(
|
|
28
|
+
chainId: number,
|
|
29
|
+
erc20Addresses: string[],
|
|
30
|
+
privateTokensAddresses: string[],
|
|
31
|
+
): Promise<ERC20Token[]> {
|
|
32
|
+
try {
|
|
33
|
+
const tokenAddresses = [...new Set(erc20Addresses.map((address) => address.toLowerCase()))].filter(
|
|
34
|
+
(address) => address && !lowerCaseIncludes(privateTokensAddresses, address),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
if (!tokenAddresses.length) return [];
|
|
38
|
+
|
|
39
|
+
const erc20Tokens = (await API.tokensInfoCall(chainId, tokenAddresses)).filter(
|
|
40
|
+
(token): token is ERC20Token => !!token,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return erc20Tokens;
|
|
44
|
+
} catch (error) {
|
|
45
|
+
Logger.error('Error fetching private tokens:', error);
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async fetchAndUpdatePrivateTokens(
|
|
51
|
+
erc20Addresses: string[],
|
|
52
|
+
chainId: number,
|
|
53
|
+
walletAddress: string,
|
|
54
|
+
preFetchedTokens?: ERC20Token[],
|
|
55
|
+
) {
|
|
56
|
+
const privateTokens = await this.getPrivateTokens(chainId, walletAddress);
|
|
57
|
+
const privateTokensAddresses = privateTokens.map((token) => token.erc20TokenAddress);
|
|
58
|
+
|
|
59
|
+
const erc20Tokens =
|
|
60
|
+
preFetchedTokens ?? (await this.fetchPrivateTokens(chainId, erc20Addresses, privateTokensAddresses));
|
|
61
|
+
|
|
62
|
+
const hiddenTokenAddresses = await tokenVisibilityStorage.hideSpamsAndFetchHiddenTokensAddresses(
|
|
63
|
+
chainId,
|
|
64
|
+
erc20Tokens,
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
const key = getStateKey(walletAddress, chainId);
|
|
68
|
+
|
|
69
|
+
await update(StorageKeys.PRIVATE_TOKENS, (privTokens: Record<string, ERC20Token[]> = {}) => {
|
|
70
|
+
const currentTokens = privTokens?.[key] || [];
|
|
71
|
+
const currentTokensAddresses = currentTokens.map((t) => t.erc20TokenAddress);
|
|
72
|
+
const newTokens = erc20Tokens.filter(
|
|
73
|
+
(token) => !lowerCaseIncludes(currentTokensAddresses, token.erc20TokenAddress),
|
|
74
|
+
);
|
|
75
|
+
currentTokens.push(...newTokens);
|
|
76
|
+
|
|
77
|
+
const nonHiddenErc20Tokens = currentTokens.filter(
|
|
78
|
+
(token) => !lowerCaseIncludes(hiddenTokenAddresses, token.erc20TokenAddress), // filter out hidden tokens
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const newPrivateTokens = {
|
|
82
|
+
...privTokens,
|
|
83
|
+
[key]: nonHiddenErc20Tokens,
|
|
84
|
+
};
|
|
85
|
+
this.fetchedTokens = newPrivateTokens;
|
|
86
|
+
return newPrivateTokens;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
public getFetchedTokens(chainId: number, walletAddress: string): ERC20Token[] {
|
|
91
|
+
const key = getStateKey(walletAddress, chainId);
|
|
92
|
+
return this.fetchedTokens[key] || [];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const privateTokensDB = new PrivateTokensDB();
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ERC20Token } from '../../types/token.types';
|
|
2
|
+
import { alchemyPublicTokensDB } from './AlchemyPublicTokensDB';
|
|
3
|
+
import { IPublicTokensDB } from './IPublicTokensDB';
|
|
4
|
+
import { eventsPublicTokensDB } from './EventsPublicTokensDB';
|
|
5
|
+
import { getStateKey } from '../../functions/utils/string.utils';
|
|
6
|
+
import { arcPublicTokensDB } from './ArcPublicTokensDB';
|
|
7
|
+
import { chainIds } from '../../constants/chains.constants';
|
|
8
|
+
|
|
9
|
+
export class PublicTokensDB implements IPublicTokensDB {
|
|
10
|
+
private useAlternativeDB: Record<string, boolean> = {};
|
|
11
|
+
|
|
12
|
+
public async getPublicTokens(chainId: number, walletAddress: string): Promise<ERC20Token[]> {
|
|
13
|
+
const state = getStateKey(walletAddress, chainId);
|
|
14
|
+
if (chainId === chainIds.arcTestnet) return arcPublicTokensDB.getPublicTokens(walletAddress);
|
|
15
|
+
|
|
16
|
+
return this.useAlternativeDB[state]
|
|
17
|
+
? eventsPublicTokensDB.getPublicTokens(chainId, walletAddress)
|
|
18
|
+
: alchemyPublicTokensDB.getPublicTokens(chainId, walletAddress);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public getFetchedTokens(chainId: number, walletAddress: string): ERC20Token[] {
|
|
22
|
+
const state = getStateKey(walletAddress, chainId);
|
|
23
|
+
if (chainId === chainIds.arcTestnet) return arcPublicTokensDB.getFetchedTokens(walletAddress);
|
|
24
|
+
|
|
25
|
+
return this.useAlternativeDB[state]
|
|
26
|
+
? eventsPublicTokensDB.getFetchedTokens(chainId, walletAddress)
|
|
27
|
+
: alchemyPublicTokensDB.getFetchedTokens(chainId, walletAddress);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// this function should be called when:
|
|
31
|
+
// 1) user first initiates wallet on a given chain
|
|
32
|
+
// 2) user manually press refresh balances button
|
|
33
|
+
public async fetchAndUpdatePublicTokens(walletAddress: string, chainId: number) {
|
|
34
|
+
const state = getStateKey(walletAddress, chainId);
|
|
35
|
+
try {
|
|
36
|
+
if (chainId === chainIds.arcTestnet) {
|
|
37
|
+
await arcPublicTokensDB.fetchAndUpdatePublicTokens(walletAddress);
|
|
38
|
+
} else {
|
|
39
|
+
await alchemyPublicTokensDB.fetchAndUpdatePublicTokens(walletAddress, chainId);
|
|
40
|
+
this.useAlternativeDB[state] = false;
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
await eventsPublicTokensDB.fetchAndUpdatePublicTokens(walletAddress, chainId);
|
|
44
|
+
this.useAlternativeDB[state] = true;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const publicTokensDB = new PublicTokensDB();
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { get, update } from 'idb-keyval';
|
|
2
|
+
import { Logger } from '../../error-handling/logger';
|
|
3
|
+
import { caseInsensitiveEqual } from '../../functions/utils/caseInsensitive.utils';
|
|
4
|
+
import { ERC20Token } from '../../types/token.types';
|
|
5
|
+
|
|
6
|
+
type HiddenTokensStorage = Record<number, ERC20Token[]>;
|
|
7
|
+
|
|
8
|
+
class TokenVisibilityStorage {
|
|
9
|
+
private readonly STORAGE_KEY = 'hiddenTokenAddresses';
|
|
10
|
+
|
|
11
|
+
private readonly UNHIDDEN_SPAM_KEY = 'unhiddenSpamTokens';
|
|
12
|
+
|
|
13
|
+
public async getHiddenTokens(chainId: number): Promise<ERC20Token[]> {
|
|
14
|
+
try {
|
|
15
|
+
const data = await get<HiddenTokensStorage>(this.STORAGE_KEY);
|
|
16
|
+
return data?.[chainId] || [];
|
|
17
|
+
} catch (err) {
|
|
18
|
+
Logger.error('Error retrieving hidden tokens', err);
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private async getHiddenTokenAddresses(chainId: number): Promise<string[]> {
|
|
24
|
+
try {
|
|
25
|
+
const hiddenTokens = await this.getHiddenTokens(chainId);
|
|
26
|
+
return hiddenTokens.map(({ erc20TokenAddress }) => erc20TokenAddress.toLowerCase());
|
|
27
|
+
} catch (err) {
|
|
28
|
+
Logger.error('Error retrieving hidden tokens', err);
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async filterOutHiddenTokens(chainId: number, tokens: ERC20Token[]): Promise<ERC20Token[]> {
|
|
34
|
+
const hiddenTokens = await this.getHiddenTokens(chainId);
|
|
35
|
+
return tokens.filter(
|
|
36
|
+
(token) =>
|
|
37
|
+
!hiddenTokens.some((hiddenToken) =>
|
|
38
|
+
caseInsensitiveEqual(hiddenToken.erc20TokenAddress, token.erc20TokenAddress),
|
|
39
|
+
),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public async hideToken(token: ERC20Token) {
|
|
44
|
+
try {
|
|
45
|
+
await update(this.STORAGE_KEY, (current: HiddenTokensStorage | undefined) => {
|
|
46
|
+
const data = current ?? {};
|
|
47
|
+
const { chainId } = token;
|
|
48
|
+
if (!data[chainId]) {
|
|
49
|
+
data[chainId] = [];
|
|
50
|
+
}
|
|
51
|
+
const alreadyHidden = data[chainId].some(({ erc20TokenAddress }) =>
|
|
52
|
+
caseInsensitiveEqual(erc20TokenAddress, token.erc20TokenAddress),
|
|
53
|
+
);
|
|
54
|
+
if (!alreadyHidden) {
|
|
55
|
+
data[chainId].push(token);
|
|
56
|
+
}
|
|
57
|
+
return data;
|
|
58
|
+
});
|
|
59
|
+
if (token.isSpam) {
|
|
60
|
+
// If the token is spam, we need to delete it from the unhidden spam tokens list to hide it if necessary in future
|
|
61
|
+
await this.deleteUnhiddenSpamToken(token);
|
|
62
|
+
}
|
|
63
|
+
} catch (err) {
|
|
64
|
+
Logger.error('Error hiding token', err);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public async showToken(token: ERC20Token) {
|
|
69
|
+
try {
|
|
70
|
+
await update(this.STORAGE_KEY, (current: HiddenTokensStorage | undefined) => {
|
|
71
|
+
const { chainId } = token;
|
|
72
|
+
const data = current ?? {};
|
|
73
|
+
if (!data[chainId]) {
|
|
74
|
+
data[chainId] = [];
|
|
75
|
+
}
|
|
76
|
+
data[chainId] = data[chainId].filter(
|
|
77
|
+
({ erc20TokenAddress }) => !caseInsensitiveEqual(erc20TokenAddress, token.erc20TokenAddress),
|
|
78
|
+
);
|
|
79
|
+
return data;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
if (token.isSpam) {
|
|
83
|
+
// If the token is spam, we need to store it in the unhidden spam tokens list to not hide it again automatically
|
|
84
|
+
await this.storeUnhiddenSpamToken(token);
|
|
85
|
+
}
|
|
86
|
+
} catch (err) {
|
|
87
|
+
Logger.error('Error showing token', err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private async storeUnhiddenSpamToken(token: ERC20Token): Promise<void> {
|
|
92
|
+
const { chainId } = token;
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
await update(this.UNHIDDEN_SPAM_KEY, (current: HiddenTokensStorage | undefined) => {
|
|
96
|
+
const data = current ?? {};
|
|
97
|
+
if (!data[chainId]) {
|
|
98
|
+
data[chainId] = [];
|
|
99
|
+
}
|
|
100
|
+
const alreadyStored = data[chainId].some(({ erc20TokenAddress }) =>
|
|
101
|
+
caseInsensitiveEqual(erc20TokenAddress, token.erc20TokenAddress),
|
|
102
|
+
);
|
|
103
|
+
if (!alreadyStored) {
|
|
104
|
+
data[chainId].push(token);
|
|
105
|
+
}
|
|
106
|
+
return data;
|
|
107
|
+
});
|
|
108
|
+
} catch (err) {
|
|
109
|
+
Logger.error('Error storing unhidden spam token', err);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private async deleteUnhiddenSpamToken(token: ERC20Token): Promise<void> {
|
|
114
|
+
await update(this.UNHIDDEN_SPAM_KEY, (current: HiddenTokensStorage | undefined) => {
|
|
115
|
+
const { chainId } = token;
|
|
116
|
+
const data = current ?? {};
|
|
117
|
+
data[chainId] = data[chainId]?.filter(
|
|
118
|
+
({ erc20TokenAddress }) => !caseInsensitiveEqual(erc20TokenAddress, token.erc20TokenAddress),
|
|
119
|
+
);
|
|
120
|
+
return data;
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public async getUnhiddenSpamTokens(chainId: number): Promise<ERC20Token[]> {
|
|
125
|
+
try {
|
|
126
|
+
const data = await get<HiddenTokensStorage>(this.UNHIDDEN_SPAM_KEY);
|
|
127
|
+
return data?.[chainId] || [];
|
|
128
|
+
} catch (err) {
|
|
129
|
+
Logger.error('Error retrieving unhidden spam tokens', err);
|
|
130
|
+
return [];
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// From the list of tokens, hide the spam tokens that are not in the unhidden spam tokens list
|
|
135
|
+
private async hideUnnecessarySpamTokens(chainId: number, tokens: ERC20Token[]) {
|
|
136
|
+
const unhiddenSpamTokens = await this.getUnhiddenSpamTokens(chainId);
|
|
137
|
+
const unnecessarySpamTokens = tokens.filter(({ erc20TokenAddress, isSpam }) => {
|
|
138
|
+
if (!isSpam) return false;
|
|
139
|
+
return !unhiddenSpamTokens.some(({ erc20TokenAddress: unhiddenSpamTokenAddress }) =>
|
|
140
|
+
caseInsensitiveEqual(erc20TokenAddress, unhiddenSpamTokenAddress),
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
await Promise.all(unnecessarySpamTokens.map((token) => this.hideToken(token)));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// First hide spam tokens from the list, then fetch the all hidden tokens addresses
|
|
148
|
+
public async hideSpamsAndFetchHiddenTokensAddresses(chainId: number, tokens: ERC20Token[]) {
|
|
149
|
+
await this.hideUnnecessarySpamTokens(chainId, tokens);
|
|
150
|
+
return this.getHiddenTokenAddresses(chainId);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export const tokenVisibilityStorage = new TokenVisibilityStorage();
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-this-alias */
|
|
2
|
+
export class ValueCache<T> {
|
|
3
|
+
constructor(timeoutInMs: number) {
|
|
4
|
+
this.timeoutInMs = timeoutInMs;
|
|
5
|
+
this.setTimer();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
protected timeoutInMs;
|
|
9
|
+
|
|
10
|
+
public values: { [key: string]: T } = {};
|
|
11
|
+
|
|
12
|
+
private setTimer() {
|
|
13
|
+
if (this.timeoutInMs > 0) {
|
|
14
|
+
const self = this;
|
|
15
|
+
setInterval(() => {
|
|
16
|
+
self.onTimeout();
|
|
17
|
+
}, this.timeoutInMs);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// clear cache when times-up
|
|
22
|
+
private onTimeout() {
|
|
23
|
+
this.clear();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
clear() {
|
|
27
|
+
this.values = {};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './ValueCache';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { CustomJSONStringify } from '../../functions/utils/serialize.utils';
|
|
2
|
+
import { BaseCacheDevice } from './BaseCacheDevice';
|
|
3
|
+
|
|
4
|
+
export class AttachableMemoryCacheDevice<T> extends BaseCacheDevice<T> {
|
|
5
|
+
private isAttached = false;
|
|
6
|
+
|
|
7
|
+
override get(key: string): T | undefined {
|
|
8
|
+
return this.getInMemory(key);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
override set(key: string, value: T): void {
|
|
12
|
+
this.setInMemory(key, value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
override serialize(): Record<string, string> {
|
|
16
|
+
return Object.fromEntries([...this.inMemoryCache].map(([key, value]) => [key, CustomJSONStringify(value)]));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
attach<ArgsType extends unknown[]>(func: (...args: ArgsType) => T) {
|
|
20
|
+
if (this.isAttached) throw new Error("can't reattach cache device");
|
|
21
|
+
this.isAttached = true;
|
|
22
|
+
const callback = (...args: ArgsType) => {
|
|
23
|
+
const key = CustomJSONStringify(args);
|
|
24
|
+
|
|
25
|
+
const valueCandidate = this.get(key);
|
|
26
|
+
if (valueCandidate !== undefined) return valueCandidate;
|
|
27
|
+
const result = func(...args);
|
|
28
|
+
this.set(key, result);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
return callback;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { CustomJSONParse } from '../../functions/utils/serialize.utils';
|
|
2
|
+
import { ICacheDevice } from '../../types/ICacheDevice';
|
|
3
|
+
|
|
4
|
+
export abstract class BaseCacheDevice<T = string> implements ICacheDevice<T> {
|
|
5
|
+
protected inMemoryCache: Map<string, T> = new Map<string, T>();
|
|
6
|
+
|
|
7
|
+
constructor(serialized?: Record<string, string>) {
|
|
8
|
+
if (serialized) this.mergeWithSerialized(serialized);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
protected getInMemory(key: string): T | undefined {
|
|
12
|
+
return this.inMemoryCache.get(key) ?? undefined;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
protected setInMemory(key: string, value: T): void {
|
|
16
|
+
this.inMemoryCache.set(key, value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
abstract get(key: string): T | undefined;
|
|
20
|
+
|
|
21
|
+
abstract set(key: string, value: T): void;
|
|
22
|
+
|
|
23
|
+
abstract serialize(): Record<string, string>;
|
|
24
|
+
|
|
25
|
+
mergeWithSerialized(serialized: Record<string, string>) {
|
|
26
|
+
Object.entries(serialized).forEach(([key, value]) => {
|
|
27
|
+
this.set(key, CustomJSONParse(value));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
/* eslint-disable global-require */
|
|
3
|
+
import { CustomJSONStringify } from '../../functions/utils/serialize.utils';
|
|
4
|
+
import { logError } from '../../error-handling/logError';
|
|
5
|
+
import { BaseCacheDevice } from './BaseCacheDevice';
|
|
6
|
+
|
|
7
|
+
export class FileCacheDevice extends BaseCacheDevice {
|
|
8
|
+
private filePath: string;
|
|
9
|
+
|
|
10
|
+
private useFileCache: boolean;
|
|
11
|
+
|
|
12
|
+
constructor(useFileCache: boolean | undefined, filePath: string) {
|
|
13
|
+
super();
|
|
14
|
+
this.useFileCache = !!useFileCache;
|
|
15
|
+
this.filePath = filePath;
|
|
16
|
+
this.readFileFromDevice();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
private readFileFromDevice() {
|
|
20
|
+
if (this.useFileCache && this.filePath) {
|
|
21
|
+
try {
|
|
22
|
+
const fs = require('fs');
|
|
23
|
+
if (fs.existsSync(this.filePath)) {
|
|
24
|
+
const fileContent: object = JSON.parse(fs.readFileSync(this.filePath).toString());
|
|
25
|
+
const entries = Object.entries(fileContent);
|
|
26
|
+
entries.forEach(([key, value]) => {
|
|
27
|
+
this.setInMemory(key, value);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
} catch (err: unknown) {
|
|
31
|
+
logError('FileCacheDevice Constructor Error', { err });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get(key: string): string | undefined {
|
|
37
|
+
return this.getInMemory(key);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
set(key: string, value: string): void {
|
|
41
|
+
// if somebody overwrite storage, we need to update inMemoryCache
|
|
42
|
+
this.readFileFromDevice();
|
|
43
|
+
|
|
44
|
+
this.setInMemory(key, value);
|
|
45
|
+
|
|
46
|
+
if (this.useFileCache && this.filePath) {
|
|
47
|
+
try {
|
|
48
|
+
const fs = require('fs');
|
|
49
|
+
fs.writeFileSync(this.filePath, JSON.stringify(Object.fromEntries(this.inMemoryCache)));
|
|
50
|
+
} catch (err: unknown) {
|
|
51
|
+
logError('FileCacheDevice set function', { err });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
serialize(): Record<string, string> {
|
|
57
|
+
return Object.fromEntries([...this.inMemoryCache].map(([key, value]) => [key, CustomJSONStringify(value)]));
|
|
58
|
+
}
|
|
59
|
+
}
|