@frakt-protocol/frakt-sdk
Version:
Frakt SDK for interacting with frakt.xyz protocols
52 lines (51 loc) • 2.63 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.getLotTicket = void 0;
const anchor_1 = require("@project-serum/anchor");
const common_1 = require("../../../common");
const helpers_1 = require("../../helpers");
const getLotTicket = ({ programId, connection, user, liquidationLot, attemptsNftMint, sendTxn, }) => __awaiter(void 0, void 0, void 0, function* () {
const encoder = new TextEncoder();
let program = (0, helpers_1.returnAnchorProgram)(programId, connection);
const lotTicket = anchor_1.web3.Keypair.generate();
const [nftAttempts, nftAttemptsBump] = yield anchor_1.web3.PublicKey.findProgramAddress([encoder.encode('nftattempts'), programId.toBuffer(), attemptsNftMint.toBuffer()], program.programId);
const instructions = [];
if (!(yield connection.getAccountInfo(nftAttempts, 'confirmed'))) {
instructions.push(program.instruction.initializeNftAttempts({
accounts: {
nftAttempts,
user,
nftMint: attemptsNftMint,
rent: anchor_1.web3.SYSVAR_RENT_PUBKEY,
systemProgram: anchor_1.web3.SystemProgram.programId,
},
}));
}
const nftUserTokenAccount = yield (0, common_1.findAssociatedTokenAddress)(user, attemptsNftMint);
instructions.push(program.instruction.getLotTicket(nftAttemptsBump, {
accounts: {
liquidationLot,
nftAttempts,
user: user,
lotTicket: lotTicket.publicKey,
attemptsNftMint: attemptsNftMint,
systemProgram: anchor_1.web3.SystemProgram.programId,
nftUserTokenAccount,
},
}));
const transaction = new anchor_1.web3.Transaction();
for (let instruction of instructions)
transaction.add(instruction);
yield sendTxn(transaction, [lotTicket]);
return lotTicket.publicKey;
});
exports.getLotTicket = getLotTicket;