LoginSignup
1
0

More than 5 years have passed since last update.

CryptoKitties: KittyMintingコントラクトの目的

Last updated at Posted at 2018-10-19

In the rising age of blockchain technology, decentralized applications just keep on getting developed around the world. A hefty amount of DAPPS are quite promising, and an application such as CryptoKitties is a very good example. However, have you ever wondered how it actually works? Moreover, did you know that the source code is even made available as open-source for the public to refer to. If you're curious about it, let's dive in and start with one of its contracts, KittyMinting.

わたしの名前はマルクス (Twitter Account: @markusveeyola) です。

今回はKittyMintingコントラクトのKittyMintingをまとめました。

Before we get to the code, let's first discuss about Kryptokitties.

CryptoKittiesとは

cryptokitties.jpg

Cryptokitties is a blockchain based decentralized virtual game built from Ethereum. The application is heavily anchored by the token standard, ERC721, solely because of its ownership properties. This game lets you purchase, breed, and sell your kitties to other people. Creators release kitties from time to time and put them all in to an auction, letting other people bid for the kitties. Owners can even hold an auction event for other users to buy.

The cool thing about this game is that no one can ever have the same kitty, since each kitty has their own unique genetic code, the creators call it the GeneScience. Thanks to the ERC721 token standard, this was made possible. The kitty is your asset, it will forever be yours, and it will never be stolen from you.

You want to try it out? Check the header link!

KittyMintingコントラクト

The Cryptokitties has components that defines it. With that said, like the other blockchain applications, it is composed of many smart contracts, KittyMinting is one of them. So, KittyMinting is a smart contract that handles the minting operations of the kitties, where kitties are produced and created.

コード

In this section, I'll be slowly explaining the code, one block to another. It's more like we're creating the contract from scratch. We'll start from top to bottom.

コントラクトは「KittyAuction」を継承しています。

This block of code is where we will be putting all our functions and variables to shape the contract, KittyMinting. You can see that the contract is extending to another contract called KittyAuction, we will be discussing its purpose for the proceeding contents.


contract KittyMinting is KittyAuction {
   ...
}

変数

For the contract's variables, all are set in public, so everyone can see this values, and this can be used by other contracts as well.

Promo & GEN0 Creation Limit

These are variables that set the max values the creator can create for the entire application.


    uint256 public constant PROMO_CREATION_LIMIT = 5000;
    uint256 public constant GEN0_CREATION_LIMIT = 45000;

GEN0 Starting Price & Auction Duration

The price of kitties being generated is set 10 finney as a starting price. And the auction duration is also set to 1 day, to make more time for the bidding to purchase a kitty.


    uint256 public constant GEN0_STARTING_PRICE = 10 finney;
    uint256 public constant GEN0_AUCTION_DURATION = 1 days;

Promo and GEN0 Counters

These variables simply tracks the count of the created kitties for the entire application lifecycle.


    uint256 public promoCreatedCount;
    uint256 public gen0CreatedCount;

関数

Now let's head on to the functions. For this contract, there are only 3 that completes the operations for creating or minting kitties. Let's get down to business!

createPromoKitty

What this function does is it soley creates a kitty made for promos. As you can see, the function extends to a modifier called onlyCOO, that's because only COO users can create kitties. When this function is triggered, it requires the gene value, and the address of the owner. By default, it sets the C00 as the owner of the kitties. It's also worht noticing that it limits the the promo creation, if it goes beyond the limit. There's no way kitties can be created.


    function createPromoKitty(uint256 _genes, address _owner) external onlyCOO {
        address kittyOwner = _owner;
        if (kittyOwner == address(0)) {
             kittyOwner = cooAddress;
        }
        require(promoCreatedCount < PROMO_CREATION_LIMIT);

        promoCreatedCount++;
        _createKitty(0, 0, 0, _genes, kittyOwner);
    }

createGEN0Auction

Next, is a function that creates kitties, that directly puts it on an auction. This is the reason why the contract is extending to the KittyAuction, because kitties being created in this function will go for a bidding process. Same with the previous function for creating kitties, this too has a limit for minting a kitty asset. The price of the kitty will also be calculated based on its generation, this will be discussed in the next function.


    function createGen0Auction(uint256 _genes) external onlyCOO {
        require(gen0CreatedCount < GEN0_CREATION_LIMIT);

        uint256 kittyId = _createKitty(0, 0, 0, _genes, address(this));
        _approve(kittyId, saleAuction);

        saleAuction.createAuction(
            kittyId,
            _computeNextGen0Price(),
            0,
            GEN0_AUCTION_DURATION,
            address(this)
        );

        gen0CreatedCount++;
    }

_computeNextGen0Price

Our last function is the function that calculates the values of the newly minted kitties, the value of each kitty depends on the price of its past 5 kitties on average, added with 50% of its resulting value. This is also where the starting price variable is used, the starting price. If the calculated price is lower than the starting price, it'll follow the starting price instead as its final price.


    function _computeNextGen0Price() internal view returns (uint256) {

        uint256 avePrice = saleAuction.averageGen0SalePrice();
        require(avePrice == uint256(uint128(avePrice)));
        uint256 nextPrice = avePrice + (avePrice / 2);

        if (nextPrice < GEN0_STARTING_PRICE) {
            nextPrice = GEN0_STARTING_PRICE;
        }

        return nextPrice;
    }
}

結論

There you go, the smart contract is finished. I hope you learned a lot, and have lined the pieces up a little bit. If you want to know more about its contracts, here is the link for the CryptoKitties source code: CryptoKittiesソースコード

That's a wrap. Until then, ありがとうございました!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0