Follow cryptokitties (ether Fang Yun Cat) Learn to write intelligent contract (next)

Source: Internet
Author: User

I bred the Ducks, as long as the 0.2ETH cheap

Kittes Array, all cats will be stored in the kitties array, this array will grow, Kittyid is actually the kitty object in this array subscript. And Kittyid is actually erc721 's tokenid. The kitties array establishes the mapping relationship between the erc721 token and the CAT data structure. The Kitty object data can be obtained through the erc721 Tokenid.

Kittyindextoowner table, through this mapping, you can quickly know, who is the cat. So why is the owner attribute not in Kitty's data structure? The author speculated that many scenes do not need to obtain the specific data on Kitty, if the first to find the cat to obtain the owner, the efficiency may be lower, after all, the implementation of intelligent contract is to be money.

Ownershiptokencount the table and recorded who had several cats.

kittyindextoapproved table, recording a licensing operation, you can authorize your cat to an address, such as your auction, your cat is actually authorized to the auction contract, the contract has the right to transfer your cat to others.

Sireallowedtoaddress Watch, you can take your cat to the breeding, the other side and your cat breeding, must get your authorization, then this table records, you have your kind of cat to allow who breeding.

These attributes almost record the ownership and mating operations of all cats.
kittybreeding Contract


Event pregnant defines the pregnancy, and two cats will be notified after successful breeding.

The Genescience attribute is a more important attribute of the so-called genetic engineering, he is an external contract, the contract is responsible for determining the appearance of two cats born kittens. The cat genetic engineering algorithm can be replaced at any time through the setgenescienceaddress function.

_isreadytobreed to determine whether the cat can reproduce, 2 conditions, one is the male partner ID if 0, the second is cooling time OK.

_issiringpermitted to determine whether two cats can be mated, first through the male cat ID, to get the owner. If two cats are yours, you can breed them. If not the same person, you need to check the Sireallowedtoaddress table to determine whether the male cat owner allows you to breed.

_triggercooldown This function is to trigger the birth cooling time after the cat has given birth, as can be seen. The first line calculates the estimated cooling time of the cooldownindex after it has been cooled to a few blocks, that is to say, each time a child is born, the rate of birth will drop until the last child is given a few hours.

Approvesiring authorized breeding, the core or modify the Sireallowedtoaddress table.

Ispregnant to judge the mother of the pregnancy function, the core or look at the Siringwithid attribute.

_isvalidmatingpair is a more interesting function. The cat in the cryptokitties is not what you want to match, he will meet a few conditions.

You can't match yourself (this is not nonsense, but the code does not know OH.) )

You can't mate with your parents (this is a common thing in a real cat house, probably cryptokitties or humane).

Have a brother relationship can not match (plainly, half of the mother, I can not match the different)

Two cats are allowed to breed new cats when they meet the top 3 o'clock.

_breedwith the core function of the two cat breeding. Do a few things

Matron.siringwithid = UInt32 (_sireid); Set up the mother cat's spouse, said earlier, to determine whether pregnancy is to see this attribute

Triggering a fertility cooling, _triggercooldown This function also says that each time a cooling is triggered, the cat's fertility rate drops a notch.

Empty the mating sireallowedtoaddress mating power sheet (agree to be matched once at a time, or else they will be repeated)

Send two cat pregnancy events

Givebirth function, the call later produces a new cat. The author here has the question, this function actually did not have seen in the actual operation, does not know who calls, is cryptokitties the company to call Mody (function this who can call). This function does the following things:

To make sure the new born cat is the first generation, notice that he always chooses a larger algebra +1. For example, you have a 0-generation cat and a 5-generation cat with, with a 6 generation of cats. Sensory algebra is less cost-effective if the gap is greater than the larger mating.

Genescience.mixgenes Gene Engineering contract to determine the newborn cat's genes, because the genetic engineering contract is not open source, so the cat can still be controlled, perhaps the day of the activities produced a lot of Super dream Cat.

Call _createkitty produces a new cat, the owner of which is the owner of the newborn cat.

Empty the parent cat's mate information.

Calling this function also requires payment of maternity money.

At this point, the cat's breeding part of the finished. Cat auctions and breeding Auctions Clockauctionbase Base Auction contract based on time

This is a great base contract, and he defines an auction based operation.

Defines a foundation for auction data structure auction. Nonfungiblecontract is the ERC721 (here is the only cat, the contract details in the last term). This is the real auction of any ERC721 tokens, in fact, this code can not only be used in cats, but also can be used in many places.

Check if you own this token.

When you are going to have an auction, you need to entrust your items to this contract.

Ownership of the goods transferred.

_addauction adds an auction that the real object of the auction is created externally. Due to different auctions, the auction object may be different, so this is the object level.

_cancelauction Delete an auction, here you can see, delete the time, will put the hosted token (CAT) back to the user.

_bid said to take the item, this code is relatively long, I deleted some checks for easy screenshots.

Calculate the price in real time and see if the offer is enough

Delete Auctions while charging for auction fees

And then pay the sellers.

Return the extra money

Publish an auction success event. kittyauction Contract

The contract is actually really responsible for the cat's auction and the stud auction.

Bidonsiringauction when we click on a pay stud in the interface, we actually call this function.

This function will go first.

SiringAuction.bid.value (Msg.value-autobirthfee) (_sireid); To do a breeding auction, if you can succeed will go the next line.

Finally, call _breedwith to get two cats for breeding.

There is also a cat auction, in fact this is the basis of the auction ERC721 toke Transfer, the above has been said. kittyminting Contract

This contract is controlled by the Cryptokitties team and is used to produce 0 generations of cats.

The creation has been 0 generations of cats, and put into the auction market.

The 0-generation cat's price calculation is 150% of the average, and the development team is really a bit black hearted.

Written here, Cryptokitties's smart contract code is almost finished. Personally, the code is written with great skill, clarity, and clarity of purpose. At the same time, there are more annotations, very suitable for the intelligent contract learning materials. Some do not know where to find the source of friends, you can pay attention to me and then DMS to me, I wrote some of my own version of the comments issued to you. There will be many more good text to share.

Turn from: http://dawaehuo.com/portal.php?mod=view&aid=7426

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.