@symmetry-hq/baskets-v2-sdk
Version:
Symmetry Baskets V2 SDK
67 lines (66 loc) • 3.54 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.claimTokensIxs = claimTokensIxs;
const web3_js_1 = require("@solana/web3.js");
const spl_token_1 = require("@solana/spl-token");
const programAccounts_1 = require("../../utils/programAccounts");
const withdrawState_1 = require("../../state/withdrawState");
function claimTokensIxs(params) {
return __awaiter(this, void 0, void 0, function* () {
// Destructure params and fetch state data
const { program, payer, withdrawState } = params;
const withdrawStateData = yield (0, withdrawState_1.fetchWithdrawState)(program, withdrawState);
// Get key data from states
const withdrawStateSeed = withdrawStateData.withdrawStateSeed;
const basket = withdrawStateData.basket;
const basketPda = (0, programAccounts_1.getBasketPda)(basket);
const user = withdrawStateData.owner;
const stateCreator = (0, programAccounts_1.getStateCreatorAccount)();
// Build list of token mints to claim
const tokenMints = [];
// Add mints with non-zero amounts
for (let i = 0; i < parseInt(withdrawStateData.numTokens.toString()); i++) {
const amount = parseInt(withdrawStateData.compositionAmounts[i].toString());
if (amount > 0) {
tokenMints.push(withdrawStateData.compositionMints[i]);
}
}
// Add destination mint if not already included and valid
const destinationAmount = parseInt(withdrawStateData.destinationAmount.toString());
const isDestinationMintIncluded = tokenMints.find(mint => mint.equals(withdrawStateData.destinationMint));
if (!isDestinationMintIncluded && destinationAmount !== 0) {
tokenMints.push(withdrawStateData.destinationMint);
}
// Get token accounts for basket and user
const basketTokenAccounts = tokenMints.map(mint => (0, programAccounts_1.getAta)(basketPda, mint));
const userTokenAccounts = tokenMints.map(mint => (0, programAccounts_1.getAta)(user, mint));
// Build claim instructions for each token
const ixs = yield Promise.all(tokenMints.map((mint, index) => program.methods
.withdrawClaim(withdrawStateSeed)
.accountsStrict({
worker: payer,
basket,
basketPda,
withdrawState,
tokenMint: mint,
basketTokenAccount: basketTokenAccounts[index],
user,
userTokenAccount: userTokenAccounts[index],
stateCreator,
systemProgram: web3_js_1.SystemProgram.programId,
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
})
.instruction()));
return ixs;
});
}