主要围绕链游相关内容展开,包括在主网完成游戏的上链部署这一目标。具体需求涵盖完成链游知识学习、随机数相关学习(游戏需包含随机数)、游戏资金池的存取操作。
Faucet Coin
,用task2
的 Faucet Coin
作为游戏输赢的资产github id
的元素主要功能包括: 初始化:创建游戏对象和管理员权限对象,并将管理员权限对象转移给发送者。 玩游戏:玩家通过猜测硬币的正反面来参与游戏,根据结果赢得或失去 SUI 币。 添加 SUI:允许玩家向游戏中添加 SUI 币。 移除 SUI:允许管理员从游戏中移除 SUI 币。
CFG流程图
/// 定义游戏对象
public struct Game has key {
id: UID,
val: Balance<SUI>
}
/// 定义管理员权限对象
public struct AdminCap has key {
id: UID,
}
/// 创建游戏对象和管理员权限对象,并将管理员权限对象转移给发送者。
fun init(ctx: &mut TxContext) {
let game = Game {
id: object::new(ctx),
val: balance::zero()
};
share_object(game);
let admin = AdminCap {
id: object::new(ctx)
};
transfer(admin, sender(ctx));
}
entry fun play(game: &mut Game, flip_value: bool, in: Coin<SUI>, rand: &Random,
ctx: &mut TxContext) {
let coin_value = coin::value(&in);
let play_address = sender(ctx);
let game_val = balance::value(&game.val) ;
/// 检查游戏余额是否足够。
if (game_val < coin_value) {
abort 100u64;
};
let mut gen = random::new_generator(rand, ctx);
/// 生成随机布尔值。
let mut flag = random::generate_bool(&mut gen);
if (flip_value == flag) {
/// 如果玩家猜对了,将相应的余额转移给玩家。
let win_balance = balance::split(&mut game.val, coin_value);
let win_coin = from_balance(win_balance, ctx);
public_transfer(win_coin, play_address);
public_transfer(in, play_address);
} else {
/// 否则,将玩家的投注金额加入游戏余额。
let in_balance = into_balance(in);
balance::join(&mut game.val, in_balance);
}
}
public entry fun add_sui(game: &mut Game, in: Coin<SUI>, _ctx: &TxContext) {
let in_balance = into_balance(in);
balance::join(&mut game.val, in_balance);
}
public entry fun remove_sui(_: &AdminCap, game: &mut Game,
amt: u64, ctx: &mut TxContext) {
let win_balance = balance::split(&mut game.val, amt);
let win_coin = from_balance(win_balance, ctx);
public_transfer(win_coin, sender(ctx));
}
例子里面留了两个todo,
/// 防止 all in 战神
if (coin_value == game_val) {
abort(ERROR_EXCEED_MAX_BET);
};
/// 这个限制住了自然也不存在ALLIN了
if (coin_value > game_val / 10) {
abort 101u64;
}
/*
/// Module: move_game
module move_game::move_game;
*/
module move_game::flip_coin {
use sui::balance;
use sui::balance::Balance;
use sui::coin;
use sui::coin::{Coin, from_balance, into_balance};
use sui::object;
use sui::random;
use sui::random::Random;
use sui::transfer::{share_object, transfer, public_transfer};
use sui::tx_context::sender;
use faucet_coin::leon_dev_1024_faucet_coin::{LEON_DEV_1024_FAUCET_COIN};
public struct Game has key {
id: UID,
val: Balance<LEON_DEV_1024_FAUCET_COIN>
}
public struct AdminCap has key {
id: UID,
}
fun init(ctx: &mut TxContext) {
let game = Game {
id: object::new(ctx),
val: balance::zero()
};
share_object(game);
let admin = AdminCap {
id: object::new(ctx)
};
transfer(admin, sender(ctx));
}
entry fun play(game: &mut Game, flip_value: bool, in: Coin<LEON_DEV_1024_FAUCET_COIN>, rand: &Random,
ctx: &mut TxContext) {
let coin_value = coin::value(&in);
let play_address = sender(ctx);
let game_val = balance::value(&game.val) ;
if (game_val < coin_value) {
abort 100u64;
};
//防止 all in 战神
//每次最大值 就是合约里面钱的10分之一就行了
if (coin_value > game_val / 10) {
abort 101u64;
};
let mut gen = random::new_generator(rand, ctx);
let mut flag = random::generate_bool(&mut gen);
if (flip_value == flag) {
let win_balance = balance::split(&mut game.val, coin_value);
let win_coin = from_balance(win_balance, ctx);
public_transfer(win_coin, play_address);
public_transfer(in, play_address);
}else {
let in_balance = into_balance(in);
balance::join(&mut game.val, in_balance);
}
}
// 存钱
public entry fun add_sui(game: &mut Game, in: Coin<LEON_DEV_1024_FAUCET_COIN>, _ctx: &TxContext) {
let in_balance = into_balance(in);
balance::join(&mut game.val, in_balance);
}
// 取钱
public entry fun remove_sui(_: &AdminCap, game: &mut Game,
amt: u64, ctx: &mut TxContext) {
let win_balance = balance::split(&mut game.val, amt);
let win_coin = from_balance(win_balance, ctx);
public_transfer(win_coin, sender(ctx));
}
}
修改Move.toml文件,添加之前的faucet_coin
sui move build --skip-fetch-latest-git-deps
当前环境查看和网络环境切换参考之前的命令
sui client envs
sui client switch --env mainnet
sui client publish --gas-budget 100000000 --skip-fetch-latest-git-deps --skip-dependency-verification
发布成功后的交易摘要
Skipping dependency verification
Transaction Digest: 2kBk7UGLLrAodgVkxfmoaCh4SvQxHMuqYKKkWd8dyZeL
sui client call --package 0x1bd47788320409d493dacf5c55fccca592b4cbde9012e3cdcd4ace2b3489a48e --module flip_coin --function play --args 0xa16a5c2a95a4d9fa151dd7faf44e160bc0f1c4ef12b6fc628c3c9cd1895a0da1 true 0xafcdeac741cb1b84d72d7ac8fbb851ce81089e187f8ead34b81a6f0b544cbb59 0x8 --gas-budget 1000000
remove_sui
提现参数说明,
AdminCap
:确保只有管理员可以调用这个函数。Game
:用于访问和修改游戏的状态。amt
:指定要取出的金额。TxContext
:提供交易上下文信息,用于创建 Coin
对象和获取发送者的地址。sui client call --package 0x1bd47788320409d493dacf5c55fccca592b4cbde9012e3cdcd4ace2b3489a48e --module flip_coin --function remove_sui --args 0xda2dc3c6cc563fb9b3d0a1050bbb237da0949014a7ca38de1fb16a62664c55cc 0xa16a5c2a95a4d9fa151dd7faf44e160bc0f1c4ef12b6fc628c3c9cd1895a0da1 100 --gas-budget 10000000
Q1: 直接钱包浏览器页面play 报错, Expect true to be boolean, received string
A:提现报错,这边用Bash命令操作,注意参数的顺序,Random的object id为0x8
Q2: 提现报错 Error executing transaction 'DWtg68kXBTVoHYHgRxXbTbE2ckQx9zeLQPnGDnJ6H6UA': InsufficientGas
A. 提现消耗的Gas手续费比较多,增加gas费用,最后提现成功
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!