@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,92 @@
|
|
|
1
|
+
import { networkRegistry } from '../../constants/chains.constants';
|
|
2
|
+
|
|
3
|
+
/** Capitalize the first letter of each word
|
|
4
|
+
* "This is an example" => "This Is An Example"
|
|
5
|
+
*/
|
|
6
|
+
export function toTitleCase(str: string) {
|
|
7
|
+
return str
|
|
8
|
+
?.toLowerCase()
|
|
9
|
+
.split(' ')
|
|
10
|
+
.map((s) => s.charAt(0).toUpperCase() + s.substring(1))
|
|
11
|
+
.join(' ');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Capitalize the first letter of a string */
|
|
15
|
+
export function capitalizeFirstLetter(str: string) {
|
|
16
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Returns true if a value is null, undefined, or an empty object, array (length 0), or string (e.g. '') */
|
|
20
|
+
export function isNullOrEmpty(obj: unknown) {
|
|
21
|
+
if (!obj) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
if (obj === null) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
// Check for an empty array too
|
|
28
|
+
if (Array.isArray(obj)) {
|
|
29
|
+
if (obj.length === 0) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return Object.keys(obj).length === 0 && obj.constructor === Object;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const hexToUtf8 = (hex: string): string => {
|
|
37
|
+
// Remove the '0x' prefix if it exists
|
|
38
|
+
if (hex.startsWith('0x')) {
|
|
39
|
+
hex = hex.slice(2);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Convert hex to bytes
|
|
43
|
+
const bytes = [];
|
|
44
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
45
|
+
const byte = parseInt(hex.slice(i, i + 2), 16);
|
|
46
|
+
bytes.push(byte);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Convert bytes to a UTF-8 string
|
|
50
|
+
const utf8String = new TextDecoder('utf-8').decode(new Uint8Array(bytes));
|
|
51
|
+
return utf8String;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// If you make any changes to the logic in this function,
|
|
55
|
+
// ensure that the getChainIdFromStateKey function is also updated accordingly.
|
|
56
|
+
export const getStateKey = (address: string, chainId: number, withHinkalContractAddress = false): string => {
|
|
57
|
+
const baseKey = `${address}-${chainId}`;
|
|
58
|
+
if (withHinkalContractAddress) {
|
|
59
|
+
const { hinkalAddress } = networkRegistry[chainId].contractData;
|
|
60
|
+
return `${baseKey}-${hinkalAddress}`.toLowerCase();
|
|
61
|
+
}
|
|
62
|
+
return baseKey.toLowerCase();
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// This function parses the chainId from the state key generated by getStateKey.
|
|
66
|
+
// Note: Must stay in sync with getStateKey's format.
|
|
67
|
+
export const getChainIdFromStateKey = (stateKey: string): number => {
|
|
68
|
+
const [, chainId] = stateKey.split('-');
|
|
69
|
+
return Number(chainId);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const replaceAllInstances = (text: string, oldPart: string, newPart: string): string => {
|
|
73
|
+
if (!oldPart || !newPart) {
|
|
74
|
+
throw Error('oldPart and newPart must be provided');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// replaceAll() not available: using split&join
|
|
78
|
+
return text.toLowerCase().split(oldPart.toLowerCase()).join(newPart.toLowerCase());
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const capitalizeName = (name: string | undefined | null): string | null => {
|
|
82
|
+
if (!name || name.trim().length === 0) return null;
|
|
83
|
+
|
|
84
|
+
return name.toLowerCase().replace(/\b\w/g, (char) => char.toUpperCase());
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const validateEmail = (email: string | undefined | null): boolean => {
|
|
88
|
+
if (!email) return false;
|
|
89
|
+
|
|
90
|
+
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
91
|
+
return emailRegex.test(email);
|
|
92
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export const waitLittle = (time = 1000) =>
|
|
2
|
+
new Promise((res) => {
|
|
3
|
+
setTimeout(() => {
|
|
4
|
+
res(undefined);
|
|
5
|
+
}, time);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
export const getCurrentTimeInSeconds = (): number => Math.floor(Date.now() / 1000);
|
|
9
|
+
|
|
10
|
+
export const msToISOString = (ms: number) => new Date(ms).toISOString();
|
|
11
|
+
|
|
12
|
+
export const getEffectiveToday = (): Date => {
|
|
13
|
+
const now = new Date();
|
|
14
|
+
const today6Utc = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 6, 0, 0, 0));
|
|
15
|
+
const effectiveToday = now < today6Utc ? new Date(today6Utc.setUTCDate(today6Utc.getUTCDate() - 1)) : today6Utc;
|
|
16
|
+
return effectiveToday;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const getEffectiveSevenDaysAgo = (): Date => {
|
|
20
|
+
const effectiveToday = getEffectiveToday();
|
|
21
|
+
const sevenDaysAgo = new Date(effectiveToday);
|
|
22
|
+
sevenDaysAgo.setUTCDate(sevenDaysAgo.getUTCDate() - 6);
|
|
23
|
+
return sevenDaysAgo;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const getEffectiveMonthStart = (): Date => {
|
|
27
|
+
const effectiveToday = getEffectiveToday();
|
|
28
|
+
const monthStart = new Date(Date.UTC(effectiveToday.getUTCFullYear(), effectiveToday.getUTCMonth(), 1, 6, 0, 0, 0));
|
|
29
|
+
return monthStart;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const getCurrentWeekStart = (): Date => {
|
|
33
|
+
const effectiveDate = getEffectiveToday();
|
|
34
|
+
const day = effectiveDate.getUTCDay();
|
|
35
|
+
const diff = (day + 6) % 7;
|
|
36
|
+
const monday = new Date(effectiveDate);
|
|
37
|
+
monday.setUTCDate(effectiveDate.getUTCDate() - diff);
|
|
38
|
+
monday.setUTCHours(6, 0, 0, 0);
|
|
39
|
+
return monday;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const getPreviousWeekStart = (): Date => {
|
|
43
|
+
const currentWeekStart = getCurrentWeekStart();
|
|
44
|
+
currentWeekStart.setUTCDate(currentWeekStart.getUTCDate() - 7);
|
|
45
|
+
return currentWeekStart;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const getNextDayDelay = () => {
|
|
49
|
+
const now = new Date();
|
|
50
|
+
const next = new Date(now);
|
|
51
|
+
next.setUTCHours(6, 0, 0, 0);
|
|
52
|
+
if (next <= now) next.setUTCDate(next.getUTCDate() + 1);
|
|
53
|
+
return next.getTime() - now.getTime();
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const getCurrentDayStart = (): Date => {
|
|
57
|
+
const now = new Date();
|
|
58
|
+
const today6Utc = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 6, 0, 0, 0));
|
|
59
|
+
if (now < today6Utc) today6Utc.setUTCDate(today6Utc.getUTCDate() - 1);
|
|
60
|
+
return today6Utc;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const getNextDayISO = (): string => {
|
|
64
|
+
const now = new Date();
|
|
65
|
+
const next = new Date(now);
|
|
66
|
+
next.setUTCHours(6, 0, 0, 0);
|
|
67
|
+
if (next <= now) next.setUTCDate(next.getUTCDate() + 1);
|
|
68
|
+
return next.toISOString();
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const formatDate = (date: Date): string => {
|
|
72
|
+
const now = new Date();
|
|
73
|
+
const isSameYear = now.getFullYear() === date.getFullYear();
|
|
74
|
+
|
|
75
|
+
const options: Intl.DateTimeFormatOptions = isSameYear
|
|
76
|
+
? { month: 'long', day: 'numeric' }
|
|
77
|
+
: { month: 'long', day: 'numeric', year: 'numeric' };
|
|
78
|
+
|
|
79
|
+
return date.toLocaleDateString(undefined, options);
|
|
80
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { API } from '../../API/API';
|
|
2
|
+
|
|
3
|
+
export const checkTokensForShielding = async (chainId: number, erc20Addresses: string[]) => {
|
|
4
|
+
const tokens = await API.tokensInfoCall(chainId, erc20Addresses);
|
|
5
|
+
const nonEligibleTokens: string[] = [];
|
|
6
|
+
tokens.forEach((token, index) => {
|
|
7
|
+
if (!token || !token.balanceStorageOffset || !token.allowanceStorageOffset) {
|
|
8
|
+
nonEligibleTokens.push(erc20Addresses[index]);
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
return nonEligibleTokens;
|
|
12
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const demarcationsDefaultValue = [
|
|
2
|
+
{ value: 1000000000, unit: 'b' },
|
|
3
|
+
{ value: 1000000, unit: 'm' },
|
|
4
|
+
{ value: 1000, unit: 'k' },
|
|
5
|
+
];
|
|
6
|
+
export const trimFieldValues = (
|
|
7
|
+
value: number,
|
|
8
|
+
demarcations: { value: number; unit: string }[] = demarcationsDefaultValue,
|
|
9
|
+
showDollarSign = true,
|
|
10
|
+
): string => {
|
|
11
|
+
demarcations.sort((d1, d2) => d2.value - d1.value);
|
|
12
|
+
|
|
13
|
+
const prefix = showDollarSign ? '$' : '';
|
|
14
|
+
|
|
15
|
+
if (value < demarcations[demarcations.length - 1].value) return `$${value.toFixed(2)}`;
|
|
16
|
+
|
|
17
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
18
|
+
for (const dem of demarcations) {
|
|
19
|
+
if (value >= dem.value) {
|
|
20
|
+
return `${prefix}${(Math.round((100 * value) / dem.value) / 100).toFixed(2) + dem.unit}`;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return `${prefix}${value}`;
|
|
24
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { transactionErrorCodes } from '../../error-handling/error-codes.constants';
|
|
2
|
+
import { IHinkal } from '../../data-structures/Hinkal/IHinkal';
|
|
3
|
+
import { caseInsensitiveEqual } from './caseInsensitive.utils';
|
|
4
|
+
|
|
5
|
+
export const checkChainOrAddressNotUpdated = async (hinkal: IHinkal, oldChainId: number, oldEthAddress: string) => {
|
|
6
|
+
const newEthAddress = await hinkal.getEthereumAddress();
|
|
7
|
+
const newChainId = hinkal.getCurrentChainId();
|
|
8
|
+
if (newChainId !== oldChainId || !caseInsensitiveEqual(newEthAddress, oldEthAddress)) {
|
|
9
|
+
throw Error(transactionErrorCodes.ERROR_MESSAGE_CHAIN_ADDRESS_CHANGED);
|
|
10
|
+
}
|
|
11
|
+
return true;
|
|
12
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { UNSUPPORTED_WALLET_IDS, UNSUPPORTED_WALLETS_WALLETCONNECT_NAMES } from '../../constants';
|
|
2
|
+
import { UAParser } from 'ua-parser-js';
|
|
3
|
+
import { Connector } from 'wagmi';
|
|
4
|
+
|
|
5
|
+
const parser = new UAParser();
|
|
6
|
+
|
|
7
|
+
const SUPPORTED_SAFARI_VERSIONS = 17; // Supported 17 and up
|
|
8
|
+
|
|
9
|
+
export const browserSupported = (): boolean => {
|
|
10
|
+
const { name, version } = parser.getBrowser();
|
|
11
|
+
const majorVersion = parseInt(version?.split('.')[0] ?? '0', 10);
|
|
12
|
+
|
|
13
|
+
if (name === 'Safari' && majorVersion && SUPPORTED_SAFARI_VERSIONS > majorVersion) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const walletSupported = (provider: any, connector: Connector): boolean => {
|
|
20
|
+
const isWalletConnect = connector?.type === 'walletConnect';
|
|
21
|
+
const providerName = provider?.session?.peer?.metadata?.name;
|
|
22
|
+
if (isWalletConnect && Object.values(UNSUPPORTED_WALLETS_WALLETCONNECT_NAMES).includes(providerName)) return false;
|
|
23
|
+
if (Object.values(UNSUPPORTED_WALLET_IDS).includes(connector?.id)) return false;
|
|
24
|
+
|
|
25
|
+
return true;
|
|
26
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { zeroAddress } from '../../constants/protocol.constants';
|
|
2
|
+
import { IHinkal } from '../../data-structures/Hinkal/IHinkal';
|
|
3
|
+
import { ERC20Token, TokenBalance, TokenChanges } from '../../types/token.types';
|
|
4
|
+
import { absBigInt } from './amounts.utils';
|
|
5
|
+
import { caseInsensitiveEqual } from './caseInsensitive.utils';
|
|
6
|
+
import { getPublicBalancesOfTokens } from './publicBalance.utils';
|
|
7
|
+
import { getAmountInWei } from '../web3/etherFunctions';
|
|
8
|
+
|
|
9
|
+
export const getNecessaryAssetsForFundingFromBalance = (
|
|
10
|
+
tokenChanges: TokenChanges<bigint>[],
|
|
11
|
+
subAccountBalances: TokenBalance[],
|
|
12
|
+
): {
|
|
13
|
+
fundAmounts: bigint[];
|
|
14
|
+
tokensToFund: ERC20Token[];
|
|
15
|
+
} => {
|
|
16
|
+
if (!tokenChanges?.length) return { fundAmounts: [], tokensToFund: [] };
|
|
17
|
+
|
|
18
|
+
const changes = tokenChanges.reduce<{
|
|
19
|
+
fundAmounts: bigint[];
|
|
20
|
+
tokensToFund: ERC20Token[];
|
|
21
|
+
}>(
|
|
22
|
+
(acc, { amount, token }) => {
|
|
23
|
+
if (amount >= 0n) return acc;
|
|
24
|
+
const requiredAmount = -amount;
|
|
25
|
+
const availableAmount =
|
|
26
|
+
subAccountBalances.find((balance) =>
|
|
27
|
+
caseInsensitiveEqual(balance.token.erc20TokenAddress, token.erc20TokenAddress),
|
|
28
|
+
)?.balance ?? 0n;
|
|
29
|
+
|
|
30
|
+
if (availableAmount < requiredAmount) {
|
|
31
|
+
acc.tokensToFund.push(token);
|
|
32
|
+
acc.fundAmounts.push(requiredAmount - availableAmount);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return acc;
|
|
36
|
+
},
|
|
37
|
+
{ fundAmounts: [], tokensToFund: [] },
|
|
38
|
+
);
|
|
39
|
+
return changes;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const getNecessaryAssetsForFunding = async (
|
|
43
|
+
hinkal: IHinkal,
|
|
44
|
+
walletAddress: string,
|
|
45
|
+
tokenChanges: TokenChanges<bigint>[],
|
|
46
|
+
) => {
|
|
47
|
+
if (!tokenChanges?.length)
|
|
48
|
+
return { fundAmounts: [], tokensToFund: [], approveTokenAddresses: [], approvedTokenAmounts: [] };
|
|
49
|
+
|
|
50
|
+
const chainId = hinkal.getCurrentChainId();
|
|
51
|
+
const subAccountBalances = await getPublicBalancesOfTokens(
|
|
52
|
+
chainId,
|
|
53
|
+
walletAddress,
|
|
54
|
+
tokenChanges.map(({ token }) => token),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const { fundAmounts, tokensToFund } = getNecessaryAssetsForFundingFromBalance(tokenChanges, subAccountBalances);
|
|
58
|
+
|
|
59
|
+
const approveTokenAddresses = tokenChanges.length
|
|
60
|
+
? tokenChanges
|
|
61
|
+
.filter(({ amount, token }) => amount < 0n && token.erc20TokenAddress !== zeroAddress)
|
|
62
|
+
.map(({ token }) => token.erc20TokenAddress)
|
|
63
|
+
: [];
|
|
64
|
+
|
|
65
|
+
const approvedTokenAmounts = approveTokenAddresses.map(() => 2n ** 256n - 1n);
|
|
66
|
+
|
|
67
|
+
return { fundAmounts, tokensToFund, approveTokenAddresses, approvedTokenAmounts };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const getNecessaryApprovalChangesData = (hinkal: IHinkal, tokenChanges: TokenChanges<bigint>[], to: string) => {
|
|
71
|
+
const myApprovedAmounts = tokenChanges.map((erc20Token) => {
|
|
72
|
+
if (erc20Token.amount < 0n) {
|
|
73
|
+
const myApproval = hinkal.getMyApprovalAmountForInteraction(to, erc20Token.token.erc20TokenAddress);
|
|
74
|
+
return myApproval;
|
|
75
|
+
}
|
|
76
|
+
return 0n;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// approvalChanges is essentially the min(myApprovedAmounts,amount)
|
|
80
|
+
const approvalChanges = tokenChanges.map((_token, index) => {
|
|
81
|
+
if (_token.amount < 0n) {
|
|
82
|
+
if (myApprovedAmounts[index] > 0n) {
|
|
83
|
+
// if my approval is more than is required -> put only what required
|
|
84
|
+
// if my approval is less than is required -> put approval
|
|
85
|
+
return absBigInt(_token.amount) < absBigInt(myApprovedAmounts[index])
|
|
86
|
+
? _token.amount
|
|
87
|
+
: -myApprovedAmounts[index];
|
|
88
|
+
}
|
|
89
|
+
return 0n;
|
|
90
|
+
}
|
|
91
|
+
return 0n;
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return { approvalChanges };
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export function hasEnoughBalance(
|
|
98
|
+
tokenAmount: string,
|
|
99
|
+
maxPossibleAmount: string,
|
|
100
|
+
token?: ERC20Token,
|
|
101
|
+
isDollarMode?: boolean,
|
|
102
|
+
tokenPriceDollar?: number,
|
|
103
|
+
): boolean {
|
|
104
|
+
try {
|
|
105
|
+
if (!token || !tokenAmount) return true;
|
|
106
|
+
|
|
107
|
+
if (isDollarMode) {
|
|
108
|
+
if (!tokenPriceDollar) return true;
|
|
109
|
+
|
|
110
|
+
return +tokenAmount * tokenPriceDollar <= +maxPossibleAmount;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return getAmountInWei(token, tokenAmount) <= getAmountInWei(token, maxPossibleAmount);
|
|
114
|
+
} catch (err) {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { getPermitInfo } from './EIP-712';
|
|
2
|
+
|
|
3
|
+
describe('getPermitInfo', () => {
|
|
4
|
+
describe('Different types of permit', () => {
|
|
5
|
+
it('Permit', () => {
|
|
6
|
+
const requestMessage = {
|
|
7
|
+
types: {
|
|
8
|
+
Permit: [
|
|
9
|
+
{ name: 'owner', type: 'address' },
|
|
10
|
+
{ name: 'spender', type: 'address' },
|
|
11
|
+
{ name: 'value', type: 'uint256' },
|
|
12
|
+
{ name: 'nonce', type: 'uint256' },
|
|
13
|
+
{ name: 'deadline', type: 'uint256' },
|
|
14
|
+
],
|
|
15
|
+
},
|
|
16
|
+
primaryType: 'Permit',
|
|
17
|
+
domain: {
|
|
18
|
+
name: 'USD Coin',
|
|
19
|
+
version: '2',
|
|
20
|
+
chainId: 1,
|
|
21
|
+
verifyingContract: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3',
|
|
22
|
+
},
|
|
23
|
+
message: {
|
|
24
|
+
owner: '0x1234...5678',
|
|
25
|
+
spender: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45',
|
|
26
|
+
value: '1000000000',
|
|
27
|
+
nonce: 0,
|
|
28
|
+
deadline: 1703980800,
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
const result = getPermitInfo(
|
|
32
|
+
requestMessage.message,
|
|
33
|
+
requestMessage.types,
|
|
34
|
+
requestMessage.domain,
|
|
35
|
+
requestMessage.primaryType,
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
expect(result.isPermit).toBe(true);
|
|
39
|
+
expect(result.tokenDetails).toEqual([
|
|
40
|
+
{
|
|
41
|
+
token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3',
|
|
42
|
+
amount: '115792089237316195423570985008687907853269984665640564039457584007913129639935',
|
|
43
|
+
},
|
|
44
|
+
]);
|
|
45
|
+
expect(result.spender).toBeUndefined();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('PermitBatch', () => {
|
|
49
|
+
const requestMessage = {
|
|
50
|
+
types: {
|
|
51
|
+
PermitBatch: [
|
|
52
|
+
{ name: 'details', type: 'PermitDetails[]' },
|
|
53
|
+
{ name: 'spender', type: 'address' },
|
|
54
|
+
{ name: 'sigDeadline', type: 'uint256' },
|
|
55
|
+
],
|
|
56
|
+
PermitDetails: [
|
|
57
|
+
{ name: 'token', type: 'address' },
|
|
58
|
+
{ name: 'amount', type: 'uint160' },
|
|
59
|
+
{ name: 'expiration', type: 'uint48' },
|
|
60
|
+
{ name: 'nonce', type: 'uint48' },
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
primaryType: 'PermitBatch',
|
|
64
|
+
domain: {
|
|
65
|
+
name: 'Permit2',
|
|
66
|
+
chainId: 1,
|
|
67
|
+
verifyingContract: '0x000000000022D473030F116dDEE9F6B43aC78BA3',
|
|
68
|
+
},
|
|
69
|
+
message: {
|
|
70
|
+
details: [
|
|
71
|
+
{
|
|
72
|
+
token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3',
|
|
73
|
+
amount: '1000000000',
|
|
74
|
+
expiration: 1703980800,
|
|
75
|
+
nonce: 0,
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
token: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
|
|
79
|
+
amount: '500000000',
|
|
80
|
+
expiration: 1703980800,
|
|
81
|
+
nonce: 1,
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
spender: '0x1111111254EEB25477B68fb85Ed929f73A960582',
|
|
85
|
+
sigDeadline: 1703980800,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
const result = getPermitInfo(
|
|
89
|
+
requestMessage.message,
|
|
90
|
+
requestMessage.types,
|
|
91
|
+
requestMessage.domain,
|
|
92
|
+
requestMessage.primaryType,
|
|
93
|
+
);
|
|
94
|
+
expect(result.isPermit).toBe(true);
|
|
95
|
+
const tokenChanges = result.tokenDetails?.map((token) => ({
|
|
96
|
+
token: token.token,
|
|
97
|
+
amount: token.amount,
|
|
98
|
+
}));
|
|
99
|
+
expect(tokenChanges).toEqual([
|
|
100
|
+
{ token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3', amount: '1000000000' },
|
|
101
|
+
{ token: '0xdAC17F958D2ee523a2206206994597C13D831ec7', amount: '500000000' },
|
|
102
|
+
]);
|
|
103
|
+
expect(result.spender).toBe('0x000000000022D473030F116dDEE9F6B43aC78BA3');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('PermitSingle', () => {
|
|
107
|
+
const requestMessage = {
|
|
108
|
+
types: {
|
|
109
|
+
PermitSingle: [
|
|
110
|
+
{ name: 'details', type: 'PermitDetails' },
|
|
111
|
+
{ name: 'spender', type: 'address' },
|
|
112
|
+
{ name: 'sigDeadline', type: 'uint256' },
|
|
113
|
+
],
|
|
114
|
+
PermitDetails: [
|
|
115
|
+
{ name: 'token', type: 'address' },
|
|
116
|
+
{ name: 'amount', type: 'uint160' },
|
|
117
|
+
{ name: 'expiration', type: 'uint48' },
|
|
118
|
+
{ name: 'nonce', type: 'uint48' },
|
|
119
|
+
],
|
|
120
|
+
},
|
|
121
|
+
primaryType: 'PermitSingle',
|
|
122
|
+
domain: {
|
|
123
|
+
name: 'Permit2',
|
|
124
|
+
chainId: 1,
|
|
125
|
+
verifyingContract: '0x000000000022D473030F116dDEE9F6B43aC78BA3',
|
|
126
|
+
},
|
|
127
|
+
message: {
|
|
128
|
+
details: {
|
|
129
|
+
token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3',
|
|
130
|
+
amount: '1000000000',
|
|
131
|
+
expiration: 1703980800,
|
|
132
|
+
nonce: 0,
|
|
133
|
+
},
|
|
134
|
+
spender: '0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45',
|
|
135
|
+
sigDeadline: 1703980800,
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
const result = getPermitInfo(
|
|
139
|
+
requestMessage.message,
|
|
140
|
+
requestMessage.types,
|
|
141
|
+
requestMessage.domain,
|
|
142
|
+
requestMessage.primaryType,
|
|
143
|
+
);
|
|
144
|
+
expect(result.isPermit).toBe(true);
|
|
145
|
+
const tokenChanges = result.tokenDetails?.map((token) => ({
|
|
146
|
+
token: token.token,
|
|
147
|
+
amount: token.amount,
|
|
148
|
+
}));
|
|
149
|
+
expect(tokenChanges).toEqual([{ token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3', amount: '1000000000' }]);
|
|
150
|
+
expect(result.spender).toBe('0x000000000022D473030F116dDEE9F6B43aC78BA3');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('PermitTransferFrom', () => {
|
|
154
|
+
const requestMessage = {
|
|
155
|
+
types: {
|
|
156
|
+
PermitTransferFrom: [
|
|
157
|
+
{ name: 'permitted', type: 'TokenPermissions' },
|
|
158
|
+
{ name: 'spender', type: 'address' },
|
|
159
|
+
{ name: 'nonce', type: 'uint256' },
|
|
160
|
+
{ name: 'deadline', type: 'uint256' },
|
|
161
|
+
],
|
|
162
|
+
TokenPermissions: [
|
|
163
|
+
{ name: 'token', type: 'address' },
|
|
164
|
+
{ name: 'amount', type: 'uint256' },
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
primaryType: 'PermitTransferFrom',
|
|
168
|
+
domain: {
|
|
169
|
+
name: 'Permit2',
|
|
170
|
+
chainId: 1,
|
|
171
|
+
verifyingContract: '0x000000000022D473030F116dDEE9F6B43aC78BA3',
|
|
172
|
+
},
|
|
173
|
+
message: {
|
|
174
|
+
permitted: {
|
|
175
|
+
token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3',
|
|
176
|
+
amount: '1000000000',
|
|
177
|
+
},
|
|
178
|
+
spender: '0x1234...5678',
|
|
179
|
+
nonce: 0,
|
|
180
|
+
deadline: 1703980800,
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
const result = getPermitInfo(
|
|
184
|
+
requestMessage.message,
|
|
185
|
+
requestMessage.types,
|
|
186
|
+
requestMessage.domain,
|
|
187
|
+
requestMessage.primaryType,
|
|
188
|
+
);
|
|
189
|
+
expect(result.isPermit).toBe(true);
|
|
190
|
+
expect(result.tokenDetails).toEqual([
|
|
191
|
+
{ token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3', amount: '1000000000' },
|
|
192
|
+
]);
|
|
193
|
+
expect(result.spender).toBe('0x000000000022D473030F116dDEE9F6B43aC78BA3');
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('PermitBatchTransferFrom', () => {
|
|
197
|
+
const requestMessage = {
|
|
198
|
+
types: {
|
|
199
|
+
PermitBatchTransferFrom: [
|
|
200
|
+
{ name: 'permitted', type: 'TokenPermissions[]' },
|
|
201
|
+
{ name: 'spender', type: 'address' },
|
|
202
|
+
{ name: 'nonce', type: 'uint256' },
|
|
203
|
+
{ name: 'deadline', type: 'uint256' },
|
|
204
|
+
],
|
|
205
|
+
TokenPermissions: [
|
|
206
|
+
{ name: 'token', type: 'address' },
|
|
207
|
+
{ name: 'amount', type: 'uint256' },
|
|
208
|
+
],
|
|
209
|
+
},
|
|
210
|
+
primaryType: 'PermitBatchTransferFrom',
|
|
211
|
+
domain: {
|
|
212
|
+
name: 'Permit2',
|
|
213
|
+
chainId: 1,
|
|
214
|
+
verifyingContract: '0x000000000022D473030F116dDEE9F6B43aC78BA3',
|
|
215
|
+
},
|
|
216
|
+
message: {
|
|
217
|
+
permitted: [
|
|
218
|
+
{
|
|
219
|
+
token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3',
|
|
220
|
+
amount: '1000000000',
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
token: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
|
|
224
|
+
amount: '500000000',
|
|
225
|
+
},
|
|
226
|
+
],
|
|
227
|
+
spender: '0x1234...5678',
|
|
228
|
+
nonce: 0,
|
|
229
|
+
deadline: 1703980800,
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
const result = getPermitInfo(
|
|
233
|
+
requestMessage.message,
|
|
234
|
+
requestMessage.types,
|
|
235
|
+
requestMessage.domain,
|
|
236
|
+
requestMessage.primaryType,
|
|
237
|
+
);
|
|
238
|
+
expect(result.isPermit).toBe(true);
|
|
239
|
+
expect(result.tokenDetails).toEqual([
|
|
240
|
+
{ token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3', amount: '1000000000' },
|
|
241
|
+
{ token: '0xdAC17F958D2ee523a2206206994597C13D831ec7', amount: '500000000' },
|
|
242
|
+
]);
|
|
243
|
+
expect(result.spender).toBe('0x000000000022D473030F116dDEE9F6B43aC78BA3');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('PermitWitnessTransferFrom', () => {
|
|
247
|
+
const requestMessage = {
|
|
248
|
+
types: {
|
|
249
|
+
PermitWitnessTransferFrom: [
|
|
250
|
+
{ name: 'permitted', type: 'TokenPermissions' },
|
|
251
|
+
{ name: 'spender', type: 'address' },
|
|
252
|
+
{ name: 'nonce', type: 'uint256' },
|
|
253
|
+
{ name: 'deadline', type: 'uint256' },
|
|
254
|
+
{ name: 'witness', type: 'OrderWitness' },
|
|
255
|
+
],
|
|
256
|
+
TokenPermissions: [
|
|
257
|
+
{ name: 'token', type: 'address' },
|
|
258
|
+
{ name: 'amount', type: 'uint256' },
|
|
259
|
+
],
|
|
260
|
+
OrderWitness: [
|
|
261
|
+
{ name: 'buyer', type: 'address' },
|
|
262
|
+
{ name: 'seller', type: 'address' },
|
|
263
|
+
{ name: 'orderHash', type: 'bytes32' },
|
|
264
|
+
{ name: 'price', type: 'uint256' },
|
|
265
|
+
],
|
|
266
|
+
},
|
|
267
|
+
primaryType: 'PermitWitnessTransferFrom',
|
|
268
|
+
domain: {
|
|
269
|
+
name: 'Permit2',
|
|
270
|
+
chainId: 1,
|
|
271
|
+
verifyingContract: '0x000000000022D473030F116dDEE9F6B43aC78BA3',
|
|
272
|
+
},
|
|
273
|
+
message: {
|
|
274
|
+
permitted: {
|
|
275
|
+
token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3',
|
|
276
|
+
amount: '1000000000',
|
|
277
|
+
},
|
|
278
|
+
spender: '0x1234...5678',
|
|
279
|
+
nonce: 0,
|
|
280
|
+
deadline: 1703980800,
|
|
281
|
+
witness: {
|
|
282
|
+
buyer: '0x1234...5678',
|
|
283
|
+
seller: '0x8765...4321',
|
|
284
|
+
orderHash: '0xabcd...ef01',
|
|
285
|
+
price: '1000000000',
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
};
|
|
289
|
+
const result = getPermitInfo(
|
|
290
|
+
requestMessage.message,
|
|
291
|
+
requestMessage.types,
|
|
292
|
+
requestMessage.domain,
|
|
293
|
+
requestMessage.primaryType,
|
|
294
|
+
);
|
|
295
|
+
expect(result.isPermit).toBe(true);
|
|
296
|
+
expect(result.tokenDetails).toEqual([
|
|
297
|
+
{ token: '0xA0b86a33E6441c60c6A5b3c01c3De85A3c9Cb4F3', amount: '1000000000' },
|
|
298
|
+
]);
|
|
299
|
+
expect(result.spender).toBe('0x000000000022D473030F116dDEE9F6B43aC78BA3');
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
});
|