# Blind Box Purchase Contract


Blind Boxes will contain cards with levels vary from 1 to 5. You will get 1 S-NFT card for every blind box purchased. The level of the card will be determined by a ramdon function.

function buyBoxes(uint256 boxesLength, uint256 tokenId)
        external
        payable
        nonReentrant
    {
        require(boxesLength > 0, "Boxes length must > 0");
        require(boxesLength <= 100, "Boxes length must <= 100");
        require(getBoxesLeftSupply() >= boxesLength, "Not enough boxes supply");
    
    uint256 price = boxesLength * boxTokenPrices[tokenId];
    if (tokenId == 0) {
        require(msg.value == price, "Price mismatch");
        payable(receivingAddress).transfer(price);
    } else {
        IERC20 token = IERC20(tokenAddrs[tokenId]);
        token.transferFrom(msg.sender, receivingAddress, price);
    }

    spawnHns(msg.sender, boxesLength);

    userBoxesLength[msg.sender] += boxesLength;
    userTokenBuyAmount[msg.sender][tokenId] += price;
    totalBoxesLength += boxesLength;
    totalTokenBuyAmount[tokenId] += price;
    users.add(msg.sender);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

View full code at: https://github.com/SquidGameGamefi/SquidGame-core/blob/main/contracts/pool/HNBox.sol (opens new window)

Last Updated: 11/10/2021, 1:10:11 PM