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

Random Collection URI Prefix #2

Open
sagaratalatti opened this issue Mar 9, 2023 · 0 comments
Open

Random Collection URI Prefix #2

sagaratalatti opened this issue Mar 9, 2023 · 0 comments

Comments

@sagaratalatti
Copy link
Owner

sagaratalatti commented Mar 9, 2023

// Attach to deployed contract
const contract = await NftContractProvider.getContract();

// Define the 6 different URIs and their assigned ranges
const uris = [
  { uri: process.env.COLLECTION_URI_PREFIX_1, range: [1, 500] },
  { uri: process.env.COLLECTION_URI_PREFIX_2, range: [501, 1000] },
  { uri: process.env.COLLECTION_URI_PREFIX_3, range: [1001, 1500] },
  { uri: process.env.COLLECTION_URI_PREFIX_4, range: [1501, 2000] },
  { uri: process.env.COLLECTION_URI_PREFIX_5, range: [2001, 2500] },
  { uri: process.env.COLLECTION_URI_PREFIX_6, range: [2501, 3000] },
];

// Shuffle the array of NFT IDs
const nftIds = Array.from({ length: 3000 }, (_, i) => i + 1);
for (let i = nftIds.length - 1; i > 0; i--) {
  const j = Math.floor(Math.random() * (i + 1));
  [nftIds[i], nftIds[j]] = [nftIds[j], nftIds[i]];
}

// Assign the URIs to the NFTs based on the predefined ranges
for (const { uri, range } of uris) {
  const [start, end] = range;
  for (let i = start; i <= end; i++) {
    const nftId = nftIds[i - 1]; // NFT IDs are 1-indexed
    console.log(`Assigning URI ${uri} to NFT ${nftId}`);
    await (await contract.setTokenURI(nftId, uri)).wait();
  }
}

The uris array is an array of objects, where each object has a uri property that specifies the URI, and a range property that specifies the range of NFT IDs that should be assigned to that URI. The nftIds array is shuffled using the Fisher-Yates algorithm, as before. The code then loops through each URI and assigns the URI to the NFTs whose IDs fall within the predefined range for that URI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant