版本比较

密钥

  • 该行被添加。
  • 该行被删除。
  • 格式已经改变。

...

代码块
module rooch_demo::movecraft {
     use std::vector;
     use std::string::{String, utf8};
     
     use rooch::state_channel::{Self, StateChannel};
     
     public fun land_session_join(account: &signer, land_id: u64) {
         let addr = signer::address_of(account);
     
         let game_account = borrow_global_mut<GameAccount>(addr);
         let session = game_account_borrow_session(game_account, land_id);
         
         let inventory = game_account_get_inventory(game_account)
         state_channel::join<Inventory>(account, session.state_channel_id, inventory);
     }
}

1.2.3 离开状态通道

代码块
leave_rooch::state_channel::leave

类型参数:

类型参数

约束

描述

StateT

store

状态类型

...

参数名称

类型

描述

sender

signer

发起方

state_channel_id

u256

状态通道ID

事件:

事件名称

事件数据

描述

leave_state_channel_leave_event

{

“member_address“: “vector<u8>“,

}

离开状态通道事件

...

离开某个状态通道,同时触发状态通道结算,其他用户可以继续使用该状态通道。

示例:

协同编辑器,离开状态通道

代码块
module rooch_demo::editor {
     use rooch::state_channel::{Self, StateChannel};
     
     const ERR_FILE_NOT_EXISTS: u64 = 5;
     
     public fun editing_document_leave(account: &signer, doc_id: u64) {
          let editor = borrow_global_mut<DocumentEditor>(EDITOR_ADDRESS); 
          let editing_doc = table::borrow<u64, EditingDocument>(&editor.editingDocuments, doc_id)
          state_channel::leave(account, editing_doc.channel_id);
     }
}

聊天合约,加入群聊

代码块
module rooch_demo::chat {
     use std::vector;
     use std::string::{String, utf8};
     
     use rooch::state_channel::{Self, StateChannel};
     
     public fun chat_session_leave(account: &signer, chat_group_id: u64) {
         let addr = signer::address_of(account);
  
         let chat_account = borrow_global_mut<ChatAccount>(addr);
         let session = chat_account_borrow_chat_session(chat_account, chat_group_id)
         state_channel::leave(account, session.state_channel_id);
     }
}

MoveCraft合约,加入地块

代码块
module rooch_demo::movecraft {
     use std::vector;
     use std::string::{String, utf8};
     
     use rooch::state_channel::{Self, StateChannel};
     
     public fun land_session_leave(account: &signer, land_id: u64) {
         let addr = signer::address_of(account);
     
         let game_account = borrow_global_mut<GameAccount>(addr);
         let session = game_account_borrow_session(game_account, land_id);
         
         let inventory = state_channel::leave<Inventory>(account, session.state_channel_id);
         game_account_deposit_inventory(game_account, inventory)
     }
}

1.2.4 关闭状态通道

代码块
close_state_channel

...