CryptoZombies学习笔记——Lesson2
第二课是僵尸猎食,将把app变得更像一个游戏,添加多人模式,建立更多创造僵尸的方法。
chapter1
依然是简介
chapter2:映射和地址
映射相当于一个索引,指向不同地址,不同地址存储的数据不同,相当于一种特殊的存储结构。此例中用来存储每个僵尸的上一级。
mapping(address=>uint) public accountBalance;
key是address,value是uint。
chapter3: msg.sender
指向调用函数的用户地址。注意:智能合约部署在区块链上,只有当一个外部账户调用智能合约的函数时才会执行合约,所以msg.sender总是存在的。
一个例子:
mapping (address => uint) favoriteNumber; function setMyNumber(uint _myNumber) public {
// Update our `favoriteNumber` mapping to store `_myNumber` under `msg.sender`
favoriteNumber[msg.sender] = _myNumber;
// ^ The syntax for storing data in a mapping is just like with arrays
} function whatIsMyNumber() public view returns (uint) {
// Retrieve the value stored in the sender's address
// Will be `0` if the sender hasn't called `setMyNumber` yet
return favoriteNumber[msg.sender];
}
相对于其他存储方式,运用_msg.sender更为安全,只有当你的私钥泄露时,别人才能更改你的账户数据。
chapter4:require
使用require编辑限制条件,不满足条件时程序会抛出异常。
例子:
function sayHiToVitalik(string _name) public returns (string) {
// Compares if _name equals "Vitalik". Throws an error and exits if not true.
// (Side note: Solidity doesn't have native string comparison, so we
// compare their keccak256 hashes to see if the strings are equal)
require(keccak256(_name) == keccak256("Vitalik"));
// If it's true, proceed with the function:
return "Hi!";
}
solidity中字符串不能直接比较,所以将字符串进行keccak256编码再做比较。
注:solidity比较时对两边顺序没有固定要求。
chapter5:继承
类似其他面向对象语言的继承,可继承父亲的public函数等。
contract Dog is Animal{}
chapter6:引入
类似其他语言的库,易于长代码的模块化编辑和管理。
chapter7:storage 和 memory
storage 和 memory 是solidity中变量的两种存储方式,storage 永久存储在区块链上,而memory会在外部函数调用时清除,有点类似计算机的磁盘和ram。solidity默认函数外定义的状态变量是storage存储,而函数内定义的变量是memory形式存储。
当然有时需要手动定义来满足一些需求,不过不用担心,solidity编译器会在你定义不恰当的时候提出warning。
contract SandwichFactory {
struct Sandwich {
string name;
string status;
} Sandwich[] sandwiches; function eatSandwich(uint _index) public {
// Sandwich mySandwich = sandwiches[_index]; // ^ Seems pretty straightforward, but solidity will give you a warning
// telling you that you should explicitly declare `storage` or `memory` here. // So instead, you should declare with the `storage` keyword, like:
Sandwich storage mySandwich = sandwiches[_index];
// ...in which case `mySandwich` is a pointer to `sandwiches[_index]`
// in storage, and...
mySandwich.status = "Eaten!";
// ...this will permanently change `sandwiches[_index]` on the blockchain. // If you just want a copy, you can use `memory`:
Sandwich memory anotherSandwich = sandwiches[_index + 1];
// ...in which case `anotherSandwich` will simply be a copy of the
// data in memory, and...
anotherSandwich.status = "Eaten!";
// ...will just modify the temporary variable and have no effect
// on `sandwiches[_index + 1]`. But you can do this:
sandwiches[_index + 1] = anotherSandwich;
// ...if you want to copy the changes back into blockchain storage.
}
}
chapter8: Zombie Dna
chapter9: 更多关于函数可见化的问题
除了public和private,solidity中还有internal和external来控制函数可见化。
internal类似于private,但有一点不同,internal定义的函数可以为外部继承合约调用,而private不行。
external类似于public,但external定义的函数只能被其他合约调用,本合约内的函数不能调用。
两者添加定义的方法相同。
chapter10:僵尸吃什么
吃cryptoketties。估计是作者的恶作剧。实际上并不会对cryptokitties的数据产生什么影响,只是读取其中的数据。
所以本章的主题就是和其他的智能合约交互。定义一个新合约,合约只含需要调用的合约内的函数头部,不含其他任何函数或者变量,且用“;”代替函数体大括号。
比如有一个合约:
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函数,则定义接口:
contract NumberInterface {
function getNum(address _myAddress) public view returns (uint);
}
chapter11: 使用接口
contract MyContract {
address NumberInterfaceAddress = 0xab38...
// ^ The address of the FavoriteNumber contract on Ethereum
NumberInterface numberContract = NumberInterface(NumberInterfaceAddress);
// Now `numberContract` is pointing to the other contract function someFunction() public {
// Now we can call `getNum` from that contract:
uint num = numberContract.getNum(msg.sender);
// ...and do something with `num` here
}
}
定义一个接口合约地址就好
chapter12:多参数返回值
例子:
function multipleReturns() internal returns(uint a, uint b, uint c) {
return (1, 2, 3);
} function processMultipleReturns() external {
uint a;
uint b;
uint c;
// This is how you do multiple assignment:
(a, b, c) = multipleReturns();
} // Or if we only cared about one of the values:
function getLastReturnValue() external {
uint c;
// We can just leave the other fields blank:
(,,c) = multipleReturns();
}
后续函数调用多参数返回函数时,可以只返回一个或若干个值,但要把其他返回值空出来,加逗号隔开就行。
chapter13:奖励:kitty基因
让那些继承自kitty的zombies有独特的特征,即修改定义僵尸特征的16位数的最后两位。
chapter14:部署合约
CryptoZombies学习笔记——Lesson2的更多相关文章
- CryptoZombies学习笔记——Lesson1
CryptoZombies是一个学习以太坊开发的平台,我将在这里记录学习过程中的一些笔记. 课程网址:cryptozombies.io 首先是第一课——Lesson1:Making the Zombi ...
- CryptoZombies学习笔记——Lesson5
chapter1:token代币 简而言之,通证就是支持交易的包含一系列规范的函数接口的一个智能合约,发币可以用ERC20标准,但是像僵尸这种非同质化代币,需要用ERC721标准 chapter2:e ...
- CryptoZombies学习笔记——Lesson3
第三课就开始深入讲解solidity编程技巧了. chapter1: 智能合约的不变性. 合约一旦部署到以太坊后,就不可更改了,所以从一方面来说,智能合约代码的安全性是如此重要,因为一旦发现你的代码里 ...
- CryptoZombies学习笔记——Lesson4
第四课主要介绍payable函数相关. chapter1: payable修饰函数 以太坊允许同时调用函数和eth转账.msg.value显示发送到合约的以太币数,ether是内置整型数.如果函数没有 ...
- 孙鑫视频VC++深入详解学习笔记
孙鑫视频VC++深入详解学习笔记 VC++深入详解学习笔记 Lesson1: Windows程序运行原理及程序编写流程 Lesson2: 掌握C++基本语法 Lesson3: MFC框架程序剖析 Le ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- PHP-自定义模板-学习笔记
1. 开始 这几天,看了李炎恢老师的<PHP第二季度视频>中的“章节7:创建TPL自定义模板”,做一个学习笔记,通过绘制架构图.UML类图和思维导图,来对加深理解. 2. 整体架构图 ...
- PHP-会员登录与注册例子解析-学习笔记
1.开始 最近开始学习李炎恢老师的<PHP第二季度视频>中的“章节5:使用OOP注册会员”,做一个学习笔记,通过绘制基本页面流程和UML类图,来对加深理解. 2.基本页面流程 3.通过UM ...
- 2014年暑假c#学习笔记目录
2014年暑假c#学习笔记 一.C#编程基础 1. c#编程基础之枚举 2. c#编程基础之函数可变参数 3. c#编程基础之字符串基础 4. c#编程基础之字符串函数 5.c#编程基础之ref.ou ...
随机推荐
- TiDB数据库 mydumper与loader导入数据
从mysql导出数据最好的方法是使用tidb官方的工具mydumper. 导入tidb最好的方法是使用loader工具,大概19.4G每小时的速度. 详细的步骤可以参考官网:https://pingc ...
- PgSQL基础之 安装postgresql数据系统
参考这位仁兄的文章,真的非常好:https://blog.csdn.net/jerry_sc/article/details/76408116#创建数据目录 后来我又自己写了一个shell脚本,来自动 ...
- MMIO----Wav格式文件解析
DirectSound只支持Wav格式的音频文件,在创建次缓冲区之前需要先确定播放的Wav音频数据的格式.如果是从本地Wav文件播放,则需要先读出它的数据格式. 1. Wav音频格式布局 Wav是WA ...
- 【12】python 栈型数据结构模拟、队列型数据结构模拟
一.压栈操作模拟 #__author:"吉*佳" #date: 2018/10/21 0021 #function:栈 # 栈:即是先进后出的一种数据结构 # (1)模拟压栈操作 ...
- 安装和配置Apache服务器(下)
Apache的配置文档:http://httpd.apache.org/docs/current/. 1.监听端口: 默认的端口号为80端口,如果端口号冲突改为8080端口. 注:每改一次httpd. ...
- .Net Core Api 跨域配置
.Net Core 和Asp.Net 不同,不需要再去引用其他的跨域组件.创建项目时,就有了. 让接口实现跨域,需要配置两个地方. 一.Startup.cs 这里需要配置两个地方 public voi ...
- Android自定义布局的背景在多分辨率的情况下设置fill_parent时背景不能够横向全屏的问题解决
问题描述:最近做了一个自定义的控件LinearLayout就是公用的底部菜单条,在指定分辨率下(例如:480x800,480x854)下背景是正常的,但是当程序运行到非指定(默认)的分辨率下就不正常了 ...
- day32
今日内容 1.基于TCP协议(通信循环) 2.基于TCP协议(连接循环) 3.粘包问题 4.模拟SSH实现远程执行命令 服务器端 ################################### ...
- android获取内置和外置SD卡路径 - z
本文将介绍Android真机环境下如何获取内置和外置SD卡路径. 测试环境:三星Note3,其他手机待测试... 所需权限(AndroidManifest.xml文件里) <uses-permi ...
- 大数据入门第二十二天——spark(二)RDD算子(1)
一.RDD概述 1.什么是RDD RDD(Resilient Distributed Dataset)叫做分布式数据集,是Spark中最基本的数据抽象,它代表一个不可变.可分区.里面的元素可并行计算的 ...