2023-10-16 08:06:17 -07:00
|
|
|
import { toUiDecimals } from '@blockworks-foundation/mango-v4'
|
2023-07-24 11:17:07 -07:00
|
|
|
import {
|
|
|
|
AuctionHouse,
|
|
|
|
Metaplex,
|
|
|
|
LazyListing,
|
|
|
|
LazyBid,
|
|
|
|
} from '@metaplex-foundation/js'
|
2023-10-16 08:06:17 -07:00
|
|
|
import {
|
|
|
|
ALL_FILTER,
|
|
|
|
PRICE_LOW_HIGH,
|
|
|
|
YOUR_LISTINGS,
|
|
|
|
} from 'hooks/market/useAuctionHouse'
|
2023-07-24 11:17:07 -07:00
|
|
|
import { AUCTION_HOUSE_ID } from 'utils/constants'
|
2023-10-16 08:06:17 -07:00
|
|
|
import { MANGO_MINT_DECIMALS } from 'utils/governance/constants'
|
2023-07-24 11:17:07 -07:00
|
|
|
|
|
|
|
export const fetchAuctionHouse = async (metaplex: Metaplex) => {
|
|
|
|
const auctionHouse = await metaplex
|
|
|
|
.auctionHouse()
|
|
|
|
.findByAddress({ address: AUCTION_HOUSE_ID })
|
|
|
|
return auctionHouse
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fetchFilteredListing = async (
|
|
|
|
metaplex: Metaplex,
|
|
|
|
auctionHouse: AuctionHouse,
|
|
|
|
filter: string,
|
|
|
|
page: number,
|
2023-07-24 15:52:09 -07:00
|
|
|
perPage: number,
|
2023-07-24 11:17:07 -07:00
|
|
|
) => {
|
|
|
|
const listings = (
|
|
|
|
await metaplex.auctionHouse().findListings({
|
|
|
|
auctionHouse,
|
|
|
|
})
|
|
|
|
)
|
2023-10-16 08:06:17 -07:00
|
|
|
.filter((x) => {
|
|
|
|
if (filter === ALL_FILTER || filter.includes('Price')) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if (filter === YOUR_LISTINGS) {
|
|
|
|
return x.sellerAddress.equals(metaplex.identity().publicKey)
|
|
|
|
}
|
|
|
|
})
|
2023-07-24 11:17:07 -07:00
|
|
|
.filter((x) => !x.canceledAt && !x.purchaseReceiptAddress)
|
2023-10-16 08:06:17 -07:00
|
|
|
.sort((a, b) => {
|
|
|
|
if (filter.includes('Price')) {
|
|
|
|
const aPrice = toUiDecimals(
|
|
|
|
a.price.basisPoints.toNumber(),
|
|
|
|
MANGO_MINT_DECIMALS,
|
|
|
|
)
|
|
|
|
const bPrice = toUiDecimals(
|
|
|
|
b.price.basisPoints.toNumber(),
|
|
|
|
MANGO_MINT_DECIMALS,
|
|
|
|
)
|
|
|
|
return filter === PRICE_LOW_HIGH ? aPrice - bPrice : bPrice - aPrice
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.createdAt.toNumber() - a.createdAt.toNumber()
|
|
|
|
})
|
|
|
|
|
2023-07-24 11:17:07 -07:00
|
|
|
const filteredListings = listings.slice(
|
|
|
|
(page - 1) * perPage,
|
2023-07-24 15:52:09 -07:00
|
|
|
page * perPage,
|
2023-07-24 11:17:07 -07:00
|
|
|
) as LazyListing[]
|
|
|
|
|
|
|
|
return {
|
|
|
|
results: filteredListings,
|
|
|
|
totalPages: Math.ceil(listings.length / perPage),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fetchFilteredBids = async (
|
|
|
|
metaplex: Metaplex,
|
2023-07-24 15:52:09 -07:00
|
|
|
auctionHouse: AuctionHouse,
|
2023-07-24 11:17:07 -07:00
|
|
|
) => {
|
|
|
|
const bids = await metaplex.auctionHouse().findBids({
|
|
|
|
auctionHouse,
|
|
|
|
})
|
|
|
|
return bids.filter(
|
2023-07-24 15:52:09 -07:00
|
|
|
(x) => !x.canceledAt && !x.purchaseReceiptAddress,
|
2023-07-24 11:17:07 -07:00
|
|
|
) as LazyBid[]
|
|
|
|
}
|