From 47b348d64eb0035c7a04378ffe2870d1cf2b0766 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 1 Apr 2021 17:54:43 +0300 Subject: [PATCH] Interleave crowdloan for non-existing bids (#5001) --- .../page-parachains/src/Auctions/Auction.tsx | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/page-parachains/src/Auctions/Auction.tsx b/packages/page-parachains/src/Auctions/Auction.tsx index 74d3711fec..7dd65d24e7 100644 --- a/packages/page-parachains/src/Auctions/Auction.tsx +++ b/packages/page-parachains/src/Auctions/Auction.tsx @@ -8,6 +8,7 @@ import React, { useCallback, useRef } from 'react'; import { Table } from '@polkadot/react-components'; import { useApi, useCall } from '@polkadot/react-hooks'; +import { BN_THREE } from '@polkadot/util'; import { useTranslation } from '../translate'; import WinRange from './WinRange'; @@ -33,24 +34,36 @@ function Auction ({ auctionInfo, campaigns, className, winningData }: Props): Re ]); const interleave = useCallback( - (winners: WinnerData[], newRaise: ParaId[], asIs: boolean): WinnerData[] => { - if (asIs) { + (winners: WinnerData[], asIs: boolean): WinnerData[] => { + if (asIs || !newRaise || !auctionInfo?.leasePeriod) { return winners; } + const leasePeriod = auctionInfo.leasePeriod; + const leasePeriodEnd = leasePeriod.add(BN_THREE); const sorted = (campaigns.funds || []) - .filter(({ paraId }) => newRaise.some((n) => n.eq(paraId))) + .filter(({ firstSlot, lastSlot, paraId }) => newRaise.some((n) => n.eq(paraId)) && firstSlot.gte(leasePeriod) && lastSlot.lte(leasePeriodEnd)) .sort((a, b) => b.value.cmp(a.value)); - - return winners.map((w): WinnerData => - sorted.find(({ firstSlot, lastSlot, value }) => - w.firstSlot.eq(firstSlot) && - w.lastSlot.eq(lastSlot) && - w.value.lt(value) - ) || w + const excluded = sorted.filter(({ firstSlot, lastSlot }) => + !winners.some((w) => w.firstSlot.eq(firstSlot) && w.lastSlot.eq(lastSlot)) ); + + return winners + .map((w): WinnerData => + sorted.find(({ firstSlot, lastSlot, value }) => + w.firstSlot.eq(firstSlot) && + w.lastSlot.eq(lastSlot) && + w.value.lt(value) + ) || w + ) + .concat(...excluded) + .sort((a, b) => + a.firstSlot.eq(b.firstSlot) + ? a.lastSlot.cmp(b.lastSlot) + : a.firstSlot.cmp(b.firstSlot) + ); }, - [campaigns] + [auctionInfo, campaigns, newRaise] ); return ( @@ -68,7 +81,7 @@ function Auction ({ auctionInfo, campaigns, className, winningData }: Props): Re > {auctionInfo && newRaise && winningData?.map(({ blockNumber, winners }, round) => ( - {interleave(winners, newRaise, round !== 0 || winningData.length !== 1).map((value, index) => ( + {interleave(winners, round !== 0 || winningData.length !== 1).map((value, index) => (