Swaps
DeepBookV3 provides a swap-like interface commonly seen in automatic market makers (AMMs). The DeepBookV3 SDK provides functions to leverage the features of this interface. See Swaps in the API section for more details.
Swap functions
The SDK provides the following functions to perform swaps between the base and quote asset. In the SUI_DBUSDC pool used in the examples on this page, SUI is the base asset and DBUSDC is the quote asset.
Each function returns a function that takes a Transaction object. Pass your transaction to the returned function, as the examples do with this.deepBook.swapExactBaseForQuote({ ... })(tx).
swapExactBaseForQuote
Use swapExactBaseForQuote to swap exact base amount for quote amount. The call returns a function that takes a Transaction object.
Parameters
params:SwapParamsobject that represents the parameters for the swap. The fields ofSwapParamsare listed underswapExactQuantity, which takes the same object plusisBaseToCoin.
swapExactBaseForQuote({ params: SwapParams });
swapExactQuoteForBase
Use swapExactQuoteForBase to swap exact quote amount for base amount. The call returns a function that takes a Transaction object.
Parameters
params:SwapParamsobject that represents the parameters for the swap. The fields ofSwapParamsare listed underswapExactQuantity, which takes the same object plusisBaseToCoin.
swapExactQuoteForBase({ params: SwapParams });
swapExactQuantity
Use swapExactQuantity to swap an exact quantity in either direction (base to quote or quote to base) without using a balance manager. The call returns a function that takes a Transaction object.
Parameters
params:SwapParams & { isBaseToCoin: boolean }object containing:poolKey: String that identifies the pool.amount: Number representing the amount to swap.deepAmount: Number representing the DEEP amount for fees. Excess DEEP is returned.minOut: Number representing minimum output amount. The transaction fails if the swap returns less than this amount.isBaseToCoin: Boolean indicating swap direction (true = base to quote).baseCoin: OptionalTransactionArgumentfor base coin input.quoteCoin: OptionalTransactionArgumentfor quote coin input.deepCoin: OptionalTransactionArgumentfor DEEP coin input.
packages/deepbook-v3/src/transactions/deepbook.ts. You probably need to run `pnpm prebuild` and restart the site.swapExactQuantityWithManager
Use swapExactQuantityWithManager to swap an exact quantity using a balance manager. The call returns a function that takes a Transaction object.
Parameters
params:SwapWithManagerParams & { isBaseToCoin: boolean }object containing:poolKey: String that identifies the pool.balanceManagerKey: String that identifies the balance manager.amount: Number representing the amount to swap.minOut: Number representing minimum output amount. The transaction fails if the swap returns less than this amount.isBaseToCoin: Boolean indicating swap direction (true = base to quote).tradeCap: OptionalTransactionArgumentfor trade capability.depositCap: OptionalTransactionArgumentfor deposit capability.withdrawCap: OptionalTransactionArgumentfor withdraw capability.baseCoin: OptionalTransactionArgumentfor base coin input.quoteCoin: OptionalTransactionArgumentfor quote coin input.
packages/deepbook-v3/src/transactions/deepbook.ts. You probably need to run `pnpm prebuild` and restart the site.How do I swap tokens with the DeepBook SDK?
The following examples demonstrate custom swap functions that you can place into the DeepBookMarketMaker class. Base coin, quote coin, and deep coin are automatically determined by the coin available in the user address unless you explicitly pass one in as an argument. You can transfer the coin outputs to their address or execute other operations using the outputs.
Calling swapExactBaseForQuote or swapExactQuoteForBase with a transaction produces three coin outputs, baseOut, quoteOut, and deepOut, which the examples transfer back to the active address.
swapExactBaseForQuote = (tx: Transaction) => {
const [baseOut, quoteOut, deepOut] = this.deepBook.swapExactBaseForQuote({
poolKey: 'SUI_DBUSDC',
amount: 1, // amount of SUI to swap
deepAmount: 1, // amount of DEEP to pay as fees, excess is returned
minOut: 0.1, // minimum amount of DBUSDC to receive or transaction fails
})(tx);
// Transfer received coins to own address
tx.transferObjects([baseOut, quoteOut, deepOut], this.getActiveAddress());
};
swapExactQuoteForBase = (tx: Transaction) => {
const [baseOut, quoteOut, deepOut] = this.deepBook.swapExactQuoteForBase({
poolKey: 'SUI_DBUSDC',
amount: 1, // amount of DBUSDC to swap
deepAmount: 1, // amount of DEEP to pay as fees, excess is returned
minOut: 0.1, // minimum amount of SUI to receive or transaction fails
})(tx);
// Transfer received coins to own address
tx.transferObjects([baseOut, quoteOut, deepOut], this.getActiveAddress());
};