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

feat: adding all structs for easier bcs encoding-decoding. #55

Merged
merged 19 commits into from
Jul 9, 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
2 changes: 1 addition & 1 deletion move/axelar_gateway/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[move]
version = 2
manifest_digest = "B3F1114AF6420DAFC5501B8A333236271B0467A5D00AF701F02D79AEC565E6E5"
manifest_digest = "1EB54C26C3FA638760DAA54E631C0B13A09FFC472C1CAA730C9A7879E463B455"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ name = "Sui" },
Expand Down
3 changes: 1 addition & 2 deletions move/axelar_gateway/Move.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[package]
name = "AxelarGateway"
version = "0.1.0"
published-at = "0x571841ce66dce70e51a123299ed140d6509566c97eb905331b5f28732c707c56"
edition = "2024.beta"

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.25.3" }

[addresses]
axelar_gateway = "0x100"
axelar_gateway = "0xcd391ade5ab55a218f8988df8f3aa38ec019651c217244f7f689616c87d8c289"
clock = "0x6"
62 changes: 41 additions & 21 deletions move/squid/sources/squid/deepbook_v2.move
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module squid::deepbook_v2 {
use std::type_name;
use std::ascii;
use std::ascii::{Self, String};

use sui::coin::{Self, Coin};
use sui::clock::Clock;
Expand All @@ -25,7 +25,32 @@ module squid::deepbook_v2 {
const EWrongCoinType: u64 = 2;
const ENotEnoughOutput: u64 = 3;

public fun swap_base<T1, T2>(pool: &mut Pool<T1, T2>, coin: Coin<T1>, clock: &Clock, ctx: &mut TxContext): (Coin<T1>, Coin<T2>) {
public struct DeepbookV2SwapData has drop{
swap_type: u8,
pool_id: address,
has_base: bool,
min_output: u64,
base_type: String,
quote_type: String,
lot_size: u64,
should_sweep: bool,
}

fun new(data: vector<u8>): DeepbookV2SwapData {
Foivos marked this conversation as resolved.
Show resolved Hide resolved
let mut bcs = bcs::new(data);
DeepbookV2SwapData {
swap_type: bcs.peel_u8(),
pool_id: bcs.peel_address(),
has_base: bcs.peel_bool(),
min_output: bcs.peel_u64(),
base_type: ascii::string(bcs.peel_vec_u8()),
quote_type: ascii::string(bcs.peel_vec_u8()),
lot_size: bcs.peel_u64(),
should_sweep: bcs.peel_bool(),
}
}

fun swap_base<T1, T2>(pool: &mut Pool<T1, T2>, coin: Coin<T1>, clock: &Clock, ctx: &mut TxContext): (Coin<T1>, Coin<T2>) {
let account = clob::create_account(ctx);
let (base_coin, quote_coin, _) = pool.swap_exact_base_for_quote(
0,
Expand All @@ -40,7 +65,7 @@ module squid::deepbook_v2 {
(base_coin, quote_coin)
}

public fun swap_quote<T1, T2>(pool: &mut Pool<T1, T2>, coin: Coin<T2>, clock: &Clock, ctx: &mut TxContext): (Coin<T1>, Coin<T2>) {
fun swap_quote<T1, T2>(pool: &mut Pool<T1, T2>, coin: Coin<T2>, clock: &Clock, ctx: &mut TxContext): (Coin<T1>, Coin<T2>) {
let account = clob::create_account(ctx);
let (base_coin, quote_coin, _) = pool.swap_exact_quote_for_base(
0,
Expand Down Expand Up @@ -149,7 +174,7 @@ module squid::deepbook_v2 {
(filled_quote_quantity, filled_base_quantity)
}

public fun predict_base_for_quote<T1, T2>(pool: &Pool<T1, T2>, amount: u64, lot_size: u64, clock: &Clock): (u64, u64) {
fun predict_base_for_quote<T1, T2>(pool: &Pool<T1, T2>, amount: u64, lot_size: u64, clock: &Clock): (u64, u64) {
let max_price = (1u128 << 64 - 1 as u64);
let (prices, depths) = clob::get_level2_book_status_bid_side(pool, 0, max_price, clock);
let mut amount_left = amount;
Expand All @@ -171,7 +196,7 @@ module squid::deepbook_v2 {

}

public fun predict_quote_for_base<T1, T2>(pool: &Pool<T1, T2>, amount: u64, lot_size: u64, clock: &Clock): (u64, u64) {
fun predict_quote_for_base<T1, T2>(pool: &Pool<T1, T2>, amount: u64, lot_size: u64, clock: &Clock): (u64, u64) {
let max_price = (1u128 << 64 - 1 as u64);
let (prices, depths) = clob::get_level2_book_status_ask_side(pool, 0, max_price, clock);

Expand Down Expand Up @@ -257,32 +282,27 @@ module squid::deepbook_v2 {
public fun swap<T1, T2>(self: &mut SwapInfo, pool: &mut Pool<T1, T2>, squid: &mut Squid, clock: &Clock, ctx: &mut TxContext) {
let data = self.get_data_swapping();
if(data.length() == 0) return;
let mut bcs = bcs::new(data);
let swap_data = new(data);

assert!(bcs.peel_u8() == SWAP_TYPE, EWrongSwapType);
assert!(swap_data.swap_type == SWAP_TYPE, EWrongSwapType);

assert!(bcs.peel_address() == object::id_address(pool), EWrongPool);

let has_base = bcs.peel_bool();
let min_output = bcs.peel_u64();
assert!(swap_data.pool_id == object::id_address(pool), EWrongPool);

assert!(
&bcs.peel_vec_u8() == &type_name::get<T1>().into_string().into_bytes(),
&swap_data.base_type == &type_name::get<T1>().into_string(),
EWrongCoinType,
);

assert!(
&bcs.peel_vec_u8() == &type_name::get<T2>().into_string().into_bytes(),
&swap_data.quote_type == &type_name::get<T2>().into_string(),
EWrongCoinType,
);

let lot_size = bcs.peel_u64();
let should_sweep = bcs.peel_bool();
if(has_base) {
if(swap_data.has_base) {
let mut base_balance = self.coin_bag().get_balance<T1>().destroy_some();
let leftover = base_balance.value() % lot_size;
let leftover = base_balance.value() % swap_data.lot_size;
if(leftover > 0) {
if(should_sweep) {
if(swap_data.should_sweep) {
squid.coin_bag().store_balance<T1>(
base_balance.split(leftover)
);
Expand All @@ -298,7 +318,7 @@ module squid::deepbook_v2 {
clock,
ctx,
);
assert!(min_output <= quote_coin.value(), ENotEnoughOutput);
assert!(swap_data.min_output <= quote_coin.value(), ENotEnoughOutput);
base_coin.destroy_zero();
self.coin_bag().store_balance<T2>(quote_coin.into_balance());
} else {
Expand All @@ -309,9 +329,9 @@ module squid::deepbook_v2 {
clock,
ctx,
);
assert!(min_output <= base_coin.value(), ENotEnoughOutput);
assert!(swap_data.min_output <= base_coin.value(), ENotEnoughOutput);
self.coin_bag().store_balance<T1>(base_coin.into_balance());
if(should_sweep) {
if(swap_data.should_sweep) {
squid.coin_bag().store_balance<T2>(quote_coin.into_balance());
} else {
self.coin_bag().store_balance<T2>(quote_coin.into_balance());
Expand Down
81 changes: 57 additions & 24 deletions move/squid/sources/squid/transfers.move
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module squid::transfers {
use std::type_name;
use std::ascii;
use std::ascii::{Self, String};

use sui::bcs::{Self, BCS};
use sui::coin;
Expand All @@ -9,7 +9,7 @@ module squid::transfers {

use its::service;
use its::its::ITS;
use its::token_id;
use its::token_id::{Self, TokenId};

use squid::swap_info::{SwapInfo};

Expand All @@ -19,15 +19,51 @@ module squid::transfers {
const EWrongSwapType: u64 = 0;
const EWrongCoinType: u64 = 1;

public struct SuiTransferSwapData has drop {
swap_type: u8,
coin_type: String,
recipient: address,
}

public struct ItsTransferSwapData has drop {
Foivos marked this conversation as resolved.
Show resolved Hide resolved
swap_type: u8,
coin_type: String,
token_id: TokenId,
destination_chain: String,
destination_address: vector<u8>,
metadata: vector<u8>,
}

fun new_sui_transfer_swap_data(data: vector<u8>): SuiTransferSwapData {
let mut bcs = bcs::new(data);
SuiTransferSwapData {
swap_type: bcs.peel_u8(),
coin_type: ascii::string(bcs.peel_vec_u8()),
recipient: bcs.peel_address(),
}
}

fun new_its_transfer_swap_data(data: vector<u8>): ItsTransferSwapData {
let mut bcs = bcs::new(data);
ItsTransferSwapData {
swap_type: bcs.peel_u8(),
coin_type: ascii::string(bcs.peel_vec_u8()),
token_id: token_id::from_address(bcs.peel_address()),
destination_chain: ascii::string(bcs.peel_vec_u8()),
destination_address: bcs.peel_vec_u8(),
metadata: bcs.peel_vec_u8(),
}
}

public fun sui_estimate<T>(swap_info: &mut SwapInfo) {
let data = swap_info.get_data_estimating();
if (data.length() == 0) return;
let mut bcs = bcs::new(data);
let swap_data = new_sui_transfer_swap_data(data);

assert!(bcs.peel_u8() == SWAP_TYPE_SUI_TRANSFER, EWrongSwapType);
assert!(swap_data.swap_type == SWAP_TYPE_SUI_TRANSFER, EWrongSwapType);

assert!(
&bcs.peel_vec_u8() == &type_name::get<T>().into_string().into_bytes(),
&swap_data.coin_type == &type_name::get<T>().into_string(),
EWrongCoinType,
);

Expand All @@ -37,12 +73,12 @@ module squid::transfers {
public fun its_estimate<T>(swap_info: &mut SwapInfo) {
let data = swap_info.get_data_estimating();
if (data.length() == 0) return;
let mut bcs = bcs::new(data);
let swap_data = new_its_transfer_swap_data(data);

assert!(bcs.peel_u8() == SWAP_TYPE_ITS_TRANSFER, EWrongSwapType);
assert!(swap_data.swap_type == SWAP_TYPE_ITS_TRANSFER, EWrongSwapType);

assert!(
&bcs.peel_vec_u8() == &type_name::get<T>().into_string().into_bytes(),
&swap_data.coin_type == &type_name::get<T>().into_string(),
EWrongCoinType,
);

Expand All @@ -52,12 +88,12 @@ module squid::transfers {
public fun sui_transfer<T>(swap_info: &mut SwapInfo, ctx: &mut TxContext) {
let data = swap_info.get_data_swapping();
if (data.length() == 0) return;
let mut bcs = bcs::new(data);
let swap_data = new_sui_transfer_swap_data(data);

assert!(bcs.peel_u8() == SWAP_TYPE_SUI_TRANSFER, EWrongSwapType);
assert!(swap_data.swap_type == SWAP_TYPE_SUI_TRANSFER, EWrongSwapType);

assert!(
&bcs.peel_vec_u8() == &type_name::get<T>().into_string().into_bytes(),
&swap_data.coin_type == &type_name::get<T>().into_string(),
EWrongCoinType,
);

Expand All @@ -66,19 +102,19 @@ module squid::transfers {
option.destroy_none();
return
};
let address = bcs.peel_address();
transfer::public_transfer(coin::from_balance(option.destroy_some(), ctx), address);

transfer::public_transfer(coin::from_balance(option.destroy_some(), ctx), swap_data.recipient);
}

public fun its_transfer<T>(swap_info: &mut SwapInfo, its: &mut ITS, ctx: &mut TxContext) {
let data = swap_info.get_data_swapping();
if (data.length() == 0) return;
let mut bcs = bcs::new(data);
let swap_data = new_its_transfer_swap_data(data);

assert!(bcs.peel_u8() == SWAP_TYPE_SUI_TRANSFER, EWrongSwapType);
assert!(swap_data.swap_type == SWAP_TYPE_SUI_TRANSFER, EWrongSwapType);

assert!(
&bcs.peel_vec_u8() == &type_name::get<T>().into_string().into_bytes(),
&swap_data.coin_type == &type_name::get<T>().into_string(),
EWrongCoinType,
);

Expand All @@ -87,17 +123,14 @@ module squid::transfers {
option.destroy_none();
return
};
let token_id = token_id::from_address(bcs.peel_address());
let destination_chain = ascii::string(bcs.peel_vec_u8());
let destination_address = bcs.peel_vec_u8();
let metadata = bcs.peel_vec_u8();

service::interchain_transfer(
its,
token_id,
swap_data.token_id,
coin::from_balance(option.destroy_some(), ctx),
destination_chain,
destination_address,
metadata,
swap_data.destination_chain,
swap_data.destination_address,
swap_data.metadata,
ctx,
);
}
Expand Down
Loading
Loading