import BN from "bn.js"; import { PublicKey } from "@solana/web3.js"; import { array, publicKey, str, struct, u128, u16, u64, u8 } from '@coral-xyz/borsh'; import { Fraction, FractionLayout } from "../fraction"; import { Bounty, BountyLayout } from "./bounty"; import { OraclePriceLayout, OraclePriceOnChain } from "../oracle"; import { MAX_SUPPORTED_TOKENS_PER_BASKET } from '../../constants'; import { FeeSettings, FeeSettingsLayout } from "../config"; import { FormattedFeeSettings } from "../basket"; import { FormattedBountySchedule } from "./intent"; export enum RebalanceType { Deposit, Withdraw, Basket, BasketCustom, }; export enum RebalanceAction { NotActive, DepositTokens, PlaceholderAction, UpdatePrices, Auction }; export interface AuctionTimestamps { startTime: BN; // u64 endTime: BN; // u64 }; export const AuctionTimestampsLayout = struct([ u64('startTime'), u64('endTime'), ]); export interface TokenAuction { mint: PublicKey; amount: BN; // u64 targetAmount: BN; // u64 price: OraclePriceOnChain; keepToken: number; // u8 }; export const TokenAuctionLayout = struct([ publicKey('mint'), u64('amount'), u64('targetAmount'), OraclePriceLayout.replicate('price'), u8('keepToken'), ]); export interface TaskCompletion { completedBy: PublicKey; //Pubkey completedBounty: BN; //u64 completedTime: BN; //u64 }; export const TaskCompletionLayout = struct([ publicKey('completedBy'), u64('completedBounty'), u64('completedTime'), ]); export type FormattedRebalanceType = "deposit" | "withdraw" | "basket" | "basket_custom"; export const REBALANCE_TYPE_STRINGS: Map = new Map([ [0, "deposit"], [1, "withdraw"], [2, "basket"], [3, "basket_custom"], ]); export type FormattedRebalanceAction = "not_active" | "deposit_tokens" | "placeholder_action" | "update_prices" | "auction"; export const REBALANCE_ACTION_STRINGS: Map = new Map([ [0, "not_active"], [1, "deposit_tokens"], [2, "placeholder_action"], [3, "update_prices"], [4, "auction"], ]); export interface FormattedOraclePrice { price: number; conf: number; update_time: number; } export interface FormattedTokenAuction { mint: string; amount: number; target_amount: number; price: FormattedOraclePrice; keep_token: boolean; } export interface FormattedTaskCompletion { completed_by: string; completed_bounty: number; completed_time: number; } export interface DepositData { tokens: { mint: string; amount: number; }[]; }; export interface PriceUpdatesData { tokens: { mint: string; updated: boolean; expiration: number; update_time: number; price: number; conf: number; }[]; }; export interface AuctionData { auction_stages: { start_time: number; end_time: number; }[]; tokens: { mint: string; amount: number; target_amount: number; price: number; conf: number; }[]; }; export interface MintData { mintAmount: number; mintValue: number; fees: { host: number; creator: number; managers: number; symmetry: number; basket: number; }; tokens: { mint: string; contribution_amount: number; remaining_amount: number; }[]; }[]; export interface RedeemData { tokens: { mint: string; amount: number; }[]; }[]; export interface ClaimBountyData { bounty_mint: string; keepers: { pubkey: string; bounty_amount: number; }[]; unused_bounty: { pubkey: string; bounty_amount: number; }; rent: { pubkey: string; sol_amount: number; } }[]; export interface UIRebalanceIntent { rebalance_type: FormattedRebalanceType; deposit_data: DepositData | null; price_updates_data: PriceUpdatesData | null; auction_data: AuctionData | null; mint_data: MintData | null; redeem_data: RedeemData | null; claim_bounty_data: ClaimBountyData | null; formatted_data: FormattedRebalanceIntent; chain_data: RebalanceIntent; } export interface FormattedRebalanceIntent { pubkey: string; sol_balance: number; basket: string; owner: string; rent_payer: string; rebalance_type: FormattedRebalanceType; current_action: FormattedRebalanceAction; withdraw_params_burn_amount: number; withdraw_params_amount_wo_fees: number; withdraw_params_keep_tokens_bitmask: number[]; withdraw_params_keep_all_tokens: boolean; basket_fee_settings: FormattedFeeSettings; execution_start_time: number; rebalance_threshold_slippage_bps: number; per_trade_rebalance_threshold_slippage_bps: number; initial_tvl: number; auction_update_timestamp: number; auctions: { start_time: number; end_time: number; }[]; tokens: FormattedTokenAuction[]; last_action_timestamp: number; bounty: { bounty_depositor: string; bounty_mint: string; bounty_per_price_update_task: FormattedBountySchedule; bounty_per_task: FormattedBountySchedule; bounty_total: number; bounty_left: number; }; bounty_adjustment_amount: number; placeholder_task: FormattedTaskCompletion; price_update_tasks: FormattedTaskCompletion[]; finish_price_update_task: FormattedTaskCompletion; auction_creation_task: FormattedTaskCompletion; mint_basket_task: FormattedTaskCompletion; cancel_rebalance_task: FormattedTaskCompletion; token_settlement_tasks: FormattedTaskCompletion[]; } export interface RebalanceIntent { basket: PublicKey; owner: PublicKey; rentPayer: PublicKey; rebalanceType: RebalanceType; currentAction: RebalanceAction; withdrawParamsBurnAmount: BN, withdrawParamsAmountWoFees: BN, withdrawParamsKeepTokensBitmask: number[], withdrawParamsKeepAllTokens: u8, basketFeeSettings: FeeSettings; executionStartTime: BN; // u64 rebalanceThresholdSlippageBps: number; // u16 perTradeRebalanceThresholdSlippageBps: number; // u16 initialTvl: Fraction; // auctionUpdateTimestamp: BN; // u64 auctions: AuctionTimestamps[]; // AuctionTimestamps[] tokens: TokenAuction[]; // MAX_SUPPORTED_TOKENS_PER_BASKET lastActionTimestamp: BN; // u64 bounty: Bounty; // u64 bountyAdjustmentAmount: BN; // u64 placeholderTask: TaskCompletion; priceUpdateTasks: TaskCompletion[]; // MAX_SUPPORTED_TOKENS_PER_BASKET finishPriceUpdateTask: TaskCompletion; auctionCreationTask: TaskCompletion; mintBasketTask: TaskCompletion; cancelRebalanceTask: TaskCompletion; tokenSettlementTasks: TaskCompletion[]; // MAX_SUPPORTED_TOKENS_PER_BASKET ownAddress?: PublicKey; solBalance?: number; }; export const RebalanceIntentLayout = struct([ publicKey('basket'), publicKey('owner'), publicKey('rentPayer'), u8('rebalanceType'), u8('currentAction'), u64('withdrawParamsBurnAmount'), u64('withdrawParamsAmountWoFees'), array(u8(), 16, 'withdrawParamsKeepTokensBitmask'), u8('withdrawParamsKeepAllTokens'), FeeSettingsLayout.replicate('basketFeeSettings'), u64('executionStartTime'), u16('rebalanceThresholdSlippageBps'), u16('perTradeRebalanceThresholdSlippageBps'), FractionLayout.replicate('initialTvl'), u64('auctionUpdateTimestamp'), array(AuctionTimestampsLayout, 3, 'auctions'), array(TokenAuctionLayout, MAX_SUPPORTED_TOKENS_PER_BASKET, 'tokens'), u64('lastActionTimestamp'), BountyLayout.replicate('bounty'), u64('bountyAdjustmentAmount'), TaskCompletionLayout.replicate('placeholderTask'), array(TaskCompletionLayout, MAX_SUPPORTED_TOKENS_PER_BASKET, 'priceUpdateTasks'), TaskCompletionLayout.replicate('finishPriceUpdateTask'), TaskCompletionLayout.replicate('auctionCreationTask'), TaskCompletionLayout.replicate('mintBasketTask'), TaskCompletionLayout.replicate('cancelRebalanceTask'), array(TaskCompletionLayout, MAX_SUPPORTED_TOKENS_PER_BASKET, 'tokenSettlementTasks'), ]);