Skip to content

Commit

Permalink
refactor: move big.js and axios-prevent-concurrency to separate modul…
Browse files Browse the repository at this point in the history
…es; update axios dep
  • Loading branch information
shrpne committed Apr 14, 2023
1 parent 82bea72 commit f040515
Show file tree
Hide file tree
Showing 52 changed files with 276 additions and 228 deletions.
6 changes: 4 additions & 2 deletions api/chainik.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import axios from 'axios';
import {cacheAdapterEnhancer, Cache} from 'axios-extensions';
import {CHAINIK_API_URL, NETWORK, MAINNET} from "~/assets/variables.js";
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addToCamelInterceptor from '~/assets/axios-to-camel.js';

const instance = axios.create({
baseURL: CHAINIK_API_URL,
adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false}),
adapter: cacheAdapterEnhancer(getDefaultAdapter(), { enabledByDefault: false}),
});
addToCamelInterceptor(instance);

// 10 min cache
const coinsCache = new Cache({ttl: 10 * 60 * 1000, max: 100});
/**
* @return {Promise<Object.<number, string|null>>}
* @return {Promise<Object.<number, string>>}
*/
export function getCoinIconList() {
if (NETWORK !== MAINNET) {
Expand All @@ -23,6 +24,7 @@ export function getCoinIconList() {
})
.then((response) => {
const coins = response.data;
/** @type {Record<number, string>} */
let iconMap = {};
coins.forEach((coin) => {
iconMap[coin.id] = coin.icon;
Expand Down
5 changes: 3 additions & 2 deletions api/explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import coinBlockList from 'minter-coin-block-list';
import {getVerifiedMinterCoinList} from '~/api/hub.js';
import {getCoinIconList as getChainikIconList} from '~/api/chainik.js';
import {BASE_COIN, EXPLORER_API_URL, TX_STATUS} from '~/assets/variables.js';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import {addTimeInterceptor} from '~/assets/axios-time-offset.js';
import preventConcurrencyAdapter from '~/assets/axios-prevent-concurrency.js';
import preventConcurrencyAdapter from 'axios-prevent-concurrency';
import debounceAdapter from '~/assets/axios-debounce.js';


Expand Down Expand Up @@ -42,7 +43,7 @@ function restoreErrorAdapter(adapter) {
};
}

const adapter = (($ = axios.defaults.adapter) => {
const adapter = (($ = getDefaultAdapter()) => {
$ = save404Adapter($);
$ = cacheAdapterEnhancer($, { enabledByDefault: false});
$ = restoreErrorAdapter($);
Expand Down
33 changes: 19 additions & 14 deletions api/farm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {cacheAdapterEnhancer, Cache} from 'axios-extensions';
import coinBlockList from 'minter-coin-block-list';
import {FARM_API_URL, NETWORK, MAINNET} from "~/assets/variables.js";
import {arrayToMap} from '~/assets/utils/collection.js';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import {getPoolList, getPoolByToken} from '~/api/explorer.js';

Expand All @@ -26,7 +27,7 @@ function isFarmTrusted(farmItem) {

const instance = axios.create({
baseURL: FARM_API_URL,
adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false}),
adapter: cacheAdapterEnhancer(getDefaultAdapter(), { enabledByDefault: false}),
});
addToCamelInterceptor(instance);

Expand All @@ -35,7 +36,8 @@ addToCamelInterceptor(instance);
const farmCache = new Cache({ttl: 1 * 60 * 1000, max: 100});

/**
* @param {boolean} [onlyTrusted=false]
* @param {object} [options]
* @param {boolean} [options.onlyTrusted=false]
* @return {Promise<Array<FarmItem>>}
*/
export function getFarmList({onlyTrusted = false} = {}) {
Expand All @@ -61,11 +63,13 @@ export function getFarmList({onlyTrusted = false} = {}) {
.forEach((farmItem) => {
// ensure farm map item
if (!farmMap[farmItem.tokenSymbol]) {
const cleanFarmItem = {...farmItem};
const cleanFarmItem = /** @type {FarmItem} */({
...farmItem,
rewardCoinList: [],
percent: 0,
finishDateList: [],
});
delete cleanFarmItem.rewardCoin;
cleanFarmItem.rewardCoinList = [];
cleanFarmItem.percent = 0;
cleanFarmItem.finishDateList = [];
farmMap[farmItem.tokenSymbol] = cleanFarmItem;
}

Expand All @@ -75,7 +79,7 @@ export function getFarmList({onlyTrusted = false} = {}) {
// check if finishDateList should be updated
const hasCurrentDate = farmMap[farmItem.tokenSymbol].finishDateList.find((item) => {
// less than 1 day difference
return Math.abs(new Date(item) - new Date(farmItem.finishAt)) <= 24 * 60 * 60 * 1000;
return Math.abs(new Date(item).getTime() - new Date(farmItem.finishAt).getTime()) <= 24 * 60 * 60 * 1000;
});
if (!hasCurrentDate) {
farmMap[farmItem.tokenSymbol].finishDateList.push(farmItem.finishAt);
Expand Down Expand Up @@ -153,7 +157,8 @@ function prepareFarmProgram(farmItem) {
/**
* Fill with pool data and aggregate identical pools
* @param {Promise<Array<FarmItem>>|Array<FarmItem>} farmPromise
* @param {boolean} [trySharePoolsRequest] - will fetch default top 50 pools and additionally will fetch all trusted (other untrusted and not in top50 will be skipped)
* @param {object} [options]
* @param {boolean} [options.trySharePoolsRequest] - will fetch default top 50 pools and additionally will fetch all trusted (other untrusted and not in top50 will be skipped)
* @return {Promise<Array<FarmItemWithPoolData>>}
*/
export function fillFarmWithPoolData(farmPromise, {trySharePoolsRequest} = {}) {
Expand Down Expand Up @@ -217,7 +222,7 @@ function addPoolFields(farmProgram, pool) {
/**
* @param {Pool|FarmProgramWithPoolData|FarmItemWithPoolData} pool
* @param {string} symbol
* @return {Pool.amount0|Pool.amount1}
* @return {Pool['amount0']|Pool['amount1']}
*/
export function getAmountFromPool(pool, symbol) {
if (pool.coin0.symbol === symbol) {
Expand All @@ -240,11 +245,11 @@ export function getAmountFromPool(pool, symbol) {

/**
* @typedef {object} FarmExtraPoolData
* @property {Pool.liquidity} liquidity
* @property {Pool.liquidityBip} liquidityBip
* @property {Pool.tradeVolumeBip1D} tradeVolumeBip1D
* @property {Pool.amount0} amount0
* @property {Pool.amount1} amount1
* @property {Pool['liquidity']} liquidity
* @property {Pool['liquidityBip']} liquidityBip
* @property {Pool['tradeVolumeBip1D']} tradeVolumeBip1D
* @property {Pool['amount0']} amount0
* @property {Pool['amount1']} amount1
*/

/**
Expand Down
3 changes: 2 additions & 1 deletion api/gate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import EstimateTxCommission from 'minter-js-sdk/src/api/estimate-tx-commission.j
import {ESTIMATE_SWAP_TYPE} from 'minter-js-sdk/src/variables.js';
import {ReplaceCoinSymbol, ReplaceCoinSymbolByPath} from 'minter-js-sdk/src/api/replace-coin.js';
import {GATE_API_URL, CHAIN_ID} from '~/assets/variables.js';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import debounceAdapter from '~/assets/axios-debounce.js';
import {getSwapEstimate as explorerGetSwapEstimate} from '~/api/explorer.js';

const adapter = (($ = axios.defaults.adapter) => {
const adapter = (($ = getDefaultAdapter()) => {
$ = cacheAdapterEnhancer($, { enabledByDefault: false});
$ = debounceAdapter($, {time: 500, leading: false});
return $;
Expand Down
5 changes: 3 additions & 2 deletions api/hub.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import stripZeros from 'pretty-num/src/strip-zeros.js';
import {isValidAddress as isValidMinterAddress} from 'minterjs-util';
import {isValidAddress as isValidEthAddress} from 'ethereumjs-util';
import {getCoinList, getTransaction} from '~/api/explorer.js';
import Big from '~/assets/big.js';
import Big from 'minterjs-util/src/big.js';
import {HUB_API_URL, HUB_TRANSFER_STATUS, HUB_CHAIN_ID, HUB_NETWORK, NETWORK, MAINNET, BASE_COIN, HUB_CHAIN_BY_ID, HUB_CHAIN_DATA, HUB_COIN_DATA, NATIVE_COIN_ADDRESS, HUB_BUY_STAGE as LOADING_STAGE} from "~/assets/variables.js";
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import {isHubTransferFinished} from '~/assets/utils.js';

const instance = axios.create({
baseURL: HUB_API_URL,
adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false}),
adapter: cacheAdapterEnhancer(getDefaultAdapter(), { enabledByDefault: false}),
});
addToCamelInterceptor(instance);

Expand Down
3 changes: 2 additions & 1 deletion api/metagarden.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import axios from 'axios';
import {cacheAdapterEnhancer, Cache} from 'axios-extensions';
import {METAGARDEN_API_URL, METAGARDEN_ADMIN_API_KEY, NETWORK, MAINNET} from "~/assets/variables.js";
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import addEcdsaAuthInterceptor, {authHeaderKeyGenerator} from '~/assets/axios-ecdsa-auth.js';

const instance = axios.create({
baseURL: METAGARDEN_API_URL,
// adapter: cacheAdapterEnhancer(axios.defaults.adapter, {
// adapter: cacheAdapterEnhancer(getDefaultAdapter(), {
// enabledByDefault: false,
// cacheKeyGenerator: authHeaderKeyGenerator,
// }),
Expand Down
4 changes: 3 additions & 1 deletion api/portfolio.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {PORTFOLIO_API_URL, NETWORK, MAINNET} from "~/assets/variables.js";
import {toSnake} from '~/assets/utils/snake-case.js';
import {arrayToMap} from '~/assets/utils/collection.js';
import NotFoundError from '~/assets/utils/error-404.js';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import addEcdsaAuthInterceptor, {authHeaderKeyGenerator} from '~/assets/axios-ecdsa-auth.js';


const instance = axios.create({
baseURL: PORTFOLIO_API_URL,
adapter: cacheAdapterEnhancer(axios.defaults.adapter, {
adapter: cacheAdapterEnhancer(getDefaultAdapter(), {
enabledByDefault: false,
cacheKeyGenerator: authHeaderKeyGenerator,
}),
Expand Down Expand Up @@ -62,6 +63,7 @@ export function updatePortfolio(id, portfolio, privateKey) {
}

/**
* @param {string|number} id
* @return {Promise<Portfolio>}
*/
export function getPortfolio(id) {
Expand Down
3 changes: 2 additions & 1 deletion api/referral.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import axios from 'axios';
import {cacheAdapterEnhancer, Cache} from 'axios-extensions';
import {REFERRAL_API_URL, NETWORK, MAINNET} from "~/assets/variables.js";
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import addEcdsaAuthInterceptor, {authHeaderKeyGenerator} from '~/assets/axios-ecdsa-auth.js';

const instance = axios.create({
baseURL: REFERRAL_API_URL,
// adapter: cacheAdapterEnhancer(axios.defaults.adapter, {
// adapter: cacheAdapterEnhancer(getDefaultAdapter(), {
// enabledByDefault: false,
// cacheKeyGenerator: authHeaderKeyGenerator,
// }),
Expand Down
3 changes: 2 additions & 1 deletion api/smart-wallet-relay.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import axios from 'axios';
// import {cacheAdapterEnhancer, Cache} from 'axios-extensions';
import {HUB_CHAIN_BY_ID, SMART_WALLET_RELAY_API_URL} from "~/assets/variables.js";
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import {createCancelableSignal} from '~/assets/utils/cancelable-signal.js';
import CancelError from '~/assets/utils/error-cancel.js';
import {wait} from '~/assets/utils/wait.js';

const instance = axios.create({
baseURL: SMART_WALLET_RELAY_API_URL,
// adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false}),
// adapter: cacheAdapterEnhancer(getDefaultAdapter(), { enabledByDefault: false}),
});
addToCamelInterceptor(instance);

Expand Down
6 changes: 4 additions & 2 deletions api/staking.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import axios from 'axios';
import {cacheAdapterEnhancer, Cache} from 'axios-extensions';
import Big from '~/assets/big.js';
import Big from 'minterjs-util/src/big.js';
import {STAKING_API_URL, PREMIUM_STAKE_PROGRAM_ID} from "~/assets/variables.js";
import NotFoundError from '~/assets/utils/error-404.js';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addToCamelInterceptor from '~/assets/axios-to-camel.js';

const instance = axios.create({
baseURL: STAKING_API_URL,
adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false}),
adapter: cacheAdapterEnhancer(getDefaultAdapter(), { enabledByDefault: false}),
});
addToCamelInterceptor(instance);

Expand Down Expand Up @@ -100,6 +101,7 @@ export function getAddressPremiumLevel(address) {
* @return {number}
*/
export function getPremiumLevel(amount) {
amount = +amount;
if (amount >= 1000000) {
return 4;
}
Expand Down
5 changes: 3 additions & 2 deletions api/swap-0x.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {NATIVE_COIN_ADDRESS, ZERO_X_ETHEREUM_API_URL, ZERO_X_BSC_API_URL} from "
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import {getMaxEstimationLimit, getMinEstimationLimit} from '~/assets/utils/swap-limit.js';
import {buildApproveTx, getAllowance} from '~/api/web3.js';
import Big from '~/assets/big.js';
import Big from 'minterjs-util/src/big.js';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';

const ZERO_X_API_URL_LIST = {
1: ZERO_X_ETHEREUM_API_URL,
Expand All @@ -14,7 +15,7 @@ const ZERO_X_API_URL_LIST = {

const instance = axios.create({
// baseURL: ZERO_X_API_URL,
adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false}),
adapter: cacheAdapterEnhancer(getDefaultAdapter(), { enabledByDefault: false}),
});
// addToCamelInterceptor(instance);

Expand Down
5 changes: 3 additions & 2 deletions api/swap-1inch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import axios from 'axios';
import {Cache, cacheAdapterEnhancer} from 'axios-extensions';
import {ONE_INCH_API_URL, NETWORK, MAINNET, NATIVE_COIN_ADDRESS} from "~/assets/variables.js";
import preventConcurrencyAdapter from '~/assets/axios-prevent-concurrency.js';
import preventConcurrencyAdapter from 'axios-prevent-concurrency';
import {fromErcDecimals, addApproveTx} from '~/api/web3.js';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';

const adapter = (($ = axios.defaults.adapter) => {
const adapter = (($ = getDefaultAdapter()) => {
$ = cacheAdapterEnhancer($, { enabledByDefault: false});
$ = preventConcurrencyAdapter($);
return $;
Expand Down
5 changes: 3 additions & 2 deletions api/swap-hub-deposit-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import axios from 'axios';
import {Cache, cacheAdapterEnhancer} from 'axios-extensions';
import {HUB_DEPOSIT_PROXY_API_URL, HUB_DEPOSIT_PROXY_ETHEREUM_CONTRACT_ADDRESS, HUB_DEPOSIT_PROXY_BSC_CONTRACT_ADDRESS, NATIVE_COIN_ADDRESS, HUB_CHAIN_BY_ID} from "~/assets/variables.js";
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import preventConcurrencyAdapter from '~/assets/axios-prevent-concurrency.js';
import preventConcurrencyAdapter from 'axios-prevent-concurrency';
import {buildTxForSwap as buildOneInchTx, prepareProtocolsCached} from '~/api/swap-1inch.js';
import {AbiEncoder, addApproveTx, getHubDestinationAddressBytes, getHubDestinationChainBytes} from '~/api/web3.js';
import hubProxyAbi from '~/assets/abi-hub-proxy.js';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';

const HUB_DEPOSIT_PROXY_CONTRACT_ADDRESS_LIST = {
1: HUB_DEPOSIT_PROXY_ETHEREUM_CONTRACT_ADDRESS,
56: HUB_DEPOSIT_PROXY_BSC_CONTRACT_ADDRESS,
};


const adapter = (($ = axios.defaults.adapter) => {
const adapter = (($ = getDefaultAdapter()) => {
$ = cacheAdapterEnhancer($, { enabledByDefault: false});
$ = preventConcurrencyAdapter($);
return $;
Expand Down
5 changes: 3 additions & 2 deletions api/swap-paraswap.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import axios from 'axios';
import {Cache, cacheAdapterEnhancer} from 'axios-extensions';
import Big from 'minterjs-util/src/big.js';
import {ParaSwapSwapSide} from '~/api/swap-paraswap-models';
import {fromErcDecimals, getAllowance, buildApproveTx} from '~/api/web3.js';
import Big from '~/assets/big.js';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import {getMaxEstimationLimit, getMinEstimationLimit} from '~/assets/utils/swap-limit.js';
// import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import {PARASWAP_API_URL, NETWORK, MAINNET, SMART_WALLET_RELAY_BROADCASTER_ADDRESS, NATIVE_COIN_ADDRESS} from "~/assets/variables.js";


const instance = axios.create({
baseURL: PARASWAP_API_URL,
adapter: cacheAdapterEnhancer(axios.defaults.adapter, { enabledByDefault: false}),
adapter: cacheAdapterEnhancer(getDefaultAdapter(), { enabledByDefault: false}),
});
// addToCamelInterceptor(instance);

Expand Down
5 changes: 3 additions & 2 deletions api/telegram.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import axios from 'axios';
import {cacheAdapterEnhancer, Cache} from 'axios-extensions';
import {getDefaultAdapter} from '~/assets/axios-default-adapter.js';
import addEcdsaAuthInterceptor, {signRequest, authHeaderKeyGenerator} from '~/assets/axios-ecdsa-auth.js';
import {TELEGRAM_AUTH_API_URL, TELEGRAM_LEGACY_AUTH_API_URL} from "~/assets/variables.js";
import addToCamelInterceptor from '~/assets/axios-to-camel.js';
import {TELEGRAM_AUTH_API_URL, TELEGRAM_LEGACY_AUTH_API_URL} from "~/assets/variables.js";

const instance = axios.create({
baseURL: TELEGRAM_AUTH_API_URL,
adapter: cacheAdapterEnhancer(axios.defaults.adapter, {
adapter: cacheAdapterEnhancer(getDefaultAdapter(), {
enabledByDefault: false,
cacheKeyGenerator: authHeaderKeyGenerator,
}),
Expand Down
2 changes: 1 addition & 1 deletion api/web3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Big from '~/assets/big.js';
import Big from 'minterjs-util/src/big.js';
import Eth from 'web3-eth';
import Utils from 'web3-utils';
import Contract from 'web3-eth-contract';
Expand Down
12 changes: 12 additions & 0 deletions assets/axios-default-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import axios from 'axios';
import adapters from 'axios/lib/adapters/adapters.js';

const getAdapter = adapters.getAdapter;

/**
*
* @return {import('axios').AxiosAdapter}
*/
export function getDefaultAdapter() {
return getAdapter(axios.defaults.adapter);
}
5 changes: 0 additions & 5 deletions assets/axios-ecdsa-auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* @author Kuitos
* @homepage https://github.com/kuitos/
* @since 2017-10-12
*/
import { AxiosInstance } from 'axios';
declare module 'axios' {
interface AxiosRequestConfig {
Expand Down
Loading

0 comments on commit f040515

Please sign in to comment.