crema-cli 0.1.0

Blockchain, Crema for Solana
Documentation
use borsh::{BorshDeserialize, BorshSerialize};
use solana_client::rpc_client::RpcClient;
use solana_program::pubkey::Pubkey;
use solana_sdk::account::Account;
use tabled::Tabled;

#[derive(BorshDeserialize, BorshSerialize, Tabled)]
pub struct Rewarder {
    /// Random pubkey used for generating the program address.
    pub base: Pubkey,
    /// Bump seed for program address.
    pub bump: u8,

    /// Authority who controls the rewarder
    pub authority: Pubkey,
    /// Pending authority which must accept the authority
    pub pending_authority: Pubkey,

    /// Number of [Quarry]s the [Rewarder] manages.
    /// If more than this many [Quarry]s are desired, one can create
    /// a second rewarder.
    pub num_quarries: u16,
    /// Amount of reward tokens distributed per day
    pub annual_rewards_rate: u64,
    /// Total amount of rewards shares allocated to [Quarry]s
    pub total_rewards_shares: u64,
    /// Mint wrapper.
    pub mint_wrapper: Pubkey,
    /// Mint of the rewards token for this [Rewarder].
    pub rewards_token_mint: Pubkey,

    /// Claim fees are placed in this account.
    pub claim_fee_token_account: Pubkey,
    /// Maximum amount of tokens to send to the Quarry DAO on each claim,
    /// in terms of milliBPS. 1,000 milliBPS = 1 BPS = 0.01%
    /// This is stored on the [Rewarder] to ensure that the fee will
    /// not exceed this in the future.
    pub max_claim_fee_millibps: u64,

    /// Authority allowed to pause a [Rewarder].
    pub pause_authority: Pubkey,
    /// If true, all instructions on the [Rewarder] are paused other than [quarry_mine::unpause].
    pub is_paused: bool,
}

impl Rewarder {
    pub fn get_info(rpc_client: &RpcClient, pubkey: &Pubkey) -> Self {
        let data_slice = &rpc_client.get_account_data(pubkey).unwrap()[8..];
        Rewarder::try_from_slice(data_slice).unwrap()
    }
    pub fn get_account(rpc_client: &RpcClient, pubkey: &Pubkey) -> Account {
        rpc_client.get_account(pubkey).unwrap()
    }
    pub fn new() -> Self {
        Self {
            base: Default::default(),
            bump: 0,
            authority: Default::default(),
            pending_authority: Default::default(),
            num_quarries: 0,
            annual_rewards_rate: 0,
            total_rewards_shares: 0,
            mint_wrapper: Default::default(),
            rewards_token_mint: Default::default(),
            claim_fee_token_account: Default::default(),
            max_claim_fee_millibps: 0,
            pause_authority: Default::default(),
            is_paused: false
        }
    }
}

#[derive(BorshDeserialize, BorshSerialize, Tabled)]
pub struct Quarry {
    /// Rewarder which manages this quarry
    pub rewarder: Pubkey,
    /// LP token this quarry is designated to
    pub token_mint_key: Pubkey,
    /// Bump.
    pub bump: u8,

    /// Index of the [Quarry].
    pub index: u16,
    /// Decimals on the token [Mint].
    pub token_mint_decimals: u8,
    // This field is never used.
    /// Timestamp when quarry rewards cease
    pub famine_ts: i64,
    /// Timestamp of last checkpoint
    pub last_update_ts: i64,
    /// Rewards per token stored in the quarry
    pub rewards_per_token_stored: u128,
    /// Amount of rewards distributed to the quarry per year.
    pub annual_rewards_rate: u64,
    /// Rewards shared allocated to this quarry
    pub rewards_share: u64,

    /// Total number of tokens deposited into the quarry.
    pub total_tokens_deposited: u64,
    /// Number of [Miner]s.
    pub num_miners: u64,
}

impl Quarry {
    pub fn get_info(rpc_client: &RpcClient, pubkey: &Pubkey) -> Self {
        let data_slice = &rpc_client.get_account_data(pubkey).unwrap()[8..];
        Quarry::try_from_slice(data_slice).unwrap()
    }
    pub fn get_account(rpc_client: &RpcClient, pubkey: &Pubkey) -> Account {
        rpc_client.get_account(pubkey).unwrap()
    }
    pub fn new() -> Self {
        Self {
            rewarder: Default::default(),
            token_mint_key: Default::default(),
            bump: 0,
            index: 0,
            token_mint_decimals: 0,
            famine_ts: 0,
            last_update_ts: 0,
            rewards_per_token_stored: 0,
            annual_rewards_rate: 0,
            rewards_share: 0,
            total_tokens_deposited: 0,
            num_miners: 0
        }
    }
}


#[derive(BorshDeserialize, BorshSerialize, Tabled)]
pub struct Miner {
    /// Key of the [Quarry] this [Miner] works on.
    pub quarry: Pubkey,
    /// Authority who manages this [Miner].
    /// All withdrawals of tokens must accrue to [TokenAccount]s owned by this account.
    pub authority: Pubkey,

    /// Bump.
    pub bump: u8,

    /// [TokenAccount] to hold the [Miner]'s staked LP tokens.
    pub token_vault_key: Pubkey,

    /// Stores the amount of tokens that the [Miner] may claim.
    /// Whenever the [Miner] claims tokens, this is reset to 0.
    pub rewards_earned: u64,

    /// A checkpoint of the [Quarry]'s reward tokens paid per staked token.
    ///
    /// When the [Miner] is initialized, this number starts at 0.
    /// On the first [quarry_mine::stake_tokens], the [Quarry]#update_rewards_and_miner
    /// method is called, which updates this checkpoint to the current quarry value.
    ///
    /// On a [quarry_mine::claim_rewards], the difference in checkpoints is used to calculate
    /// the amount of tokens owed.
    pub rewards_per_token_paid: u128,

    /// Number of tokens the [Miner] holds.
    pub balance: u64,

    /// Index of the [Miner].
    pub index: u64,
}

impl Miner {
    pub fn get_info(rpc_client: &RpcClient, pubkey: &Pubkey) -> Self {
        let data_slice = &rpc_client.get_account_data(pubkey).unwrap()[8..];
        Miner::try_from_slice(data_slice).unwrap()
    }
    pub fn get_account(rpc_client: &RpcClient, pubkey: &Pubkey) -> Account {
        rpc_client.get_account(pubkey).unwrap()
    }
}