Solidity 环境配置与简介

  1. 配置 remix-ide 和 remixd,共享本地文件,防止清除浏览器缓存丢失合约文件
  2. 解决 remixed bug 只指向目录
  3. Configure "Deploy & Run Transactions" Plugin, so it uses our MetaMask Wallet to access the Blockchain.

Smart Contract

  • A piece of code running on the blockchain.
  • It's a state machine.
  • Needs transactions to change state(Statechange happens through mining+transactions.)
  • Can do logic operations
  • It's turing complete That means in theory it can solve any computation problem

Smart Contract Programming Language

Such as Solidity、Vyper、LLL etc, they are all getting complied. What you actually send to the blockchain is EVM Bytecode.

Smart Contracts are running on the blockchain,Deployed as EVM Bytecode They are turing complete

Solidity

  • The most popular smart contract programming language
  • Every high-level language code compiles to Bytecode.
  • Every Ethereum node in the network executes the same code Because every node has a copy of chain.
  • Second popular language is Vyper which is research-oriented, derived from Python.

Introduction

文件结构 数据类型 错误处理(通过回退) 参数 控制结构 可见性 函数

合约文件结构

  1. 版本声明
  2. import
  3. 合约
    1. 状态变量
    2. 函数
    3. 结构类型
    4. 时间
    5. 函数修改器
  4. 代码注释

版本声明

First line: Version Pragma, Pre-Compiler Statement Lock in the Solidity Compiler Version

pragma solidity ^0.4.24 指大于 0.4.24 版本的 solidity 编译器才可以运行该文件 solidity 版本更迭比较快,因此不提供向后兼容(Backward Compatibility):指新的版本的软/硬件可以使用老版本的软/硬件产生的数据。

Type

DataTypes:(U)int, Boolean, Array,Struct, Mapping. Address, No Floats!

值类型

solidity 0.6.7 types

布尔类型(Booleans) 整型(Integers) 定长浮点型(Fixed Point Numbers) 定长字节数组(Fixed-size byte arrays) 有理数和整型常量(Rational and Integer Literals) 字符串常量(String literals) 十六进制常量(Hexadecimal literals) 枚举(Enums) 函数类型(Function Types) 地址类型(Address) 地址常量(Address Literals)

bool

取值:true/false 运算符 (三种逻辑运算以及等于、不等于) !&&||==!=

int/uint

int 和 uint 都支持后面加数字,关键字 uint8 到 uint256(以 8 步进) 运算符: 比较运算: <=, <, ==, !=, >=, > 位运算: &, |, ^(异或),~(位取反) 算术运算: +,-,一元运算符-, 一元运算+,*,/,%(取余数),**(幂),<<(左移位),>>(右移位)

literals

四类常量

  1. 有理数和整型常量(Rational and Integer literals) 常量运算不会有溢出
  2. 字符串常量(String literals)
    1. 可以隐式地转换为自己数组或者字符串数组
    2. 字符串常量和 C,C++不一样,没有结尾的 '', 即:有几个字符就是几个字节
  3. 十六进制常量(Hexadecimal literals)
    1
    2
    3
    4
    function restHexLiterals() public constant return(byte2, byte1, byte1){
    byte2 a = hex"abcd"
    return (a,a[0], a[1])
    }
    byte{n} 表示 n 个字节的数组
  4. 地址常量(Address Literals)

地址类型

address:表示一个账户地址(20 字节) 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2 成员 属性:balance 函数:transfer()