Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ids too large #11315

Merged
merged 8 commits into from
Jan 20, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class="flex justify-end gallery-item-trade"
>
<NeoButton
data-testid="gallery-item-trade-make-offer"
:label="$t('transaction.offer')"
variant="k-blue"
size="large"
Expand Down
5 changes: 3 additions & 2 deletions composables/drop/massmint/useDropMassMint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Prefix } from '@kodadot1/static'
import type { ToMintNft } from '@/components/collection/drop/types'
import { generateId, setDyndataUrl } from '@/services/dyndata'
import { generateIdAssethub, setDyndataUrl } from '@/services/dyndata'

export type MassMintNFT = Omit<ToMintNft, 'priceUSD'> & {
image: string
Expand All @@ -18,7 +19,7 @@ export default () => {
const tokenIds = ref<number[]>([])
const populateTokenIds = async () => {
for (const _ of Array.from({ length: amountToMint.value })) {
const tokenId = Number.parseInt(await generateId())
const tokenId = Number.parseInt(await generateIdAssethub(parseInt(drop.value.collection), usePrefix().urlPrefix.value as Prefix))
hassnian marked this conversation as resolved.
Show resolved Hide resolved
if (!tokenIds.value.includes(tokenId)) {
tokenIds.value.push(tokenId)
}
Expand Down
2 changes: 1 addition & 1 deletion composables/transaction/mintToken/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const lastIndexUsed = async (collection: MintedCollection, api) => {
return Math.max(lastIndexUsed, lastOnChainIndex, 0)
}

const getLastIndexUsedOnChain = async (api, collectionId) => {
export const getLastIndexUsedOnChain = async (api, collectionId) => {
const collectionItems = await api.query.nfts.item.entries(collectionId)
const itemIds = collectionItems.map(([key]) => key.args[1].toNumber())
return Math.max(...itemIds)
Expand Down
28 changes: 28 additions & 0 deletions services/dyndata.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Prefix } from '@kodadot1/static'
import { getLastIndexUsedOnChain } from '@/composables/transaction/mintToken/utils'

const BASE_URL = isProduction
? 'https://dyndata.koda.art'
: 'https://dyndata-beta.koda.art'
Expand All @@ -6,6 +9,31 @@ const api = $fetch.create({
baseURL: BASE_URL,
})

// store latest IDs for each collection
const latestIdsMap = new Map<string, number>()
preschian marked this conversation as resolved.
Show resolved Hide resolved

export const generateIdAssethub = async (collectionId: number, prefix?: Prefix) => {
// 237 is the latest drops collection id to handle backwards compatibility
if (collectionId <= 273 && prefix) {
const mapKey = `${prefix}-${collectionId}`

// Always check the chain for the latest ID
const { apiInstance } = useApi()
const lastIdFromChain = await getLastIndexUsedOnChain(await apiInstance.value, collectionId)
const currentStoredId = latestIdsMap.get(mapKey) || 0

// Use the larger of the two values to ensure we don't create duplicates
const currentId = Math.max(lastIdFromChain, currentStoredId)
const nextId = currentId + 1

// Store the new ID
latestIdsMap.set(mapKey, nextId)
return nextId.toString()
}

return (await api('/generate-id')) as string
}

export const generateId = async () => {
return (await api('/generate-id')) as string
}
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/offer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from './fixtures'

test('show offer modal', async ({ page, Commands }) => {
await Commands.e2elogin()
await page.goto('/ahp/gallery/256-4235681345')
await page.getByTestId('gallery-item-trade-make-offer').click()

// at least show the offer modal
await expect(page.locator('.modal-card-title')).toHaveText('Create Offer')
})
Loading