Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

The duck I bred is cheap as long as 0.2ETH

The kittes array , all cats will be stored in the  kitties array , this array will continue to grow, kittyId is actually the subscript of the Kitty object in this array. And kittyId is actually the tokenId of erc721 . The kitties array establishes the mapping relationship between the erc721 token and the cat data structure. Kitty object data can be obtained through the tokenid of erc721.

The kittyIndexToOwner table , through this mapping, can quickly know who the cat is. So why is the owner property not placed in Kitty's data structure? The author speculates that in many scenarios, it is not necessary to obtain specific data on Kitty. If you have to find the cat first and then obtain the owner every time, the efficiency may be relatively low. After all, the execution of smart contracts requires money.

The ownershipTokenCount table records who owns how many cats.

The kittyIndexToApproved table records an authorization operation. You can authorize your cat to a certain address. For example, when you conduct an auction, your cat is actually authorized to the auction contract, which has the right to transfer your cat to others. .

sireAllowedToAddress table , you can bring your cat to breed, the other party must get your authorization to breed with your cat, then this table records who you allow your cat to breed.

These properties are almost recorded, all cat ownership and breeding operations.


KittyBreedingContract

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)


event Pregnant  defines a pregnancy event that will be notified when two cats are successfully bred.

The geneScience attribute is a relatively important attribute called genetic engineering. It is an external contract. This contract is responsible for determining the appearance of a kitten when two cats are born. The cat genetic engineering algorithm can be changed at any time through the setGeneScienceAddress function.

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

_isReadyToBreed  judges whether the cat can give birth. There are two conditions. One is that the male cat's spouse ID is 0, and the second is that the cooling time is OK.

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

_isSiringPermitted  determines whether two cats can be bred. First, get the owner through the ID of the male cat and the female cat. If both cats are yours, you can breed. If it is not the same person, you need to query the sireAllowedToAddress table to determine whether the owner of the male cat allows you to breed.

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

_triggerCooldown  This function triggers the birth cooldown after the cat is born, as you can see. The first line first calculates the number of blocks that are expected to cool down and then updates the fertility cooling time cooldownIndex , that is to say, every spawning, the spawning rate will decrease until the last spawning a few hours.

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

ApproveSiring  authorized breeding, the core is to modify the sireAllowedToAddress table .

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

The function of isPregnant to judge the pregnancy of the female cat, the core still depends on the siringWithId property.

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

_isValidMatingPair is an interesting function. The cat in cryptokitties is not something you can match with whatever you want. He has to meet several conditions.

  1. 自己不能和自己配(这不是废话么,但是代码不知道哦。)

  2. 不能和自己的父母交配(其实这个事情在现实的猫舍中很常见,可能cryptokitties还是很人道的)

  3. 有兄弟关系的不能配(说白了,同父异母,同母异父都不能配)

满足上面3点就才允许两只猫配种生新的猫。

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

_breedWith两只猫配种的核心函数。做了几件事情

  1. matron.siringWithId = uint32(_sireId); 设置母猫的配偶,前面说了,确定是否怀孕就是看这个属性

  2. 触发生育冷却,_triggerCooldown这个函数上文也讲过,每次触发冷却,都会让猫的生育速度下降一个档次

  3. 清空配种sireAllowedToAddress配种权力表(同意一次只能配一次,不然人家就反复配啦)

  4. 发送两只猫怀孕事件

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

giveBirth函数,调用以后就产生新的猫了。(笔者这里有疑问,这个函数其实并没有在实际操作中见到过,不知道是谁调用的,难道是cryptokitties公司调用么(函数本谁都可以调用)。这个函数做了如下事情:

  1. 确定新出生猫是第几代的,注意,他总是选更大代数+1。比如你一只0代猫和一只5代猫配,配出来是一直6代猫。感觉代数如果差距比较大配种不太划算。

  2. geneScience.mixGenes基因工程合约确定新生猫的基因,由于基因工程的合约并不开源,所以生什么猫还是可以控制的,说不定哪天搞活动生出很多超梦猫。

  3. 调用_createKitty产生一只新猫,母猫的所有者即是新生猫的所有者。

  4. 清空母猫的配偶信息。

  5. 调用该函数还需要支付生育金。

至此,猫咪培育的部分就说完了。

猫咪拍卖和配种拍卖

ClockAuctionBase基于时间的基础拍卖合约

这是一个很棒的基类合约,他定义了一次拍卖的基础操作。

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

定义了一个基础的拍卖数据结构Auction。nonFungibleContract是ERC721代币(这里就是只每只猫,该合约细节上期讲过)。这个拍牌是实质是拍卖任何ERC721代币,其实这代码不止可以用在猫上,还可以用在很多地方。

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

检验你是否拥有这个TOKEN。

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

当你要进行拍卖的时候,你需要将你的物品交给这个合约托管。

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

移交物品的所有权。

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

_addAuction添加一个拍卖,实质拍卖对象是外部创建的。由于不同拍卖,拍卖对象可能不同,所以这个里是对象级别。

_cancelAuction删除一个拍卖,这里可以看到,删除的时候,会把托管的token(猫)还给用户。

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

_bid表示拍下物品,这个代码比较长,我删除一些检查方便截图。

  1. 先实时计算价格,看看出价够不够

  2. 删除拍卖,同时收取拍卖手续费

  3. 然后给卖家打钱

  4. 退回多余的钱

  5. 发布拍卖成功事件。

KittyAuction合约

这个合约实际真正负责了,猫咪的拍卖和配种拍卖。

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

bidOnSiringAuction当我们在界面上点击付费配种的时候,实际调用的是这个函数。

这个函数会先走

siringAuction.bid.value(msg.value - autoBirthFee)(_sireId); 做配种竞拍,如果能成功才会走下一行。

最后就调用 _breedWith 让两只猫进行配种。

还有一个是猫咪拍卖,其实这个就是基础的拍卖ERC721 TOKE转让,上文已经说过。

KittyMinting合约

This contract is controlled by the cryptokitties team and is used to produce Gen 0 cats.

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

Create a 0-generation cat and put it on the auction market.

Follow cryptokitties (Ethereum cloud cat) to learn to write smart contracts (below)

The price calculation of the 0-generation cat is 150% of the average selling price. The development team is really a little black.

At this point, the smart contract code of cryptokitties is basically analyzed. Personally, I feel that the code is very skillful, well organized, and has a clear purpose. At the same time, there are also many annotations, which are very suitable as learning materials for smart contracts. Some friends who don't know where to find the source code can follow me and send me a private message, and I will send you my own version with some comments. There will be many more good articles to share in the future.

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325370278&siteId=291194637