SlideShare uma empresa Scribd logo
1 de 35
Baixar para ler offline
区块链智能合约开发
张宇 | October 26, 2022
In Solidity
什么是智能合约
智能合约是运⾏在链上的程序
合约开发者可以通过智能合约实现
与链上资产、数据进⾏交互
⽤户可以通过⾃⼰的链上账户来
调⽤合约、访问资产与数据
与⼀般程序的差异
原⽣⽀持资产流动
部署与后续写⼊需要⼀定费⽤
存储数据的成本更⾼
部署后⽆法更改(可升级合约?)
…
Solidity
⼀⻔⾯向合约的、为实现智能合约⽽创建的⾼级编程语⾔
在 EVM 虚拟机上运⾏
语法整体类似于 javascript
是⽬前最流⾏的智能合约语⾔
也是⼊⻔区块链与 web3 所必须掌握的语⾔
如何部署智能合约
local Ethereum
Solidity 合约是以 .sol 为后缀的⽂件,⽆法直接执⾏,需要编译为 EVM
(Ethereum Virtual Machine)可识别的字节码才能在链上运⾏。
.sol Contract Bytecode
compile deploy
Contract
开发框架 & ⼯具
Truf
f
le
(javascript)
Brownie
(python)
Hardhat
(javascript)
⽐较⽼牌的开发框架
提供完整的开发、测试、
调试⼯具链
通过简洁的 python 语法
便于本地调试与部署测试
丰富的插件系统
适合复杂的合约项⽬
Remix
IDE
Ganache
提供完整的 IDE、编译⼯具、部署调试的测
试节点环境、账户等,可以通过 MetaMask
插件与测试⽹、主⽹进⾏直接交互
提供了⼀个虚拟区块链⽹络,可
以通过各类 Remix IDE 或⼀些
框架⼯具与之交互,适合有⼀定
规模的项⽬进⾏本地调试与测试
开发框架 & ⼯具
MetaMask
Infura
OpenZeppelin
Chainlink
…
Others
Solidity 核⼼语法讲解
数据类型
基本
boolean
int
uint
address
bytes
…
数据类型
enum
数据类型
array
数据类型
mapping
数据类型
struct
变量
类型
local
state
global
关键字声明
storage
memory
calldata
constant
/
immutable
constant
值不可变
节约 gas fee
immutable
可以在 constructor 中初始化
但不可以再次改变
可⻅性
public
private
internal
external
关键字
view
pure
函数
可⻅性与关键字
函数
modi
f
ier
函数
选择器
条件
if / else if / else
循环
for / while / do while
合约
constructor
合约
interface
合约
继承
合约
创建
合约
导⼊合约
合约
导⼊库
事件
event
错误处理
require / revert / assert
错误处理
try / catch
资产
payable
资产
发送
资产
接收
参数
gas spent
gas price
gas limit
block gas limit
技巧
使⽤ calldata 替换 memory
将状态变量载⼊内存
使⽤ i++ ⽽不是 ++i
缓存数组元素
…
资产
Gas Fee
References
Demo code repository
https://github.com/pseudoyu/learn-solidity
Other resources
Solidity by Example
Ethereum 區塊鏈!智能合約(Smart Contract)與分散式網⾴應⽤(dApp)⼊⾨
区块链⼊⻔指南
Solidity 智能合约开发 - 基础
https://www.pseudoyu.com/zh/2022/05/25/learn_solidity_from_scratch_basic/
通过状态锁在 Solidity 智能合约中实现两阶段提交
https://www.pseudoyu.com/zh/2022/07/01/two_phase_commit_contract_practice_in_solidity/
Thank you!
emit Log(msg.sender, “Thank You”);
张宇 | October 26, 2022

Mais conteúdo relacionado

Semelhante a Solidity 智能合约开发 - 基础

物聯網應用全貌以及微軟全球案例
物聯網應用全貌以及微軟全球案例物聯網應用全貌以及微軟全球案例
物聯網應用全貌以及微軟全球案例Herman Wu
 
IoT Cloud Platforms- Players, Vendors and Vertical Segments -20160519
IoT Cloud Platforms- Players, Vendors and Vertical Segments -20160519IoT Cloud Platforms- Players, Vendors and Vertical Segments -20160519
IoT Cloud Platforms- Players, Vendors and Vertical Segments -20160519August Lin
 
線上區塊鏈投票.pdf
線上區塊鏈投票.pdf線上區塊鏈投票.pdf
線上區塊鏈投票.pdfBrownWood28
 
美国云计算发展现状及趋势-2010
美国云计算发展现状及趋势-2010美国云计算发展现状及趋势-2010
美国云计算发展现状及趋势-2010Jiang Zhu
 
SDChain - Blockchain 4.0: 创建可信物联网数字资产的区块链生态体系
SDChain - Blockchain 4.0: 创建可信物联网数字资产的区块链生态体系SDChain - Blockchain 4.0: 创建可信物联网数字资产的区块链生态体系
SDChain - Blockchain 4.0: 创建可信物联网数字资产的区块链生态体系Liu Jen Hao
 
貫通物聯網每一哩路 with Microsfot Azure IoT Sutie
貫通物聯網每一哩路 with Microsfot Azure IoT Sutie貫通物聯網每一哩路 with Microsfot Azure IoT Sutie
貫通物聯網每一哩路 with Microsfot Azure IoT SutieHerman Wu
 
速解元宇宙
速解元宇宙速解元宇宙
速解元宇宙Jedi Lu
 
Azure IoT Solution Guilde - Traditional Chinese
Azure IoT Solution Guilde - Traditional ChineseAzure IoT Solution Guilde - Traditional Chinese
Azure IoT Solution Guilde - Traditional ChineseDaniel Li
 
雲端運算的演進與定義
雲端運算的演進與定義雲端運算的演進與定義
雲端運算的演進與定義Awei Hsu
 
云桌面远程传输技术综述-李承东
云桌面远程传输技术综述-李承东云桌面远程传输技术综述-李承东
云桌面远程传输技术综述-李承东承东 李
 
Rockbay 20180403-scm saa s introduction
Rockbay 20180403-scm saa s introductionRockbay 20180403-scm saa s introduction
Rockbay 20180403-scm saa s introductionRalph Yin
 
跨越科技公司介紹-商業模式的雲端服務
跨越科技公司介紹-商業模式的雲端服務跨越科技公司介紹-商業模式的雲端服務
跨越科技公司介紹-商業模式的雲端服務yccservice
 
Lean Message Architecture Highlights
Lean Message Architecture HighlightsLean Message Architecture Highlights
Lean Message Architecture Highlightssunng87
 
LeanMessage 实时通信云架构
LeanMessage 实时通信云架构LeanMessage 实时通信云架构
LeanMessage 实时通信云架构wenny yuan
 
CRM 2.0 透過資訊科技以提升客戶關係管理質素
CRM 2.0 透過資訊科技以提升客戶關係管理質素CRM 2.0 透過資訊科技以提升客戶關係管理質素
CRM 2.0 透過資訊科技以提升客戶關係管理質素Charles Mok
 
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)William Liang
 

Semelhante a Solidity 智能合约开发 - 基础 (20)

物聯網應用全貌以及微軟全球案例
物聯網應用全貌以及微軟全球案例物聯網應用全貌以及微軟全球案例
物聯網應用全貌以及微軟全球案例
 
IoT Cloud Platforms- Players, Vendors and Vertical Segments -20160519
IoT Cloud Platforms- Players, Vendors and Vertical Segments -20160519IoT Cloud Platforms- Players, Vendors and Vertical Segments -20160519
IoT Cloud Platforms- Players, Vendors and Vertical Segments -20160519
 
線上區塊鏈投票.pdf
線上區塊鏈投票.pdf線上區塊鏈投票.pdf
線上區塊鏈投票.pdf
 
雲端運算簡介
雲端運算簡介雲端運算簡介
雲端運算簡介
 
美国云计算发展现状及趋势-2010
美国云计算发展现状及趋势-2010美国云计算发展现状及趋势-2010
美国云计算发展现状及趋势-2010
 
SDChain - Blockchain 4.0: 创建可信物联网数字资产的区块链生态体系
SDChain - Blockchain 4.0: 创建可信物联网数字资产的区块链生态体系SDChain - Blockchain 4.0: 创建可信物联网数字资产的区块链生态体系
SDChain - Blockchain 4.0: 创建可信物联网数字资产的区块链生态体系
 
貫通物聯網每一哩路 with Microsfot Azure IoT Sutie
貫通物聯網每一哩路 with Microsfot Azure IoT Sutie貫通物聯網每一哩路 with Microsfot Azure IoT Sutie
貫通物聯網每一哩路 with Microsfot Azure IoT Sutie
 
速解元宇宙
速解元宇宙速解元宇宙
速解元宇宙
 
什麼是InitVerse.pdf
什麼是InitVerse.pdf什麼是InitVerse.pdf
什麼是InitVerse.pdf
 
Azure IoT Solution Guilde - Traditional Chinese
Azure IoT Solution Guilde - Traditional ChineseAzure IoT Solution Guilde - Traditional Chinese
Azure IoT Solution Guilde - Traditional Chinese
 
sun 云计算
sun 云计算sun 云计算
sun 云计算
 
雲端運算的演進與定義
雲端運算的演進與定義雲端運算的演進與定義
雲端運算的演進與定義
 
云桌面远程传输技术综述-李承东
云桌面远程传输技术综述-李承东云桌面远程传输技术综述-李承东
云桌面远程传输技术综述-李承东
 
Dell
DellDell
Dell
 
Rockbay 20180403-scm saa s introduction
Rockbay 20180403-scm saa s introductionRockbay 20180403-scm saa s introduction
Rockbay 20180403-scm saa s introduction
 
跨越科技公司介紹-商業模式的雲端服務
跨越科技公司介紹-商業模式的雲端服務跨越科技公司介紹-商業模式的雲端服務
跨越科技公司介紹-商業模式的雲端服務
 
Lean Message Architecture Highlights
Lean Message Architecture HighlightsLean Message Architecture Highlights
Lean Message Architecture Highlights
 
LeanMessage 实时通信云架构
LeanMessage 实时通信云架构LeanMessage 实时通信云架构
LeanMessage 实时通信云架构
 
CRM 2.0 透過資訊科技以提升客戶關係管理質素
CRM 2.0 透過資訊科技以提升客戶關係管理質素CRM 2.0 透過資訊科技以提升客戶關係管理質素
CRM 2.0 透過資訊科技以提升客戶關係管理質素
 
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
智慧應用與物聯網發展趨勢 (A Development Trend of Smart Applications and IoT)
 

Solidity 智能合约开发 - 基础