Comment on page
Trade and Swap
This section shows how to construct and execute a trade on the DeepBook protocol.
When we create a pool in DeepBook, we need to specify the
BaseAsset
, QuoteAsset
, tick_size
, and lot_size
. We also need to pay a transaction fee (in SUI token) to create a pool.Function signature for create_pool in Sui Move contract
/// Parameters expected by this func
///
/// 0. `[registry]` Object ID refers to the pool registery
/// 1. `[tick_size]` Minimal Price Change Accuracy of this pool
/// 2. `[lot_size]` Minimal lot Change Accuracy of this pool
/// 3. `[coin]` Object ID of the sui coin, to pay fee for create pool (100 MIST sui charged)
/// Returns the AccountCap
public fun create_pool<BaseAsset, QuoteAsset, SUI>(
registry: &mut Registry,
tick_size: u64,
lot_size: u64,
coin: Coin<SUI>,
ctx: &mut TxContext,
)
Typescript SDK for invoking create_pool
inputs | type | description |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
tickSize | string | minimal price change of the trading pair |
lotSize | string | minimal amount change of the trading pair |
/**
* @description: Create pool for trading pair
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param tickSize Minimal Price Change Accuracy of this pool, eg: 10000000
* @param lotSize Minimal Lot Change Accuracy of this pool, eg: 10000
*/
public createPool(
token1: string,
token2: string,
tickSize: number,
lotSize: number,
): TransactionBlock {
const txb = new TransactionBlock();
// 100 sui to create a pool
const [coin] = txb.splitCoins(txb.gas, [txb.pure(100000000000)]);
txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::create_pool`,
arguments: [txb.pure(`${tickSize}`), txb.pure(`${lotSize}`), coin],
});
txb.setGasBudget(this.gasBudget);
return txb;
}
There are two types of orders,
limit order
and market order
in DeepBook. For
limit order
, users need to create a custodian
account the first time they interact with a particular Pool
and deposit assets to the Pool.There are four types of restrictions that you could put on limit orders;
NO_RESTRICTION
: 0- Fill as much quantity as possible in the current transaction as taker, and inject the remaining as a maker order.
IMMEDIATE_OR_CANCEL
: 1- Fill as much quantity as possible in the current transaction as taker, and cancel the rest of the order.
FILL_OR_KILL
: 2- Only fill if the entire order size can be filled as taker in the current transaction. Otherwise, abort the entire transaction.
POST_OR_ABORT
: 3- Only proceed if the entire order size can be posted to the order book as maker in the current transaction. Otherwise, abort the entire transaction.
Before placing a limit order, traders need to host a
custodian
account with asset deposited. Traders could generate a custodian account by invoking create_account
and will get an AccountCap
object authorizing them to access their custodian accounts. Note that the AccountCap
object will be the key to the custodian account, and if you transfer this object to others, the new owner will have access to all the funds and orders in the custodian account.Function signature for create_account in Sui Move contract
/// Parameters expected by this func
///
/// 0. `[ctx]` Information about the current transaction.
/// Returns the AccountCap
public fun create_account(ctx: &mut TxContext): AccountCap {
mint_account_cap(ctx)
}
Typescript SDK for invoking create_account
inputs | type | description |
---|---|---|
currentAddress | string | current user address |
/**
* @description: Create and Transfer custodian account to user
* @param currentAddress: current user address, eg: "0xbddc9d4961b46a130c2e1f38585bbc6fa8077ce54bcb206b26874ac08d607966"
*/
public createAccount(currentAddress: string): TransactionBlock {
const txb = new TransactionBlock();
let [cap] = txb.moveCall({
typeArguments: [],
target: `dee9::clob::create_account`,
arguments: [],
});
txb.transferObjects([cap], txb.pure(currentAddress));
txb.setSenderIfNotSet(currentAddress);
txb.setGasBudget(this.gasBudget);
return txb;
}
2.1.3 Deposit Asset
Traders could deposit base/quote assets to their custodian account, so they could later use it for placing limit orders. We provide two functions to deposit base and quote assets, respectively.
Function signature for deposit base asset in Sui Move contract
/// Parameters expected by this func
///
/// 0. `[pool]` Object ID refers to the pool containing the trading pair
/// 1. `[coin]` Object ID of the base asset coin
/// 2. `[account_cap]` Object ID of the account_cap authorizing the
/// accessilility to the escrow account
/// Returns none
public fun deposit_base<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
coin: Coin<BaseAsset>,
account_cap: &AccountCap
)
Typescript SDK for invoking deposit_base
inputs | type | description |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
poolId | string | |
coin | string | the objectId of the base coin you want to deposit |
accountCap | string |
/**
* @description: Deposit base asset into custodian account
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
* @param coin Object id of coin to deposit, eg: "0x316467544c7e719384579ac5745c75be5984ca9f004d6c09fd7ca24e4d8a3d14"
* @param accountCap Object id of Account Capacity under user address, created after invoking createAccount, eg: "0x6f699fef193723277559c8f499ca3706121a65ac96d273151b8e52deb29135d3"
*/
public depositBase(
token1: string,
token2: string,
poolId: string,
coin: string,
accountCap: string
): TransactionBlock {
const txb = new TransactionBlock();
txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::deposit_base`,
arguments: [
txb.object(`${poolId}`),
txb.object(coin),
txb.object(`${accountCap}`),
],
});
txb.setGasBudget(this.gasBudget);
return txb;
}
Function signature for deposit quote in contract
/// Parameters expected by this func
///
/// 0. `[pool]` Object ID refers to the pool containing the trading pair
/// 1. `[coin]` Object ID of the coin
/// 2. `[account_cap]` Object ID of the account_cap authorizing the
/// accessilility to the escrow account
/// Returns none
public fun deposit_quote<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
coin: Coin<QuoteAsset>,
account_cap: &AccountCap
)
Typescript SDK for invoking deposit_quote
inputs | type | description |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
poolId | string | |
coin | string | the objectId of the quote coin you want to deposit |
accountCap | string |
/**
* @description: Deposit quote asset into custodian account
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
* @param coin: Object id of coin to deposit, eg: "0x6e566fec4c388eeb78a7dab832c9f0212eb2ac7e8699500e203def5b41b9c70d"
* @param accountCap Object id of Account Capacity under user address, created after invoking createAccount, eg: "0x6f699fef193723277559c8f499ca3706121a65ac96d273151b8e52deb29135d3"
*/
public depositQuote(
token1: string,
token2: string,
poolId: string,
coin: string,
accountCap: string
): TransactionBlock {
const txb = new TransactionBlock();
txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::deposit_quote`,
arguments: [
txb.object(`${poolId}`),
txb.object(coin),
txb.object(`${accountCap}`),
],
});
txb.setGasBudget(this.gasBudget);
return txb;
}
2.1.4 Place Limit Order
Ensure you have a custodian account with enough base/quote assets to trade. You can now place limit orders on DeepBook with the following functions:
Function signature for placing limit order in Sui Move contract
/// Parameters expected by this func
///
/// 0. `[pool]` Object ID refers to the pool of the trading pair
/// 1. `[price]` Price of the placed limit order to sell or buy
/// 2. `[quantity]` Quantity of the base asset want to sell or buy
/// 3. `[is_bid]` Flag indicate sell base asset out or buy base asset in
/// 4. `[expire_timestamp]` Expire timestamp in ms in absolute value inclusive., if exceeded, order turn invalid
/// 5. `[restriction]` Object ID of the pool containing the trading pair
/// 6. `[clock]` Object ID of global system clock
/// 7. `[account_cap]` Object ID of the account_cap authorizing the
/// accessilility to the escrow account
/// 8. `[ctx]` Information about the current transaction.
/// Returns (base quantity filled, quote quantity filled, whether a maker order is being placed, order id of the maker order)
public fun place_limit_order<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
price: u64,
quantity: u64,
is_bid: bool,
expire_timestamp: u64,
restriction: u8,
clock: &Clock,
account_cap: &AccountCap,
ctx: &mut TxContext
): (u64, u64, bool, u64)
Typescript SDK for invoking place_limit_order
inputs | type | description |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
poolId | string | |
price | number | the limit price of your order |
quantity | number | the quantity of your order, always in base asset |
isBid | boolean | true for buying base with quote, false for selling base to quote |
expireTimestamp | number | expire timestamp of your limit order in ms. |
restriction | number | |
accountCap | string |
/**
* @description: place a limit order
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
* @param price: price of the limit order, eg: 180000000
* @param quantity: quantity of the limit order in BASE ASSET, eg: 100000000
* @param isBid: true for buying base with quote, false for selling base for quote
* @param expireTimestamp: expire timestamp of the limit order in ms, eg: 1620000000000
* @param restriction restrictions on limit orders, explain in doc for more details, eg: 0
* @param accountCap Object id of Account Capacity under user address, created after invoking createAccount, eg: "0x6f699fef193723277559c8f499ca3706121a65ac96d273151b8e52deb29135d3"
*/
public placeLimitOrder(
token1: string,
token2: string,
poolId: string,
price: number,
quantity: number,
isBid: boolean,
expireTimestamp: number,
restriction: number,
accountCap: string
): TransactionBlock {
const txb = new TransactionBlock();
const args = [
txb.object(String(poolId)),
txb.pure(Math.floor(price * 1000000000)), // to avoid float number
txb.pure(quantity),
txb.pure(isBid),
txb.pure(expireTimestamp),
txb.pure(restriction),
txb.object(normalizeSuiObjectId("0x6")),
txb.object(accountCap),
];
txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::place_limit_order`,
arguments: args,
});
txb.setGasBudget(this.gasBudget);
return txb;
}
Placing a market order does NOT require a custodian account. The remaining order will be canceled if it can only be filled partially.
Function signature for placing market order in Sui Move contract
/// Parameters expected by this func
///
/// 0. `[pool]` Object ID refers to the pool of the trading pair
/// 1. `[quantity]` Quantity of the base asset usr want to buy or sell
/// 2. `[is_bid]` Flag indicates buy or sell, true for buy and false for sell
/// 3. `[base_coin]` Object id of base coin, can be zero balance
/// 4. `[quote_coin]` Object id of the quote coin, can be zero balance
/// 5. `[clock]` Object ID refers to global system clock
/// 6. `[ctx]` Information about the transaction currently being executed.
/// Returns the base asset coin and quote asset coin after transaction
public fun place_market_order<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
quantity: u64,
is_bid: bool,
base_coin: Coin<BaseAsset>,
quote_coin: Coin<QuoteAsset>,
clock: &Clock,
ctx: &mut TxContext,
): (Coin<BaseAsset>, Coin<QuoteAsset>)
Typescript SDK for invoking place_market_order
inputs | type | description |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
poolId | string | |
quantity | number | the quantity of your order, always in base asset |
is_bid | boolean | true for buying base with quote, false for selling base to quote |
baseCoin | string | objectId of the token you want to swap, in a bid scenario, you can create a zero base coin using mint function |
quoteCoin | string | objectId of the token you want to swap, in a ask scenario, you can create a zero base coin using mint function |
currentAddress | string | address of the user |
/**
* @description: place the market order
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
* @param quantity Amount of quote asset to swap in base asset
* @param is_bid true if the order is bid, false if the order is ask
* @param baseCoin the objectId of the base coin
* @param quoteCoin the objectId of the quote coin
* @param currentAddress: current user address, eg: "0xbddc9d4961b46a130c2e1f38585bbc6fa8077ce54bcb206b26874ac08d607966"
*/
public place_market_order(
token1: string,
token2: string,
poolId: string,
quantity: number,
is_bid: boolean,
baseCoin: string,
quoteCoin: string,
currentAddress: string
): TransactionBlock {
const txb = new TransactionBlock();
const [base_coin_ret, quote_coin_ret ] = txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::place_market_order`,
arguments: [
txb.object(`${poolId}`),
txb.pure(quantity),
txb.pure(is_bid),
txb.object(baseCoin),
txb.object(quoteCoin),
],
});
txb.transferObjects([base_coin_ret], txb.pure(currentAddress));
txb.transferObjects([quote_coin_ret], txb.pure(currentAddress));
txb.setSenderIfNotSet(currentAddress);
txb.setGasBudget(this.gasBudget);
return txb;
}
Cancel a limit order placed onto the CLOB. Abort if order_id is invalid or if the order is not submitted by the transaction sender.
Function signature for canceling single order in Sui Move contract.
/// parameters expected by this func
///
/// 0. `[pool]` Object ID refers to the pool of the trading pair
/// 1. `[order_id]` Order ID of the placed limit order
/// 2. `[account_cap]` Object ID of the account_cap authorizing the
/// accessilility to the escrow account
/// Returns none
public fun cancel_order<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
order_id: u64,
account_cap: &AccountCap
)
Typescript SDK for invoking cancel_order
inputs | types | description |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
poolId | string | |
orderId | string | |
currentAddress | string | address of the user |
accountCap | string |
/**
* @description: cancel an order
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
* @param orderId orderId of a limit order, you can find them through function query.list_open_orders eg: "0"
* @param currentAddress: current user address, eg: "0xbddc9d4961b46a130c2e1f38585bbc6fa8077ce54bcb206b26874ac08d607966"
* @param accountCap Object id of Account Capacity under user address, created after invoking createAccount, eg: "0x6f699fef193723277559c8f499ca3706121a65ac96d273151b8e52deb29135d3"
*/
public cancelOrder(
token1: string,
token2: string,
poolId: string,
orderId: string,
currentAddress: strileng,
accountCap: string
): TransactionBlock {
const txb = new TransactionBlock();
txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::cancel_order`,
arguments: [
txb.object(poolId),
txb.pure(orderId),
txb.object(accountCap),
],
});
txb.setGasBudget(this.gasBudget);
return txb;
}
Cancel all limit orders under a certain account capacity
Function signature for canceling single order in Sui Move contract.
/// parameters expected by this func
///
/// 0. `[pool]` Object ID refers to the pool of the trading pair
/// 1. `[account_cap]` Object ID of the account_cap authorizing the
/// accessilility to the escrow account
/// Returns none
public fun cancel_all_orders<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
account_cap: &AccountCap
)
Typescript SDK for cancel_all_orders
inputs | type | description |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
poolId | string | |
accountCap | string |
/**
* @description: Cancel all limit orders under a certain account capacity
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
* @param accountCap Object id of Account Capacity under user address, created after invoking createAccount, eg: "0x6f699fef193723277559c8f499ca3706121a65ac96d273151b8e52deb29135d3"
*/
public cancelAllOrders(
token1: string,
token2: string,
poolId: string,
accountCap: string
): TransactionBlock {
const txb = new TransactionBlock();
txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::cancel_all_orders`,
arguments: [txb.object(poolId), txb.object(accountCap)],
});
txb.setGasBudget(this.gasBudget);
return txb;
}
Cancel multiple limit orders to save gas costs. Abort if any of the order ids is invalid or is not submitted by the sender. Note that this function can reduce gas costs even further if the caller has multiple orders at the same price level and if orders with the same price are grouped in the vector.
For example, if we have the following order_id to price mapping, {0: 100., 1: 200., 2: 100., 3: 200.}.Grouping order_ids like [0, 2, 1, 3] would make it the most gas efficient.
Function signature for batch cancel order.
/// parameters expected by this func
///
/// 0. `[pool]` Object ID refers to the pool containing the trading pair
/// 1. `[order_ids]` List of object ID of the pool containing the trading pair
/// 2. `[account_cap]` Object ID of the account_cap authorize the
/// accessilility to the escrow account
/// Returns none
public fun batch_cancel_order<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
order_ids: vector<u64>,
account_cap: &AccountCap
)
Typescript SDK for invoking batch_cancel_order
inputs | type | description |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
poolId | string | |
orderIds | string[] | orderId of a limit order, you can find them through the function list_open_orders, for example: ["0", "1"] |
accountCap | string |
/**
* @description: batch cancel order
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
* @param orderIds array of order ids you want to cancel, you can find your open orders by query.list_open_orders eg: ["0", "1", "2"]
* @param accountCap Object id of Account Capacity under user address, created after invoking createAccount, eg: "0x6f699fef193723277559c8f499ca3706121a65ac96d273151b8e52deb29135d3"
*/
public batchCancelOrder(
token1: string,
token2: string,
poolId: string,
orderIds: string[],
accountCap: string
): TransactionBlock {
const txb = new TransactionBlock();
txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::batch_cancel_order`,
arguments: [
txb.object(String(poolId)),
txb.pure(orderIds),
txb.object(accountCap),
],
});
txb.setGasBudget(defaultGasBudget);
return txb;
}
After transaction executed in the blockchain or cancel all the pending orders, one have to withdraw the token in the custodian account, in the base assets case, we have withdraw_base
Function signature for withdrawing the base assets in Sui Move contract.
public fun withdraw_base<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
quantity: u64,
account_cap: &AccountCap,
ctx: &mut TxContext
): Coin<BaseAsset> {
assert!(quantity > 0, EInvalidQuantity);
custodian::withdraw_asset(&mut pool.base_custodian, quantity, account_cap, ctx)
}
Typescript SDK for invoking withdraw_base
inputs | type | description |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
poolId | string | |
quantity | number | amount of base asset to withdraw |
currentAddress | string | current user address |
accountCap | string |
/
* @description: Withdraw base asset from custodian account
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
* @param quantity Amount of base asset to withdraw, eg: 10000000
* @param currentAddress: current user address, eg: "0xbddc9d4961b46a130c2e1f38585bbc6fa8077ce54bcb206b26874ac08d607966"
* @param accountCap Object id of Account Capacity under user address, created after invoking createAccount, eg: "0x6f699fef193723277559c8f499ca3706121a65ac96d273151b8e52deb29135d3"
*/
public async withdrawBase(
token1: string,
token2: string,
poolId: string,
quantity: number,
currentAddress: string,
accountCap: string
): Promise<TransactionBlock> {
const txb = new TransactionBlock();
const withdraw = txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::withdraw_base`,
arguments: [
txb.object(`${poolId}`),
txb.pure(quantity),
txb.object(`${accountCap}`),
],
});
txb.transferObjects([withdraw], txb.pure(currentAddress));
txb.setGasBudget(this.gasBudget);
return txb;
}
After transaction executed in the blockchain or cancel all the pending orders, one have to withdraw the token in the custodian account, in the base assets case, we have withdraw_base
Function signature for withdrawing the base assets in Sui Move contract.
public fun withdraw_quote<BaseAsset, QuoteAsset>(
pool: &mut Pool<BaseAsset, QuoteAsset>,
quantity: u64,
account_cap: &AccountCap,
ctx: &mut TxContext
): Coin<QuoteAsset> {
assert!(quantity > 0, EInvalidQuantity);
custodian::withdraw_asset(&mut pool.quote_custodian, quantity, account_cap, ctx)
}
Typescript SDK for invoking withdraw_quote
inputs | Text | Text |
---|---|---|
token1 | string | base asset of the trading pair |
token2 | string | quote asset of the trading pair |
poolId | string | |
quantity | number | amount of quote asset to withdraw |
currentAddress | string | current user address |
accountCap | string |
/**
* @description: Withdraw quote asset from custodian account
* @param token1 Full coin type of the base asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::wbtc::WBTC"
* @param token2 Full coin type of quote asset, eg: "0x3d0d0ce17dcd3b40c2d839d96ce66871ffb40e1154a8dd99af72292b3d10d7fc::usdt::USDT"
* @param poolId Object id of pool, created after invoking createPool, eg: "0xcaee8e1c046b58e55196105f1436a2337dcaa0c340a7a8c8baf65e4afb8823a4"
* @param quantity Amount of base asset to withdraw, eg: 10000000
* @param currentAddress: current user address, eg: "0xbddc9d4961b46a130c2e1f38585bbc6fa8077ce54bcb206b26874ac08d607966"
* @param accountCap Object id of Account Capacity under user address, created after invoking createAccount, eg: "0x6f699fef193723277559c8f499ca3706121a65ac96d273151b8e52deb29135d3"
*/
public withdrawQuote(
token1: string,
token2: string,
poolId: string,
quantity: number,
currentAddress: string,
accountCap: string
): TransactionBlock {
const txb = new TransactionBlock();
const withdraw = txb.moveCall({
typeArguments: [token1, token2],
target: `dee9::clob::withdraw_quote`,
arguments: [
txb.object(`${poolId}`),
txb.pure(quantity),
txb.object(`${accountCap}`),
],
});
txb.transferObjects([withdraw], txb.pure(currentAddress));
txb.setGasBudget(this.gasBudget);
return txb;
}
Last modified 5mo ago