Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完成task5与第三周学习和答疑 #2051

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
8 changes: 4 additions & 4 deletions mover/AlexWaker/co-learn-2411/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

- [✅] 第一周:![学习记录截图](./images/week1learn.png)
- [✅] 第二周:![学习记录截图](./images/week2learn.jpg)
- [] 第三周:![学习记录截图](./images/你的图片地址)
- [] 第四周:![学习记录截图](./images/你的图片地址)
- [] 第三周:![学习记录截图](./images/week3learn.png)
- [] 第四周:![学习记录截图](./images/week4learn.jpg)

## 参加直播答疑

- [✅] 第一周:![学习记录截图](./images/week1answer.jpeg)
- [✅] 第二周:![学习记录截图](./images/week2answer.png)
- [] 第三周:![学习记录截图](./images/你的图片地址)
- [] 第四周:![学习记录截图](./images/你的图片地址)
- [] 第三周:![学习记录截图](./images/week3answer.jpeg)
- [] 第四周:![学习记录截图](./images/week4answer.png)

## 群里分享学习笔记

Expand Down
108 changes: 108 additions & 0 deletions mover/AlexWaker/code/task5/task5_swap.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
module task5_swap::simple_swap {
// use sui::object::{Self, UID};
// use sui::balance;
// use sui::transfer::{share_object, public_transfer};
// use sui::balance::{Balance, zero, join, split};
// use sui::coin::{Coin, into_balance, from_balance};
// use sui::tx_context::TxContext;
use sui::object::{Self, UID};
use sui::transfer::{share_object, public_transfer};
use sui::tx_context::{Self, TxContext};
use sui::coin::{Self, Coin, into_balance, from_balance};
use sui::balance::{Self, Balance, zero, join, split};
use sui::event;
use std::string::{Self, String};
use my_coin::alexwaker_coin::{ALEXWAKER_COIN};
use my_faucet_coin::alexwaker_faucet_coin::{ALEXWAKER_FAUCET_COIN};

/// 定义一个简单的池(Pool),存储两个代币的余额
public struct Pool has key {
id: UID,
balance_a: Balance<ALEXWAKER_COIN>,
balance_b: Balance<ALEXWAKER_FAUCET_COIN>,
}

/// 初始化池
fun init( //初始化方法只能这么写
// initial_a: Balance<ALEXWAKER_COIN>,
// initial_b: Balance<ALEXWAKER_FAUCET_COIN>,
ctx: &mut TxContext
) {
let pool = Pool {
id: object::new(ctx),
balance_a: zero(),
balance_b: zero(),
};
share_object(pool);
}

public fun addLiquidityAC(
pool: &mut Pool,
coin_a: Coin<ALEXWAKER_COIN>,
//coin_b: Coin<ALEXWAKER_FAUCET_COIN>,
ctx: &mut TxContext
) {
let value_a = into_balance(coin_a); //
//let value_b = into_balance(coin_b);
join(&mut pool.balance_a, value_a);
//join(&mut pool.balance_b, value_b);

}
public fun addLiquidityAFC(
pool: &mut Pool,
//coin_a: Coin<ALEXWAKER_COIN>,
coin_b: Coin<ALEXWAKER_FAUCET_COIN>,
ctx: &mut TxContext
) {
//let value_a = into_balance(coin_a); //
let value_b = into_balance(coin_b);
join(&mut pool.balance_b, value_b);
//join(&mut pool.balance_a, value_a);

}

/// 代币 A 换代币 B (1:1)
public fun swap_a_for_b(
pool: &mut Pool,
input_a: Coin<ALEXWAKER_COIN>,
ctx: &mut TxContext
) {
let value = input_a.value(); // 获取代币 A 的数量
let output_b = split(&mut pool.balance_b, value); // 从池中提取等量的 B
join(&mut pool.balance_a, into_balance(input_a)); // 将代币 A 加入池中
let coin_b = from_balance(output_b, ctx);
public_transfer(coin_b, ctx.sender());
//output_b // 返回用户获得的代币 B
}

/// 代币 B 换代币 A (1:1)
public fun swap_b_for_a(
pool: &mut Pool,
input_b: Coin<ALEXWAKER_FAUCET_COIN>,
ctx: &mut TxContext
) {
let value = input_b.value(); // 获取代币 B 的数量
let output_a = split(&mut pool.balance_a, value); // 从池中提取等量的 A
join(&mut pool.balance_b, into_balance(input_b)); // 将代币 B 加入池中
let coin_a = from_balance(output_a, ctx);
public_transfer(coin_a, ctx.sender());
//output_a // 返回用户获得的代币 A
}
}

//testnet
//packageid: 0x921a8fdf8796fa4274bfff3f74cc651f26e82fe456041d19378ef8e513cd44c3
//pool object: 0x1ddd04a1b6a231c164c8720332bfa9a241f88c16f9dbbf6babcd4a379957419d
//add my_coin coin id:0xeea3c46210acf139fecd7ff19d90861917f5c2d6b814c48fb24f1d5ad6b82935
//my_faucet_coin coin id: 0xe8e8ec8df6023003ef45d0f6b369b6520847d830d36e22cb93dec485904dbdb9
//duihuan my_coin id: 0xe160e9c0dbd20f403277b2fb8cd98ca1cfe6671cfc381551e17ac67f6744f326
//duihuan my_faucet_coin id: 0xcbfdfedb2ba4a0f4f6177e32781ed5292fb33c696e43af67dd242b1425b0221a
//测试网跑通了,记得coinid是领取之后coin的objectid,不是treasury的

//mainnet
//packageid: 0x7088ece81ebbbe992bda5097f7f07fec86c7ddfd2759dc2218121ebadf09c8e6
//pool: 0xf2fea0cdb54ea274b270aeb9c1c842617627ad8b944574abf3faaaab2cda0f2a
//add my_coin coin id: 0xa6c9687ecd917ceab6f903aaaa6e1e817f27954d5691b4326e74455e0d46b06d
//duihuan my_coin id: 0x6381ff9d8c5b7dbf062d25aebb78d951c2e4426b902f8359cb0471e5312f5920
//add my_faucet_coin id: 0xa2c7ee767d942ea58954111a78d154fb1e16d01e46e60312b9583fc1d9be3842
//duihuan my_faucet_coin id: 0x316b5ab339dc3981467f42f00e08f126095228d00b849a8a97e233f6bdee6bb8
101 changes: 101 additions & 0 deletions mover/AlexWaker/code/task6/naviPTB.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Button, Container, Flex, Text, Box, Heading } from "@radix-ui/themes"; // 导入 Radix UI 组件
import { useCurrentAccount, useSignAndExecuteTransaction } from "@mysten/dapp-kit"; // 导入 Mysten DApp Kit 钩子
import { Transaction } from "@mysten/sui/transactions"; // 导入 Sui 交易类
import { Pool, PoolConfig } from "navi-sdk/dist/types"; // 导入 Navi SDK 中的池和池配置类型
import { pool, Sui, wUSDC } from "navi-sdk/dist/address"; // 导入 Navi SDK 中的池地址和代币信息
import { borrowCoin, depositCoin } from "navi-sdk/dist/libs/PTB"; // 导入 Navi SDK 中的借币和存币函数
import { useState } from "react";

// 计算借款金额
const calculateBorrowAmount = (month: number, day: number, hour: number): number => {
const borrowAmountStr = `0.${month.toString().padStart(2, "0")}${day.toString().padStart(2, "0")}${hour.toString().padStart(2, "0")}`;
return parseFloat(borrowAmountStr) * 10 ** wUSDC.decimal;
};

// 处理存款和借款的异步函数
const depositAndBorrowSui = async (account: any, signAndExecuteTransaction: any, setMessage: any, setDigest: any) => {
if (!account) {
setMessage("Please connect your wallet first");
return;
}

try {
const date = new Date(); // 获取当前日期
const borrowAmount = calculateBorrowAmount(date.getMonth() + 1, date.getDate(), date.getHours());

// 创建新的交易对象
const tx = new Transaction();
tx.setSender(account.address);

// 获取池配置
const suiPool: PoolConfig = pool[Sui.symbol as keyof Pool];
const wusdcPool: PoolConfig = pool[wUSDC.symbol as keyof Pool];

if (!suiPool || !wusdcPool) {
throw new Error("Invalid pool configuration");
}

// 存款 SUI
const [suiCoin] = tx.splitCoins(tx.gas, [1_000_000_000]); // 分割 SUI 代币
if (!suiCoin) throw new Error("Failed to split SUI coins");

await depositCoin(tx, suiPool, suiCoin, 1_000_000_000); // 存款 SUI

// 借款 USDC
const [toBorrowCoin] = await borrowCoin(tx, wusdcPool, borrowAmount);
if (!toBorrowCoin) throw new Error("Failed to borrow USDC");

// 存款借出的 USDC
await depositCoin(tx, wusdcPool, toBorrowCoin, borrowAmount);

// 清除之前的消息
setMessage("");
setDigest("");

// 签名并执行交易
signAndExecuteTransaction(
{ transaction: tx, chain: "sui:mainnet" },
{
// onSuccess: (result: any) => { // 移除或注释掉
// console.log("Transaction successful:", result);
// setDigest(result.digest);
// },
onError: (error: any) => { // 为 error 参数指定类型
console.error("Transaction failed:", error);
setMessage(error.message || "Transaction failed");
},
}
);
} catch (error) {
console.error("Error in depositAndBorrowSui:", error);
setMessage(error instanceof Error ? error.message : "An unknown error occurred");
}
};

export function NaviPTB() {
const account = useCurrentAccount();
const { mutate: signAndExecuteTransaction } = useSignAndExecuteTransaction();
const [digest, setDigest] = useState<string>("");
const [message, setMessage] = useState<string>("");

return (
<Container>
<Box>
<Heading as="h2">Navi Protocol 任务</Heading>
<Text>
任务将会把 1 SUI 存入 Navi 协议,根据当前日期借出相应数量的 USDC,然后存入等额的 USDC。
</Text>
<Flex gap="3" direction={"column"}>
<Button onClick={() => depositAndBorrowSui(account, signAndExecuteTransaction, setMessage, setDigest)} variant="solid">
开始交易
</Button>
{digest && <Text>Transaction submitted: {digest}</Text>} {/* 显示交易摘要 */}
{message && <Text>Error: {message}</Text>} {/* 显示错误消息 */}
</Flex>
</Box>
</Container>
);
}

export default NaviPTB;

8 changes: 4 additions & 4 deletions mover/AlexWaker/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
- [✅] play game hash: `Efd8wLmV3QNkF74Cao7jtDu1UXk8r9vTwgzSt6hnKhxn`

## 05 Move Swap
- [] swap package id :
- [] call swap CoinA-> CoinB hash :
- [] call swap CoinB-> CoinA hash :
- [] swap package id : `0x7088ece81ebbbe992bda5097f7f07fec86c7ddfd2759dc2218121ebadf09c8e6`
- [] call swap CoinA-> CoinB hash : `2Q8CNauAxwqP4Gb54B1ohvXfWiXecUMTNiHqY1jAQDaN`
- [] call swap CoinB-> CoinA hash : `G4bsraFcW7qvdr4Sg1vX5kFWNCEd1tzSeUHWo19T717C`

## 06 Dapp-kit SDK PTB
- [] save hash :
- [] save hash : `HaN5ivgXWD34FxxAjE4yPg58XaCDYwLaC28erCAJ1WSH`

## 07 Move CTF Check In
- [] CLI call 截图 : ![截图](./images/你的图片地址)
Expand Down
Loading