UNPKG

@etherspot/prime-sdk

Version:

Etherspot Prime (Account Abstraction) SDK

118 lines (117 loc) 5.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DataUtils = void 0; require("reflect-metadata"); const data_1 = require("./data"); const dto_1 = require("./dto"); const ethers_1 = require("ethers"); class DataUtils { constructor(apiKey) { this.defaultDataApiKey = 'eyJvcmciOiI2NTIzZjY5MzUwOTBmNzAwMDFiYjJkZWIiLCJpZCI6IjI4ZWJiMGQ5YTMxYjQ3MmY4NmU4MWY2YTVhYzBhMzE1IiwiaCI6Im11cm11cjEyOCJ9'; this.dataModule = new data_1.DataModule(apiKey || this.defaultDataApiKey); } async getAccountBalances(dto) { const { account, tokens, chainId, provider } = await (0, dto_1.validateDto)(dto, dto_1.GetAccountBalancesDto, { addressKeys: ['account', 'tokens'], }); return this.dataModule.getAccountBalances(account, tokens, chainId, provider); } async getTransaction(dto) { const { hash, chainId } = await (0, dto_1.validateDto)(dto, dto_1.GetTransactionDto); return this.dataModule.getTransaction(hash, chainId); } async getTransactions(dto) { const { account, chainId, page, limit } = await (0, dto_1.validateDto)(dto, dto_1.GetTransactionsDto, { addressKeys: ['account'], }); return this.dataModule.getTransactions(account, chainId, page, limit); } async getNftList(dto) { const { account, chainId } = await (0, dto_1.validateDto)(dto, dto_1.GetNftListDto, { addressKeys: ['account'], }); return this.dataModule.getNftList(account, chainId); } async getAdvanceRoutesLiFi(dto) { const { fromChainId, toChainId, fromTokenAddress, toTokenAddress, fromAmount, allowSwitchChain, showZeroUsd, fromAddress, } = await (0, dto_1.validateDto)(dto, dto_1.GetAdvanceRoutesLiFiDto, { addressKeys: ['fromTokenAddress', 'toTokenAddress', 'fromAddress'], }); let { toAddress } = dto; if (!toAddress) toAddress = fromAddress; const data = await this.dataModule.getAdvanceRoutesLiFi(fromTokenAddress, toTokenAddress, fromChainId, toChainId, ethers_1.BigNumber.from(fromAmount), toAddress, allowSwitchChain, fromAddress, showZeroUsd); return data; } async getStepTransaction(dto) { const { route, account } = await (0, dto_1.validateDto)(dto, dto_1.GetStepTransactionsLiFiDto, { addressKeys: ['account'] }); return this.dataModule.getStepTransaction(route, account); } async getExchangeSupportedAssets(dto) { const { page, limit, chainId, account } = await (0, dto_1.validateDto)(dto, dto_1.GetExchangeSupportedAssetsDto, { addressKeys: ['account'] }); return this.dataModule.getExchangeSupportedAssets(page, limit, chainId, account); } async getExchangeOffers(dto) { const { fromTokenAddress, toTokenAddress, fromAmount, fromChainId, showZeroUsd, fromAddress } = await (0, dto_1.validateDto)(dto, dto_1.GetExchangeOffersDto, { addressKeys: ['fromTokenAddress', 'toTokenAddress', 'fromAddress'], }); let { toAddress } = dto; if (!toAddress) toAddress = fromAddress; return this.dataModule.getExchangeOffers(fromTokenAddress, toTokenAddress, ethers_1.BigNumber.from(fromAmount), fromChainId, fromAddress, toAddress, showZeroUsd); } async getTokenLists(dto) { const { chainId } = await (0, dto_1.validateDto)(dto, dto_1.GetTokenListsDto); return this.dataModule.getTokenLists(chainId); } async getTokenListTokens(dto) { const { chainId, name } = await (0, dto_1.validateDto)(dto, dto_1.GetTokenListDto); return this.dataModule.getTokenListTokens(chainId, name); } async fetchExchangeRates(dto) { const { tokens, chainId } = dto; let data; const promises = []; const batches = [...Array(Math.ceil(tokens.length / 50))].map(() => tokens.splice(0, 50)); batches.forEach((batch) => { promises.push(this.dataModule.fetchExchangeRates(batch, chainId)); }); await Promise .allSettled(promises) .then((response) => response === null || response === void 0 ? void 0 : response.forEach((result) => { var _a; if ((result === null || result === void 0 ? void 0 : result.status) === 'fulfilled') { !data ? (data = result.value ? result.value : {}) : (data.items = ((_a = result === null || result === void 0 ? void 0 : result.value) === null || _a === void 0 ? void 0 : _a.items) ? [...data.items, ...result.value.items] : [...data.items]); } })); if (data && data.items && data.items.length) { data.error = ''; data.errored = false; data.items = [...new Map(data.items.map(item => [item['address'], item])).values()]; } else { data.items = []; } return data; } async getSupportedAssets(dto) { const { chainId, provider } = await (0, dto_1.validateDto)(dto, dto_1.GetSupportedAssetsDto); return this.dataModule.getSupportedAssets(chainId, provider); } async getQuotes(dto) { const { fromAddress, toAddress, fromChainId, toChainId, fromToken, fromAmount, slippage, provider } = await (0, dto_1.validateDto)(dto, dto_1.GetQuotesDto, { addressKeys: ['fromAddress', 'toAddress', 'fromToken'], }); return this.dataModule.getQuotes(fromAddress, toAddress, fromChainId, toChainId, fromToken, ethers_1.BigNumber.from(fromAmount), slippage, provider); } async getTransactionStatus(dto) { const { fromChainId, toChainId, transactionHash, provider } = await (0, dto_1.validateDto)(dto, dto_1.GetTransactionStatusDto); return this.dataModule.getTransactionStatus(fromChainId, toChainId, transactionHash, provider); } } exports.DataUtils = DataUtils;