UNPKG

fbonds-core

Version:

Banx protocol sdk

252 lines (251 loc) • 12.9 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.initializeBanxAdventureSubscription = exports.initializeBanxAdventure = exports.banxAdventureTimestampToWeeks = exports.weeksToBanxAdventureTimestamp = void 0; const anchor_1 = require("@coral-xyz/anchor"); const constants_1 = require("../../../constants"); const types_1 = require("../../../types"); const weeksToBanxAdventureTimestamp = (weeks) => { return constants_1.BANX_ADVENTURE_START + constants_1.BANX_ADVENTURE_DURATION * weeks; }; exports.weeksToBanxAdventureTimestamp = weeksToBanxAdventureTimestamp; const banxAdventureTimestampToWeeks = (adventureTimestamp) => Math.floor((adventureTimestamp - constants_1.BANX_ADVENTURE_START) / constants_1.BANX_ADVENTURE_DURATION); exports.banxAdventureTimestampToWeeks = banxAdventureTimestampToWeeks; // export const optimisticSubsribeToBanxAdventure = (args: { // banxTokensStake: BanxTokenStake; // adventure: BanxAdventure; // adventureSubscription: BanxAdventureSubscription; // isNftStake: boolean; // }): { // banxTokensStake: BanxTokenStake; // adventure: BanxAdventure; // adventureSubscription: BanxAdventureSubscription; // } => { // const difPartnerPoints = args.banxTokensStake.partnerPointsStaked - args.adventureSubscription.stakePartnerPointsAmount; // const difPlayerPoints = args.banxTokensStake.playerPointsStaked - args.adventureSubscription.stakePlayerPointsAmount; // const difTokens = args.banxTokensStake.tokensStaked - args.adventureSubscription.stakeTokensAmount; // const difBanx = // args.adventureSubscription.adventureSubscriptionState == BanxAdventureSubscriptionState.None // ? args.banxTokensStake.banxNftsStakedQuantity // : args.isNftStake // ? 1 // : 0; // return { // adventure: { // ...args.adventure, // adventureState: BanxAdventureState.Active, // totalBanxSubscribed: args.adventure.totalBanxSubscribed + difBanx, // totalPartnerPoints: args.adventure.totalPartnerPoints + difPartnerPoints, // totalPlayerPoints: args.adventure.totalPlayerPoints + difPlayerPoints, // totalTokensStaked: args.adventure.totalTokensStaked + difTokens, // }, // adventureSubscription: { // ...args.adventureSubscription, // stakePartnerPointsAmount: args.adventureSubscription.stakePartnerPointsAmount + difPartnerPoints, // stakePlayerPointsAmount: args.adventureSubscription.stakePlayerPointsAmount + difPlayerPoints, // stakeTokensAmount: args.adventureSubscription.stakeTokensAmount + difTokens, // stakeNftAmount: args.adventureSubscription.stakeNftAmount + difBanx, // subscribedAt: nowInSeconds(), // adventureSubscriptionState: BanxAdventureSubscriptionState.Active, // adventure: args.adventure.publicKey, // banxTokenStake: args.banxTokensStake.publicKey, // user: args.banxTokensStake.user, // }, // banxTokensStake: { // ...args.banxTokensStake, // adventureSubscriptionsQuantity: // args.adventureSubscription.adventureSubscriptionState == BanxAdventureSubscriptionState.None // ? args.banxTokensStake.adventureSubscriptionsQuantity + 1 // : args.banxTokensStake.adventureSubscriptionsQuantity, // }, // }; // }; const initializeBanxAdventure = (week, settings) => __awaiter(void 0, void 0, void 0, function* () { const timestamp = (0, exports.weeksToBanxAdventureTimestamp)(week); const [adventure] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_ADVENTURE_PREFIX), constants_1.ENCODER.encode(week.toString())], new anchor_1.web3.PublicKey(constants_1.BONDS_PROGRAM_PUBKEY)); return { adventureState: types_1.BanxAdventureState.None, week: new anchor_1.BN(week), periodEndingAt: new anchor_1.BN((timestamp + constants_1.BANX_ADVENTURE_DURATION)), periodStartedAt: new anchor_1.BN(timestamp), publicKey: adventure, rewardsToBeDistributed: settings.tokensPerWeek, tokensPerPoints: settings.tokensPerPartnerPoints, amountOfTokensHarvested: new anchor_1.BN(0), totalBanxSubscribed: new anchor_1.BN(0), totalPartnerPoints: new anchor_1.BN(0), totalPlayerPoints: new anchor_1.BN(0), totalTokensStaked: new anchor_1.BN(0), }; }); exports.initializeBanxAdventure = initializeBanxAdventure; const initializeBanxAdventureSubscription = (adventure, banxTokensStake, user) => __awaiter(void 0, void 0, void 0, function* () { const [adventureSubscription] = yield anchor_1.web3.PublicKey.findProgramAddress([constants_1.ENCODER.encode(constants_1.BANX_ADVENTURE_SUBSCRIPTION_PREFIX), user.toBuffer()], new anchor_1.web3.PublicKey(constants_1.BONDS_PROGRAM_PUBKEY)); const subscription = { adventureSubscriptionState: types_1.BanxAdventureSubscriptionState.None, user: user, banxTokenStake: banxTokensStake, publicKey: adventureSubscription, stakeTokensAmount: new anchor_1.BN(0), stakeNftAmount: new anchor_1.BN(0), stakePartnerPointsAmount: new anchor_1.BN(0), stakePlayerPointsAmount: new anchor_1.BN(0), subscribedAt: new anchor_1.BN(0), unsubscribedAt: new anchor_1.BN(0), harvestedAt: new anchor_1.BN(0), amountOfTokensHarvested: new anchor_1.BN(0), amountOfHadesTokensHarvested: new anchor_1.BN(0), lastClaimedWeek: new anchor_1.BN(0), nextEpochPlayerPoints: new anchor_1.BN(0), nextEpochPartnerPoints: new anchor_1.BN(0), nextEpochNfts: new anchor_1.BN(0), nextEpochTokens: new anchor_1.BN(0) }; return subscription; }); exports.initializeBanxAdventureSubscription = initializeBanxAdventureSubscription; // export const unsubscribeBanxAdventrueOptimistics = async ( // optimistic: BanxSubscribeAdventureOptimistic, // weeks: number[], // isNftUnstaked: boolean = false, // ): Promise<BanxSubscribeAdventureOptimistic> => { // let banxTokensStake = { ...optimistic.banxTokenStake }; // const newOptimistic = ( // await Promise.all( // weeks.map(async (week) => { // const timestamp = weeksToBanxAdventureTimestamp(week); // const adventureOptions = optimistic.banxAdventures.find( // (tempAdventure) => tempAdventure.adventure && tempAdventure.adventure.periodStartedAt == timestamp, // ); // const adventure = // adventureOptions && adventureOptions.adventure // ? adventureOptions.adventure // : await initializeBanxAdventure(week, optimistic.banxStakingSettings); // const adventureSubscription = // adventureOptions && adventureOptions.adventureSubscription // ? adventureOptions.adventureSubscription // : await initializeBanxAdventureSubscription( // new web3.PublicKey(adventure.publicKey), // new web3.PublicKey(banxTokensStake.publicKey), // new web3.PublicKey(banxTokensStake.user), // ); // const tempOptimistic = optimisticUnsubsribeFromBanxAdventure({ // banxTokensStake, // adventure, // adventureSubscription, // isNftUnstaked, // }); // banxTokensStake = tempOptimistic.banxTokensStake; // return { // adventure: tempOptimistic.adventure, // adventureSubscription: tempOptimistic.adventureSubscription, // }; // }), // ) // ).flat(); // const newSubscribeBanxAdventrueOptimistics: BanxSubscribeAdventureOptimistic = { // banxStakingSettings: optimistic.banxStakingSettings, // banxTokenStake: banxTokensStake, // banxAdventures: newOptimistic, // }; // return newSubscribeBanxAdventrueOptimistics; // }; // export const subscribeBanxAdventrueOptimistics = async ( // optimistic: BanxSubscribeAdventureOptimistic, // weeks: number[], // isNftStake: boolean = false, // ): Promise<BanxSubscribeAdventureOptimistic> => { // let banxTokensStake = { ...optimistic.banxTokenStake }; // const newOptimistic = ( // await Promise.all( // weeks.map(async (week) => { // const timestamp = weeksToBanxAdventureTimestamp(week); // const adventureOptions = optimistic.banxAdventures.find( // (tempAdventure) => tempAdventure.adventure && tempAdventure.adventure.periodStartedAt == timestamp, // ); // const adventure = // adventureOptions && adventureOptions.adventure // ? adventureOptions.adventure // : await initializeBanxAdventure(week, optimistic.banxStakingSettings); // const adventureSubscription = // adventureOptions && adventureOptions.adventureSubscription // ? adventureOptions.adventureSubscription // : await initializeBanxAdventureSubscription( // new web3.PublicKey(adventure.publicKey), // new web3.PublicKey(banxTokensStake.publicKey), // new web3.PublicKey(banxTokensStake.user), // ); // const tempOptimistic = optimisticSubsribeToBanxAdventure({ // banxTokensStake, // adventure, // adventureSubscription, // isNftStake, // }); // banxTokensStake = tempOptimistic.banxTokensStake; // return { // adventure: tempOptimistic.adventure, // adventureSubscription: tempOptimistic.adventureSubscription, // }; // }), // ) // ).flat(); // const newSubscribeBanxAdventrueOptimistics: BanxSubscribeAdventureOptimistic = { // banxStakingSettings: optimistic.banxStakingSettings, // banxTokenStake: banxTokensStake, // banxAdventures: newOptimistic, // }; // return newSubscribeBanxAdventrueOptimistics; // }; // export const optimisticUnsubsribeFromBanxAdventure = (args: { // banxTokensStake: BanxTokenStake; // adventure: BanxAdventure; // adventureSubscription: BanxAdventureSubscription; // isNftUnstaked: boolean; // }): { // banxTokensStake: BanxTokenStake; // adventure: BanxAdventure; // adventureSubscription: BanxAdventureSubscription; // } => { // const difPartnerPoints = args.adventureSubscription.stakePartnerPointsAmount - args.banxTokensStake.partnerPointsStaked; // const difPlayerPoints = args.adventureSubscription.stakePlayerPointsAmount - args.banxTokensStake.playerPointsStaked; // let difTokens = args.adventureSubscription.stakeTokensAmount - args.banxTokensStake.tokensStaked; // if (args.adventure.week == banxAdventureTimestampToWeeks(nowInSeconds()) && args.adventureSubscription.stakeTokensAmount < args.banxTokensStake.tokensStaked) { // difTokens = 0; // } // const difBanx = args.isNftUnstaked ? 1 : 0; // return { // adventure: { // ...args.adventure, // totalBanxSubscribed: args.adventure.totalBanxSubscribed - difBanx, // totalPartnerPoints: args.adventure.totalPartnerPoints - difPartnerPoints, // totalPlayerPoints: args.adventure.totalPlayerPoints - difPlayerPoints, // totalTokensStaked: args.adventure.totalTokensStaked - difTokens, // }, // adventureSubscription: { // ...args.adventureSubscription, // stakePartnerPointsAmount: args.adventureSubscription.stakePartnerPointsAmount - difPartnerPoints, // stakePlayerPointsAmount: args.adventureSubscription.stakePlayerPointsAmount - difPlayerPoints, // stakeTokensAmount: args.adventureSubscription.stakeTokensAmount - difTokens, // stakeNftAmount: args.adventureSubscription.stakeNftAmount - difBanx, // subscribedAt: nowInSeconds(), // adventure: args.adventure.publicKey, // banxTokenStake: args.banxTokensStake.publicKey, // user: args.banxTokensStake.user, // }, // banxTokensStake: { // ...args.banxTokensStake, // adventureSubscriptionsQuantity: // args.banxTokensStake.banxStakeState == BanxTokenStakeState.Unstaked // ? args.banxTokensStake.adventureSubscriptionsQuantity - 1 // : args.banxTokensStake.adventureSubscriptionsQuantity, // }, // }; // };