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.
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.
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.
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.minOut: Number representing minimum output 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.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.Examples
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.
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());
};
Related links
The DeepBookV3 SDK repository on GitHub.
The DeepBookV3 SDK node package on NPM.
The DeepBookV3 repository on GitHub.