Skip to content

Commit

Permalink
fix: incorrect looping deposit amount
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Dec 17, 2024
1 parent 69f6a44 commit 6a93b96
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/marginfi-client-v2/src/models/account/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ class MarginfiAccountWrapper {
: [ComputeBudgetProgram.setComputeUnitLimit({ units: 500_000 })];
// tiny priority fee just in case bundle fails
const [priorityFeeIx] = makePriorityFeeIx(0.00001);
console.log("borrowAmount", borrowAmount.toString());
const borrowIxs = await this.makeBorrowIx(
borrowAmount,
borrowBankAddress,
Expand All @@ -706,13 +707,15 @@ class MarginfiAccountWrapper {
wrapAndUnwrapSol: false,
}
);
console.log("depositAmount", depositAmount.toString());
const depositIxs = await this.makeDepositIx(
depositAmount,
depositBankAddress,
depositOpts ?? {
wrapAndUnwrapSol: false,
}
);

const { instructions: updateFeedIxs, luts: feedLuts } = await this.makeUpdateFeedIx([
depositBankAddress,
borrowBankAddress,
Expand Down
22 changes: 20 additions & 2 deletions packages/mrgn-utils/src/actions/flashloans/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,23 @@ export async function calculateLoopingParams({
additionalTxs: [],
};

console.log("===========================================");
console.log("========== Flashloan Loop Params =========");
console.log("===========================================");
console.log("Max Accounts:", maxAccounts);
console.log("Borrow Amount:", borrowAmount.toString());
console.log("Deposit Amount:", depositAmount.toString());
console.log("Borrow Amount Native:", borrowAmountNative);
console.log("Min Swap Amount Out (UI):", minSwapAmountOutUi);
console.log("Actual Deposit Amount (UI):", actualDepositAmountUi);
console.log("-------------------------------------------");
console.log("Quote Details:");
console.log("Input Mint:", loopingProps.borrowBank.info.state.mint.toBase58());
console.log("Output Mint:", loopingProps.depositBank.info.state.mint.toBase58());
console.log("Slippage (bps):", slippageBps);
console.log("Platform Fee (bps):", platformFeeBps);
console.log("===========================================");

if (loopingProps.marginfiAccount) {
txn = await verifyTxSizeLooping({
...loopingProps,
Expand Down Expand Up @@ -335,7 +352,8 @@ export async function calculateLoopingTransaction(props: LoopingProps): Promise<
*/
export async function loopingBuilder({
marginfiAccount,
depositAmount,
// depositAmount,
actualDepositAmount,
borrowAmount,
depositBank,
borrowBank,
Expand Down Expand Up @@ -378,7 +396,7 @@ export async function loopingBuilder({
const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash("confirmed");

const { flashloanTx, additionalTxs, txOverflown } = await marginfiAccount.makeLoopTxV2({
depositAmount,
depositAmount: actualDepositAmount,
borrowAmount,
depositBankAddress: depositBank.address,
borrowBankAddress: borrowBank.address,
Expand Down
14 changes: 13 additions & 1 deletion packages/mrgn-utils/src/actions/flashloans/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export function getLoopingParamsForAccount(
amount: number,
slippageBps: number
) {
const principalBufferAmountUi = amount * targetLeverage * (slippageBps / 10000);
const principalBufferAmountUi = amount * targetLeverage * ((slippageBps + 30) / 10000);
const adjustedPrincipalAmountUi = amount - principalBufferAmountUi;

const { borrowAmount, totalDepositAmount: depositAmount } = marginfiAccount.computeLoopingParams(
Expand All @@ -295,5 +295,17 @@ export function getLoopingParamsForAccount(

const borrowAmountNative = uiToNative(borrowAmount, borrowBank.info.state.mintDecimals).toNumber();

console.log("===========================================");
console.log("========== Looping Params Result =========");
console.log("===========================================");
console.log("Target Leverage:", targetLeverage);
console.log("Amount:", amount);
console.log("Slippage (bps):", slippageBps);
console.log("-------------------------------------------");
console.log("Final borrowAmount:", borrowAmount.toString());
console.log("Final depositAmount:", depositAmount.toString());
console.log("Final borrowAmountNative:", borrowAmountNative);
console.log("===========================================");

return { borrowAmount, depositAmount, borrowAmountNative };
}

0 comments on commit 6a93b96

Please sign in to comment.