-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:gracecampo/letsmove
- Loading branch information
Showing
136 changed files
with
4,312 additions
and
49 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,52 @@ | ||
# @generated by Move, please check-in and do not edit manually. | ||
|
||
[move] | ||
version = 3 | ||
manifest_digest = "1049038A46FD51A3B4DFB943C2E8C4D463DEFCDC8A02216DD081D0491C946185" | ||
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://gitee.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates\\sui-framework\\packages\\move-stdlib" } | ||
|
||
[[move.package]] | ||
id = "Sui" | ||
source = { git = "https://gitee.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 = "..\\faucet_coin" } | ||
|
||
dependencies = [ | ||
{ id = "Sui", name = "Sui" }, | ||
] | ||
|
||
[[move.package]] | ||
id = "my_coin" | ||
source = { local = "..\\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 = "0xea75b8e26aefbace54c8c434070c1c655c0341f5dbb1aed28b229761393bb958" | ||
latest-published-id = "0xea75b8e26aefbace54c8c434070c1c655c0341f5dbb1aed28b229761393bb958" | ||
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 = "LeonDev1024" | ||
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://gitee.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" } | ||
faucet_coin = { local = "../faucet_coin" } | ||
my_coin = { local = "../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] | ||
leondev1024 = "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" | ||
|
153 changes: 153 additions & 0 deletions
153
mover/LeonDev1024/code/task5/LeonDev1024/sources/leondev1024.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,153 @@ | ||
/* | ||
/// Module: leondev1024 | ||
module leondev1024::leondev1024; | ||
swap 必须是 swap 自己发行的 task2 两个 Coin的互换,包名必须是自己的github id | ||
task2铸造的my_coin和faucet_coin的swap, | ||
*/ | ||
module leondev1024::leondev1024 { | ||
// 导入必要的模块 | ||
use sui::object::{Self, UID}; | ||
use sui::transfer; | ||
use sui::tx_context::{Self, TxContext}; | ||
use sui::coin::{Self, Coin}; | ||
use sui::balance::{Self, Balance}; | ||
use sui::event; | ||
use std::string::{Self, String}; | ||
|
||
// 导入自定义的代币模块 | ||
use my_coin::leon_dev_1024_coin::LEON_DEV_1024_COIN; | ||
use faucet_coin::leon_dev_1024_faucet_coin::LEON_DEV_1024_FAUCET_COIN; | ||
|
||
// 定义错误码, 流动性不足,金额为零 | ||
const E_INSUFFICIENT_LIQUIDITY: u64 = 0; | ||
const E_ZERO_AMOUNT: u64 = 1; | ||
|
||
// 流动性池结构体 | ||
public struct Pool has key { | ||
id: UID, | ||
// 代币A的余额 | ||
coin_a: Balance<LEON_DEV_1024_COIN>, | ||
// 代币B的余额 | ||
coin_b: Balance<LEON_DEV_1024_FAUCET_COIN>, | ||
} | ||
|
||
// 添加流动性事件结构体 | ||
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 SwapEvent has copy, drop { | ||
sender: address, | ||
coin_in_amount: u64, | ||
coin_out_amount: u64, | ||
coin_in_type: String, | ||
coin_out_type: String, | ||
timestamp: u64, | ||
} | ||
|
||
// 初始化函数,创建一个新的流动性池 | ||
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<LEON_DEV_1024_COIN>, | ||
coin_b: Coin<LEON_DEV_1024_FAUCET_COIN>, | ||
ctx: &mut TxContext | ||
) { | ||
let a_amount = coin::value(&coin_a); | ||
let b_amount = coin::value(&coin_b); | ||
|
||
// 确保提供的代币数量大于零 | ||
assert!(a_amount > 0 && b_amount > 0, E_ZERO_AMOUNT); | ||
|
||
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"LEON_DEV_1024_COIN"), | ||
coin_b_type: string::utf8(b"LEON_DEV_1024_FAUCET_COIN"), | ||
timestamp: tx_context::epoch(ctx), | ||
}) | ||
} | ||
|
||
// 代币A换代币B的函数 | ||
public entry fun swap_a_to_b(pool: &mut Pool, coin_a_in: Coin<LEON_DEV_1024_COIN>, ctx: &mut TxContext) { | ||
let a_amount = coin::value(&coin_a_in); | ||
assert!(a_amount > 0, E_ZERO_AMOUNT); | ||
|
||
let b_reserve = balance::value(&pool.coin_b); | ||
assert!(b_reserve > 0, E_INSUFFICIENT_LIQUIDITY); | ||
|
||
let a_reserve = balance::value(&pool.coin_a); | ||
let b_out = (a_amount * b_reserve) / (a_reserve + a_amount); | ||
|
||
assert!(b_out > 0 && b_out <= b_reserve, E_INSUFFICIENT_LIQUIDITY); | ||
|
||
// change | ||
balance::join(&mut pool.coin_a, coin::into_balance(coin_a_in)); | ||
let coin_b_out = coin::take(&mut pool.coin_b, b_out, ctx); | ||
transfer::public_transfer(coin_b_out, tx_context::sender(ctx)); | ||
|
||
event::emit(SwapEvent { | ||
sender: tx_context::sender(ctx), | ||
coin_in_amount: a_amount, | ||
coin_out_amount: b_out, | ||
coin_in_type: string::utf8(b"LEON_DEV_1024_COIN"), | ||
coin_out_type: string::utf8(b"LEON_DEV_1024_FAUCET_COIN"), | ||
timestamp: tx_context::epoch(ctx), | ||
}); | ||
} | ||
|
||
// 代币B LEON_DEV_1024_FAUCET_COIN 换代币A LEON_DEV_1024_COIN的函数 | ||
public entry fun swap_b_to_a(pool: &mut Pool, coin_b_in: Coin<LEON_DEV_1024_FAUCET_COIN>, | ||
ctx: &mut TxContext) { | ||
let b_amount = coin::value(&coin_b_in); | ||
// 确保输入的代币数量大于零 | ||
assert!(b_amount > 0, E_ZERO_AMOUNT); | ||
|
||
let a_reserve = balance::value(&pool.coin_a); | ||
// 确保可以换取的代币A数量有效 | ||
assert!(a_reserve > 0, E_INSUFFICIENT_LIQUIDITY); | ||
|
||
let b_reserve = balance::value(&pool.coin_b); | ||
let a_out = (b_amount * a_reserve) / (b_reserve + b_amount); | ||
|
||
assert!(a_out > 0 && a_out <= a_reserve, E_INSUFFICIENT_LIQUIDITY); | ||
|
||
// 更新流动性池的余额 | ||
balance::join(&mut pool.coin_b, coin::into_balance(coin_b_in)); | ||
let coin_a_out = coin::take(&mut pool.coin_a, a_out, ctx); | ||
transfer::public_transfer(coin_a_out, tx_context::sender(ctx)); | ||
|
||
event::emit(SwapEvent { | ||
sender: tx_context::sender(ctx), | ||
coin_in_amount: b_amount, | ||
coin_out_amount: a_out, | ||
coin_in_type: string::utf8(b"LEON_DEV_1024_FAUCET_COIN"), | ||
coin_out_type: string::utf8(b"LEON_DEV_1024_COIN"), | ||
timestamp: tx_context::epoch(ctx), | ||
}); | ||
} | ||
} | ||
|
||
|
||
|
18 changes: 18 additions & 0 deletions
18
mover/LeonDev1024/code/task5/LeonDev1024/tests/leondev1024_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 leondev1024::leondev1024_tests; | ||
// uncomment this line to import the module | ||
// use leondev1024::leondev1024; | ||
const ENotImplemented: u64 = 0; | ||
#[test] | ||
fun test_leondev1024() { | ||
// pass | ||
} | ||
#[test, expected_failure(abort_code = ::leondev1024::leondev1024_tests::ENotImplemented)] | ||
fun test_leondev1024_fail() { | ||
abort ENotImplemented | ||
} | ||
*/ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.