0

My smart-contract has a mapping for Token-ID’s and their Prices, declared as follows:

   mapping (uint256 => uint256) public tokenIdAndItsPriceMapping;

-I intend to have only 5,000 tokens in total, and I want to pre-set their prices during the Contract’s deployment, but do so in ranges. Here’s what I mean:

-Tokens 0 through 49 are going to be 0.1 ETH -Tokens 50 through 99 are going to be 0.2 ETH -Tokens 100 through 149 are going to be 0.3 ETH

etc.

I can obviously do this in the Constructor using FOR loops, as follows:

//  0 -- 49
for(uint i = 0; i <= 49; i++) {
     tokenIdAndItsPriceDictionary[i] = 1000000000000000000 wei; // 0.1 ETH
}

// 50 -- 99
for(uint i = 50; i <= 99; i++) {
     tokenIdAndItsPriceDictionary[i] = 2000000000000000000 wei; // 0.2 ETH 
}


// etc.

But with there being 5,000 values to fill, that’s still going to require quite a lot of FOR loops - such that I might reach the Block’s Gas Limit during the contract’s deployment - causing it to fail. And if the project’s requirements were to change such that 10,000 Tokens will be needed instead of 5,000, what then?

Ordinarily - meaning in other languages, we can easily create a fixed-length Array and pre-populate it with default values - which would come in very handily for this. But it doesn't look like Solidity lets you do this.

Another approach would be to create a function that’ll return any particular Token’s price using it’s ID - and a big long IF statement:

function getTokenPrice(uint256 tokenID) returns (uint 256) {
    if(tokenID >= 0 && tokenID <= 49) {
     tokenIdAndItsPriceDictionary[tokenID] = 1000000000000000000 wei; // 0.1 ETH
  }
  else if(tokenID >= 50 && tokenID <= 99) {
     tokenIdAndItsPriceDictionary[tokenID] = 2000000000000000000 wei; // 0.2 ETH
  }
  else if(tokenID >= 100 && tokenID <= 149) {
     tokenIdAndItsPriceDictionary[tokenID] = 3000000000000000000 wei; // 0.3 ETH
  }  
  // etc.  

  return tokenIdAndItsPriceDictionary[tokenID]
}

This could work - but for only one Token at a time.

What if I wanted to have a function that returns ALL the prices of all the Tokens all at once, in one big Array? Like what if my front-end web3 App needed to get all those prices from the smart-contract as it loaded into the browser?

1

In your example. You may create another map to record the init price, the key is unint256 key = tokenID/50, the value is the init price. Then the size of the map changes from 5000 to 100.

mapping (uint256 => uint256) public initPrice;
 
initPrice[tokenID / 50] = "the token's init Price xxxxxxx"

But it seems a bad idea and hard to updata.

1
  • Hey that's pretty interesting - I'm not familiar with this init Price syntax and I do like the initPrice[tokenID / 50] approach. Might be something there to explore! Thanks!
    – Mark55
    Apr 26 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.