Skip to content

Commit b3876a2

Browse files
authored
Sui withdraw and call (#254)
1 parent 3941f97 commit b3876a2

File tree

7 files changed

+217
-50
lines changed

7 files changed

+217
-50
lines changed

examples/call/scripts/localnet.sh

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ npx hardhat compile --force --quiet
1212
ZRC20_ETHEREUM=$(jq -r '.addresses[] | select(.type=="ZRC-20 ETH on 5") | .address' localnet.json)
1313
ERC20_ETHEREUM=$(jq -r '.addresses[] | select(.type=="ERC-20 USDC" and .chain=="ethereum") | .address' localnet.json)
1414
ZRC20_BNB=$(jq -r '.addresses[] | select(.type=="ZRC-20 BNB on 97") | .address' localnet.json)
15-
ZRC20_SOL=$(jq -r '.addresses[] | select(.type=="ZRC-20 SOL on 901") | .address' localnet.json)
15+
# ZRC20_SOL=$(jq -r '.addresses[] | select(.type=="ZRC-20 SOL on 901") | .address' localnet.json)
1616
ZRC20_SPL=$(jq -r '.addresses[] | select(.type=="ZRC-20 USDC on 901") | .address' localnet.json)
1717
USDC_SPL=$(jq -r '.addresses[] | select(.type=="SPL-20 USDC") | .address' localnet.json)
1818
GATEWAY_ETHEREUM=$(jq -r '.addresses[] | select(.type=="gatewayEVM" and .chain=="ethereum") | .address' localnet.json)
@@ -25,8 +25,8 @@ echo -e "\n🚀 Deployed contract on ZetaChain: $CONTRACT_ZETACHAIN"
2525
CONTRACT_ETHEREUM=$(npx hardhat deploy --name Connected --json --network localhost --gateway "$GATEWAY_ETHEREUM" | jq -r '.contractAddress')
2626
echo -e "🚀 Deployed contract on Ethereum: $CONTRACT_ETHEREUM"
2727

28-
CONTRACT_SOL=9BjVGjn28E58LgSi547JYEpqpgRoo1TErkbyXiRSNDQy
29-
cd solana && anchor build && npx ts-node setup/main.ts "$USDC_SPL" && cd -
28+
# CONTRACT_SOL=9BjVGjn28E58LgSi547JYEpqpgRoo1TErkbyXiRSNDQy
29+
# cd solana && anchor build && npx ts-node setup/main.ts "$USDC_SPL" && cd -
3030

3131
npx hardhat connected-deposit \
3232
--contract "$CONTRACT_ETHEREUM" \
@@ -208,27 +208,27 @@ npx hardhat zetachain-withdraw-and-call \
208208

209209
npx hardhat localnet-check
210210

211-
ENCODED_ACCOUNTS_AND_DATA=$(npx ts-node solana/setup/encodeCallArgs.ts "sol" "$USDC_SPL")
212-
npx hardhat zetachain-withdraw-and-call \
213-
--receiver "$CONTRACT_SOL" \
214-
--gateway-zeta-chain "$GATEWAY_ZETACHAIN" \
215-
--zrc20 "$ZRC20_SOL" \
216-
--amount 1 \
217-
--network localhost \
218-
--types '["bytes"]' $ENCODED_ACCOUNTS_AND_DATA
211+
# ENCODED_ACCOUNTS_AND_DATA=$(npx ts-node solana/setup/encodeCallArgs.ts "sol" "$USDC_SPL")
212+
# npx hardhat zetachain-withdraw-and-call \
213+
# --receiver "$CONTRACT_SOL" \
214+
# --gateway-zeta-chain "$GATEWAY_ZETACHAIN" \
215+
# --zrc20 "$ZRC20_SOL" \
216+
# --amount 1 \
217+
# --network localhost \
218+
# --types '["bytes"]' $ENCODED_ACCOUNTS_AND_DATA
219219

220-
npx hardhat localnet-check
220+
# npx hardhat localnet-check
221221

222-
ENCODED_ACCOUNTS_AND_DATA=$(npx ts-node solana/setup/encodeCallArgs.ts "spl" "$USDC_SPL")
223-
npx hardhat zetachain-withdraw-and-call \
224-
--receiver "$CONTRACT_SOL" \
225-
--gateway-zeta-chain "$GATEWAY_ZETACHAIN" \
226-
--zrc20 "$ZRC20_SPL" \
227-
--amount 1 \
228-
--network localhost \
229-
--types '["bytes"]' $ENCODED_ACCOUNTS_AND_DATA
222+
# ENCODED_ACCOUNTS_AND_DATA=$(npx ts-node solana/setup/encodeCallArgs.ts "spl" "$USDC_SPL")
223+
# npx hardhat zetachain-withdraw-and-call \
224+
# --receiver "$CONTRACT_SOL" \
225+
# --gateway-zeta-chain "$GATEWAY_ZETACHAIN" \
226+
# --zrc20 "$ZRC20_SPL" \
227+
# --amount 1 \
228+
# --network localhost \
229+
# --types '["bytes"]' $ENCODED_ACCOUNTS_AND_DATA
230230

231-
npx hardhat localnet-check
231+
# npx hardhat localnet-check
232232

233233
npx hardhat zetachain-withdraw-and-call \
234234
--receiver "$CONTRACT_ETHEREUM" \

examples/call/sui/Move.lock

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[move]
44
version = 3
5-
manifest_digest = "14A2399375F938EA73D29140B8CA371887691345F38828DAEC376BDB3CF2C9A3"
5+
manifest_digest = "F7637F8158027D0E2F57BA42B0A909AA6391135141CFF820577A4C3F7BA9E62D"
66
deps_digest = "3C4103934B1E040BB6B23F1D610B4EF9F2F1166A50A104EADCF77467C004C600"
77
dependencies = [
88
{ id = "Sui", name = "Sui" },
@@ -30,7 +30,7 @@ dependencies = [
3030
]
3131

3232
[move.toolchain-version]
33-
compiler-version = "1.42.2"
33+
compiler-version = "1.45.0"
3434
edition = "2024.beta"
3535
flavor = "sui"
3636

@@ -41,3 +41,9 @@ chain-id = "59d1348f"
4141
original-published-id = "0x1a4bd5ee637990fe0649d860c3ffc673d608c601dbd518d84551814c3e5902b6"
4242
latest-published-id = "0x1a4bd5ee637990fe0649d860c3ffc673d608c601dbd518d84551814c3e5902b6"
4343
published-version = "1"
44+
45+
[env.localnet]
46+
chain-id = "992fdbfb"
47+
original-published-id = "0x723be41ba96120e33a464c7b59597a4edaf934614f7ea05349b71d2545819a4b"
48+
latest-published-id = "0x723be41ba96120e33a464c7b59597a4edaf934614f7ea05349b71d2545819a4b"
49+
published-version = "1"

examples/call/sui/Move.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-fram
77
gateway = { local = "/usr/local/share/localnet/protocol-contracts-sui"}
88

99
[addresses]
10-
call = "0x0"
10+
connected = "0x0"
1111
gateway = "_"

examples/call/sui/sources/call.move

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/// This module provides a mock implementation of the Cetus DEX (Decentralized Exchange) interface
2+
/// for the Sui blockchain. It simulates token swap functionality between two token types.
3+
///
4+
/// The mock implementation includes:
5+
/// - A simulated liquidity pool for token pairs
6+
/// - Basic swap functionality between token A and token B
7+
/// - Shared objects for configuration and state management
8+
///
9+
/// This is used only for testing and development purposes to simulate
10+
/// token swaps without interacting with the actual Cetus protocol.
11+
module connected::cetusmock;
12+
13+
use sui::balance::{Self, Balance};
14+
use sui::coin::{Self, Coin};
15+
16+
/// Global configuration object
17+
public struct GlobalConfig has key {
18+
id: UID,
19+
}
20+
21+
/// Partner object representing a protocol participant
22+
public struct Partner has key {
23+
id: UID,
24+
}
25+
26+
/// Clock object for time-based operations
27+
public struct Clock has key {
28+
id: UID,
29+
}
30+
31+
/// Represents a liquidity pool for a pair of tokens (CoinA and CoinB)
32+
/// Holds balances of both tokens that can be swapped
33+
public struct Pool<phantom CoinA, phantom CoinB> has key {
34+
id: UID,
35+
balance_a: Balance<CoinA>,
36+
balance_b: Balance<CoinB>,
37+
}
38+
39+
/// Initializes the mock protocol by creating and sharing all necessary objects
40+
/// Creates a pool for SUI and TOKEN pair
41+
fun init(ctx: &mut TxContext) {
42+
let global_config = GlobalConfig {
43+
id: object::new(ctx),
44+
};
45+
let pool = Pool<sui::sui::SUI, connected::token::TOKEN> {
46+
id: object::new(ctx),
47+
balance_a: balance::zero<sui::sui::SUI>(),
48+
balance_b: balance::zero<connected::token::TOKEN>(),
49+
};
50+
let partner = Partner {
51+
id: object::new(ctx),
52+
};
53+
let clock = Clock {
54+
id: object::new(ctx),
55+
};
56+
57+
transfer::share_object(global_config);
58+
transfer::share_object(pool);
59+
transfer::share_object(partner);
60+
transfer::share_object(clock);
61+
}
62+
63+
/// Deposits tokens into the pool to be available for swaps
64+
/// @param pool The liquidity pool to deposit into
65+
/// @param coin The coin to deposit
66+
public entry fun deposit<CoinA, CoinB>(pool: &mut Pool<CoinA, CoinB>, coin: Coin<CoinB>) {
67+
balance::join(&mut pool.balance_b, coin.into_balance());
68+
}
69+
70+
/// Simulates a token swap from CoinA to CoinB
71+
/// @param _config Global configuration object
72+
/// @param pool The liquidity pool to swap from
73+
/// @param _partner Partner object
74+
/// @param coin_a The input coin to swap
75+
/// @param _clock Clock object for time-based operations
76+
/// @param ctx Transaction context
77+
/// @return The swapped coins of type CoinB
78+
public fun swap_a2b<CoinA, CoinB>(
79+
_config: &GlobalConfig,
80+
pool: &mut Pool<CoinA, CoinB>,
81+
_partner: &mut Partner,
82+
coin_a: Coin<CoinA>,
83+
_clock: &Clock,
84+
ctx: &mut TxContext,
85+
): Coin<CoinB> {
86+
// deposit all coins in
87+
balance::join(&mut pool.balance_a, coin_a.into_balance());
88+
89+
// take all the coins out
90+
let value = pool.balance_b.value();
91+
let coins_out = coin::take(&mut pool.balance_b, value, ctx);
92+
93+
coins_out
94+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/// This module provides two main functionalities:
2+
/// 1. An on_call function that gets executed when a call is made from ZetaChain.
3+
/// This function replicates Cetus's swap functionality, maintaining the same
4+
/// parameter structure and behavior for seamless integration with Cetus pools.
5+
///
6+
/// 2. Examples of how to use the ZetaChain gateway to make calls from Sui to
7+
/// universal contracts on ZetaChain through the deposit and deposit_and_call
8+
/// functions.
9+
module connected::connected;
10+
11+
use connected::cetusmock::{GlobalConfig, Partner, Pool, Clock, swap_a2b};
12+
use gateway::gateway::Gateway;
13+
use std::ascii::String;
14+
use sui::address::from_bytes;
15+
use sui::coin::Coin;
16+
use sui::event;
17+
use sui::tx_context;
18+
19+
// Withdraw and Call Example
20+
21+
public entry fun on_call<SOURCE_COIN, TARGET_COIN>(
22+
in_coins: Coin<SOURCE_COIN>,
23+
cetus_config: &GlobalConfig,
24+
pool: &mut Pool<SOURCE_COIN, TARGET_COIN>,
25+
cetus_partner: &mut Partner,
26+
clock: &Clock,
27+
data: vector<u8>,
28+
ctx: &mut TxContext,
29+
) {
30+
let coins_out = swap_a2b<SOURCE_COIN, TARGET_COIN>(
31+
cetus_config,
32+
pool,
33+
cetus_partner,
34+
in_coins,
35+
clock,
36+
ctx,
37+
);
38+
39+
let receiver = from_bytes(data);
40+
41+
// transfer the coins to the provided address
42+
transfer::public_transfer(coins_out, receiver)
43+
}
44+
45+
// Deposit and Call Example
46+
47+
public entry fun deposit<T>(
48+
gateway: &mut Gateway,
49+
coin: Coin<T>,
50+
receiver: String,
51+
ctx: &mut tx_context::TxContext,
52+
) {
53+
gateway.deposit(coin, receiver, ctx);
54+
}
55+
56+
public entry fun deposit_and_call<T>(
57+
gateway: &mut Gateway,
58+
coin: Coin<T>,
59+
receiver: String,
60+
payload: vector<u8>,
61+
ctx: &mut tx_context::TxContext,
62+
) {
63+
gateway.deposit_and_call(coin, receiver, payload, ctx);
64+
}

examples/call/sui/sources/token.move

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module connected::token;
2+
3+
use sui::coin::{Self, TreasuryCap};
4+
5+
public struct TOKEN has drop {}
6+
7+
fun init(witness: TOKEN, ctx: &mut TxContext) {
8+
let (treasury, metadata) = coin::create_currency(
9+
witness,
10+
6,
11+
b"TOKEN",
12+
b"TOKEN",
13+
b"An example token",
14+
option::none(), // url: no URL provided for this example token
15+
ctx,
16+
);
17+
transfer::public_freeze_object(metadata);
18+
transfer::public_transfer(treasury, ctx.sender())
19+
}
20+
21+
public entry fun mint_and_transfer(
22+
treasury_cap: &mut TreasuryCap<TOKEN>,
23+
amount: u64,
24+
recipient: address,
25+
ctx: &mut TxContext,
26+
) {
27+
let coin = coin::mint(treasury_cap, amount, ctx);
28+
transfer::public_transfer(coin, recipient)
29+
}

0 commit comments

Comments
 (0)