Skip to content

Commit

Permalink
1678 adjust active auction filter (#1684)
Browse files Browse the repository at this point in the history
* Adjust active auction filter & revers order auctions

* changeset

* fix imports

* fix tests

* add docs
  • Loading branch information
Valentine1898 authored Aug 12, 2024
1 parent ef54d56 commit 894d038
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 67 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-bananas-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'minifront': minor
---

Adjust active auction filter & reverse order auctions
4 changes: 4 additions & 0 deletions apps/minifront/src/components/swap/auction-list/filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const OPTIONS: PopoverMenuItem<TFilter>[] = [
label: 'Active',
value: 'active',
},
{
label: 'Inactive',
value: 'inactive',
},
{
label: 'All',
value: 'all',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ const MOCK_AUCTION_INFO_1: AuctionInfo = {
};

const MOCK_AUCTION_2 = new DutchAuction({
description: {
startHeight: 11n,
endHeight: 20n,
},
state: {
seq: 1n,
},
Expand All @@ -38,46 +34,36 @@ const MOCK_AUCTION_ID_2 = new AuctionId({ inner: new Uint8Array([2]) });
const MOCK_AUCTION_INFO_2: AuctionInfo = {
auction: MOCK_AUCTION_2,
id: MOCK_AUCTION_ID_2,
localSeqNum: 0n,
localSeqNum: 1n,
addressIndex: new AddressIndex({ account: 0 }),
};

const MOCK_AUCTION_3 = new DutchAuction({
description: {
startHeight: 1n,
endHeight: 10n,
},
state: {
seq: 0n,
seq: 1n,
},
});
const MOCK_AUCTION_ID_3 = new AuctionId({ inner: new Uint8Array([3]) });
const MOCK_AUCTION_INFO_3: AuctionInfo = {
auction: MOCK_AUCTION_3,
id: MOCK_AUCTION_ID_3,
localSeqNum: 0n,
localSeqNum: 1n,
addressIndex: new AddressIndex({ account: 0 }),
};

const MOCK_AUCTION_4 = new DutchAuction({
description: {
startHeight: 21n,
endHeight: 30n,
},
state: {
seq: 0n,
seq: 2n,
},
});
const MOCK_AUCTION_ID_4 = new AuctionId({ inner: new Uint8Array([4]) });
const MOCK_AUCTION_INFO_4: AuctionInfo = {
auction: MOCK_AUCTION_4,
id: MOCK_AUCTION_ID_4,
localSeqNum: 0n,
localSeqNum: 2n,
addressIndex: new AddressIndex({ account: 0 }),
};

const MOCK_FULL_SYNC_HEIGHT = 15n;

const AUCTION_INFOS: AuctionInfo[] = [
MOCK_AUCTION_INFO_1,
MOCK_AUCTION_INFO_2,
Expand All @@ -88,39 +74,26 @@ const AUCTION_INFOS: AuctionInfo[] = [
describe('getFilteredAuctionInfos()', () => {
describe('when the `filter` is `all`', () => {
it('returns the `auctionInfos` array as-is', () => {
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'all', MOCK_FULL_SYNC_HEIGHT)).toBe(
AUCTION_INFOS,
);
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'all')).toBe(AUCTION_INFOS);
});
});

describe('when the `filter` is `active`', () => {
it('includes active auctions', () => {
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'active', MOCK_FULL_SYNC_HEIGHT)).toContain(
MOCK_AUCTION_INFO_1,
);
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'active')).toContain(MOCK_AUCTION_INFO_1);
});

it('filters out auctions with a nonzero `seq`', () => {
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'active', MOCK_FULL_SYNC_HEIGHT)).not.toContain(
MOCK_AUCTION_INFO_2,
);
it('filters out auctions with a `seq`>=2', () => {
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'active')).not.toContain(MOCK_AUCTION_INFO_4);
});

it('filters out auctions that end before `fullSyncHeight`', () => {
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'active', MOCK_FULL_SYNC_HEIGHT)).not.toContain(
MOCK_AUCTION_INFO_3,
);
});

it('filters out everything if `fullSyncHeight` is undefined', () => {
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'active', undefined)).toEqual([]);
});
describe('when the `filter` is `inactive`', () => {
it('includes inactive auctions', () => {
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'inactive')).toContain(MOCK_AUCTION_INFO_4);
});

it('includes auctions that start after `fullSyncHeight`', () => {
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'active', MOCK_FULL_SYNC_HEIGHT)).toContain(
MOCK_AUCTION_INFO_4,
);
it('filters out auctions with a `seq`<2', () => {
expect(getFilteredAuctionInfos(AUCTION_INFOS, 'inactive')).not.toContain(MOCK_AUCTION_INFO_1);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,28 @@ type FilterMatchableAuctionInfo = AuctionInfo & {
const haveEnoughDataToDetermineIfAuctionMatchesFilter = (
auctionInfo: AuctionInfo,
): auctionInfo is FilterMatchableAuctionInfo => {
return !!auctionInfo.auction.description && !!auctionInfo.auction.state;
return !!auctionInfo.auction.state;
};

const auctionIsActive = (auctionInfo: FilterMatchableAuctionInfo, fullSyncHeight: bigint) =>
auctionInfo.auction.state.seq === 0n &&
fullSyncHeight >= auctionInfo.auction.description.startHeight &&
fullSyncHeight <= auctionInfo.auction.description.endHeight;

const auctionIsUpcoming = (auctionInfo: FilterMatchableAuctionInfo, fullSyncHeight: bigint) =>
auctionInfo.auction.state.seq === 0n &&
fullSyncHeight < auctionInfo.auction.description.startHeight;
// Dutch auctions move from:
// 0 (opened) => 1 (closed) => n (withdrawn)
const auctionIsActive = (auctionInfo: FilterMatchableAuctionInfo) =>
auctionInfo.auction.state.seq < 2n;

export const getFilteredAuctionInfos = (
auctionInfos: AuctionInfo[],
filter: Filter,
fullSyncHeight?: bigint,
): AuctionInfo[] => {
if (filter === 'all') {
return auctionInfos;
}

return auctionInfos.filter(auctionInfo => {
if (!fullSyncHeight) {
return false;
}
if (!haveEnoughDataToDetermineIfAuctionMatchesFilter(auctionInfo)) {
return false;
}
return (
auctionIsActive(auctionInfo, fullSyncHeight) || auctionIsUpcoming(auctionInfo, fullSyncHeight)
);
const isActive = auctionIsActive(auctionInfo);

return filter === 'active' ? isActive : !isActive;
});
};
4 changes: 2 additions & 2 deletions apps/minifront/src/components/swap/auction-list/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export const filterWithLimit = <T>(
return result;
};

export const byStartHeightAscending = (a: AuctionInfo, b: AuctionInfo) => {
export const byStartHeightDescending = (a: AuctionInfo, b: AuctionInfo) => {
if (!a.auction.description?.startHeight || !b.auction.description?.startHeight) {
return 0;
}
return Number(a.auction.description.startHeight - b.auction.description.startHeight);
return Number(b.auction.description.startHeight - a.auction.description.startHeight);
};
8 changes: 3 additions & 5 deletions apps/minifront/src/components/swap/auction-list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { getFilteredAuctionInfos } from './get-filtered-auction-infos';
import { LayoutGroup, motion } from 'framer-motion';
import { useAuctionInfos } from '../../../state/swap/dutch-auction';
import { useStatus } from '../../../state/status';
import { byStartHeightAscending } from './helpers';
import { byStartHeightDescending } from './helpers';
import { Filters } from './filters';
import { AddressIndex } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb.js';
import { EndOrWithdrawAllButton } from './end-or-withdraw-all-button.tsx';
Expand Down Expand Up @@ -52,10 +52,8 @@ export const AuctionList = () => {

const filteredAuctionInfos = useMemo(
() =>
[...getFilteredAuctionInfos(auctionInfos.data ?? [], filter, status?.fullSyncHeight)].sort(
byStartHeightAscending,
),
[auctionInfos.data, filter, status?.fullSyncHeight],
[...getFilteredAuctionInfos(auctionInfos.data ?? [], filter)].sort(byStartHeightDescending),
[auctionInfos.data, filter],
);

return (
Expand Down
4 changes: 2 additions & 2 deletions apps/minifront/src/state/swap/dutch-auction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const MIN_OUTPUT_ESTIMATE_MULTIPLIER = 0.5;
*/
export const OUTPUT_LIMIT = 2 ** 52 - 1;

export type Filter = 'active' | 'all';
export type Filter = 'active' | 'inactive' | 'all';

interface Actions {
setMinOutput: (minOutput: string) => void;
Expand Down Expand Up @@ -106,7 +106,7 @@ const INITIAL_STATE: State = {
maxOutput: '',
txInProgress: false,
auctionInfos,
filter: 'all',
filter: 'active',
estimateLoading: false,
estimatedOutput: undefined,
};
Expand Down

0 comments on commit 894d038

Please sign in to comment.