fbonds-core
Version:
Banx protocol sdk
111 lines (110 loc) • 6.11 kB
JavaScript
"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.stakeBanxToken = void 0;
const common_1 = require("../../../../common");
const constants_1 = require("../../../constants");
const helpers_1 = require("../../../helpers");
const banxAdventure_1 = require("../banxAdventure");
const anchor_1 = require("@coral-xyz/anchor");
const token_1 = require("@coral-xyz/anchor/dist/cjs/utils/token");
const spl_token_1 = require("@solana/spl-token");
const stakeBanxToken = (_a) => __awaiter(void 0, [_a], void 0, function* ({ programId, connection, accounts, args, sendTxn }) {
const program = (0, helpers_1.returnAnchorProgram)(connection);
const instructions = [];
const managedAdventures = [];
const managedSubscriptions = [];
const [banxStakingSettings] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_STAKING_SETTINGS_PREFIX)], program.programId);
const [banxStakingVault] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_STAKING_VAULT_PREFIX)], program.programId);
const [banxTokenStake] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_TOKEN_STAKE_PREFIX), accounts.userPubkey.toBuffer()], program.programId);
const userTokenAccount = yield (0, common_1.findAssociatedTokenAddress)(accounts.userPubkey, accounts.tokenMint);
const banxStakingVaultTokenAccount = yield (0, common_1.findAssociatedTokenAddress)(banxStakingVault, accounts.tokenMint);
const [banxAdventureSubscription] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_ADVENTURE_SUBSCRIPTION_PREFIX), accounts.userPubkey.toBuffer()], program.programId);
const [rentVault, rentVaultBump] = yield anchor_1.web3.PublicKey.findProgramAddress([
constants_1.ENCODER.encode(constants_1.RENT_VAULT),
], program.programId);
if (args.createStakeAccount) {
instructions.push(yield program.methods
.initializeBanxStakeAccounts()
.accountsStrict({
user: accounts.userPubkey,
systemProgram: anchor_1.web3.SystemProgram.programId,
rent: anchor_1.web3.SYSVAR_RENT_PUBKEY,
rentVault,
banxTokenStake,
banxAdventureSubscription,
banxStakeSettings: banxStakingSettings,
})
.remainingAccounts([])
.instruction());
}
instructions.push(yield program.methods
.stakeBanxToken(new anchor_1.BN(args.tokensToStake))
.accountsStrict({
banxStakingSettings,
banxTokenStake: banxTokenStake,
user: accounts.userPubkey,
systemProgram: anchor_1.web3.SystemProgram.programId,
rent: anchor_1.web3.SYSVAR_RENT_PUBKEY,
tokenMint: accounts.tokenMint,
userTokenAccount,
banxStakingVault,
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
associatedTokenProgram: token_1.ASSOCIATED_PROGRAM_ID,
banxStakingVaultTokenAccount,
banxAdventureSubscription: banxAdventureSubscription
})
.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, helpers_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,
advenureSubscriptions: banxAdventureSubscription,
adventureCurrentWeek: adventure,
adventureNextWeek: adventureNext,
},
};
});
exports.stakeBanxToken = stakeBanxToken;