fbonds-core
Version:
Banx protocol sdk
143 lines (142 loc) • 8.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHistoryAdventureStakeUserInfo = exports.getAdventureStakeUserInfo = exports.AdventureStakeStatus = exports.optimisticInitializeBanxStake = exports.optimisticInitializeBanxTokenStake = exports.calculatePlayerPointsForTokens = exports.calculateTokensForPlayerPoints = exports.calculateAdventureRewards = exports.calculateRewardsFromSubscriptions = void 0;
const lodash_1 = require("lodash");
const constants_1 = require("../../../constants");
const types_1 = require("../../../types");
const perpetual_1 = require("../../perpetual");
const anchor_1 = require("@coral-xyz/anchor");
const anchor_2 = require("@coral-xyz/anchor");
const banxAdventure_1 = require("../banxAdventure");
const calculateRewardsFromSubscriptions = (args) => {
let reward = new anchor_2.BN(0);
args.map((subscription) => {
const totalTokensInSubscription = subscription.subscriptuionStakeTokensAmount.add(subscription.subscriptionStakePartnerPointsAmount.mul(subscription.adventureTokensPerPoints));
const totalTokensInAdventure = subscription.adventureTotalTokensStaked.add(subscription.adventureTotalPartnerPoints.mul(subscription.adventureTokensPerPoints));
if (totalTokensInAdventure.isZero() || subscription.adventureRewardsToBeDistributed.isZero()) {
return;
}
const tokensToClaim = totalTokensInSubscription
.mul(subscription.adventureRewardsToBeDistributed.mul(new anchor_2.BN(constants_1.BANX_TOKEN_DECIMALS)).div(totalTokensInAdventure))
.div(new anchor_2.BN(constants_1.BANX_TOKEN_DECIMALS));
reward = reward.add(tokensToClaim);
});
return reward;
};
exports.calculateRewardsFromSubscriptions = calculateRewardsFromSubscriptions;
const calculateAdventureRewards = (params) => {
if (!params.adventure.length)
return constants_1.ZERO_BN;
const currentWeekAdventure = (0, banxAdventure_1.banxAdventureTimestampToWeeks)((0, perpetual_1.nowInSeconds)());
const calculateRewardsParams = (0, lodash_1.chain)(params.adventure)
.filter(adventure => adventure.periodEndingAt.lt(new anchor_2.BN((0, perpetual_1.nowInSeconds)())) && (!adventure.totalTokensStaked.isZero() || !adventure.totalBanxSubscribed.isZero()) && adventure.week.gt(params.subscription.lastClaimedWeek))
.map(adventure => {
const check_current_week = params.subscription.lastClaimedWeek.add(new anchor_2.BN(1)).eq(adventure.week);
return {
subscriptuionStakeTokensAmount: check_current_week ? params.subscription.stakeTokensAmount : params.subscription.stakeTokensAmount.add(params.subscription.nextEpochTokens),
subscriptionStakePartnerPointsAmount: check_current_week ? params.subscription.stakePartnerPointsAmount : params.subscription.stakePartnerPointsAmount.add(params.subscription.stakePartnerPointsAmount),
adventureTotalPartnerPoints: new anchor_2.BN(adventure.totalPartnerPoints),
adventureTokensPerPoints: adventure.tokensPerPoints,
adventureTotalTokensStaked: adventure.totalTokensStaked,
adventureRewardsToBeDistributed: adventure.rewardsToBeDistributed.mul(new anchor_2.BN(constants_1.BANX_TOKEN_DECIMALS)),
};
})
.value();
const amount = (0, exports.calculateRewardsFromSubscriptions)(calculateRewardsParams);
return amount;
};
exports.calculateAdventureRewards = calculateAdventureRewards;
const calculateTokensForPlayerPoints = (playerPoints) => {
return playerPoints * constants_1.TOKENS_PER_PLAYER_POINT * constants_1.BANX_TOKEN_DECIMALS;
};
exports.calculateTokensForPlayerPoints = calculateTokensForPlayerPoints;
const calculatePlayerPointsForTokens = (tokens) => {
return Number(tokens.div(new anchor_2.BN(constants_1.BANX_TOKEN_DECIMALS))) / constants_1.TOKENS_PER_PLAYER_POINT;
};
exports.calculatePlayerPointsForTokens = calculatePlayerPointsForTokens;
const optimisticInitializeBanxTokenStake = (args) => {
const [banxTokenStake] = anchor_1.web3.PublicKey.findProgramAddressSync([constants_1.ENCODER.encode(constants_1.BANX_TOKEN_STAKE_PREFIX), args.user.toBuffer()], new anchor_1.web3.PublicKey(constants_1.BONDS_PROGRAM_PUBKEY));
return {
banxTokensStake: {
banxStakeState: types_1.BanxTokenStakeState.None,
user: args.user,
publicKey: banxTokenStake,
adventureSubscriptionsQuantity: new anchor_2.BN(0),
tokensStaked: new anchor_2.BN(0),
partnerPointsStaked: new anchor_2.BN(0),
playerPointsStaked: new anchor_2.BN(0),
banxNftsStakedQuantity: new anchor_2.BN(0),
stakedAt: new anchor_2.BN(0),
unstakedAt: new anchor_2.BN(0),
farmedAmount: new anchor_2.BN(0),
nftsStakedAt: new anchor_2.BN(0),
nftsUnstakedAt: new anchor_2.BN(0),
},
};
};
exports.optimisticInitializeBanxTokenStake = optimisticInitializeBanxTokenStake;
const optimisticInitializeBanxStake = (args) => {
return {
banxStake: {
banxStakeState: types_1.BanxStakeState.Staked,
adventureSubscriptionsQuantity: new anchor_2.BN(0),
nftMint: args.nftMint,
collateralTokenAccount: args.collateralTokenAccount,
user: args.userKey,
stakedAt: new anchor_2.BN((0, perpetual_1.nowInSeconds)()),
unstakedOrLiquidatedAt: new anchor_2.BN(0),
isLoaned: false,
bond: constants_1.EMPTY_PUBKEY,
playerPoints: args.banxPointsMap.playerPoints,
partnerPoints: args.banxPointsMap.partnerPoints,
farmedAmount: new anchor_2.BN(0),
placeholderOne: constants_1.EMPTY_PUBKEY,
publicKey: args.banxStakeKey,
},
};
};
exports.optimisticInitializeBanxStake = optimisticInitializeBanxStake;
var AdventureStakeStatus;
(function (AdventureStakeStatus) {
AdventureStakeStatus[AdventureStakeStatus["None"] = 0] = "None";
AdventureStakeStatus[AdventureStakeStatus["Active"] = 1] = "Active";
AdventureStakeStatus[AdventureStakeStatus["Upcoming"] = 2] = "Upcoming";
AdventureStakeStatus[AdventureStakeStatus["Claimed"] = 3] = "Claimed";
})(AdventureStakeStatus || (exports.AdventureStakeStatus = AdventureStakeStatus = {}));
const getAdventureStakeUserInfo = (args) => {
if (args.subscription.lastClaimedWeek.gt(args.adventure.week)) {
throw new Error('Subscription has already claimed rewards for this adventure');
}
else if (args.subscription.lastClaimedWeek.add(new anchor_2.BN(1)).eq(args.adventure.week)) {
const tokensPartnerPoints = args.subscription.stakeTokensAmount.div(args.adventure.tokensPerPoints);
return {
partnerPointsStaked: args.subscription.stakePartnerPointsAmount.add(tokensPartnerPoints),
tokensStaked: args.subscription.stakeTokensAmount,
nftsStaked: args.subscription.stakeNftAmount,
rewardsClaimed: new anchor_2.BN(0),
adventureStaus: AdventureStakeStatus.Active,
};
}
else {
const tokensPartnerPoints = args.subscription.stakeTokensAmount.add(args.subscription.nextEpochTokens).div(args.adventure.tokensPerPoints);
return {
partnerPointsStaked: args.subscription.stakePartnerPointsAmount.add(args.subscription.nextEpochPartnerPoints).add(tokensPartnerPoints),
tokensStaked: args.subscription.stakeTokensAmount.add(args.subscription.nextEpochTokens),
nftsStaked: args.subscription.stakeNftAmount.add(args.subscription.nextEpochNfts),
rewardsClaimed: new anchor_2.BN(0),
adventureStaus: AdventureStakeStatus.Upcoming,
};
}
};
exports.getAdventureStakeUserInfo = getAdventureStakeUserInfo;
const getHistoryAdventureStakeUserInfo = (args) => {
const tokensPartnerPoints = args.snapshot.stakeTokensAmount.div(args.adventure.tokensPerPoints);
return {
partnerPointsStaked: args.snapshot.stakePartnerPointsAmount.add(tokensPartnerPoints),
tokensStaked: args.snapshot.stakeTokensAmount,
nftsStaked: args.snapshot.stakeNftAmount,
rewardsClaimed: args.snapshot.claimedRewards,
adventureStaus: AdventureStakeStatus.Claimed,
};
};
exports.getHistoryAdventureStakeUserInfo = getHistoryAdventureStakeUserInfo;