Skip to content

Commit

Permalink
fix: Synchronous updateAuctionsPrices calls, fixed net profit panel (#…
Browse files Browse the repository at this point in the history
…566)

Co-authored-by: Kirill Dogadin <[email protected]>
  • Loading branch information
aomafarag and KirillDogadin-std authored Dec 12, 2022
1 parent b04eb97 commit c82676e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
/>
<ProfitCheckPanel
:is-correct.sync="isProfitCheckPassed"
:gross-profit="auctionTransaction.transactionGrossProfit"
:net-profit="auctionTransaction.transactionNetProfit"
:gross-profit="transactionGrossProfit"
:net-profit="transactionNetProfit"
:is-explanations-shown="isExplanationsShown"
/>
</div>
Expand All @@ -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,
Expand All @@ -103,6 +102,7 @@

<script lang="ts">
import Vue from 'vue';
import BigNumber from 'bignumber.js';
import { Alert } from 'ant-design-vue';
import WalletConnectionCheckPanel from '~/components/panels/WalletConnectionCheckPanel.vue';
import WalletAuthorizationCheckPanel from '~/components/panels/WalletAuthorizationCheckPanel.vue';
Expand Down Expand Up @@ -197,6 +197,18 @@ export default Vue.extend({
marketSuggestionOrSelection(): string | undefined {
return this.currentMarketId || this.auctionTransaction.suggestedMarketId;
},
transactionGrossProfit(): BigNumber | undefined {
if (!this.auctionTransaction.marketDataRecords) {
return undefined;
}
return this.auctionTransaction.marketDataRecords[this.marketSuggestionOrSelection]?.transactionGrossProfit;
},
transactionNetProfit(): BigNumber | undefined {
if (!this.auctionTransaction.marketDataRecords) {
return undefined;
}
return this.auctionTransaction.marketDataRecords[this.marketSuggestionOrSelection]?.transactionNetProfit;
},
},
});
</script>
37 changes: 30 additions & 7 deletions frontend/helpers/generateFakeAuction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -29,6 +34,24 @@ export const generateFakeMarketData = function (
};
};

const sortMarketDataRecords = function (marketDataRecords: Record<string, MarketData>): [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()));
Expand All @@ -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'],
Expand Down Expand Up @@ -68,18 +90,19 @@ export const generateFakeAuction = function () {

const marketDataRecords: Record<string, MarketData> = {
'Uniswap V3': {
...generateFakeMarketData(isActive, approximateUnitPrice, collateralAmount),
...generateFakeMarketData(isActive, approximateUnitPrice, collateralAmount, true),
pools: fakePoolsTwoSteps,
},
'Curve V3': {
marketUnitPrice: new BigNumber(NaN),
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;
Expand Down
18 changes: 13 additions & 5 deletions frontend/store/auctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<State, State>,
Expand Down Expand Up @@ -330,12 +333,17 @@ export const actions = {
console.error(`Auction redo error: ${error.message}`);
}
},
updateAuctionsPrices({ getters, dispatch }: ActionContext<State, State>) {
const auctions = getters.listAuctions;
async updateAuctionsPrices({ getters, dispatch }: ActionContext<State, State>) {
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<State, State>, id: string) {
const network = rootGetters['network/getMakerNetwork'];
Expand Down

0 comments on commit c82676e

Please sign in to comment.