@dgpub/prime-sdk
Version:
Etherspot Prime (Account Abstraction) SDK
279 lines (278 loc) • 10.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataModule = void 0;
const common_1 = require("../common");
const api_1 = require("../api");
const constants_1 = require("../api/constants");
const constants_2 = require("./constants");
class DataModule {
constructor(apiKey = '') {
this.apiKey$ = new common_1.ObjectSubject('');
this.apiService = new api_1.RestApiService();
this.switchCurrentApi(apiKey);
}
get currentApi() {
return this.apiKey$.value;
}
switchCurrentApi(currentApi) {
this.apiKey$.nextData(currentApi);
return this.currentApi;
}
async getAccountBalances(account, tokens, chainId, provider) {
try {
const queryParams = {
'api-key': this.currentApi,
account,
chainId,
provider,
tokens: tokens.length ? tokens : []
};
const balances = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_ACCOUNT_BALANCES, constants_1.MethodTypes.GET, queryParams);
return balances;
}
catch (error) {
throw new Error(error.message || 'Failed to get account balances');
}
}
async getTransaction(hash, chainId) {
try {
const queryParams = {
'api-key': this.currentApi,
hash,
chainId,
};
const response = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_TRANSACTION, constants_1.MethodTypes.GET, queryParams);
return response.transaction;
}
catch (error) {
throw new Error(error.message || 'Failed to get transaction');
}
}
async getTransactions(account, chainId, page, limit) {
try {
const queryParams = {
'api-key': this.currentApi,
account,
chainId,
page,
limit,
};
const response = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_TRANSACTIONS, constants_1.MethodTypes.GET, queryParams);
return response;
}
catch (error) {
throw new Error(error.message || 'Failed to get transactions');
}
}
async getNftList(account, chainId) {
try {
const queryParams = {
'api-key': this.currentApi,
account,
chainId,
};
const nfts = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_ACCOUNT_NFTS, constants_1.MethodTypes.GET, queryParams);
return nfts;
}
catch (error) {
throw new Error(error.message || 'Failed to get nft list');
}
}
async getAdvanceRoutesLiFi(fromTokenAddress, toTokenAddress, fromChainId, toChainId, fromAmount, toAddress, allowSwitchChain, fromAddress, showZeroUsd) {
const account = fromAddress;
let data = null;
try {
const queryParams = {
'api-key': this.currentApi,
account,
fromTokenAddress,
toTokenAddress,
fromChainId,
toChainId,
fromAmount: fromAmount.toString(),
toAddress,
allowSwitchChain,
fromAddress,
showZeroUsd,
};
const response = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_ADVANCE_ROUTES_LIFI, constants_1.MethodTypes.GET, queryParams);
data = JSON.parse(response.data);
return data;
}
catch (error) {
throw new Error(error.message || 'Failed to advance routes from LiFi');
}
}
async getStepTransaction(selectedRoute, account) {
try {
const route = JSON.stringify(selectedRoute);
const queryParams = {
'api-key': this.currentApi,
};
const body = {
route,
account
};
const response = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_STEP_TRANSACTIONS, constants_1.MethodTypes.POST, queryParams, body);
return {
items: response.transactions
};
}
catch (error) {
throw new Error(error.message || 'Failed to get step transaction from LIFI');
}
}
async getExchangeSupportedAssets(page = null, limit = null, chainId, account) {
try {
const queryParams = {
'api-key': this.currentApi,
account,
page: page || 1,
limit: limit || 100,
chainId
};
const assets = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_EXCHANGE_SUPPORTED_ASSETS, constants_1.MethodTypes.GET, queryParams);
return assets;
}
catch (error) {
throw new Error(error.message || 'Failed to get exchange supported assets');
}
}
async getExchangeOffers(fromTokenAddress, toTokenAddress, fromAmount, fromChainId, fromAddress, toAddress, showZeroUsd) {
const account = fromAddress;
try {
const queryParams = {
'api-key': this.currentApi,
account,
fromTokenAddress,
toTokenAddress,
fromAmount: fromAmount.toString(),
chainId: fromChainId,
fromAddress,
toAddress,
showZeroUsd,
};
const result = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_EXCHANGE_OFFERS, constants_1.MethodTypes.GET, queryParams);
return result ? result.items : null;
}
catch (error) {
throw new Error(error.message || 'Failed to get exchange offers');
}
}
async getTokenLists(chainId) {
try {
const queryParams = {
'api-key': this.currentApi,
chainId,
};
const result = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_TOKEN_LISTS, constants_1.MethodTypes.GET, queryParams);
return result ? result.items : [];
}
catch (error) {
throw new Error(error.message || 'Failed to get token lists');
}
}
async getTokenListTokens(chainId, name = null) {
try {
const queryParams = {
'api-key': this.currentApi,
chainId,
name,
};
const result = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.GET_TOKEN_LIST_TOKENS, constants_1.MethodTypes.GET, queryParams);
return result ? result.tokens : [];
}
catch (error) {
throw new Error(error.message || 'Failed to get token list tokens');
}
}
async fetchExchangeRates(tokens, chainId) {
try {
const queryParams = {
'api-key': this.currentApi,
chainId,
tokens,
};
const result = await this.apiService.makeRequest(constants_1.API_ENDPOINTS.EXCHANGE_RATES, constants_1.MethodTypes.GET, queryParams);
return result ? result.exchangeRates : null;
}
catch (error) {
throw new Error(error.message || 'Failed to fetch exchange rates');
}
}
async getSupportedAssets(chainId, provider) {
try {
const queryParams = {
'api-key': this.currentApi,
chainId,
};
let apiUrl;
switch (provider) {
case constants_2.BridgingProvider.Connext:
apiUrl = constants_1.API_ENDPOINTS.GET_CONNEXT_SUPPORTED_ASSETS;
break;
default:
apiUrl = constants_1.API_ENDPOINTS.GET_CONNEXT_SUPPORTED_ASSETS;
break;
}
const result = await this.apiService.makeRequest(apiUrl, constants_1.MethodTypes.GET, queryParams);
return result ? result.tokens : [];
}
catch (error) {
throw new Error(error.message || 'Failed to get supported assets');
}
}
async getQuotes(fromAddress, toAddress, fromChainId, toChainId, fromToken, fromAmount, slippage, provider) {
try {
const queryParams = {
'api-key': this.currentApi,
fromAddress,
toAddress,
fromChainId,
toChainId,
fromToken,
fromAmount: fromAmount.toString(),
slippage
};
let apiUrl;
switch (provider) {
case constants_2.BridgingProvider.Connext:
apiUrl = constants_1.API_ENDPOINTS.GET_CONNEXT_QUOTE_TRANSACTIONS;
break;
default:
apiUrl = constants_1.API_ENDPOINTS.GET_CONNEXT_QUOTE_TRANSACTIONS;
break;
}
const result = await this.apiService.makeRequest(apiUrl, constants_1.MethodTypes.GET, queryParams);
return result ? result.transactions : [];
}
catch (error) {
throw new Error(error.message || 'Failed to get quotes transactions');
}
}
async getTransactionStatus(fromChainId, toChainId, transactionHash, provider) {
try {
const queryParams = {
'api-key': this.currentApi,
fromChainId,
toChainId,
transactionHash,
};
let apiUrl;
switch (provider) {
case constants_2.BridgingProvider.Connext:
apiUrl = constants_1.API_ENDPOINTS.GET_CONNEXT_TRANSACTION_STATUS;
break;
default:
apiUrl = constants_1.API_ENDPOINTS.GET_CONNEXT_TRANSACTION_STATUS;
break;
}
const result = await this.apiService.makeRequest(apiUrl, constants_1.MethodTypes.GET, queryParams);
return result ? result : null;
}
catch (error) {
throw new Error(error.message || 'Failed to get transaction status');
}
}
}
exports.DataModule = DataModule;