Mengapa Kami Menciptakan Sui Move

  • Sui Move adalah fitur lengkap dan siap bagi Anda untuk menulis kontrak pintar yang aman dan efisien
  • Sui adalah blockchain pertama yang meningkatkan desain Diem asli dalam cara mengintegrasikan Move, dan kami berbagi contoh konkret dari peningkatan ini.

Perkenalan

  • Seri tutorial tentang pemrograman dengan objek Sui Move
  • Buku masak dasar-dasar Sui Move, pola desain, dan sampel
  • Plugin VSCode yang disempurnakan dengan dukungan untuk pemahaman kode dan diagnostik kesalahan yang dibangun oleh tim Mysten Move!
  • Integrasi build Move, pengujian, manajemen paket, pembuatan dokumentasi, dan Move Prover dengan CLIsui
  • Serangkaian contoh termasuk token yang dapat dipertukarkan, NFT, DeFi, dan game.

Tunggu, ada Move yang berbeda?

Penciptaan aset massal tanpa gesekan

struct CoolAsset { id: GUID, creation_date: u64 } has key, storepublic entry fun mass_mint(creator: &signer, recipients: vector<address>) {
assert!(signer::address_of(creator) == CREATOR, EAuthFail);
let i = 0;
while (!vector::is_empty(recipients)) {
let recipient = vector::pop_back(&mut recipients);
assert!(exists<Account>(recipient), ENoAccountAtAddress);
let id = guid::create(creator);
let creation_date = timestamp::today();
// error! recipient must be `&signer`, not `address`
move_to(recipient, CoolAsset { id, creation_date })
}
  1. Alamat non-A mengirimkan transaksi untuk membuat akun di A
  2. Pemilik A mengirim transaksi untuk secara eksplisit ikut serta dalam menerima objek jenis CoolAsset
struct CoolAsset { id: VersionedID, creation_date: u64 } has keypublic entry fun mass_mint(recipients: vector<address>, ctx: &mut TxContext) {
assert!(tx_context::sender(ctx) == CREATOR, EAuthFail);
let i = 0;
while (!vector::is_empty(recipients)) {
let recipient = vector::pop_back(&mut recipients);
let id = tx_context::new_id(ctx);
let creation_date = tx_context::epoch(); // Sui epochs are 24 hours
transfer(CoolAsset { id, creation_date }, recipient)
}
}

Kepemilikan dan transfer aset native

struct CoolAssetStore has key {
assets: Table<TokenId, CoolAsset>
}public fun opt_in(addr: &signer) {
move_to(addr, CoolAssetHolder { assets: table::new() }
}public entry fun cool_transfer(
addr: &signer, recipient: address, id: TokenId
) acquires CoolAssetStore {
// withdraw
let sender = signer::address_of(addr);
assert!(exists<CoolAssetStore>(sender), ETokenStoreNotPublished);
let sender_assets = &mut borrow_global_mut<CoolAssetStore>(sender).assets;
assert!(table::contains(sender_assets, id), ETokenNotFound);
let asset = table::remove(&sender_assets, id);
// check that 30 days have elapsed
assert!(time::today() > asset.creation_date + 30, ECantTransferYet)
// deposit
assert!(exists<CoolAssetStore>(recipient), ETokenStoreNotPublished);
let recipient_assets = &mut borrow_global_mut<CoolAssetStore>(recipient).assets;
assert!(table::contains(recipient_assets, id), ETokenIdAlreadyUsed);
table::add(recipient_assets, asset)
}
public entry fun cool_transfer(
asset: CoolAsset, recipient: address, ctx: &mut TxContext
) {
assert!(tx_context::epoch(ctx) > asset.creation_date + 30, ECantTransferYet);
transfer(asset, recipient)
}
  • Resolves the ID’s to object values (removing the need for the and parts in the Diem-style code above)borrow_global_muttable_remove
  • Checks that the object is owned by the sender of the transaction (removing the need for the part + associated code above). This part is especially interesting, as we will explain shortly: in Sui, safe object ownership ownership checks are part of the runtime!signer::address_of
  • Checks the types of the object values against the parameter types of the invoked function cool_transfer
  • Binds the object values and other arguments to the parameters of and invokes the functioncool_transfer

Bundling aset heterogen

  • Alice telah mendefinisikan objek yang akan digunakan dalam gameCharacter
  • Alice ingin mendukung aksesorisasi karakternya dengan aksesori pihak ketiga dari berbagai jenis yang dibuat nanti
  • Siapa pun harus dapat membuat aksesori, tetapi pemilik harus memutuskan apakah akan menambahkan aksesori atau tidak.Character
  • Mentransfer a harus secara otomatis mentransfer semua aksesorinya.Character
// in the Character module, created by Alice
struct Character has key {
id: VersionedID,
favorite_color: u8,
strength: u64,
...
}/// The owner of `c` can choose to add `accessory`
public entry fun accessorize<T: key>(c: &mut Character, accessory: T) {
transfer_to_object(c, accessory)
}// ... in a module added later by Bob
struct SpecialShirt has key {
id: VersionedID,
color: u8
}public entry fun dress(c: &mut Character, s: Shirt) {
// a special shirt has to be the character's favorite color
assert!(character::favorite_color(c) == s.color, EBadColor);
character::accessorize(c, shirt)
}// ... in a module added later by Clarissa
struct Sword has key {
id: VersionedID,
power: u64
}public entry fun equip(c: &mut Character, s: Sword) {
// a character must be very strong to use a powerful sword
assert!(character::strength(c) > sword.power * 2, ENotStrongEnough);
character::accessorize(c, s)
}
// attempt 1
struct Character {
// won't work because every Accessory would need to be the same type + have
// the same fields. There is no subtyping in Move.
// Bob's shirt needs a color, and Clarissa's sword needs power--no standard
// representation of Accessory can anticipate everything devs will want to
// create
accessories: vector<Accessory>
}// attempt 2
struct Character {
// perhaps Alice anticipates the need for a Sword and a Shirt up front...
sword: Option<Sword>,
shirt: Option<Shirt>
// ...but what happens when Daniel comes along later and wants to add Pants?
}// attempt 3
// Does not support accessory compositions. For example: how do we represent a
// Character with Pants and a Shirt, but no Sword?
struct Shirt { c: Character }
struct Sword { s: Shirt }
struct Pants { s: Sword }
  • Hanya koleksi homogen yang didukung (seperti yang ditunjukkan oleh upaya pertama), tetapi aksesori pada dasarnya heterogen
  • Asosiasi antara objek hanya dapat dibuat melalui “pembungkusan” (yaitu, menyimpan objek di dalam objek lain); tetapi himpunan objek yang dapat dibungkus harus didefinisikan di depan (seperti pada upaya kedua) atau ditambahkan secara ad-hoc yang tidak mendukung komposisi aksesori (seperti pada upaya ketiga)

Kesimpulan

Pelajari lebih lanjut tentang Sui

Bangun bersama kami!!

--

--

Just ordinary Crypto 🖤 NFT!

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store