Define new asset types
Read, write, and transfer assets
Check and enforce access control policies (权限控制,包含访问权限,操作权限)
DeFi and Payments (Swap 线下支付工具 交易所 微信支付 支付宝支付)
NFT and Gaming ( 发行NFT ,NFT交易市场,构建游戏)
DID, Governance and Social ( 身份认证 社交软件 类似推特,知乎)
目前大家手机里面的App 大部分都可以用Move在Sui在构建一次
Move 是一种强类型的编程语言,所有的数据结构都必须被显式的申明数据类型
u8
u16
u32
u64
u128
u256
bool
fun main () {
// define empty variable, set value later
let a: u8 ;
a = 10 ;
// define variable, set type
let a: u64 = 10 ;
// finally simple assignment
let a = 10 ;
// simple assignment with defined value type
let a = 10u128 ;
// in function calls or expressions you can use ints as constant values
if (a < 10 ) {};
// or like this, with type
if (a < 10u8 ) {}; // usually you don't need to specify type
}
Move没有小数和负数,需要程序设计的时候自己构建小数或者负数
构建小数,首先我们知道小数的定义 是 A/B 不能被整除的场景 ,我们就可以定义一个结构体来表示A/B的场景 详细代码
public fun create_from_rational ( numerator: u64 , denominator: u64 ) : FixedPoint32 {
let scaled_numerator = ( numerator as u128 ) << 64 ;
let scaled_denominator = ( denominator as u128 ) << 32 ;
assert ! ( scaled_denominator != 0 , EDENOMINATOR ) ;
let quotient = scaled_numerator / scaled_denominator;
FixedPoint32 { value : ( quotient as u64 ) }
}
负数的话用u8类型举例子 1-127表示负数 128-256表示正数展现应用自行转换一下就行
address
数组vector
字符串 string
hashmap( table ,bag)
和 C Rust GO的struct一致,C++ Java Swift Javascript面向对象编程语言的类类似
public struct DonutShop has key {
id : UID ,
price : u64 ,
balance : Balance < SUI >
}
public entry fun eat_donut ( d: Donut ) {
let Donut { id } = d;
object:: delete ( id) ;
}
只能是私有的
会在发布合约的是自动调用一次
只有两种形式
fun init ( ctx: & mut TxContext ) { }
fun init ( witness: Struct , ctx: & mut TxContext ) { }
module examples ::one_timer ;
public struct CreatorCapability has key {
id: UID
}
fun init (ctx: &mut TxContext ) {
transfer::transfer (CreatorCapability {
id: object::new (ctx),
}, tx_context::sender (ctx))
}
方法签名
调用范围
返回值
fun call()
模块内调用
可以有
public fun call()
只能模块能调用
可以有
public entry fun call()
模块能调用,DApp能调用
暂时没有
entry fun call()
只能DApp调用
暂时没有
public(package) fun call()
当前模块调用
可以有
public struct Box_U64 {
value : u64
}
public struct Box < T > {
value : T
}
public fun create_box ( value: u64 ) : Box <u64> {
Box <u64>{ value }
}
public fun create_box<T >( value: T ) : Box <T > {
Box <T > { value }
}
struct T has key { }
struct T has key, store { }
struct T has copy, drop { }
copy 被修饰的值可以被复制。
drop 被修饰的值在作用域结束时可以被丢弃。
key 被修饰的值可以作为键值对全局状态进行访问。
store 被修饰的值可以被存储到全局状态
只有key 可以自定义转移对象所有权,比如实现不可转移的资产,不可转移的身份信息 例子
key+store 可以拥有者可以自由转移对象所有权,token这种资产 例子
store 简单理解为这种类型的结构体可以作为类型存储在别的结构体里面
drop 实现Move特定的设计模式,比如 见证者模式
没有任何能力可以直接实现闪电贷 这种其他合约语言非常难实现的合约
module examples ::item {
use sui::transfer;
use sui::object::{Self , UID };
use std::string::{Self , String };
use sui::tx_context::{Self , TxContext };
public struct AdminCap has key { id: UID }
public struct Item has key , store { id: UID , name: String }
fun init (ctx: &mut TxContext ) {
transfer::transfer (AdminCap {
id: object::new (ctx)
}, tx_context::sender (ctx))
}
public entry fun create_and_send (
_: &AdminCap , name: vector <u8 >, to: address , ctx: &mut TxContext
) {
transfer::transfer (Item {
id: object::new (ctx),
name: string::utf8 (name)
}, to)
}
}
public struct DonutShop has key {
id : UID ,
price : u64 ,
balance : Balance < SUI >
}
Sui 维护了一个全局的 map<id,object>的结构,所有的对象都是全局存储的
有一个字段会标明每一个对象都是谁在拥有
对象被某一个具体的钱包地址拥有
对象被全局共享,所以人拥有
对象是个常量,不可修改的状态权限共享
对象拥有者可以使用这对象,包含读取,修改,转移拥有权等
如何改变对象所有权限 参考 Sui Framework transfer
接下来现场coding环节 并完整的演示如何上链并且查看数据
module blog_demo ::bolg ;
use std::string::String ;
public struct Blog has key {
id: UID ,
title: String ,
content: String
}
// add blog
public entry fun add (title: String , content: String , ctx: &mut TxContext ) {
let blog = Blog {
id: object::new (ctx),
title,
content
};
transfer::share_object (blog);
}
// update title
public entry fun update_title (blog: &mut Blog , title: String ) {
blog.title = title
}
// delete blog
public entry fun delete_bolg (blog: Blog ) {
let Blog { id, title: _, content: _ } = blog;
object::delete (id);
}
不在需要传统的数据库存储数据
依然需要前端(比如网页,APP,PC)
需要理解如何把写好的程序发布到链上
需要理解如何把数据展现给前端 - (RPC)发起HTTP调用
需要理解如果用Move做权限控制
用户需要支付一定的费用(GAS)
起于区块链,不止于区块链
Sui能真正把Move构建的应用带入日常生活中各个领域,真正实现下一个10亿用户级别的应用