I would like to initialize an array of custom token IDs (uint32) in my Solidity smart contract.
I tried these two options, but none of them worked:
(1) Calling a function at deployment that would return a memory array of the correct size. Remix gets stuck in "pending" mode.
// SPDX-License-Identifier: MIT pragma solidity ^0.8.2; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract TestStackOverflow is ERC721{ constructor() ERC721("Test", "T") {} uint32 [] public twinsListA = [ 2, 5, 8, 12, 15, 19, 25, 29, 32, 35, 39, 43, 45, 47 ]; uint32 [] public twinsListB = [ 3, 6, 9, 13, 16, 20, 26, 30, 33, 36, 40, 44, 46, 48 ]; uint32 [] public tokenIDs_BW = buildArrays(96, 12, 2, 1); function buildArrays(uint32 length, uint32 cols, uint32 tot_colourVars, uint32 tot_versions) public returns(uint32[] memory){ uint32 twinNumber; uint32 tokenID; uint32 [] memory tokenIDsArray = new uint32[](length); uint32 counter = 0; for (uint32 colourVar=1; colourVar <= tot_colourVars; colourVar++) { for (uint32 m=1; m <= 48; m++) { twinNumber = 0; for (uint8 i=0; i<14; i++){ if (m == twinsListA[i] || m == twinsListB[i]) { twinNumber = 1; break; } } for (uint32 version=1; version <= tot_versions; version++) { tokenID = cols*1000000 + colourVar*10000 + m*100 + twinNumber*10 + version; tokenIDsArray[counter] = cols; counter++; } } } return tokenIDsArray; } }
(2) Calling a function after deployment that would push the custom IDs one by one. But again, if I call buildArrays with e.g. parameters (12, 2, 1), Remix gets stuck in “pending”.
// SPDX-License-Identifier: MIT pragma solidity ^0.8.2; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; contract Test is ERC721 { constructor() ERC721("Test", "T") {} uint32 [] public twinsListA = [ 2, 5, 8, 12, 15, 19, 25, 29, 32, 35, 39, 43, 45, 47 ]; uint32 [] public twinsListB = [ 3, 6, 9, 13, 16, 20, 26, 30, 33, 36, 40, 44, 46, 48 ]; uint32 [] public tokenIDs_BW; function buildArrays(uint8 cols, uint8 tot_colourVars, uint8 tot_versions) public { uint8 twinNumber; uint32 tokenID; for (uint8 colourVar=1; colourVar <= tot_colourVars; colourVar++) { for (uint32 m=1; m <= 48; m++) { twinNumber = 0; for (uint8 i=0; i<14; i++){ if (m == twinsListA[i] || m == twinsListB[i]) { twinNumber = 1; break; } } for (uint8 version=1; version <= tot_versions; version++) { tokenID = cols*1000000 + colourVar*10000 + m*100 + twinNumber*10 + version; tokenIDs_BW.push(tokenID); } } } } }
The test arrays are actually very small… why this happens and how can I solve the problem? Does the problem depend on the nested loops to build the encoded IDs?