UNPKG

fbonds-core

Version:

Banx protocol sdk

136 lines (135 loc) • 7.5 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.flashRepayReserveLiquidityInstruction = exports.SOLEND_PRODUCTION_PROGRAM_ID = exports.flashBorrowReserveLiquidityInstruction = void 0; const anchor_1 = require("@coral-xyz/anchor"); const spl_token_1 = require("@solana/spl-token"); const BufferLayout = __importStar(require("buffer-layout")); var LendingInstruction; (function (LendingInstruction) { LendingInstruction[LendingInstruction["InitLendingMarket"] = 0] = "InitLendingMarket"; LendingInstruction[LendingInstruction["SetLendingMarketOwner"] = 1] = "SetLendingMarketOwner"; LendingInstruction[LendingInstruction["InitReserve"] = 2] = "InitReserve"; LendingInstruction[LendingInstruction["RefreshReserve"] = 3] = "RefreshReserve"; LendingInstruction[LendingInstruction["DepositReserveLiquidity"] = 4] = "DepositReserveLiquidity"; LendingInstruction[LendingInstruction["RedeemReserveCollateral"] = 5] = "RedeemReserveCollateral"; LendingInstruction[LendingInstruction["InitObligation"] = 6] = "InitObligation"; LendingInstruction[LendingInstruction["RefreshObligation"] = 7] = "RefreshObligation"; LendingInstruction[LendingInstruction["DepositObligationCollateral"] = 8] = "DepositObligationCollateral"; LendingInstruction[LendingInstruction["WithdrawObligationCollateral"] = 9] = "WithdrawObligationCollateral"; LendingInstruction[LendingInstruction["BorrowObligationLiquidity"] = 10] = "BorrowObligationLiquidity"; LendingInstruction[LendingInstruction["RepayObligationLiquidity"] = 11] = "RepayObligationLiquidity"; LendingInstruction[LendingInstruction["LiquidateObligation"] = 12] = "LiquidateObligation"; LendingInstruction[LendingInstruction["FlashLoan"] = 13] = "FlashLoan"; LendingInstruction[LendingInstruction["DepositReserveLiquidityAndObligationCollateral"] = 14] = "DepositReserveLiquidityAndObligationCollateral"; LendingInstruction[LendingInstruction["WithdrawObligationCollateralAndRedeemReserveLiquidity"] = 15] = "WithdrawObligationCollateralAndRedeemReserveLiquidity"; LendingInstruction[LendingInstruction["UpdateReserveConfig"] = 16] = "UpdateReserveConfig"; LendingInstruction[LendingInstruction["FlashBorrowReserveLiquidity"] = 19] = "FlashBorrowReserveLiquidity"; LendingInstruction[LendingInstruction["FlashRepayReserveLiquidity"] = 20] = "FlashRepayReserveLiquidity"; })(LendingInstruction || (LendingInstruction = {})); /** * Layout for a 64bit unsigned value */ const uint64 = (property = "uint64") => { const layout = BufferLayout.blob(8, property); const _decode = layout.decode.bind(layout); const _encode = layout.encode.bind(layout); layout.decode = (buffer, offset) => { const data = _decode(buffer, offset); return new anchor_1.BN([...data] .reverse() .map((i) => `00${i.toString(16)}`.slice(-2)) .join(""), 16); }; layout.encode = (num, buffer, offset) => { const a = num.toArray().reverse(); let b = Buffer.from(a); if (b.length !== 8) { const zeroPad = Buffer.alloc(8); b.copy(zeroPad); b = zeroPad; } return _encode(b, buffer, offset); }; return layout; }; const flashBorrowReserveLiquidityInstruction = (liquidityAmount, sourceLiquidity, destinationLiquidity, reserve, lendingMarket) => { const dataLayout = BufferLayout.struct([ BufferLayout.u8("instruction"), uint64("liquidityAmount"), ]); const data = Buffer.alloc(dataLayout.span); dataLayout.encode({ instruction: LendingInstruction.FlashBorrowReserveLiquidity, liquidityAmount: new anchor_1.BN(liquidityAmount), }, data); const [lendingMarketAuthority, _] = anchor_1.web3.PublicKey.findProgramAddressSync([lendingMarket.toBytes()], exports.SOLEND_PRODUCTION_PROGRAM_ID); const keys = [ { pubkey: sourceLiquidity, isSigner: false, isWritable: true }, { pubkey: destinationLiquidity, isSigner: false, isWritable: true }, { pubkey: reserve, isSigner: false, isWritable: true }, { pubkey: lendingMarket, isSigner: false, isWritable: false }, { pubkey: lendingMarketAuthority, isSigner: false, isWritable: false }, { pubkey: anchor_1.web3.SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false }, { pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, ]; return new anchor_1.web3.TransactionInstruction({ keys, programId: exports.SOLEND_PRODUCTION_PROGRAM_ID, data, }); }; exports.flashBorrowReserveLiquidityInstruction = flashBorrowReserveLiquidityInstruction; exports.SOLEND_PRODUCTION_PROGRAM_ID = new anchor_1.web3.PublicKey("So1endDq2YkqhipRh3WViPa8hdiSpxWy6z3Z6tMCpAo"); const flashRepayReserveLiquidityInstruction = (liquidityAmount, borrowInstructionIndex, sourceLiquidity, destinationLiquidity, reserveLiquidityFeeReceiver, hostFeeReceiver, reserve, lendingMarket, userTransferAuthority) => { const dataLayout = BufferLayout.struct([ BufferLayout.u8("instruction"), uint64("liquidityAmount"), BufferLayout.u8("borrowInstructionIndex"), ]); const data = Buffer.alloc(dataLayout.span); dataLayout.encode({ instruction: LendingInstruction.FlashRepayReserveLiquidity, liquidityAmount: new anchor_1.BN(liquidityAmount), borrowInstructionIndex: borrowInstructionIndex, }, data); const keys = [ { pubkey: sourceLiquidity, isSigner: false, isWritable: true }, { pubkey: destinationLiquidity, isSigner: false, isWritable: true }, { pubkey: reserveLiquidityFeeReceiver, isSigner: false, isWritable: true }, { pubkey: hostFeeReceiver, isSigner: false, isWritable: true }, { pubkey: reserve, isSigner: false, isWritable: false }, { pubkey: lendingMarket, isSigner: false, isWritable: false }, { pubkey: userTransferAuthority, isSigner: true, isWritable: false }, { pubkey: anchor_1.web3.SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false }, { pubkey: spl_token_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, ]; return new anchor_1.web3.TransactionInstruction({ keys, programId: exports.SOLEND_PRODUCTION_PROGRAM_ID, data, }); }; exports.flashRepayReserveLiquidityInstruction = flashRepayReserveLiquidityInstruction;