第二课概览
在第一课中,我们创建了一个函数用来生成僵尸,并且将它放入区块链上的僵尸数据库中。 在第二课里,我们会让我们的 app 看起来更像一个游戏: 它得支持多用户,并且采用更加有趣,而不仅仅使用随机的方式,来生成新的僵尸。
如何生成新的僵尸呢?通过让现有的僵尸猎食其他生物!
僵尸猎食
僵尸猎食的时候,僵尸病毒侵入猎物,这些病毒会将猎物变为新的僵尸,加入你的僵尸大军。系统会通过猎物和猎食者僵尸的DNA计算出新僵尸的DNA。
僵尸最喜欢猎食什么物种呢? 等你学完第二课就知道了!
实战演习
右边是一个简单的猎食演示。点击一个“人”,看看僵尸猎食的时候会发生什么? 可见,新僵尸的DNA是通过从原来的僵尸的DNA, 加上猎物的DNA计算得来的。
学完这一章,请点击“下一章”, 我们该让游戏支持多玩家模式了。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oXCb4HuW-1593677529917)(https://i.loli.net/2020/06/27/SMihkOofrwEHxTF.png)]
映射(Mapping)和地址(Address)
我们通过给数据库中的僵尸指定“主人”, 来支持“多玩家”模式。
如此一来,我们需要引入2个新的数据类型:
mapping
(映射) 和
address
(地址)。
Addresses (地址)
以太坊区块链由
_ account _
(账户)组成,你可以把它想象成银行账户。一个帐户的余额是
以太
(在以太坊区块链上使用的币种),你可以和其他帐户之间支付和接受以太币,就像你的银行帐户可以电汇资金到其他银行帐户一样。
每个帐户都有一个“地址”,你可以把它想象成银行账号。这是账户唯一的标识符,它看起来长这样:
0x0cE446255506E92DF41614C46F1d6df9Cc969183
(这是 CryptoZombies 团队的地址,如果你喜欢 CryptoZombies 的话,请打赏我们一些以太币!😉)
我们将在后面的课程中介绍地址的细节,现在你只需要了解
地址属于特定用户(或智能合约)的
。
所以我们可以指定“地址”作为僵尸主人的 ID。当用户通过与我们的应用程序交互来创建新的僵尸时,新僵尸的所有权被设置到调用者的以太坊地址下。
Mapping(映射)
在第1课中,我们看到了
_ 结构体 _
和
_ 数组 _
。
映射
是另一种在 Solidity 中存储有组织数据的方法。
映射是这样定义的:
//对于金融应用程序,将用户的余额保存在一个 uint类型的变量中:
mapping (address => uint) public accountBalance;
//或者可以用来通过userId 存储/查找的用户名
mapping (uint => string) userIdToName;
映射本质上是存储和查找数据所用的键-值对。在第一个例子中,键是一个
address
,值是一个
uint
,在第二个例子中,键是一个
uint
,值是一个
string
。
实战演习
为了存储僵尸的所有权,我们会使用到两个映射:一个记录僵尸拥有者的地址,另一个记录某地址所拥有僵尸的数量。
1.创建一个叫做
zombieToOwner
的映射。其键是一个
uint
(我们将根据它的 id 存储和查找僵尸),值为
address
。映射属性为
public
。
2.创建一个名为
ownerZombieCount
的映射,其中键是
address
,值是
uint
。
合约完善
ZombieFactory.sol
pragma solidity ^0.4.19;
contract ZombieFactory {
event NewZombie(uint zombieId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
function _createZombie(string _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
NewZombie(id, _name, _dna);
}
function _generateRandomDna(string _str) private view returns (uint) {
uint rand = uint(keccak256(_str));
return rand % dnaModulus;
}
function createRandomZombie(string _name) public {
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
Msg.sender
现在有了一套映射来记录僵尸的所有权了,我们可以修改
_createZombie
方法来运用它们。
为了做到这一点,我们要用到
msg.sender
。
msg.sender
在 Solidity 中,有一些全局变量可以被所有函数调用。 其中一个就是
msg.sender
,它指的是当前调用者(或智能合约)的
address
。
注意:在 Solidity 中,功能执行始终需要从外部调用者开始。 一个合约只会在区块链上什么也不做,除非有人调用其中的函数。所以
msg.sender
总是存在的。
以下是使用
msg.sender
来更新
mapping
的例子:
mapping (address => uint) favoriteNumber;
function setMyNumber(uint _myNumber) public {
favoriteNumber[msg.sender] = _myNumber;
}
function whatIsMyNumber() public view returns (uint) {
return favoriteNumber[msg.sender];
}
在这个小小的例子中,任何人都可以调用
setMyNumber
在我们的合约中存下一个
uint
并且与他们的地址相绑定。 然后,他们调用
whatIsMyNumber
就会返回他们存储的
uint
。
使用
msg.sender
很安全,因为它具有以太坊区块链的安全保障 —— 除非窃取与以太坊地址相关联的私钥,否则是没有办法修改其他人的数据的。
实战演习
我们来修改第1课的
_createZombie
方法,将僵尸分配给函数调用者吧。
-
首先,在得到新的僵尸
id
后,更新
zombieToOwner
映射,在
id
下面存入
msg.sender
。
-
然后,我们为这个
msg.sender
名下的
ownerZombieCount
加 1。
跟在 JavaScript 中一样, 在 Solidity 中你也可以用
++
使
uint
递增。
uint number = 0;
number++;
修改两行代码即可。
合约完善
ZombieFactory.sol
pragma solidity ^0.4.19;
contract ZombieFactory {
event NewZombie(uint zombieId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
function _createZombie(string _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
zombieToOwner[id] = msg.sender;
ownerZombieCount[msg.sender]++;
NewZombie(id, _name, _dna);
}
function _generateRandomDna(string _str) private view returns (uint) {
uint rand = uint(keccak256(_str));
return rand % dnaModulus;
}
function createRandomZombie(string _name) public {
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
Require
在第一课中,我们成功让用户通过调用
createRandomZombie
函数 并输入一个名字来创建新的僵尸。 但是,如果用户能持续调用这个函数来创建出无限多个僵尸加入他们的军团,这游戏就太没意思了!
于是,我们作出限定:每个玩家只能调用一次这个函数。 这样一来,新玩家可以在刚开始玩游戏时通过调用它,为其军团创建初始僵尸。
我们怎样才能限定每个玩家只调用一次这个函数呢?
答案是使用
require
。
require
使得函数在执行过程中,当不满足某些条件时抛出错误,并停止执行:
function sayHiToVitalik(string _name) public returns (string) {
require(keccak256(_name) == keccak256("Vitalik"));
return "Hi!";
}
如果你这样调用函数
sayHiToVitalik(“Vitalik”)
,它会返回“Hi!”。而如果调用的时候使用了其他参数,它则会抛出错误并停止执行。
因此,在调用一个函数之前,用
require
验证前置条件是非常有必要的。
实战演习
在我们的僵尸游戏中,我们不希望用户通过反复调用
createRandomZombie
来給他们的军队创建无限多个僵尸 —— 这将使得游戏非常无聊。
我们使用了
require
来确保这个函数只有在每个用户第一次调用它的时候执行,用以创建初始僵尸。
-
在
createRandomZombie
的前面放置
require
语句。 使得函数先检查
ownerZombieCount [msg.sender]
的值为
0
,不然就抛出一个错误。
注意:在 Solidity 中,关键词放置的顺序并不重要
-
虽然参数的两个位置是等效的。 但是,由于我们的答案检查器比较呆板,它只能认定其中一个为正确答案
-
于是在这里,我们就约定把
ownerZombieCount [msg.sender]
放前面吧
合约完善
ZombieFactory.sol
pragma solidity ^0.4.19;
contract ZombieFactory {
event NewZombie(uint zombieId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
function _createZombie(string _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
zombieToOwner[id] = msg.sender;
ownerZombieCount[msg.sender]++;
NewZombie(id, _name, _dna);
}
function _generateRandomDna(string _str) private view returns (uint) {
uint rand = uint(keccak256(_str));
return rand % dnaModulus;
}
function createRandomZombie(string _name) public {
require(ownerZombieCount[msg.sender] == 0);
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
继承(Inheritance)
我们的游戏代码越来越长。 当代码过于冗长的时候,最好将代码和逻辑分拆到多个不同的合约中,以便于管理。
有个让 Solidity 的代码易于管理的功能,就是合约
inheritance
(继承):
contract Doge {
function catchphrase() public returns (string) {
return "So Wow CryptoDoge";
}
}
contract BabyDoge is Doge {
function anotherCatchphrase() public returns (string) {
return "Such Moon BabyDoge";
}
}
由于
BabyDoge
是从
Doge
那里
inherits
(继承)过来的。 这意味着当你编译和部署了
BabyDoge
,它将可以访问
catchphrase()
和
anotherCatchphrase()
和其他我们在
Doge
中定义的其他公共函数。
这可以用于逻辑继承(比如表达子类的时候,
Cat
是一种
Animal
)。 但也可以简单地将类似的逻辑组合到不同的合约中以组织代码。
实战演习
在接下来的章节中,我们将要为僵尸实现各种功能,让它可以“猎食”和“繁殖”。 通过将这些运算放到父类
ZombieFactory
中,使得所有
ZombieFactory
的继承者合约都可以使用这些方法。
-
在
ZombieFactory
下创建一个叫
ZombieFeeding
的合约,它是继承自 `ZombieFactory 合约的。
合约完善
ZombieFactory.sol
pragma solidity ^0.4.19;
contract ZombieFactory {
event NewZombie(uint zombieId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
function _createZombie(string _name, uint _dna) private {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
zombieToOwner[id] = msg.sender;
ownerZombieCount[msg.sender]++;
NewZombie(id, _name, _dna);
}
function _generateRandomDna(string _str) private view returns (uint) {
uint rand = uint(keccak256(_str));
return rand % dnaModulus;
}
function createRandomZombie(string _name) public {
require(ownerZombieCount[msg.sender] == 0);
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
contract ZombieFeeding is ZombieFactory {
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
引入(Import)
哇!你有没有注意到,我们只是清理了下右边的代码,现在你的编辑器的顶部就多了个选项卡。 尝试点击它的标签,看看会发生什么吧!
代码已经够长了,我们把它分成多个文件以便于管理。 通常情况下,当 Solidity 项目中的代码太长的时候我们就是这么做的。
在 Solidity 中,当你有多个文件并且想把一个文件导入另一个文件时,可以使用
import
语句:
import "./someothercontract.sol";
contract newContract is SomeOtherContract {
}
这样当我们在合约(contract)目录下有一个名为
someothercontract.sol
的文件(
./
就是同一目录的意思),它就会被编译器导入。
实战演习
现在我们已经建立了一个多文件架构,并用
import
来读取来自另一个文件中合约的内容:
1.将
zombiefactory.sol
导入到我们的新文件
zombiefeeding.sol
中。
合约完善
zombiefeeding.sol
pragma solidity ^0.4.19;
import "./zombiefactory.sol";
contract ZombieFeeding is ZombieFactory {
}
Storage与Memory
在 Solidity 中,有两个地方可以存储变量 ——
storage
或
memory
。
*
Storage*
变量是指永久存储在区块链中的变量。 *
Memory*
变量则是临时的,当外部函数对某合约调用完成时,内存型变量即被移除。 你可以把它想象成存储在你电脑的硬盘或是RAM中数据的关系。
大多数时候你都用不到这些关键字,默认情况下 Solidity 会自动处理它们。 状态变量(在函数之外声明的变量)默认为“存储”形式,并永久写入区块链;而在函数内部声明的变量是“内存”型的,它们函数调用结束后消失。
然而也有一些情况下,你需要手动声明存储类型,主要用于处理函数内的
_ 结构体 _
和
_ 数组 _
时:
contract SandwichFactory {
struct Sandwich {
string name;
string status;
}
Sandwich[] sandwiches;
function eatSandwich(uint _index) public {
Sandwich storage mySandwich = sandwiches[_index];
mySandwich.status = "Eaten!";
Sandwich memory anotherSandwich = sandwiches[_index + 1];
anotherSandwich.status = "Eaten!";
sandwiches[_index + 1] = anotherSandwich;
}
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
如果你还没有完全理解究竟应该使用哪一个,也不用担心 —— 在本教程中,我们将告诉你何时使用
storage
或是
memory
,并且当你不得不使用到这些关键字的时候,Solidity 编译器也发警示提醒你的。
现在,只要知道在某些场合下也需要你显式地声明
storage
或
memory
就够了!
实战演习
是时候给我们的僵尸增加“猎食”和“繁殖”功能了!
当一个僵尸猎食其他生物体时,它自身的DNA将与猎物生物的DNA结合在一起,形成一个新的僵尸DNA。
-
创建一个名为
feedAndMultiply
的函数。 使用两个参数:
_zombieId
(
uint
类型 )和
_targetDna
(也是
uint
类型)。 设置属性为
public
的。
-
我们不希望别人用我们的僵尸去捕猎。 首先,我们确保对自己僵尸的所有权。 通过添加一个
require
语句来确保
msg.sender
只能是这个僵尸的主人(类似于我们在
createRandomZombie
函数中做过的那样)。
注意:同样,因为我们的答案检查器比较呆萌,只认识把
msg.sender
放在前面的答案,如果你切换了参数的顺序,它就不认得了。 但你正常编码时,如何安排参数顺序都是正确的。
-
为了获取这个僵尸的DNA,我们的函数需要声明一个名为
myZombie
数据类型为
Zombie
的本地变量(这是一个
storage
型的指针)。 将其值设定为在
zombies
数组中索引为
_zombieId
所指向的值。
到目前为止,包括函数结束符
}
的那一行, 总共4行代码。
下一章里,我们会继续丰富这个功能。
合约完善
zombiefeeding.sol
pragma solidity ^0.4.19;
import "./zombiefactory.sol";
contract ZombieFeeding is ZombieFactory {
function feedAndMultiply(uint _zombieId, uint _targetDna) public {
require(msg.sender == zombieToOwner[_zombieId]);
Zombie storage myZombie = zombies[_zombieId];
}
}
僵尸的DNA
我们来把
feedAndMultiply
函数写完吧。
获取新的僵尸DNA的公式很简单:计算猎食僵尸DNA和被猎僵尸DNA之间的平均值。
例如:
function testDnaSplicing() public {
uint zombieDna = 2222222222222222;
uint targetDna = 4444444444444444;
uint newZombieDna = (zombieDna + targetDna) / 2;
}
以后,我们也可以让函数变得更复杂些,比方给新的僵尸的 DNA 增加一些随机性之类的。但现在先从最简单的开始 —— 以后还可以回来完善它嘛。
实战演习
-
首先我们确保
_targetDna
不长于16位。要做到这一点,我们可以设置
_targetDna
为
_targetDna % dnaModulus
,并且只取其最后16位数字。
-
接下来为我们的函数声明一个名叫
newDna
的
uint
类型的变量,并将其值设置为
myZombie
的 DNA 和
_targetDna
的平均值(如上例所示)。
注意:您可以用
myZombie.name
或
myZombie.dna
访问
myZombie
的属性。
-
一旦我们计算出新的DNA,再调用
_createZombie
就可以生成新的僵尸了。如果你忘了调用这个函数所需要的参数,可以查看
zombiefactory.sol
选项卡。请注意,需要先给它命名,所以现在我们把新的僵尸的名字设为
NoName
- 我们回头可以编写一个函数来更改僵尸的名字。
注意:对于 Solidity 高手,你可能会注意到我们的代码存在一个问题。别担心,下一章会解决这个问题的 ;)
合约完善
zombiefeeding.sol
pragma solidity ^0.4.19;
import "./zombiefactory.sol";
contract ZombieFeeding is ZombieFactory {
function feedAndMultiply(uint _zombieId, uint _targetDna) public {
require(msg.sender == zombieToOwner[_zombieId]);
Zombie storage myZombie = zombies[_zombieId];
_targetDna = _targetDna % dnaModulus;
uint newDna = (myZombie.dna + _targetDna) / 2;
_createZombie("NoName", newDna);
}
}
更多关于函数可见性
我们上一课的代码有问题!
编译的时候编译器就会报错。
错误在于,我们尝试从
ZombieFeeding
中调用
_createZombie
函数,但
_createZombie
却是
ZombieFactory
的
private
(私有)函数。这意味着任何继承自
ZombieFactory
的子合约都不能访问它。
internal 和 external
除
public
和
private
属性之外,Solidity 还使用了另外两个描述函数可见性的修饰词:
internal
(内部) 和
external
(外部)。
internal
和
private
类似,不过, 如果某个合约继承自其父合约,这个合约即可以访问父合约中定义的“内部”函数。(嘿,这听起来正是我们想要的那样!)。
external
与
public
类似,只不过这些函数只能在合约之外调用 - 它们不能被合约内的其他函数调用。稍后我们将讨论什么时候使用
external
和
public
。
声明函数
internal
或
external
类型的语法,与声明
private
和
public
类 型相同:
contract Sandwich {
uint private sandwichesEaten = 0;
function eat() internal {
sandwichesEaten++;
}
}
contract BLT is Sandwich {
uint private baconSandwichesEaten = 0;
function eatWithBacon() public returns (string) {
baconSandwichesEaten++;
// 因为eat() 是internal 的,所以我们能在这里调用
eat();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
实战演习
-
将
_createZombie()
函数的属性从
private
改为
internal
, 使得其他的合约也能访问到它。
我们已经成功把你的注意力集中在到
zombiefactory.sol
这个选项卡上啦。
合约完善
ZombieFactory.sol
pragma solidity ^0.4.19;
contract ZombieFactory {
event NewZombie(uint zombieId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
struct Zombie {
string name;
uint dna;
}
Zombie[] public zombies;
mapping (uint => address) public zombieToOwner;
mapping (address => uint) ownerZombieCount;
function _createZombie(string _name, uint _dna) internal {
uint id = zombies.push(Zombie(_name, _dna)) - 1;
zombieToOwner[id] = msg.sender;
ownerZombieCount[msg.sender]++;
NewZombie(id, _name, _dna);
}
function _generateRandomDna(string _str) private view returns (uint) {
uint rand = uint(keccak256(_str));
return rand % dnaModulus;
}
function createRandomZombie(string _name) public {
require(ownerZombieCount[msg.sender] == 0);
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
僵尸吃什么?
是时候让我们的僵尸去捕猎! 那僵尸最喜欢的食物是什么呢?
Crypto 僵尸喜欢吃的是…
CryptoKitties!
😱😱😱
(正经点,我可不是开玩笑😆)
为了做到这一点,我们要读出 CryptoKitties 智能合约中的 kittyDna。这些数据是公开存储在区块链上的。区块链是不是很酷?
别担心 —— 我们的游戏并不会伤害到任何真正的CryptoKitty。 我们只
读取
CryptoKitties 数据,但却无法在物理上删除它。
与其他合约的交互
如果我们的合约需要和区块链上的其他的合约会话,则需先定义一个 *
interface*
(接口)。
先举一个简单的栗子。 假设在区块链上有这么一个合约:
contract LuckyNumber {
mapping(address => uint) numbers;
function setNum(uint _num) public {
numbers[msg.sender] = _num;
}
function getNum(address _myAddress) public view returns (uint) {
return numbers[_myAddress];
}
}
这是个很简单的合约,您可以用它存储自己的幸运号码,并将其与您的以太坊地址关联。 这样其他人就可以通过您的地址查找您的幸运号码了。
现在假设我们有一个外部合约,使用
getNum
函数可读取其中的数据。
首先,我们定义
LuckyNumber
合约的 *
interface*
:
contract NumberInterface {
function getNum(address _myAddress) public view returns (uint);
}
请注意,这个过程虽然看起来像在定义一个合约,但其实内里不同:
首先,我们只声明了要与之交互的函数 —— 在本例中为
getNum
—— 在其中我们没有使用到任何其他的函数或状态变量。
其次,我们并没有使用大括号(
{
和
}
)定义函数体,我们单单用分号(
;
)结束了函数声明。这使它看起来像一个合约框架。
编译器就是靠这些特征认出它是一个接口的。
在我们的 app 代码中使用这个接口,合约就知道其他合约的函数是怎样的,应该如何调用,以及可期待什么类型的返回值。
在下一课中,我们将真正调用其他合约的函数。目前我们只要声明一个接口,用于调用 CryptoKitties 合约就行了。
实战演习
我们已经为你查看过了 CryptoKitties 的源代码,并且找到了一个名为
getKitty
的函数,它返回所有的加密猫的数据,包括它的“基因”(我们的僵尸游戏要用它生成新的僵尸)。
该函数如下所示:
function getKitty(uint256 _id) external view returns (
bool isGestating,
bool isReady,
uint256 cooldownIndex,
uint256 nextActionAt,
uint256 siringWithId,
uint256 birthTime,
uint256 matronId,
uint256 sireId,
uint256 generation,
uint256 genes
) {
Kitty storage kit = kitties[_id];
isGestating = (kit.siringWithId != 0);
isReady = (kit.cooldownEndBlock <= block.number);
cooldownIndex = uint256(kit.cooldownIndex);
nextActionAt = uint256(kit.cooldownEndBlock);
siringWithId = uint256(kit.siringWithId);
birthTime = uint256(kit.birthTime);
matronId = uint256(kit.matronId);
sireId = uint256(kit.sireId);
generation = uint256(kit.generation);
genes = kit.genes;
}
- 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
这个函数看起来跟我们习惯的函数不太一样。 它竟然返回了…一堆不同的值! 如果您用过 JavaScript 之类的编程语言,一定会感到奇怪 —— 在 Solidity中,您可以让一个函数返回多个值。
现在我们知道这个函数长什么样的了,就可以用它来创建一个接口:
1.定义一个名为
KittyInterface
的接口。 请注意,因为我们使用了
contract
关键字, 这过程看起来就像创建一个新的合约一样。
2.在interface里定义了
getKitty
函数(不过是复制/粘贴上面的函数,但在
returns
语句之后用分号,而不是大括号内的所有内容。
合约完善
zombiefeeding.sol
pragma solidity ^0.4.19;
import "./zombiefactory.sol";
contract KittyInterface {
function getKitty(uint256 _id) external view returns (
bool isGestating,
bool isReady,
uint256 cooldownIndex,
uint256 nextActionAt,
uint256 siringWithId,
uint256 birthTime,
uint256 matronId,
uint256 sireId,
uint256 generation,
uint256 genes
);
}
contract ZombieFeeding is ZombieFactory {
function feedAndMultiply(uint _zombieId, uint _targetDna) public {
require(msg.sender == zombieToOwner[_zombieId]);
Zombie storage myZombie = zombies[_zombieId];
_targetDna = _targetDna % dnaModulus;
uint newDna = (myZombie.dna + _targetDna) / 2;
_createZombie("NoName", newDna);
}
}
- 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
- 27
- 28
- 29
- 30
- 31
使用接口
继续前面
NumberInterface
的例子,我们既然将接口定义为:
contract NumberInterface {
function getNum(address _myAddress) public view returns (uint);
}
我们可以在合约中这样使用:
contract MyContract {
address NumberInterfaceAddress = 0xab38...;
// ^ 这是FavoriteNumber合约在以太坊上的地址
NumberInterface numberContract = NumberInterface(NumberInterfaceAddress);
// 现在变量 `numberContract` 指向另一个合约对象
function someFunction() public {
// 现在我们可以调用在那个合约中声明的 `getNum`函数:
uint num = numberContract.getNum(msg.sender);
// ...在这儿使用 `num`变量做些什么
}
}
通过这种方式,只要将您合约的可见性设置为
public
(公共)或
external
(外部),它们就可以与以太坊区块链上的任何其他合约进行交互。
实战演习
我们来建个自己的合约去读取另一个智能合约-- CryptoKitties 的内容吧!
-
我已经将代码中 CryptoKitties 合约的地址保存在一个名为
ckAddress
的变量中。在下一行中,请创建一个名为
kittyContract
的 KittyInterface,并用
ckAddress
为它初始化 —— 就像我们为
numberContract
所做的一样。
合约完善
zombiefeeding.sol
pragma solidity ^0.4.19;
import "./zombiefactory.sol";
contract KittyInterface {
function getKitty(uint256 _id) external view returns (
bool isGestating,
bool isReady,
uint256 cooldownIndex,
uint256 nextActionAt,
uint256 siringWithId,
uint256 birthTime,
uint256 matronId,
uint256 sireId,
uint256 generation,
uint256 genes
);
}
contract ZombieFeeding is ZombieFactory {
address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
KittyInterface kittyContract = KittyInterface(ckAddress);
function feedAndMultiply(uint _zombieId, uint _targetDna) public {
require(msg.sender == zombieToOwner[_zombieId]);
Zombie storage myZombie = zombies[_zombieId];
_targetDna = _targetDna % dnaModulus;
uint newDna = (myZombie.dna + _targetDna) / 2;
_createZombie("NoName", newDna);
}
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
处理多返回值
getKitty
是我们所看到的第一个返回多个值的函数。我们来看看是如何处理的:
function multipleReturns() internal returns(uint a, uint b, uint c) {
return (1, 2, 3);
}
function processMultipleReturns() external {
uint a;
uint b;
uint c;
(a, b, c) = multipleReturns();
}
function getLastReturnValue() external {
uint c;
(,,c) = multipleReturns();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
实战演习
是时候与 CryptoKitties 合约交互起来了!
我们来定义一个函数,从 kitty 合约中获取它的基因:
-
创建一个名为
feedOnKitty
的函数。它需要2个
uint
类型的参数,
_zombieId
和
_kittyId
,这是一个
public
类型的函数。
-
函数首先要声明一个名为
kittyDna
的
uint
。
注意:在我们的
KittyInterface
中,
genes
是一个
uint256
类型的变量,但是如果你记得,我们在第一课中提到过,
uint
是
uint256
的别名,也就是说它们是一回事。
-
这个函数接下来调用
kittyContract.getKitty
函数, 传入
_kittyId
,将返回的
genes
存储在
kittyDna
中。记住 ——
getKitty
会返回一大堆变量。 (确切地说10个 - 我已经为你数过了,不错吧!)。但是我们只关心最后一个--
genes
。数逗号的时候小心点哦!
-
最后,函数调用了
feedAndMultiply
,并传入了
_zombieId
和
kittyDna
两个参数。
合约完善
zombiefeeding.sol
pragma solidity ^0.4.19;
import "./zombiefactory.sol";
contract KittyInterface {
function getKitty(uint256 _id) external view returns (
bool isGestating,
bool isReady,
uint256 cooldownIndex,
uint256 nextActionAt,
uint256 siringWithId,
uint256 birthTime,
uint256 matronId,
uint256 sireId,
uint256 generation,
uint256 genes
);
}
contract ZombieFeeding is ZombieFactory {
address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
KittyInterface kittyContract = KittyInterface(ckAddress);
function feedAndMultiply(uint _zombieId, uint _targetDna) public {
require(msg.sender == zombieToOwner[_zombieId]);
Zombie storage myZombie = zombies[_zombieId];
_targetDna = _targetDna % dnaModulus;
uint newDna = (myZombie.dna + _targetDna) / 2;
_createZombie("NoName", newDna);
}
function feedOnKitty(uint _zombieId, uint _kittyId) public {
uint kittyDna;
(,,,,,,,,,kittyDna) = kittyContract.getKitty(_kittyId);
feedAndMultiply(_zombieId, kittyDna);
}
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
奖励: Kitty 基因
我们的功能逻辑主体已经完成了…现在让我们来添一个奖励功能吧。
这样吧,给从小猫制造出的僵尸添加些特征,以显示他们是猫僵尸。
要做到这一点,咱们在新僵尸的DNA中添加一些特殊的小猫代码。
还记得吗,第一课中我们提到,我们目前只使用16位DNA的前12位数来指定僵尸的外观。所以现在我们可以使用最后2个数字来处理“特殊”的特征。
这样吧,把猫僵尸DNA的最后两个数字设定为
99
(因为猫有9条命)。所以在我们这么来写代码:
如果
这个僵尸是一只猫变来的,就将它DNA的最后两位数字设置为
99
。
if 语句
if语句的语法在 Solidity 中,与在 JavaScript 中差不多:
function eatBLT(string sandwich) public {
// 看清楚了,当我们比较字符串的时候,需要比较他们的 keccak256 哈希码
if (keccak256(sandwich) == keccak256("BLT")) {
eat();
}
}
实战演习
让我们在我们的僵尸代码中实现小猫的基因。
-
首先,我们修改下
feedAndMultiply
函数的定义,给它传入第三个参数:一条名为
_species
的字符串。
-
接下来,在我们计算出新的僵尸的DNA之后,添加一个
if
语句来比较
_species
和字符串
"kitty"
的
keccak256
哈希值。
-
在
if
语句中,我们用
99
替换了新僵尸DNA的最后两位数字。可以这么做:
newDna = newDna - newDna % 100 + 99;
。
解释:假设
newDna
是
334455
。那么
newDna % 100
是
55
,所以
newDna - newDna % 100
得到
334400
。最后加上
99
可得到
334499
。
-
最后,我们修改了
feedOnKitty
中的函数调用。当它调用
feedAndMultiply
时,增加
“kitty”
作为最后一个参数。
合约完善
zombiefeeding.sol
pragma solidity ^0.4.19;
import "./zombiefactory.sol";
contract KittyInterface {
function getKitty(uint256 _id) external view returns (
bool isGestating,
bool isReady,
uint256 cooldownIndex,
uint256 nextActionAt,
uint256 siringWithId,
uint256 birthTime,
uint256 matronId,
uint256 sireId,
uint256 generation,
uint256 genes
);
}
contract ZombieFeeding is ZombieFactory {
address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
KittyInterface kittyContract = KittyInterface(ckAddress);
function feedAndMultiply(uint _zombieId, uint _targetDna, string _species) public {
require(msg.sender == zombieToOwner[_zombieId]);
Zombie storage myZombie = zombies[_zombieId];
_targetDna = _targetDna % dnaModulus;
uint newDna = (myZombie.dna + _targetDna) / 2;
if (keccak256(_species) == keccak256("kitty")) {
newDna = newDna - newDna % 100 + 99;
}
_createZombie("NoName", newDna);
}
function feedOnKitty(uint _zombieId, uint _kittyId) public {
uint kittyDna;
(,,,,,,,,,kittyDna) = kittyContract.getKitty(_kittyId);
feedAndMultiply(_zombieId, kittyDna, "kitty");
}
}
- 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
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
放在一起
至此,你已经学完第二课了!
查看下→_→的演示,看看他们怎么运行起来得吧。继续,你肯定等不及看完这一页😉。点击小猫,攻击!看到你斩获一个新的小猫僵尸了吧!
JavaScript 实现
我们只用编译和部署
ZombieFeeding
,就可以将这个合约部署到以太坊了。我们最终完成的这个合约继承自
ZombieFactory
,因此它可以访问自己和父辈合约中的所有 public 方法。
我们来看一个与我们的刚部署的合约进行交互的例子, 这个例子使用了 JavaScript 和 web3.js:
var abi =
var ZombieFeedingContract = web3.eth.contract(abi)
var contractAddress =
var ZombieFeeding = ZombieFeedingContract.at(contractAddress)
let zombieId = 1;
let kittyId = 1;
let apiUrl = "https://api.cryptokitties.co/kitties/" + kittyId
$.get(apiUrl, function(data) {
let imgUrl = data.image_url
})
$(".kittyImage").click(function(e) {
ZombieFeeding.feedOnKitty(zombieId, kittyId)
})
ZombieFactory.NewZombie(function(error, result) {
if (error) return
generateZombie(result.zombieId, result.name, result.dna)
})
- 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
- 27
- 28
- 29
- 30
实战演习
选择一只你想猎食的小猫。你自家僵尸的 DNA 会和小猫的 DNA 结合,生成一个新的小猫僵尸,加入你的军团!
看到新僵尸上那可爱的猫咪腿了么?这是新僵尸最后DNA中最后两位数字
99
的功劳!
你想要的话随时可以重新开始。捕获了一只猫咪僵尸,你一定很高兴吧!(不过你只能持有一只),继续前进到下一章,完成第二课吧!
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ETnDsebq-1593677529922)(https://i.loli.net/2020/06/27/vQ8uDOcFknPNsmy.png)]
融合
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bfMuAdGd-1593677529924)(https://i.loli.net/2020/06/27/I9YxPTeVDuKcfmr.png)]
第二课打卡
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qLbtyTAL-1593677529927)(https://i.loli.net/2020/06/27/9w4d6EMHsi2Sgo3.png)]
我的僵尸大军2