Skip to content

Commit

Permalink
Merge pull request #11315 from kodadot/fix--ids-too-large
Browse files Browse the repository at this point in the history
fix: ids too large
  • Loading branch information
preschian authored Jan 20, 2025
2 parents 4942569 + c2c0714 commit 43dc9eb
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
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))
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>()

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')
})

0 comments on commit 43dc9eb

Please sign in to comment.