@0dotxyz/p0-ts-sdk 1.2.0-alpha.2 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +83 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +83 -51
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -43353,8 +43353,22 @@ async function buildSwapCollateralFlashloanTx({
|
|
|
43353
43353
|
overrideInferAccounts,
|
|
43354
43354
|
blockhash
|
|
43355
43355
|
}) {
|
|
43356
|
-
const {
|
|
43356
|
+
const {
|
|
43357
|
+
withdrawBank,
|
|
43358
|
+
tokenProgram: withdrawTokenProgram,
|
|
43359
|
+
totalPositionAmount,
|
|
43360
|
+
withdrawAmount
|
|
43361
|
+
} = withdrawOpts;
|
|
43357
43362
|
const { depositBank, tokenProgram: depositTokenProgram } = depositOpts;
|
|
43363
|
+
if (withdrawAmount !== void 0 && withdrawAmount <= 0) {
|
|
43364
|
+
throw new Error("withdrawAmount must be greater than 0");
|
|
43365
|
+
}
|
|
43366
|
+
const actualWithdrawAmount = Math.min(withdrawAmount ?? totalPositionAmount, totalPositionAmount);
|
|
43367
|
+
const isFullWithdraw = isWholePosition(
|
|
43368
|
+
{ amount: totalPositionAmount, isLending: true },
|
|
43369
|
+
actualWithdrawAmount,
|
|
43370
|
+
withdrawBank.mintDecimals
|
|
43371
|
+
);
|
|
43358
43372
|
const swapResult = [];
|
|
43359
43373
|
const cuRequestIxs = [
|
|
43360
43374
|
web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 12e5 }),
|
|
@@ -43371,7 +43385,7 @@ async function buildSwapCollateralFlashloanTx({
|
|
|
43371
43385
|
withdrawOpts.withdrawBank.tokenSymbol
|
|
43372
43386
|
);
|
|
43373
43387
|
}
|
|
43374
|
-
const adjustedAmount = new BigNumber10.BigNumber(
|
|
43388
|
+
const adjustedAmount = new BigNumber10.BigNumber(actualWithdrawAmount).div(withdrawOpts.withdrawBank.assetShareValue).times(1.0001).toNumber();
|
|
43375
43389
|
withdrawIxs = await makeKaminoWithdrawIx3({
|
|
43376
43390
|
program,
|
|
43377
43391
|
bank: withdrawBank,
|
|
@@ -43381,7 +43395,7 @@ async function buildSwapCollateralFlashloanTx({
|
|
|
43381
43395
|
marginfiAccount,
|
|
43382
43396
|
authority: marginfiAccount.authority,
|
|
43383
43397
|
reserve,
|
|
43384
|
-
withdrawAll:
|
|
43398
|
+
withdrawAll: isFullWithdraw,
|
|
43385
43399
|
isSync: true,
|
|
43386
43400
|
opts: {
|
|
43387
43401
|
createAtas: false,
|
|
@@ -43405,12 +43419,12 @@ async function buildSwapCollateralFlashloanTx({
|
|
|
43405
43419
|
bank: withdrawOpts.withdrawBank,
|
|
43406
43420
|
bankMap,
|
|
43407
43421
|
tokenProgram: withdrawOpts.tokenProgram,
|
|
43408
|
-
amount:
|
|
43422
|
+
amount: actualWithdrawAmount,
|
|
43409
43423
|
marginfiAccount,
|
|
43410
43424
|
authority: marginfiAccount.authority,
|
|
43411
43425
|
driftSpotMarket: driftState.spotMarketState,
|
|
43412
43426
|
userRewards: driftState.userRewards,
|
|
43413
|
-
withdrawAll:
|
|
43427
|
+
withdrawAll: isFullWithdraw,
|
|
43414
43428
|
isSync: false,
|
|
43415
43429
|
opts: {
|
|
43416
43430
|
createAtas: false,
|
|
@@ -43426,10 +43440,10 @@ async function buildSwapCollateralFlashloanTx({
|
|
|
43426
43440
|
bank: withdrawBank,
|
|
43427
43441
|
bankMap,
|
|
43428
43442
|
tokenProgram: withdrawTokenProgram,
|
|
43429
|
-
amount:
|
|
43443
|
+
amount: actualWithdrawAmount,
|
|
43430
43444
|
marginfiAccount,
|
|
43431
43445
|
authority: marginfiAccount.authority,
|
|
43432
|
-
withdrawAll:
|
|
43446
|
+
withdrawAll: isFullWithdraw,
|
|
43433
43447
|
isSync: true,
|
|
43434
43448
|
opts: {
|
|
43435
43449
|
createAtas: false,
|
|
@@ -43442,7 +43456,7 @@ async function buildSwapCollateralFlashloanTx({
|
|
|
43442
43456
|
}
|
|
43443
43457
|
if (depositBank.mint.equals(withdrawBank.mint)) {
|
|
43444
43458
|
swapResult.push({
|
|
43445
|
-
amountToDeposit:
|
|
43459
|
+
amountToDeposit: actualWithdrawAmount,
|
|
43446
43460
|
swapInstructions: [],
|
|
43447
43461
|
setupInstructions: [],
|
|
43448
43462
|
swapLookupTables: []
|
|
@@ -43458,7 +43472,7 @@ async function buildSwapCollateralFlashloanTx({
|
|
|
43458
43472
|
quoteParams: {
|
|
43459
43473
|
inputMint: withdrawBank.mint.toBase58(),
|
|
43460
43474
|
outputMint: depositBank.mint.toBase58(),
|
|
43461
|
-
amount: uiToNative(
|
|
43475
|
+
amount: uiToNative(actualWithdrawAmount, withdrawBank.mintDecimals).toNumber(),
|
|
43462
43476
|
dynamicSlippage: swapOpts.jupiterOptions ? swapOpts.jupiterOptions.slippageMode === "DYNAMIC" : true,
|
|
43463
43477
|
slippageBps: swapOpts.jupiterOptions?.slippageBps,
|
|
43464
43478
|
swapMode: "ExactIn",
|
|
@@ -43725,56 +43739,74 @@ async function buildSwapDebtFlashloanTx({
|
|
|
43725
43739
|
overrideInferAccounts,
|
|
43726
43740
|
blockhash
|
|
43727
43741
|
}) {
|
|
43728
|
-
const {
|
|
43742
|
+
const {
|
|
43743
|
+
repayBank,
|
|
43744
|
+
tokenProgram: repayTokenProgram,
|
|
43745
|
+
totalPositionAmount,
|
|
43746
|
+
repayAmount
|
|
43747
|
+
} = repayOpts;
|
|
43729
43748
|
const { borrowBank, tokenProgram: borrowTokenProgram } = borrowOpts;
|
|
43749
|
+
if (repayAmount !== void 0 && repayAmount <= 0) {
|
|
43750
|
+
throw new Error("repayAmount must be greater than 0");
|
|
43751
|
+
}
|
|
43752
|
+
const actualRepayAmount = Math.min(repayAmount ?? totalPositionAmount, totalPositionAmount);
|
|
43730
43753
|
const swapResult = [];
|
|
43731
43754
|
const cuRequestIxs = [
|
|
43732
43755
|
web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 12e5 }),
|
|
43733
43756
|
web3_js.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 1 })
|
|
43734
43757
|
];
|
|
43735
|
-
|
|
43736
|
-
|
|
43737
|
-
|
|
43738
|
-
|
|
43739
|
-
|
|
43740
|
-
|
|
43741
|
-
|
|
43742
|
-
|
|
43743
|
-
|
|
43744
|
-
|
|
43745
|
-
|
|
43746
|
-
|
|
43747
|
-
|
|
43748
|
-
|
|
43758
|
+
const destinationTokenAccount = getAssociatedTokenAddressSync(
|
|
43759
|
+
repayBank.mint,
|
|
43760
|
+
marginfiAccount.authority,
|
|
43761
|
+
true,
|
|
43762
|
+
repayTokenProgram.equals(TOKEN_2022_PROGRAM_ID) ? TOKEN_2022_PROGRAM_ID : void 0
|
|
43763
|
+
);
|
|
43764
|
+
const jupiterApiClient = swapOpts.jupiterOptions?.configParams?.basePath ? new api.SwapApi(new api.Configuration(swapOpts.jupiterOptions.configParams)) : api.createJupiterApiClient(swapOpts.jupiterOptions?.configParams);
|
|
43765
|
+
const estimateQuote = await jupiterApiClient.quoteGet({
|
|
43766
|
+
inputMint: borrowBank.mint.toBase58(),
|
|
43767
|
+
outputMint: repayBank.mint.toBase58(),
|
|
43768
|
+
amount: uiToNative(actualRepayAmount, repayBank.mintDecimals).toNumber(),
|
|
43769
|
+
swapMode: "ExactOut",
|
|
43770
|
+
dynamicSlippage: swapOpts.jupiterOptions ? swapOpts.jupiterOptions.slippageMode === "DYNAMIC" : true,
|
|
43771
|
+
slippageBps: swapOpts.jupiterOptions?.slippageBps
|
|
43772
|
+
});
|
|
43773
|
+
const estimatedBorrowAmount = nativeToUi(
|
|
43774
|
+
estimateQuote.otherAmountThreshold,
|
|
43775
|
+
borrowBank.mintDecimals
|
|
43776
|
+
);
|
|
43777
|
+
const swapResponses = await getJupiterSwapIxsForFlashloan({
|
|
43778
|
+
quoteParams: {
|
|
43779
|
+
inputMint: borrowBank.mint.toBase58(),
|
|
43780
|
+
outputMint: repayBank.mint.toBase58(),
|
|
43781
|
+
amount: uiToNative(estimatedBorrowAmount, borrowBank.mintDecimals).toNumber(),
|
|
43782
|
+
dynamicSlippage: swapOpts.jupiterOptions ? swapOpts.jupiterOptions.slippageMode === "DYNAMIC" : true,
|
|
43783
|
+
slippageBps: swapOpts.jupiterOptions?.slippageBps,
|
|
43784
|
+
swapMode: "ExactIn",
|
|
43785
|
+
platformFeeBps: swapOpts.jupiterOptions?.platformFeeBps,
|
|
43786
|
+
onlyDirectRoutes: swapOpts.jupiterOptions?.directRoutesOnly ?? false
|
|
43787
|
+
},
|
|
43788
|
+
authority: marginfiAccount.authority,
|
|
43789
|
+
connection,
|
|
43790
|
+
destinationTokenAccount,
|
|
43791
|
+
configParams: swapOpts.jupiterOptions?.configParams
|
|
43792
|
+
});
|
|
43793
|
+
swapResponses.forEach((response) => {
|
|
43794
|
+
const outAmount = nativeToUi(response.quoteResponse.outAmount, repayBank.mintDecimals);
|
|
43795
|
+
const outAmountThreshold = nativeToUi(
|
|
43796
|
+
response.quoteResponse.otherAmountThreshold,
|
|
43797
|
+
repayBank.mintDecimals
|
|
43749
43798
|
);
|
|
43750
|
-
const
|
|
43751
|
-
|
|
43752
|
-
|
|
43753
|
-
|
|
43754
|
-
|
|
43755
|
-
|
|
43756
|
-
|
|
43757
|
-
|
|
43758
|
-
|
|
43759
|
-
onlyDirectRoutes: swapOpts.jupiterOptions?.directRoutesOnly ?? false
|
|
43760
|
-
},
|
|
43761
|
-
authority: marginfiAccount.authority,
|
|
43762
|
-
connection,
|
|
43763
|
-
destinationTokenAccount,
|
|
43764
|
-
configParams: swapOpts.jupiterOptions?.configParams
|
|
43765
|
-
});
|
|
43766
|
-
swapResponses.forEach((response) => {
|
|
43767
|
-
const borrowAmount = nativeToUi(response.quoteResponse.inAmount, borrowBank.mintDecimals);
|
|
43768
|
-
swapResult.push({
|
|
43769
|
-
amountToRepay: totalPositionAmount,
|
|
43770
|
-
borrowAmount,
|
|
43771
|
-
swapInstructions: [response.swapInstruction],
|
|
43772
|
-
setupInstructions: response.setupInstructions,
|
|
43773
|
-
swapLookupTables: response.addressLookupTableAddresses,
|
|
43774
|
-
quoteResponse: response.quoteResponse
|
|
43775
|
-
});
|
|
43799
|
+
const amountToRepay = outAmount > totalPositionAmount ? totalPositionAmount : outAmountThreshold;
|
|
43800
|
+
const borrowAmount = nativeToUi(response.quoteResponse.inAmount, borrowBank.mintDecimals);
|
|
43801
|
+
swapResult.push({
|
|
43802
|
+
amountToRepay,
|
|
43803
|
+
borrowAmount,
|
|
43804
|
+
swapInstructions: [response.swapInstruction],
|
|
43805
|
+
setupInstructions: response.setupInstructions,
|
|
43806
|
+
swapLookupTables: response.addressLookupTableAddresses,
|
|
43807
|
+
quoteResponse: response.quoteResponse
|
|
43776
43808
|
});
|
|
43777
|
-
}
|
|
43809
|
+
});
|
|
43778
43810
|
if (swapResult.length === 0) {
|
|
43779
43811
|
throw new Error(
|
|
43780
43812
|
`No swap routes found for ${borrowBank.mint.toBase58()} -> ${repayBank.mint.toBase58()}`
|