From c82676e709c430a808fbaefe7cd52517e85af216 Mon Sep 17 00:00:00 2001 From: Abdelrhman Farag <85403429+aomafarag@users.noreply.github.com> Date: Mon, 12 Dec 2022 09:45:07 +0100 Subject: [PATCH] fix: Synchronous updateAuctionsPrices calls, fixed net profit panel (#566) Co-authored-by: Kirill Dogadin --- .../CollateralAuctionSwapTransaction.vue | 18 +++++++-- frontend/helpers/generateFakeAuction.ts | 37 +++++++++++++++---- frontend/store/auctions.ts | 18 ++++++--- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/frontend/components/auction/collateral/CollateralAuctionSwapTransaction.vue b/frontend/components/auction/collateral/CollateralAuctionSwapTransaction.vue index c02cf2cb1..c47f6107f 100644 --- a/frontend/components/auction/collateral/CollateralAuctionSwapTransaction.vue +++ b/frontend/components/auction/collateral/CollateralAuctionSwapTransaction.vue @@ -67,8 +67,8 @@ /> @@ -89,7 +89,6 @@ :is-wallet-authed="isWalletAuthorized" :is-collateral-authed="isWalletCollateralAuthorizationCheckPassed" :fees="fees" - :transaction-gross-profit="auctionTransaction.transactionGrossProfit" @execute=" $emit('execute', { id: auctionTransaction.id, @@ -103,6 +102,7 @@ diff --git a/frontend/helpers/generateFakeAuction.ts b/frontend/helpers/generateFakeAuction.ts index 32510c747..06fb3ee36 100644 --- a/frontend/helpers/generateFakeAuction.ts +++ b/frontend/helpers/generateFakeAuction.ts @@ -4,15 +4,20 @@ import BigNumber from 'bignumber.js'; import COLLATERALS from 'auctions-core/src/constants/COLLATERALS'; import { AuctionTransaction, MarketData } from '~/../core/src/types'; -const FAKE_CALLEES = ['Uniswap V3', '1inch']; // Curve V3 marketUnitPrice is NaN (see below) - export const generateFakeMarketData = function ( isActive: boolean, approximateUnitPrice: BigNumber, - collateralAmount: BigNumber + collateralAmount: BigNumber, + isRatioPositive: boolean ) { const marketUnitPriceToUnitPriceRatio = isActive - ? new BigNumber(faker.datatype.number({ min: -0.3, max: 0.3, precision: 0.001 })) + ? new BigNumber( + faker.datatype.number({ + min: isRatioPositive ? 0 : -0.3, + max: isRatioPositive ? 0.3 : 0, + precision: 0.001, + }) + ) : undefined; const marketUnitPrice = approximateUnitPrice.multipliedBy( new BigNumber(1).minus(marketUnitPriceToUnitPriceRatio || 0) @@ -29,6 +34,24 @@ export const generateFakeMarketData = function ( }; }; +const sortMarketDataRecords = function (marketDataRecords: Record): [string, MarketData][] { + const marketDataArraySorted = Object.entries(marketDataRecords || {}); + marketDataArraySorted.sort((a, b) => { + // push NaNs to the end + if (a[1].marketUnitPrice.isNaN() && b[1].marketUnitPrice.isNaN()) { + return 1; + } + if (a[1].marketUnitPrice.isNaN()) { + return 1; + } + if (b[1].marketUnitPrice.isNaN()) { + return -1; + } + return b[1].marketUnitPrice.minus(a[1].marketUnitPrice).toNumber(); + }); + return marketDataArraySorted; +}; + export const generateFakeAuction = function () { const index = faker.datatype.number(); const collateralAmount = new BigNumber(parseFloat(faker.finance.amount())); @@ -38,7 +61,6 @@ export const generateFakeAuction = function () { const isFinished = faker.datatype.boolean(); const approximateUnitPrice = totalPrice.dividedBy(collateralAmount); const collateralObject = COLLATERALS['ETH-A']; - const suggestedMarketId = faker.helpers.randomize(FAKE_CALLEES); const fakePoolsTwoSteps = [ { addresses: ['0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'], @@ -68,7 +90,7 @@ export const generateFakeAuction = function () { const marketDataRecords: Record = { 'Uniswap V3': { - ...generateFakeMarketData(isActive, approximateUnitPrice, collateralAmount), + ...generateFakeMarketData(isActive, approximateUnitPrice, collateralAmount, true), pools: fakePoolsTwoSteps, }, 'Curve V3': { @@ -76,10 +98,11 @@ export const generateFakeAuction = function () { pools: fakePoolsNanMarketUnitPrice, }, '1inch': { - ...generateFakeMarketData(isActive, approximateUnitPrice, collateralAmount), + ...generateFakeMarketData(isActive, approximateUnitPrice, collateralAmount, false), pools: fakePoolsOneStep, }, }; + const suggestedMarketId = sortMarketDataRecords(marketDataRecords)[0][0]; const marketUnitPriceToUnitPriceRatio = marketDataRecords[suggestedMarketId].marketUnitPriceToUnitPriceRatio; const marketUnitPrice = marketDataRecords[suggestedMarketId].marketUnitPrice; const transactionGrossProfit = marketDataRecords[suggestedMarketId].transactionGrossProfit; diff --git a/frontend/store/auctions.ts b/frontend/store/auctions.ts index 7da3bcccf..4cd7be806 100644 --- a/frontend/store/auctions.ts +++ b/frontend/store/auctions.ts @@ -235,7 +235,10 @@ export const actions = { clearInterval(updateAuctionsPricesIntervalId); } refetchIntervalId = setInterval(() => dispatch('update'), REFETCH_INTERVAL); - updateAuctionsPricesIntervalId = setInterval(() => dispatch('updateAuctionsPrices'), TIMER_INTERVAL); + updateAuctionsPricesIntervalId = setInterval( + async () => await dispatch('updateAuctionsPrices'), + TIMER_INTERVAL + ); }, async bidWithCallee( { getters, commit, rootGetters }: ActionContext, @@ -330,12 +333,17 @@ export const actions = { console.error(`Auction redo error: ${error.message}`); } }, - updateAuctionsPrices({ getters, dispatch }: ActionContext) { - const auctions = getters.listAuctions; + async updateAuctionsPrices({ getters, dispatch }: ActionContext) { + const auctions = getters.listAuctionTransactions; + if (!auctions) { + return; + } - auctions.forEach((auction: Auction) => { - dispatch('updateAuctionPrice', auction.id); + const promises = auctions.map((auction: Auction) => { + return dispatch('updateAuctionPrice', auction.id); }); + + await Promise.all(promises); }, async updateAuctionPrice({ getters, commit, rootGetters }: ActionContext, id: string) { const network = rootGetters['network/getMakerNetwork'];