banx-vaults-sdk
Version:
Fbonds SDK for interacting with protocol banx vaults
54 lines (53 loc) • 2.85 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.withdrawLiquidityFromPool = void 0;
const anchor_1 = require("@coral-xyz/anchor");
const token_1 = require("@coral-xyz/anchor/dist/cjs/utils/token");
const common_1 = require("../../../common");
const helpers_1 = require("../../helpers");
const constants_1 = require("../../constants");
const withdrawLiquidityFromPool = ({ connection, args, accounts, sendTxn, }) => __awaiter(void 0, void 0, void 0, function* () {
const program = (0, helpers_1.returnAnchorProgram)(connection);
const instructions = [];
const userTokenAccount = yield (0, common_1.findAssociatedTokenAddress)(accounts.userPubkey, accounts.lendingToken);
const banxPoolTokenAccount = yield (0, common_1.findAssociatedTokenAddress)(accounts.banxPool, accounts.lendingToken);
const [banxPoolUserDeposit] = anchor_1.web3.PublicKey.findProgramAddressSync([constants_1.ENCODER.encode(constants_1.BANX_USER_DEPOSIT_PREFIX), accounts.banxPool.toBuffer(), accounts.userPubkey.toBuffer()], program.programId);
instructions.push(yield program.methods
.withdrawLiquidityFromPool(args.amountToWithdraw)
.accountsStrict({
user: accounts.userPubkey,
systemProgram: anchor_1.web3.SystemProgram.programId,
rent: anchor_1.web3.SYSVAR_RENT_PUBKEY,
tokenProgram: token_1.TOKEN_PROGRAM_ID,
associatedTokenProgram: token_1.ASSOCIATED_PROGRAM_ID,
banxPool: accounts.banxPool,
banxPoolTokenAccount: banxPoolTokenAccount,
userTokenAccount: userTokenAccount,
poolLendingToken: accounts.lendingToken,
banxPoolUserDeposit: banxPoolUserDeposit
})
.instruction());
const transaction = new anchor_1.web3.Transaction();
for (let instruction of instructions)
transaction.add(instruction);
const signers = [];
yield sendTxn(transaction, signers);
return {
instructions,
signers,
accounts: {
banxPool: accounts.banxPool,
banxPoolUserDeposit: banxPoolUserDeposit,
}
};
});
exports.withdrawLiquidityFromPool = withdrawLiquidityFromPool;