Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feat/limit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos committed Nov 7, 2024
2 parents 919860f + 9f656e2 commit a76f631
Show file tree
Hide file tree
Showing 24 changed files with 1,218 additions and 1,197 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-goats-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@axelar-network/axelar-cgp-sui': minor
---

Added js e2e tests for squid, and fixed a few things with squid as well.
3 changes: 3 additions & 0 deletions .github/workflows/test-js.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ jobs:
run: sleep 30s
shell: bash

- name: Switch to localnet
run: sui client new-env --alias localnet --rpc http://0.0.0.0:9000 && sui client switch --env localnet

- name: Test
run: npm run test-js
25 changes: 25 additions & 0 deletions move/example/sources/squid/token_a.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module example::token_a;

use sui::coin;

// ------------
// Capabilities
// ------------
public struct TOKEN_A has drop {}

// -----
// Setup
// -----
fun init(witness: TOKEN_A, ctx: &mut TxContext) {
let (treasury_cap, coin_metadata) = coin::create_currency(
witness,
9,
b"TOKEN1",
b"Token 1",
b"",
option::none(),
ctx,
);
transfer::public_transfer(treasury_cap, tx_context::sender(ctx));
transfer::public_transfer(coin_metadata, tx_context::sender(ctx));
}
25 changes: 25 additions & 0 deletions move/example/sources/squid/token_b.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module example::token_b;

use sui::coin;

// ------------
// Capabilities
// ------------
public struct TOKEN_B has drop {}

// -----
// Setup
// -----
fun init(witness: TOKEN_B, ctx: &mut TxContext) {
let (treasury_cap, coin_metadata) = coin::create_currency(
witness,
9,
b"TOKEN2",
b"Token 2",
b"",
option::none(),
ctx,
);
transfer::public_transfer(treasury_cap, tx_context::sender(ctx));
transfer::public_transfer(coin_metadata, tx_context::sender(ctx));
}
25 changes: 25 additions & 0 deletions move/example/sources/squid/token_c.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module example::token_c;

use sui::coin;

// ------------
// Capabilities
// ------------
public struct TOKEN_C has drop {}

// -----
// Setup
// -----
fun init(witness: TOKEN_C, ctx: &mut TxContext) {
let (treasury_cap, coin_metadata) = coin::create_currency(
witness,
9,
b"TOKEN3",
b"Token 3",
b"",
option::none(),
ctx,
);
transfer::public_transfer(treasury_cap, tx_context::sender(ctx));
transfer::public_transfer(coin_metadata, tx_context::sender(ctx));
}
6 changes: 4 additions & 2 deletions move/squid/sources/squid/coin_bag.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use sui::bag::{Self, Bag};
use sui::balance::Balance;
use sui::hash::keccak256;

const EKeyNotExist: u64 = 0;
const ENotEnoughBalance: u64 = 1;
#[error]
const EKeyNotExist: vector<u8> = b"key not found";
#[error]
const ENotEnoughBalance: vector<u8> = b"not enough balance";

public struct CoinBag has store {
bag: Bag,
Expand Down
34 changes: 16 additions & 18 deletions move/squid/sources/squid/deepbook_v3.move
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
module squid::deepbook_v3;

use std::ascii::{Self, String};
use std::type_name;

use sui::bcs::{Self, BCS};
use sui::clock::Clock;

use deepbook::pool::Pool;

use relayer_discovery::transaction::{Self, MoveCall};

use squid::squid::Squid;
use squid::swap_info::SwapInfo;
use squid::swap_type::{Self, SwapType};

use std::ascii::{Self, String};
use std::type_name;
use sui::bcs::BCS;
use sui::clock::Clock;
use token::deep::DEEP;
use utils::utils::peel;


const EWrongSwapType: u64 = 0;
const EWrongPool: u64 = 1;
const EWrongCoinType: u64 = 2;
#[error]
const EWrongSwapType: vector<u8> = b"wrong swap type";
#[error]
const EWrongPool: vector<u8> = b"pool argument does not match required pool";
#[error]
const EWrongCoinType: vector<u8> = b"coin type expected does not match type argument";

const FLOAT_SCALING: u128 = 1_000_000_000;

Expand All @@ -44,10 +42,11 @@ public fun estimate<B, Q>(
) {
let (data, fallback) = self.data_estimating();
if (fallback) return;
let swap_data = peel_swap_data(data);
let swap_data = peel!(data, |data| peel_swap_data(data));

assert!(swap_data.swap_type == swap_type::deepbook_v3(), EWrongSwapType);
assert!(swap_data.pool_id == object::id_address(pool), EWrongPool);

assert!(
&swap_data.base_type == &type_name::get<B>().into_string(),
EWrongCoinType,
Expand Down Expand Up @@ -112,7 +111,7 @@ public fun swap<B, Q>(
) {
let (data, fallback) = self.data_swapping();
if (fallback) return;
let swap_data = peel_swap_data(data);
let swap_data = peel!(data, |data| peel_swap_data(data));

assert!(swap_data.swap_type == swap_type::deepbook_v3(), EWrongSwapType);
assert!(swap_data.pool_id == object::id_address(pool), EWrongPool);
Expand Down Expand Up @@ -259,10 +258,9 @@ public(package) fun swap_move_call(
)
}

public(package) fun peel_swap_data(data: vector<u8>): DeepbookV3SwapData {
let mut bcs = bcs::new(data);
public(package) fun peel_swap_data(bcs: &mut BCS): DeepbookV3SwapData {
DeepbookV3SwapData {
swap_type: swap_type::peel(&mut bcs),
swap_type: swap_type::peel(bcs),
pool_id: bcs.peel_address(),
has_base: bcs.peel_bool(),
min_output: bcs.peel_u64(),
Expand Down
144 changes: 60 additions & 84 deletions move/squid/sources/squid/discovery.move
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
module squid::discovery;

use std::ascii::{Self, String};

use sui::bcs;

use axelar_gateway::gateway::Gateway;

use its::its::ITS;
use relayer_discovery::discovery::RelayerDiscovery;
use relayer_discovery::transaction::{Self, MoveCall, Transaction};

use its::its::ITS;

use squid::deepbook_v3;
use squid::squid::Squid;
use squid::swap_type::{Self};
use squid::transfers;
use std::ascii::{Self, String};
use sui::bcs;

const EInvalidSwapType: u64 = 0;

const SWAP_TYPE_DEEPBOOK_V3: u8 = 1;
const SWAP_TYPE_SUI_TRANSFER: u8 = 2;
const SWAP_TYPE_ITS_TRANSFER: u8 = 3;
#[error]
const EInvalidSwapType: vector<u8> = b"swap type does not exist";

public fun register_transaction(
squid: &Squid,
Expand All @@ -43,7 +36,7 @@ public fun register_transaction(
transaction::new_function(
transaction::package_id<Squid>(),
ascii::string(b"discovery"),
ascii::string(b"get_transaction"),
ascii::string(b"transaction"),
),
vector[squid_arg, its_arg, gateway_arg, vector[3]],
vector[],
Expand Down Expand Up @@ -84,80 +77,63 @@ public fun transaction(
let mut move_calls = vector[
start_swap(package_id, squid_arg, its_arg, type_in),
];

let mut i = 0;
while (i < swap_data.length()) {
let mut bcs = bcs::new(swap_data[i]);
let swap_type = bcs.peel_u8();

if (swap_type == SWAP_TYPE_DEEPBOOK_V3) {
move_calls.push_back(
deepbook_v3::estimate_move_call(
package_id,
bcs,
swap_info_arg,
),
);
} else if (swap_type == SWAP_TYPE_SUI_TRANSFER) {
move_calls.push_back(
transfers::sui_estimate_move_call(
package_id,
bcs,
swap_info_arg,
),
);
swap_data.do_ref!(|data| {
let mut bcs = bcs::new(*data);
let swap_type = swap_type::peel(&mut bcs);

let call = if (swap_type == swap_type::deepbook_v3()) {
deepbook_v3::estimate_move_call(
package_id,
bcs,
swap_info_arg,
)
} else if (swap_type == swap_type::sui_transfer()) {
transfers::sui_estimate_move_call(
package_id,
bcs,
swap_info_arg,
)
} else {
assert!(swap_type == SWAP_TYPE_ITS_TRANSFER, EInvalidSwapType);
move_calls.push_back(
transfers::its_estimate_move_call(
package_id,
bcs,
swap_info_arg,
),
);
assert!(swap_type == swap_type::its_transfer(), EInvalidSwapType);
transfers::its_estimate_move_call(
package_id,
bcs,
swap_info_arg,
)
};

i = i + 1;
};

i = 0;
while (i < swap_data.length()) {
let mut bcs = bcs::new(swap_data[i]);
let swap_type = bcs.peel_u8();

if (swap_type == SWAP_TYPE_DEEPBOOK_V3) {
move_calls.push_back(
deepbook_v3::swap_move_call(
package_id,
bcs,
swap_info_arg,
squid_arg,
),
);
} else if (swap_type == SWAP_TYPE_SUI_TRANSFER) {
move_calls.push_back(
transfers::sui_transfer_move_call(
package_id,
bcs,
swap_info_arg,
),
);
move_calls.push_back(call);
});

swap_data.do!(|data| {
let mut bcs = bcs::new(data);
let swap_type = swap_type::peel(&mut bcs);

let call = if (swap_type == swap_type::deepbook_v3()) {
deepbook_v3::swap_move_call(
package_id,
bcs,
swap_info_arg,
squid_arg,
)
} else if (swap_type == swap_type::sui_transfer()) {
transfers::sui_transfer_move_call(
package_id,
bcs,
swap_info_arg,
)
} else {
assert!(swap_type == SWAP_TYPE_ITS_TRANSFER, EInvalidSwapType);
move_calls.push_back(
transfers::its_transfer_move_call(
package_id,
bcs,
swap_info_arg,
squid_arg,
gateway_arg,
its_arg,
),
);
assert!(swap_type == swap_type::its_transfer(), EInvalidSwapType);
transfers::its_transfer_move_call(
package_id,
bcs,
swap_info_arg,
squid_arg,
gateway_arg,
its_arg,
)
};

i = i + 1;
};
move_calls.push_back(call);
});

move_calls.push_back(finalize(package_id, swap_info_arg));

Expand Down
10 changes: 10 additions & 0 deletions move/squid/sources/squid/squid.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use squid::squid_v0::{Self, Squid_v0};
use squid::swap_info::SwapInfo;
use std::ascii;
use sui::clock::Clock;
use sui::coin::Coin;
use sui::versioned::{Self, Versioned};
use token::deep::DEEP;
use version_control::version_control::{Self, VersionControl};

// -------
Expand Down Expand Up @@ -58,6 +60,13 @@ public(package) macro fun value_mut(
value
}

// ---------------
// Entry Functions
// ---------------
entry fun give_deep(self: &mut Squid, deep: Coin<DEEP>) {
self.value_mut!(b"give_deep").give_deep(deep);
}

// ----------------
// Public Functions
// ----------------
Expand Down Expand Up @@ -103,6 +112,7 @@ fun new_version_control(): VersionControl {
b"its_transfer",
b"deepbook_v3_swap",
b"register_transaction",
b"give_deep",
].map!(|function_name| function_name.to_ascii_string()),
])
}
Expand Down
Loading

0 comments on commit a76f631

Please sign in to comment.