diff --git a/core/src/auctions.ts b/core/src/auctions.ts index b9c2517e9..3e2808599 100644 --- a/core/src/auctions.ts +++ b/core/src/auctions.ts @@ -1,4 +1,4 @@ -import type { Auction, AuctionInitialInfo, Notifier } from './types'; +import type { Auction, AuctionInitialInfo, AuctionTransaction, Notifier } from './types'; import BigNumber from './bignumber'; import fetchAuctionsByCollateralType, { fetchAuctionStatus } from './fetch'; import { getExchangeRateBySymbol, getUniswapCalleeBySymbol, getUniswapParametersByCollateral } from './uniswap'; @@ -14,6 +14,7 @@ import { import { getSupportedCollateralTypes } from './addresses'; import { getClipperNameByCollateralType } from './contracts'; import convertNumberTo32Bytes from './helpers/convertNumberTo32Bytes'; +import { enrichAuctionWithTransactionFees, getApproximateTransactionFees } from './fees'; const enrichAuctionWithActualNumbers = async function ( network: string, @@ -107,7 +108,7 @@ export const fetchAllInitialAuctions = async function (network: string): Promise return auctionGroups.flat(); }; -export const fetchAllAuctions = async function (network: string): Promise { +export const fetchAllAuctions = async function (network: string): Promise { const auctions = await fetchAllInitialAuctions(network); // enrich them with statuses @@ -120,7 +121,13 @@ export const fetchAllAuctions = async function (network: string): Promise enrichAuctionWithMarketValues(a, network))); + const auctionsWithMarketValue = await Promise.all( + auctionsWithPriceDrop.map(a => enrichAuctionWithMarketValues(a, network)) + ); + + // enrich with profit and fee calculation + const fees = await getApproximateTransactionFees(network); + return await Promise.all(auctionsWithMarketValue.map(a => enrichAuctionWithTransactionFees(a, fees))); }; export const restartAuction = async function ( diff --git a/core/src/gas.ts b/core/src/gas.ts index dae9700e8..bedb9e17d 100644 --- a/core/src/gas.ts +++ b/core/src/gas.ts @@ -2,16 +2,24 @@ import { ethers } from 'ethers'; import BigNumber from './bignumber'; import { getNetworkConfigByType } from './constants/NETWORKS'; import { ETH_NUMBER_OF_DIGITS } from './constants/UNITS'; +import memoizee from 'memoizee'; const API_URL = 'https://ethgasstation.info/json/ethgasAPI.json'; const TRANSACTION_SPEED = 'fast'; -const getCurrentGasPrice = async function (): Promise { +const GAS_CACHE = 10 * 1000; + +const _getCurrentGasPrice = async function (): Promise { const gasData = await fetch(API_URL).then(r => r.json()); const gasPriceString = ethers.utils.formatUnits(gasData[TRANSACTION_SPEED] / 10, 'gwei'); return new BigNumber(gasPriceString); }; +const getCurrentGasPrice = memoizee(_getCurrentGasPrice, { + maxAge: GAS_CACHE, + promise: true, +}); + export const getGasPrice = async function (network: string): Promise { const networkConfig = getNetworkConfigByType(network); if (networkConfig.gasPrice) { diff --git a/frontend/store/auctions.ts b/frontend/store/auctions.ts index df047d77c..290bef934 100644 --- a/frontend/store/auctions.ts +++ b/frontend/store/auctions.ts @@ -1,4 +1,4 @@ -import type { Auction, AuctionTransaction, TransactionFees } from 'auctions-core/src/types'; +import type { Auction, AuctionTransaction } from 'auctions-core/src/types'; import { ActionContext } from 'vuex'; import { message } from 'ant-design-vue'; import { @@ -8,7 +8,6 @@ import { enrichAuctionWithPriceDropAndMarketValue, } from 'auctions-core/src/auctions'; import { checkAllExchangeRates } from 'auctions-core/src/uniswap'; -import { enrichAuctionWithTransactionFees } from 'auctions-core/src/fees'; import { checkAllCalcParameters } from 'auctions-core/src/params'; import { checkAllSupportedCollaterals } from 'auctions-core/src/addresses'; import getWallet from '~/lib/wallet'; @@ -21,7 +20,7 @@ let refetchIntervalId: ReturnType | undefined; let updateAuctionsPricesIntervalId: ReturnType | undefined; interface State { - auctionStorage: Record; + auctionStorage: Record; isFetching: boolean; isBidding: boolean; error: string | null; @@ -40,11 +39,8 @@ export const getters = { listAuctions(state: State) { return Object.values(state.auctionStorage); }, - listAuctionTransactions(state: State, getters: any, _rootState: any, rootGetters: any): AuctionTransaction[] { - const fees: TransactionFees = rootGetters['fees/fees']; - const auctions = Object.values(state.auctionStorage).map(auction => - enrichAuctionWithTransactionFees(auction, fees) - ); + listAuctionTransactions(state: State, getters: any, _rootState: any): AuctionTransaction[] { + const auctions = Object.values(state.auctionStorage); auctions.forEach(auction => { auction.isRestarting = getters.isAuctionRestarting(auction.id); }); @@ -68,11 +64,14 @@ export const getters = { }; export const mutations = { - setAuctions(state: State, auctions: Auction[]) { - const newAuctionStorage = auctions.reduce((auctionStorage: Record, auction: Auction) => { - auctionStorage[auction.id] = auction; - return auctionStorage; - }, {}); + setAuctions(state: State, auctions: AuctionTransaction[]) { + const newAuctionStorage = auctions.reduce( + (auctionStorage: Record, auction: AuctionTransaction) => { + auctionStorage[auction.id] = auction; + return auctionStorage; + }, + {} + ); const finishedAuctionIds = Object.keys(state.auctionStorage).filter(auctionID => { return !newAuctionStorage[auctionID]; }); @@ -85,7 +84,7 @@ export const mutations = { ...newAuctionStorage, }; }, - setAuction(state: State, auction: Auction) { + setAuction(state: State, auction: AuctionTransaction) { state.auctionStorage[auction.id] = auction; }, setAuctionFinish(state: State, { id, transactionAddress }: { id: string; transactionAddress: string }) { diff --git a/frontend/store/fees.ts b/frontend/store/fees.ts deleted file mode 100644 index 043cd62b2..000000000 --- a/frontend/store/fees.ts +++ /dev/null @@ -1,48 +0,0 @@ -import type { TransactionFees } from 'auctions-core/src/types'; -import { ActionContext } from 'vuex'; -import { getApproximateTransactionFees } from 'auctions-core/src/fees'; - -const REFETCH_INTERVAL = 30 * 1000; -let refetchIntervalId: ReturnType | undefined; - -interface State { - fees?: TransactionFees; -} - -export const state = (): State => ({ - fees: undefined, -}); - -export const getters = { - fees(state: State) { - return state.fees || {}; - }, -}; - -export const mutations = { - setFees(state: State, fees: TransactionFees) { - state.fees = { ...fees }; - }, -}; - -export const actions = { - async fetch({ commit, rootGetters }: ActionContext) { - const network = rootGetters['network/getMakerNetwork']; - if (!network) { - return; - } - try { - const fees = await getApproximateTransactionFees(network); - commit('setFees', fees); - } catch (error) { - console.warn(`Fees calculation error: ${error.message}`); - } - }, - async setup({ dispatch }: ActionContext) { - await dispatch('fetch'); - if (refetchIntervalId) { - clearInterval(refetchIntervalId); - } - refetchIntervalId = setInterval(() => dispatch('fetch'), REFETCH_INTERVAL); - }, -};