fbonds-core
Version:
Banx protocol sdk
144 lines (143 loc) • 7.8 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.stakeBanxNft = 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 mpl_token_metadata_1 = require("@metaplex-foundation/mpl-token-metadata");
const spl_token_1 = require("@solana/spl-token");
const stakeBanxNft = (_a) => __awaiter(void 0, [_a], void 0, function* ({ args, 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 [banxTokenStake] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_TOKEN_STAKE_PREFIX), accounts.userPubkey.toBuffer()], program.programId);
const banxStake = anchor_1.web3.Keypair.generate();
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 [banxPointsMap] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_POINTS_MAP_PREFIX), accounts.tokenMint.toBuffer()], program.programId);
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 tokenRecordData = meta.tokenStandard === mpl_token_metadata_1.TokenStandard.ProgrammableNonFungible
? yield mpl_token_metadata_1.TokenRecord.fromAccountAddress(connection, ownerTokenRecord)
: { delegate: null };
const delegatePubkey = tokenRecordData.delegate;
const remainingAccountsPnft = [
{
pubkey: ruleSet || constants_1.METADATA_PROGRAM_PUBKEY,
isSigner: false,
isWritable: false,
},
{
pubkey: delegatePubkey || constants_1.METADATA_PROGRAM_PUBKEY,
isSigner: false,
isWritable: false,
},
];
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
.stakeBanxNft()
.accountsStrict({
banxStakingSettings,
banxTokenStake: banxTokenStake,
user: accounts.userPubkey,
systemProgram: anchor_1.web3.SystemProgram.programId,
rent: anchor_1.web3.SYSVAR_RENT_PUBKEY,
instructions: anchor_1.web3.SYSVAR_INSTRUCTIONS_PUBKEY,
authorizationRulesProgram: constants_1.AUTHORIZATION_RULES_PROGRAM,
metadataProgram: constants_1.METADATA_PROGRAM_PUBKEY,
edition: editionInfo,
banxStake: banxStake.publicKey,
banxPointsMap,
tokenMint: accounts.tokenMint,
userTokenAccount,
hadoRegistry: accounts.hadoRegistry,
whitelistEntry: accounts.whitelistEntry,
mutualBondTradeTxnVault,
metadata: nftMetadata,
ownerTokenRecord,
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
associatedTokenProgram: token_1.ASSOCIATED_PROGRAM_ID,
banxAdventureSubscription
})
.remainingAccounts([...remainingAccountsPnft,])
.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 = [banxStake];
yield sendTxn(transaction, signers);
return {
instructions,
signers,
accounts: {
banxStakingSettings: banxStakingSettings,
banxTokenStake: banxTokenStake,
banxStake: banxStake.publicKey,
advenureSubscriptions: banxAdventureSubscription,
adventureCurrentWeek: adventure,
adventureNextWeek: adventureNext,
},
};
});
exports.stakeBanxNft = stakeBanxNft;