UNPKG

@symmetry-hq/baskets-v2-sdk

Version:

Symmetry Baskets V2 SDK

59 lines (58 loc) 3.13 kB
"use strict"; 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.sellWithdrawBeforeRebalanceIx = sellWithdrawBeforeRebalanceIx; // Core dependencies const anchor_1 = require("@coral-xyz/anchor"); const web3_js_1 = require("@solana/web3.js"); const spl_token_1 = require("@solana/spl-token"); const programAccounts_1 = require("../../utils/programAccounts"); function sellWithdrawBeforeRebalanceIx(params) { return __awaiter(this, void 0, void 0, function* () { // Destructure params const { program, withdrawStateData, payer, fromTokenMint } = params; const fromTokenIndex = withdrawStateData.compositionMints.findIndex(mint => mint.toBase58() === fromTokenMint.toBase58()); const amountToWithdraw = parseInt(withdrawStateData.compositionAmounts[fromTokenIndex].toString()); const toTokenMint = withdrawStateData.destinationMint; // Get withdraw state data and seeds const withdrawStateSeed = withdrawStateData.withdrawStateSeed; const basket = withdrawStateData.basket; // Get basket pda const basketPda = (0, programAccounts_1.getBasketPda)(basket); // Get token accounts const basketFromTokenAccount = (0, programAccounts_1.getAta)(basketPda, fromTokenMint); const workerFromTokenAccount = (0, programAccounts_1.getAta)(payer, fromTokenMint); const basketToTokenAccount = (0, programAccounts_1.getAta)(basketPda, toTokenMint); const workerToTokenAccount = (0, programAccounts_1.getAta)(payer, toTokenMint); // Get rebalance state const rebalanceState = (0, programAccounts_1.getRebalanceStateAccount)(); // Build and return instruction return yield program.methods .sellWithdrawBeforeRebalance(withdrawStateSeed, new anchor_1.BN(amountToWithdraw)) .accountsStrict({ worker: payer, basket, basketPda, withdrawState: withdrawStateData.ownAddress, fromTokenMint, basketFromTokenAccount, workerFromTokenAccount, toTokenMint, basketToTokenAccount, workerToTokenAccount, rebalanceState, instructionsSysvar: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY, systemProgram: web3_js_1.SystemProgram.programId, tokenProgram: spl_token_1.TOKEN_PROGRAM_ID, }) .instruction(); }); }