-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2136 from wenchao13547/main
task4和5
- Loading branch information
Showing
25 changed files
with
7,658 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# @generated by Move, please check-in and do not edit manually. | ||
|
||
[move] | ||
version = 3 | ||
manifest_digest = "331D7814D75E8C838EE91CC4DE51B96111370C5E9BFA0ABAAD4CED7DC13CD5A9" | ||
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600" | ||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
{ id = "faucet_coin", name = "faucet_coin" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "MoveStdlib" | ||
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates\\sui-framework\\packages\\move-stdlib" } | ||
|
||
[[move.package]] | ||
id = "Sui" | ||
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" } | ||
|
||
dependencies = [ | ||
{ id = "MoveStdlib", name = "MoveStdlib" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "faucet_coin" | ||
source = { local = "..\\..\\task2\\faucet_coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[move.toolchain-version] | ||
compiler-version = "1.37.1" | ||
edition = "2024.beta" | ||
flavor = "sui" | ||
|
||
[env] | ||
|
||
[env.testnet] | ||
chain-id = "4c78adac" | ||
original-published-id = "0xf5ae27970420e7712a8a1b6ba9e59fdaf46e6a16a74842759c096bea75fba910" | ||
latest-published-id = "0xf5ae27970420e7712a8a1b6ba9e59fdaf46e6a16a74842759c096bea75fba910" | ||
published-version = "1" | ||
|
||
[env.mainnet] | ||
chain-id = "35834a8a" | ||
original-published-id = "0x1a3ba37d2daf49c7b05caf6a2ca77329951d19026df2d26bf9c5b9301d92ae60" | ||
latest-published-id = "0x1a3ba37d2daf49c7b05caf6a2ca77329951d19026df2d26bf9c5b9301d92ae60" | ||
published-version = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
[package] | ||
name = "flip_coin" | ||
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move | ||
# license = "" # e.g., "MIT", "GPL", "Apache 2.0" | ||
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"] | ||
|
||
[dependencies] | ||
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" } | ||
faucet_coin = { local = "../../task2/faucet_coin" } | ||
# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`. | ||
# Revision can be a branch, a tag, and a commit hash. | ||
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" } | ||
|
||
# For local dependencies use `local = path`. Path is relative to the package root | ||
# Local = { local = "../path/to" } | ||
|
||
# To resolve a version conflict and force a specific version for dependency | ||
# override use `override = true` | ||
# Override = { local = "../conflicting/version", override = true } | ||
|
||
[addresses] | ||
flip_coin = "0x1a3ba37d2daf49c7b05caf6a2ca77329951d19026df2d26bf9c5b9301d92ae60" | ||
# Named addresses will be accessible in Move as `@name`. They're also exported: | ||
# for example, `std = "0x1"` is exported by the Standard Library. | ||
# alice = "0xA11CE" | ||
|
||
[dev-dependencies] | ||
# The dev-dependencies section allows overriding dependencies for `--test` and | ||
# `--dev` modes. You can introduce test-only dependencies here. | ||
# Local = { local = "../code/task2/faucet_coin" } | ||
[dev-addresses] | ||
# The dev-addresses section allows overwriting named addresses for the `--test` | ||
# and `--dev` modes. | ||
# alice = "0xB0B" | ||
|
101 changes: 101 additions & 0 deletions
101
mover/wenchao13547/code/task4/flip_coin/sources/flip_coin.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
module flip_coin::flip_coin { | ||
use std::ascii::{String, string}; | ||
use sui::balance; | ||
use sui::balance::Balance; | ||
use sui::coin::{Self, Coin, from_balance, into_balance, split}; | ||
use sui::random; | ||
use sui::random::Random; | ||
use sui::transfer::{share_object, transfer, public_transfer}; | ||
use sui::tx_context::sender; | ||
use faucet_coin::rr::RR; | ||
// use id::模块名::资源名 | ||
// 创建一个池子,并指定代币 | ||
public struct Game has key { | ||
id: UID, | ||
val: Balance<RR>, | ||
creator: String | ||
} | ||
|
||
// 设置管理员 | ||
public struct Admin has key { | ||
id: UID | ||
} | ||
|
||
fun init(ctx: &mut TxContext) { | ||
// 创建一个游戏对象,设置池子初始余额为0 | ||
let game = Game { | ||
id: object::new(ctx), | ||
val: balance::zero(), | ||
creator: string(b"wenchao13547") | ||
}; | ||
share_object(game); // 共享游戏对象 | ||
|
||
// 设置一个管理员对象 | ||
let admin = Admin { | ||
id: object::new(ctx) | ||
}; | ||
transfer(admin,sender(ctx)); // 将管理员权限转移给发送者 | ||
} | ||
|
||
|
||
public entry fun play( | ||
game: &mut Game, | ||
// 0x575393a3353c9678533de4bda5c797405bb04f20afdd8bd2c9be795aeb814514 0x1a3ba37d2daf49c7b05caf6a2ca77329951d19026df2d26bf9c5b9301d92ae60::flip_coin::Game | ||
user_value: bool, | ||
user_in: Coin<RR>, | ||
rand: &Random, | ||
ctx: &mut TxContext | ||
) { | ||
let user_in_coin = coin::value(&user_in); // 获取玩家下注硬币的值 | ||
let user_address = sender(ctx); // 获取玩家的地址 | ||
let game_val = balance::value(&game.val); // 获取游戏池子中的余额 | ||
|
||
// 如果池子的余额少于玩家下注的,则返回错误码 100 | ||
if (game_val < user_in_coin) { | ||
abort 100u64 | ||
}; | ||
|
||
// 创建一个随机数生成器,并转换为 bool 值 | ||
let mut generator = random::new_generator(rand,ctx); | ||
let mut flag = random::generate_bool(&mut generator); | ||
|
||
// 判断玩家是否猜对了 | ||
if (user_value == flag) { | ||
let win_balance = balance::split(&mut game.val,user_in_coin); // 从池子余额中分出与玩家下注的等值金额 | ||
let win_coin = from_balance(win_balance,ctx); // 将赢得的金额转为硬币 | ||
public_transfer(win_coin,user_address); // 将赢得的硬币转移给玩家 | ||
public_transfer(user_in,user_address); // 将玩家下注的硬币退还给玩家 | ||
} else { | ||
let in_balance = into_balance(user_in); // 将玩家的硬币转换为余额 | ||
balance::join(&mut game.val,in_balance); // 将余额加入到游戏的池子中 | ||
} | ||
} | ||
|
||
// 将代币添加到游戏中 | ||
public entry fun add_coin( | ||
game: &mut Game, | ||
add_coin: Coin<RR>, | ||
// 0xc195872db444df619c1310641cb501480eed42f0da6ddf89845a4ae5a8130bf3 0x2::coin::Coin<0xdcf3749d51e66858ee2443fcc0b92b33482986580aa2db686e5a0d1e5a07ffac::rr::RR> | ||
_ctx: &mut TxContext | ||
) { | ||
let add_coin_in_balance = into_balance(add_coin); // 将硬币转换为余额 | ||
balance::join(&mut game.val, add_coin_in_balance); // 将余额加入到游戏的总余额中 | ||
} | ||
|
||
// 分割代币 | ||
public fun split_coin<FAUCETCOIN>(coin: &mut Coin<FAUCETCOIN>, amt: u64, ctx: &mut TxContext): Coin<FAUCETCOIN> { | ||
split(coin, amt, ctx) | ||
} | ||
|
||
// 从游戏池子中取出 | ||
public entry fun remove_coin( | ||
_: &Admin, | ||
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)); // 将硬币转移给发送者 | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
mover/wenchao13547/code/task4/flip_coin/tests/flip_coin_tests.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
#[test_only] | ||
module flip_coin::flip_coin_tests; | ||
// uncomment this line to import the module | ||
// use flip_coin::flip_coin; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_flip_coin() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::flip_coin::flip_coin_tests::ENotImplemented)] | ||
fun test_flip_coin_fail() { | ||
abort ENotImplemented | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# @generated by Move, please check-in and do not edit manually. | ||
|
||
[move] | ||
version = 3 | ||
manifest_digest = "0ABEA329772A8D711F86E0D3EFA3F31FF83C53CB25701CB84825DEF80240B4DB" | ||
deps_digest = "060AD7E57DFB13104F21BE5F5C3759D03F0553FC3229247D9A7A6B45F50D03A3" | ||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
{ id = "faucet_coin", name = "faucet_coin" }, | ||
{ id = "my_coin", name = "my_coin" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "MoveStdlib" | ||
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates\\sui-framework\\packages\\move-stdlib" } | ||
|
||
[[move.package]] | ||
id = "Sui" | ||
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" } | ||
|
||
dependencies = [ | ||
{ id = "MoveStdlib", name = "MoveStdlib" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "faucet_coin" | ||
source = { local = "..\\..\\task2\\faucet_coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "my_coin" | ||
source = { local = "..\\..\\task2\\my_coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[move.toolchain-version] | ||
compiler-version = "1.37.1" | ||
edition = "2024.beta" | ||
flavor = "sui" | ||
|
||
[env] | ||
|
||
[env.mainnet] | ||
chain-id = "35834a8a" | ||
original-published-id = "0x051485a42d89904bae697b2d99ef82873cb391f2e233e052959b7ced1962ecff" | ||
latest-published-id = "0x051485a42d89904bae697b2d99ef82873cb391f2e233e052959b7ced1962ecff" | ||
published-version = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
[package] | ||
name = "wenchao13547" | ||
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move | ||
# license = "" # e.g., "MIT", "GPL", "Apache 2.0" | ||
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"] | ||
|
||
[dependencies] | ||
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" } | ||
faucet_coin={ local = "../../task2/faucet_coin" } | ||
my_coin={ local = "../../task2/my_coin" } | ||
# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`. | ||
# Revision can be a branch, a tag, and a commit hash. | ||
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" } | ||
|
||
# For local dependencies use `local = path`. Path is relative to the package root | ||
# Local = { local = "../path/to" } | ||
|
||
# To resolve a version conflict and force a specific version for dependency | ||
# override use `override = true` | ||
# Override = { local = "../conflicting/version", override = true } | ||
|
||
[addresses] | ||
wenchao13547 = "0x0" | ||
|
||
# Named addresses will be accessible in Move as `@name`. They're also exported: | ||
# for example, `std = "0x1"` is exported by the Standard Library. | ||
# alice = "0xA11CE" | ||
|
||
[dev-dependencies] | ||
# The dev-dependencies section allows overriding dependencies for `--test` and | ||
# `--dev` modes. You can introduce test-only dependencies here. | ||
# Local = { local = "../path/to/dev-build" } | ||
|
||
[dev-addresses] | ||
# The dev-addresses section allows overwriting named addresses for the `--test` | ||
# and `--dev` modes. | ||
# alice = "0xB0B" | ||
|
83 changes: 83 additions & 0 deletions
83
mover/wenchao13547/code/task5/wenchao13547/sources/wenchao13547.move
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
module wenchao13547::wenchao13547 { | ||
// 引入Sui系统中与余额、货币和交易相关的模块 | ||
use sui::balance; | ||
use sui::balance::Balance; | ||
use sui::coin; | ||
use sui::coin::{Coin, into_balance, from_balance}; | ||
use sui::transfer::public_transfer; | ||
use sui::tx_context::sender; | ||
use my_coin::uu::UU; // 自定义货币 UU | ||
use faucet_coin::rr::RR; // 自定义货币 RR | ||
|
||
// 定义银行结构,包含两个余额字段,分别用于存储两种货币 | ||
public struct Bank has key { | ||
id: UID, // 唯一标识符,用于标识银行对象 | ||
hiro: Balance<UU>, // MYCOIN货币的余额 | ||
fct: Balance<RR>, // FAUCETCOIN货币的余额 | ||
} | ||
|
||
// 定义管理员权限结构,包含唯一标识符 | ||
public struct AdminCap has key { | ||
id: UID, // 唯一标识符,用于标识管理员权限对象 | ||
} | ||
|
||
// 初始化函数,用于实例化Bank和AdminCap对象 | ||
fun init(ctx: &mut TxContext) { | ||
// 创建银行对象,初始余额均为0 | ||
let bank = Bank { | ||
id: object::new(ctx), // 创建新的银行对象ID | ||
hiro: balance::zero<UU>(), // 初始化MYCOIN余额为0 | ||
fct: balance::zero<RR>(), // 初始化FAUCETCOIN余额为0 | ||
}; | ||
|
||
// 将银行对象设置为共享对象,允许多人访问 | ||
transfer::share_object(bank); | ||
|
||
// 创建管理员权限对象 | ||
let admincap = AdminCap { | ||
id: object::new(ctx), // 创建新的管理员权限对象ID | ||
}; | ||
|
||
// 将管理员权限对象转移给当前交易发起者 | ||
transfer::transfer(admincap, sender(ctx)); | ||
} | ||
|
||
// 将 MYCOIN 货币转为余额并存入银行 | ||
public entry fun deposit_hiro(bank: &mut Bank, hiro: Coin<UU>) { | ||
let hiro_balance = into_balance(hiro); // 将MYCOIN货币转为余额 | ||
balance::join(&mut bank.hiro, hiro_balance); // 将余额加入银行的MYCOIN余额中 | ||
} | ||
// 0x046911d077b4abb0cacf33a97460f39686dcf7d427f459976d5430d11afa5e6d | ||
// 将 FAUCETCOIN 货币转为余额并存入银行 | ||
public entry fun deposit_fct(bank: &mut Bank, fct: Coin<RR>) { | ||
let fct_balance = into_balance(fct); // 将FAUCETCOIN货币转为余额 | ||
balance::join(&mut bank.fct, fct_balance); // 将余额加入银行的FAUCETCOIN余额中 | ||
} | ||
|
||
// 从银行中提取指定金额的 MYCOIN 并将其转为Coin | ||
public entry fun withdrawal_hiro(bank: &mut Bank, amt: u64, ctx: &mut TxContext) { | ||
let hiro_balance = balance::split(&mut bank.hiro, amt); // 从银行的MYCOIN余额中分离出指定金额 | ||
let hiro_coin = from_balance(hiro_balance, ctx); // 将余额转为货币形式 | ||
public_transfer(hiro_coin, sender(ctx)); // 转移货币给当前交易发起者 | ||
} | ||
|
||
// 使用 MYCOIN 兑换 FAUCETCOIN,兑换比例为 1:10 | ||
public entry fun swap_hiro_fct(bank: &mut Bank, hiro: Coin<UU>, ctx: &mut TxContext) { | ||
let amt = coin::value(&hiro); // 获取MYCOIN货币的金额 | ||
balance::join(&mut bank.hiro, into_balance(hiro)); // 将MYCOIN存入银行 | ||
let amt_fct = amt * 100 / 10; // 计算需要兑换的FAUCETCOIN金额 | ||
let fct_balance = balance::split(&mut bank.fct, amt_fct); // 从银行的FAUCETCOIN余额中分离兑换金额 | ||
let fct_coin = from_balance(fct_balance, ctx); // 将FAUCETCOIN余额转为货币形式 | ||
public_transfer(fct_coin, sender(ctx)); // 转移FAUCETCOIN给交易发起者 | ||
} | ||
|
||
// 使用 FAUCETCOIN 兑换 MYCOIN,兑换比例为 10:1 | ||
public entry fun swap_fct_hiro(bank: &mut Bank, fct: Coin<RR>, ctx: &mut TxContext) { | ||
let amt = coin::value(&fct); // 获取FAUCETCOIN货币的金额 | ||
balance::join(&mut bank.fct, into_balance(fct)); // 将FAUCETCOIN存入银行 | ||
let amt_hiro = amt * 10 / 100; // 计算需要兑换的MYCOIN金额 | ||
let hiro_balance = balance::split(&mut bank.hiro, amt_hiro); // 从银行的MYCOIN余额中分离兑换金额 | ||
let hiro_coin = from_balance(hiro_balance, ctx); // 将MYCOIN余额转为货币形式 | ||
public_transfer(hiro_coin, sender(ctx)); // 转移MYCOIN给交易发起者 | ||
} | ||
} |
Oops, something went wrong.