UNPKG

fbonds-core

Version:

Banx protocol sdk

108 lines (107 loc) 6.26 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.unstakeBanxNft = void 0; const constants_1 = require("../../../constants"); const helpers_1 = require("../../../helpers"); const mpl_token_metadata_1 = require("@metaplex-foundation/mpl-token-metadata"); const common_1 = require("../../../../common"); const spl_token_1 = require("@solana/spl-token"); const banxAdventure_1 = require("../banxAdventure"); const perpetual_1 = require("../../perpetual"); const anchor_1 = require("@coral-xyz/anchor"); const token_1 = require("@coral-xyz/anchor/dist/cjs/utils/token"); const unstakeBanxNft = (_a) => __awaiter(void 0, [_a], void 0, function* ({ programId, connection, accounts, sendTxn, }) { var _b; const program = (0, helpers_1.returnAnchorProgram)(connection); const instructions = []; const [banxStakingSettings] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_STAKING_SETTINGS_PREFIX)], program.programId); const [mutualBondTradeTxnVault] = anchor_1.web3.PublicKey.findProgramAddressSync([constants_1.ENCODER.encode(constants_1.MUTUAL_BOND_TRADE_TXN_VAULT)], program.programId); const [banxAdventureSubscription] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_ADVENTURE_SUBSCRIPTION_PREFIX), accounts.userPubkey.toBuffer()], program.programId); const userTokenAccount = yield (0, common_1.findAssociatedTokenAddress)(accounts.userPubkey, accounts.tokenMint); const editionInfo = (0, helpers_1.getMetaplexEditionPda)(accounts.tokenMint); const nftMetadata = (0, helpers_1.getMetaplexMetadata)(accounts.tokenMint); const ownerTokenRecord = (0, helpers_1.findTokenRecordPda)(accounts.tokenMint, userTokenAccount); const destTokenRecord = (0, helpers_1.findTokenRecordPda)(accounts.tokenMint, userTokenAccount); const meta = yield mpl_token_metadata_1.Metadata.fromAccountAddress(connection, nftMetadata); const ruleSet = (_b = meta.programmableConfig) === null || _b === void 0 ? void 0 : _b.ruleSet; const [banxTokenStake] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_TOKEN_STAKE_PREFIX), accounts.userPubkey.toBuffer()], program.programId); instructions.push(yield program.methods .unstakeBanxNft() .accountsStrict({ banxStakingSettings, user: accounts.userPubkey, systemProgram: anchor_1.web3.SystemProgram.programId, rent: anchor_1.web3.SYSVAR_RENT_PUBKEY, tokenMint: accounts.tokenMint, userTokenAccount, mutualBondTradeTxnVault, tokenProgram: spl_token_1.TOKEN_PROGRAM_ID, associatedTokenProgram: token_1.ASSOCIATED_PROGRAM_ID, instructions: anchor_1.web3.SYSVAR_INSTRUCTIONS_PUBKEY, banxStake: accounts.banxStake, metadataProgram: constants_1.METADATA_PROGRAM_PUBKEY, editionInfo, nftMetadata, authorizationRulesProgram: constants_1.AUTHORIZATION_RULES_PROGRAM, authRules: ruleSet || constants_1.EMPTY_PUBKEY, ownerTokenRecord, destTokenRecord, banxAdventureSubscription: banxAdventureSubscription, banxTokenStake }) .instruction()); // TODO: This is a temporary solution to sync the adventure and subscription accounts, move to contract // neccecary to sync the adventure for get the correct adventure stats by end of the week const currentWeekAdventure = (0, banxAdventure_1.banxAdventureTimestampToWeeks)((0, perpetual_1.nowInSeconds)()); const [adventure] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_ADVENTURE_PREFIX), constants_1.ENCODER.encode(currentWeekAdventure.toString())], program.programId); instructions.push(yield program.methods .syncBanxAdventrure(new anchor_1.BN(currentWeekAdventure), new anchor_1.BN(0)) .accountsStrict({ banxAdventure: adventure, user: accounts.userPubkey, systemProgram: anchor_1.web3.SystemProgram.programId, rent: anchor_1.web3.SYSVAR_RENT_PUBKEY, banxStakingSettings: banxStakingSettings }) .remainingAccounts([]) .instruction()); const [adventureNext] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_ADVENTURE_PREFIX), constants_1.ENCODER.encode((currentWeekAdventure + 1).toString())], program.programId); instructions.push(yield program.methods .syncBanxAdventrure(new anchor_1.BN(currentWeekAdventure + 1), new anchor_1.BN(0)) .accountsStrict({ banxAdventure: adventureNext, user: accounts.userPubkey, systemProgram: anchor_1.web3.SystemProgram.programId, rent: anchor_1.web3.SYSVAR_RENT_PUBKEY, banxStakingSettings: banxStakingSettings }) .remainingAccounts([]) .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: { banxStakingSettings: banxStakingSettings, banxTokenStake: banxTokenStake, banxStake: accounts.banxStake, advenureSubscriptions: banxAdventureSubscription, adventureCurrentWeek: adventure, adventureNextWeek: adventureNext, }, }; }); exports.unstakeBanxNft = unstakeBanxNft;