Skip to content

Commit dfc4da5

Browse files
authored
sdk: fix exactOut swaps (#2031)
* fix exactOut swaps * rm comment
1 parent 73e9dff commit dfc4da5

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

sdk/src/driftClient.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6321,7 +6321,6 @@ export class DriftClient {
63216321
const inMarket = this.getSpotMarketAccount(inMarketIndex);
63226322

63236323
const isExactOut = swapMode === 'ExactOut';
6324-
const exactOutBufferedAmountIn = amount.muln(1001).divn(1000); // Add 10bp buffer
63256324

63266325
const preInstructions: TransactionInstruction[] = [];
63276326

@@ -6377,11 +6376,24 @@ export class DriftClient {
63776376
}
63786377
}
63796378

6379+
let amountInForBeginSwap: BN;
6380+
if (isExactOut) {
6381+
if (quote || v6?.quote) {
6382+
amountInForBeginSwap = v6?.quote
6383+
? new BN(v6.quote.inAmount)
6384+
: new BN(quote!.inAmount);
6385+
} else {
6386+
amountInForBeginSwap = amount.muln(1001).divn(1000);
6387+
}
6388+
} else {
6389+
amountInForBeginSwap = amount;
6390+
}
6391+
63806392
// Get drift swap instructions for begin and end
63816393
const { beginSwapIx, endSwapIx } = await this.getSwapIx({
63826394
outMarketIndex,
63836395
inMarketIndex,
6384-
amountIn: isExactOut ? exactOutBufferedAmountIn : amount,
6396+
amountIn: amountInForBeginSwap,
63856397
inTokenAccount: finalInAssociatedTokenAccount,
63866398
outTokenAccount: finalOutAssociatedTokenAccount,
63876399
reduceOnly,

0 commit comments

Comments
 (0)