0

My Token struct looks like this.

struct Token {
    uint256 value; // Price of token in wei
    uint256 parts; // set to 10 ** 18
}

Now:

  • Anyone can create a Token and set its value
  • Other users can come and buy the token in full or in parts

My buyTokenParts() function looks like this;

function buyTokenParts(uint256 _tokenId, uint256 _tokenParts) public payable {
    Token memory token = tokens[_tokenId];
    uint256 pricePerPart = token.value/token.parts;
    require(msg.value >= _tokenParts*pricePerPart);
    ...
}

Now the issue is, if token.value >= 1 ether then it works fine; but if token.value < 1 ether then pricePerParts will be zero.

If I try to handle it with partsPerWei;

uint256 partsPerWei = token.parts/token.value;

I end up with the issue, if token.value <= 1 ether then it works fine; but if token.value > 1 ether then partsPerWei will be zero.

How can i handle this issue?

Your Answer

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

Browse other questions tagged or ask your own question.