-
Notifications
You must be signed in to change notification settings - Fork 898
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 #2042 from Sy711/task1
Task6
- Loading branch information
Showing
31 changed files
with
5,753 additions
and
12 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
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
File renamed without changes.
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
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,43 @@ | ||
# @generated by Move, please check-in and do not edit manually. | ||
|
||
[move] | ||
version = 3 | ||
manifest_digest = "E47161DD567159FAADB592942330014AC6B1E47DA64573087150257F6F212777" | ||
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.35.2" | ||
edition = "2024.beta" | ||
flavor = "sui" | ||
|
||
[env] | ||
|
||
[env.mainnet] | ||
chain-id = "35834a8a" | ||
original-published-id = "0x57a309dc952c878fe9016bc53cbbd546ca9736e53734fe497b17cc8dae81990c" | ||
latest-published-id = "0x57a309dc952c878fe9016bc53cbbd546ca9736e53734fe497b17cc8dae81990c" | ||
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,37 @@ | ||
[package] | ||
name = "task4" | ||
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" } | ||
|
||
# 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 | ||
faucet_coin = { local = "../task2/faucet_coin" } | ||
|
||
# To resolve a version conflict and force a specific version for dependency | ||
# override use `override = true` | ||
# Override = { local = "../conflicting/version", override = true } | ||
|
||
[addresses] | ||
task4 = "0x0" | ||
faucet_coin="0xb3743a94cbc755521c27fb5d91e2c8b34ea5b0df899654c20d2f965d301bb7dd" | ||
# 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" | ||
|
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,85 @@ | ||
module task4::task4 { | ||
|
||
use sui::balance::{Self,Balance, zero}; | ||
use sui::coin::{Self}; | ||
use sui::random::{Self,Random}; | ||
use sui::transfer::{share_object, transfer}; | ||
use sui::tx_context::sender; | ||
use faucet_coin::faucet_coin::{FAUCET_COIN}; | ||
|
||
const EUserInsufficientBalance :u64 =1; | ||
public struct Game has key { | ||
id: UID, | ||
val: Balance<FAUCET_COIN> | ||
} | ||
|
||
public struct AdaminCap has key { | ||
id: UID | ||
} | ||
|
||
|
||
fun init(ctx: &mut TxContext) { | ||
let game = Game { | ||
id: object::new(ctx), | ||
val: zero() | ||
}; | ||
|
||
share_object(game); | ||
|
||
let admin = AdaminCap { | ||
id: object::new(ctx) | ||
}; | ||
|
||
transfer(admin, sender(ctx)); | ||
} | ||
#[allow(lint(public_random))] | ||
public entry fun play( | ||
game: &mut Game, | ||
flip_value: bool, | ||
in: &mut coin::Coin<FAUCET_COIN>, | ||
rand: &Random, | ||
amount: u64, | ||
ctx: &mut TxContext | ||
) { | ||
let mut gen = random::new_generator(rand, ctx); | ||
let flag = random::generate_bool(&mut gen); | ||
|
||
// 如果获胜 | ||
if (flag == flip_value) { | ||
// 投入的代币不变,另外奖励等额的代币 | ||
let reward = coin::take(&mut game.val, amount, ctx); | ||
coin::join(in, reward); | ||
} | ||
// 猜错了就损失投入的代币 | ||
else { | ||
Self::public_deposit(game, in, amount) | ||
} | ||
} | ||
|
||
fun deposit(game: &mut Game, in: &mut coin::Coin<FAUCET_COIN>, amount: u64) { | ||
let split_balance = balance::split(coin::balance_mut(in), amount); | ||
balance::join(&mut game.val, split_balance); | ||
assert!( | ||
coin::value(in) >= amount, | ||
EUserInsufficientBalance | ||
); | ||
} | ||
|
||
public entry fun public_deposit( | ||
game: &mut Game, | ||
in: &mut coin::Coin<FAUCET_COIN>, | ||
amount: u64 | ||
) { | ||
deposit(game, in, amount); | ||
} | ||
|
||
public entry fun Withdraw( | ||
game: &mut Game, | ||
_: &AdaminCap, | ||
amount: u64, | ||
ctx: &mut TxContext | ||
) { | ||
let cash = coin::take(&mut game.val, amount, ctx); | ||
transfer::public_transfer(cash, ctx.sender()); | ||
} | ||
} |
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 task4::task4_tests; | ||
// uncomment this line to import the module | ||
// use task4::task4; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_task4() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::task4::task4_tests::ENotImplemented)] | ||
fun test_task4_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 = "C15B387DD5009DA1FA5359768830B6C2DF0D87565D4D16CF64DDECD4FFB0924B" | ||
deps_digest = "060AD7E57DFB13104F21BE5F5C3759D03F0553FC3229247D9A7A6B45F50D03A3" | ||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
{ id = "faucet_coin", name = "faucet_coin" }, | ||
{ id = "mycoin", name = "mycoin" }, | ||
] | ||
|
||
[[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 = "mycoin" | ||
source = { local = "..\\task2\\mycoin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[move.toolchain-version] | ||
compiler-version = "1.35.2" | ||
edition = "2024.beta" | ||
flavor = "sui" | ||
|
||
[env] | ||
|
||
[env.mainnet] | ||
chain-id = "35834a8a" | ||
original-published-id = "0x440e4f33eab2cc50852e7b6467b88d038a836daeb79313954e563f29645a7062" | ||
latest-published-id = "0x440e4f33eab2cc50852e7b6467b88d038a836daeb79313954e563f29645a7062" | ||
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,39 @@ | ||
[package] | ||
name = "task5" | ||
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" } | ||
|
||
# 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 | ||
faucet_coin = { local = "../task2/faucet_coin" } | ||
mycoin = { local = "../task2/mycoin" } | ||
|
||
# To resolve a version conflict and force a specific version for dependency | ||
# override use `override = true` | ||
# Override = { local = "../conflicting/version", override = true } | ||
|
||
[addresses] | ||
task5 = "0x0" | ||
faucet_coin="0xb3743a94cbc755521c27fb5d91e2c8b34ea5b0df899654c20d2f965d301bb7dd" | ||
mycoin="0xc1c3bc0379fb47eb69add8e7ea01acd42eaa65f6bb7ac5dcf2b5faa235dbd126" | ||
# 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" | ||
|
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,88 @@ | ||
module task5::task5 { | ||
use faucet_coin::faucet_coin::{FAUCET_COIN}; | ||
use mycoin::mycoin::{MYCOIN}; | ||
use sui::balance::{Self,Balance, zero}; | ||
use sui::object::{Self, UID}; | ||
use sui::transfer::{ transfer, share_object, public_transfer }; | ||
use sui::tx_context::{Self, TxContext}; | ||
use sui::coin::{Self, Coin}; | ||
use sui::event; | ||
use std::string::{Self, String}; | ||
|
||
|
||
public struct LiquidityEvent has copy, drop { | ||
provider: address, | ||
coin_a_amount: u64, | ||
coin_b_amount: u64, | ||
coin_a_type: String, | ||
coin_b_type: String, | ||
timestamp: u64, | ||
} | ||
|
||
|
||
// 流动性池结构体 | ||
public struct Pool has key { | ||
id: UID, | ||
// 代币A的余额 | ||
coin_a: Balance<FAUCET_COIN>, | ||
// 代币B的余额 | ||
coin_b: Balance<MYCOIN>, | ||
} | ||
fun init(ctx: &mut TxContext) { | ||
let pool = Pool { | ||
id: object::new(ctx), | ||
coin_a: balance::zero(), | ||
coin_b: balance::zero(), | ||
}; | ||
transfer::share_object(pool); | ||
} | ||
public entry fun add_liquidity( pool: &mut Pool, coin_a: Coin<FAUCET_COIN>,coin_b:Coin<MYCOIN>,ctx: &mut TxContext){ | ||
let a_amount = coin::value(&coin_a); | ||
let b_amount = coin::value(&coin_b); | ||
balance::join(&mut pool.coin_a, coin::into_balance(coin_a)); | ||
balance::join(&mut pool.coin_b, coin::into_balance(coin_b)); | ||
|
||
event::emit(LiquidityEvent | ||
{ | ||
provider:tx_context::sender(ctx), | ||
coin_a_amount: a_amount, | ||
coin_b_amount: b_amount, | ||
coin_a_type: string::utf8(b"SY711_FAUCET"), | ||
coin_b_type: string::utf8(b"SY711Coin"), | ||
timestamp: tx_context::epoch(ctx), | ||
}) | ||
} | ||
// 代币A换代币B的函数 | ||
public entry fun swap_a_to_b( | ||
pool: &mut Pool, | ||
coin_a_in: Coin<FAUCET_COIN>, | ||
ctx: &mut TxContext | ||
) { | ||
let amount = coin::value(&coin_a_in); | ||
balance::join(&mut pool.coin_a, coin::into_balance(coin_a_in)); | ||
|
||
let amount_end = amount * 2; | ||
let coin_b_balance = balance::split(&mut pool.coin_b, amount_end); | ||
let movebcoin = coin::from_balance(coin_b_balance, ctx); | ||
public_transfer(movebcoin, ctx.sender()); | ||
} | ||
|
||
// 代币B换代币A的函数 | ||
public entry fun swap_b_to_a( | ||
pool: &mut Pool, | ||
coin_b_in: Coin<MYCOIN>, | ||
ctx: &mut TxContext | ||
) { | ||
let amount = coin::value(&coin_b_in); | ||
balance::join(&mut pool.coin_b, coin::into_balance(coin_b_in)); | ||
|
||
let amount_end = amount * 2; | ||
let coin_a_balance = balance::split(&mut pool.coin_a, amount_end); | ||
let moveacoin = coin::from_balance(coin_a_balance, ctx); | ||
public_transfer(moveacoin, ctx.sender()); | ||
|
||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.