Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit 09f5a05

Browse files
authored
Merge pull request #11370 from hassnian/issue-expired-status-delay
feat: expire trades based on `blockNumber`
2 parents 47d8d28 + e38fb8a commit 09f5a05

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

components/trade/TradeActivityTableRow.vue

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@
7575
<div class="flex-1">
7676
<div class="h-[50px] flex items-center">
7777
<template v-if="trade.expirationDate">
78-
<div v-if="isExpired">
78+
<div v-if="trade.isExpired">
7979
<span>{{ $t('expired') }}</span>
8080
</div>
8181
<div v-else>
8282
<span>{{ format(trade.expirationDate, EXPIRATION_FORMAT) }}</span>
83-
<span class="text-k-grey ml-3">({{ formatToNow(trade.expirationDate, isExpired) }})</span>
83+
<span class="text-k-grey ml-3">({{ formatToNow(trade.expirationDate, trade.isExpired) }})</span>
8484
</div>
8585
</template>
8686
<span v-else>
@@ -147,12 +147,12 @@
147147
{{ blank }}
148148
</div>
149149
<template v-if="trade.expirationDate">
150-
<div v-if="isExpired">
150+
<div v-if="trade.isExpired">
151151
<span>{{ $t('expired') }}</span>
152152
</div>
153153
<div v-else>
154154
<span>{{ format(trade.expirationDate, EXPIRATION_FORMAT) }}</span>
155-
<span class="text-k-grey ml-3">({{ formatToNow(trade.expirationDate, isExpired) }})</span>
155+
<span class="text-k-grey ml-3">({{ formatToNow(trade.expirationDate, trade.isExpired) }})</span>
156156
</div>
157157
</template>
158158
<span v-else>
@@ -241,7 +241,6 @@ const image = ref()
241241
const animationUrl = ref()
242242
243243
const isDesktop = computed(() => props.variant === 'Desktop')
244-
const isExpired = computed(() => props.trade.status === TradeStatus.EXPIRED)
245244
246245
const isTradeCollection = computed(() => desiredType === TradeDesiredType.COLLECTION)
247246
const itemPath = computed(() => isTradeCollection.value ? `/${urlPrefix.value}/collection/${item.id}` : `/${urlPrefix.value}/gallery/${item.id}`)

components/trade/TradeOwnerButton.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const details = {
6161
}
6262
6363
const buttonConfig = computed<ButtonConfig | null>(() => {
64-
if (props.trade.status === TradeStatus.EXPIRED) {
64+
if (props.trade.isExpired) {
6565
return isCreatorOfTrade.value
6666
? {
6767
label: $i18n.t(details[props.trade.type].withdraw),

components/trade/overviewModal/Details.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
{{ $t('expiration') }}
2020
</span>
2121

22-
<span v-if="trade.status === TradeStatus.EXPIRED">
22+
<span v-if="trade.isExpired">
2323
{{ $t('expired') }}
2424
</span>
2525
<span v-else>
@@ -45,7 +45,6 @@
4545
<script setup lang="ts">
4646
import { useIsTradeOverview } from './utils'
4747
import { formatToNow } from '@/utils/format/time'
48-
import { TradeStatus } from '@/composables/useTrades'
4948
import type { NFT } from '@/types'
5049
5150
const props = defineProps<{

composables/useCurrentBlock.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const currentBlock = ref(0)
2+
const subscription = ref()
3+
const syncCount = ref(0)
4+
5+
export default function useCurrentBlock() {
6+
const { apiInstance } = useApi()
7+
8+
syncCount.value++
9+
10+
if (!subscription.value) {
11+
onBeforeMount(async () => {
12+
const api = await apiInstance.value
13+
subscription.value = await api.rpc.chain.subscribeNewHeads((lastHeader) => {
14+
currentBlock.value = lastHeader.number.toNumber()
15+
})
16+
})
17+
}
18+
19+
onBeforeUnmount(() => {
20+
syncCount.value--
21+
22+
if (syncCount.value === 0 && subscription.value) {
23+
subscription.value()
24+
subscription.value = undefined
25+
}
26+
})
27+
28+
return currentBlock
29+
}

composables/useTrades.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export type TradeNftItem<T = Trade> = T & {
6464
type: TradeType
6565
desiredType: TradeDesiredType
6666
isEntireCollectionDesired: boolean
67+
isExpired: boolean
6768
}
6869

6970
export const TRADES_QUERY_MAP: Record<TradeType, { queryDocument: DocumentNode, dataKey: string }> = {
@@ -78,7 +79,6 @@ export const TRADES_QUERY_MAP: Record<TradeType, { queryDocument: DocumentNode,
7879
}
7980

8081
const BLOCKS_PER_HOUR = 300
81-
const currentBlock = ref(0)
8282

8383
export default function ({ where = {}, limit = 100, disabled = computed(() => false), type = TradeType.SWAP }: {
8484
where?: MaybeRef<Record<string, unknown>>
@@ -89,6 +89,8 @@ export default function ({ where = {}, limit = 100, disabled = computed(() => fa
8989
const { queryDocument, dataKey } = TRADES_QUERY_MAP[type]
9090

9191
const { client } = usePrefix()
92+
const currentBlock = useCurrentBlock()
93+
9294
const variables = computed(() => ({
9395
where: unref(where),
9496
limit: limit,
@@ -116,23 +118,16 @@ export default function ({ where = {}, limit = 100, disabled = computed(() => fa
116118
offered: trade.nft,
117119
desiredType: desiredType,
118120
isEntireCollectionDesired: desiredType === TradeDesiredType.COLLECTION,
121+
// Check block number to handle trades that are expired but not yet updated in indexer
122+
// @see https://github.com/kodadot/stick/blob/9eac12938c47bf0e66e93760231208e4249d8637/src/mappings/utils/cache.ts#L127
123+
isExpired: trade.status === TradeStatus.EXPIRED || currentBlock.value > Number(trade.expiration),
119124
type,
120125
} as TradeNftItem
121126
}) || []
122127
})
123128

124-
async function getCurrentBlock() {
125-
const api = await useApi().apiInstance.value
126-
const { number } = await api.rpc.chain.getHeader()
127-
return number.toNumber()
128-
}
129-
130129
const loading = computed(() => !currentBlock.value || fetching.value)
131130

132-
if (!currentBlock.value) {
133-
getCurrentBlock().then(b => currentBlock.value = b)
134-
}
135-
136131
return {
137132
items,
138133
loading,

0 commit comments

Comments
 (0)