Skip to content

Commit

Permalink
Quality of Life, more asserts and refactor (#10)
Browse files Browse the repository at this point in the history
* Added more assertions && added account types for each account && refactor

* Remove redundant account from distribute_rewards function && rename and reorganize RewardVault

* Futher removing redundant implementations

* use split_off instead of retain for operation on trees

* rename RewardVault into RewardCalculator

* fix: make source_token_account for fill vault mutable

* typos
  • Loading branch information
kstepanovdev authored Jun 27, 2024
1 parent 5e23a69 commit 2420427
Show file tree
Hide file tree
Showing 30 changed files with 361 additions and 458 deletions.
47 changes: 6 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 1 addition & 8 deletions clients/js/src/generated/instructions/distributeRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ export type DistributeRewardsInstructionAccounts = {
rewardPool: PublicKey | Pda;
/** The address of the reward mint */
rewardMint: PublicKey | Pda;
/** The address of the reward vault */
vault: PublicKey | Pda;
/** The address of Authority who is eligble for distributiong rewards for users */
distributeAuthority: Signer;
};
Expand Down Expand Up @@ -85,13 +83,8 @@ export function distributeRewards(
isWritable: false as boolean,
value: input.rewardMint ?? null,
},
vault: {
index: 2,
isWritable: true as boolean,
value: input.vault ?? null,
},
distributeAuthority: {
index: 3,
index: 2,
isWritable: false as boolean,
value: input.distributeAuthority ?? null,
},
Expand Down
2 changes: 2 additions & 0 deletions clients/js/src/generated/types/accountType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { Serializer, scalarEnum } from '@metaplex-foundation/umi/serializers';
export enum AccountType {
Uninitialized,
RewardPool,
Mining,
RewardVault,
}

export type AccountTypeArgs = AccountType;
Expand Down
12 changes: 11 additions & 1 deletion clients/js/src/generated/types/mining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ import {
u64,
u8,
} from '@metaplex-foundation/umi/serializers';
import { RewardIndex, RewardIndexArgs, getRewardIndexSerializer } from '.';
import {
AccountType,
AccountTypeArgs,
RewardIndex,
RewardIndexArgs,
getAccountTypeSerializer,
getRewardIndexSerializer,
} from '.';

export type Mining = {
accountType: AccountType;
rewardPool: PublicKey;
bump: number;
share: bigint;
Expand All @@ -26,6 +34,7 @@ export type Mining = {
};

export type MiningArgs = {
accountType: AccountTypeArgs;
rewardPool: PublicKey;
bump: number;
share: number | bigint;
Expand All @@ -37,6 +46,7 @@ export type MiningArgs = {
export function getMiningSerializer(): Serializer<MiningArgs, Mining> {
return struct<Mining>(
[
['accountType', getAccountTypeSerializer()],
['rewardPool', publicKeySerializer()],
['bump', u8()],
['share', u64()],
Expand Down
10 changes: 7 additions & 3 deletions clients/js/src/generated/types/rewardVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import {
u64,
u8,
} from '@metaplex-foundation/umi/serializers';
import { AccountType, AccountTypeArgs, getAccountTypeSerializer } from '.';

export type RewardVault = {
bump: number;
accountType: AccountType;
tokenAccountBump: number;
rewardMint: PublicKey;
indexWithPrecision: bigint;
weightedStakeDiffs: Map<bigint, bigint>;
Expand All @@ -28,7 +30,8 @@ export type RewardVault = {
};

export type RewardVaultArgs = {
bump: number;
accountType: AccountTypeArgs;
tokenAccountBump: number;
rewardMint: PublicKey;
indexWithPrecision: number | bigint;
weightedStakeDiffs: Map<number | bigint, number | bigint>;
Expand All @@ -43,7 +46,8 @@ export function getRewardVaultSerializer(): Serializer<
> {
return struct<RewardVault>(
[
['bump', u8()],
['accountType', getAccountTypeSerializer()],
['tokenAccountBump', u8()],
['rewardMint', publicKeySerializer()],
['indexWithPrecision', u128()],
['weightedStakeDiffs', map(u64(), u64())],
Expand Down
45 changes: 5 additions & 40 deletions clients/rust/src/generated/instructions/distribute_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub struct DistributeRewards {
pub reward_pool: solana_program::pubkey::Pubkey,
/// The address of the reward mint
pub reward_mint: solana_program::pubkey::Pubkey,
/// The address of the reward vault
pub vault: solana_program::pubkey::Pubkey,
/// The address of Authority who is eligble for distributiong rewards for users
pub distribute_authority: solana_program::pubkey::Pubkey,
}
Expand All @@ -29,7 +27,7 @@ impl DistributeRewards {
&self,
remaining_accounts: &[solana_program::instruction::AccountMeta],
) -> solana_program::instruction::Instruction {
let mut accounts = Vec::with_capacity(4 + remaining_accounts.len());
let mut accounts = Vec::with_capacity(3 + remaining_accounts.len());
accounts.push(solana_program::instruction::AccountMeta::new(
self.reward_pool,
false,
Expand All @@ -38,9 +36,6 @@ impl DistributeRewards {
self.reward_mint,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
self.vault, false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
self.distribute_authority,
true,
Expand Down Expand Up @@ -75,13 +70,11 @@ impl DistributeRewardsInstructionData {
///
/// 0. `[writable]` reward_pool
/// 1. `[]` reward_mint
/// 2. `[writable]` vault
/// 3. `[signer]` distribute_authority
/// 2. `[signer]` distribute_authority
#[derive(Default)]
pub struct DistributeRewardsBuilder {
reward_pool: Option<solana_program::pubkey::Pubkey>,
reward_mint: Option<solana_program::pubkey::Pubkey>,
vault: Option<solana_program::pubkey::Pubkey>,
distribute_authority: Option<solana_program::pubkey::Pubkey>,
__remaining_accounts: Vec<solana_program::instruction::AccountMeta>,
}
Expand All @@ -102,12 +95,6 @@ impl DistributeRewardsBuilder {
self.reward_mint = Some(reward_mint);
self
}
/// The address of the reward vault
#[inline(always)]
pub fn vault(&mut self, vault: solana_program::pubkey::Pubkey) -> &mut Self {
self.vault = Some(vault);
self
}
/// The address of Authority who is eligble for distributiong rewards for users
#[inline(always)]
pub fn distribute_authority(
Expand Down Expand Up @@ -140,7 +127,6 @@ impl DistributeRewardsBuilder {
let accounts = DistributeRewards {
reward_pool: self.reward_pool.expect("reward_pool is not set"),
reward_mint: self.reward_mint.expect("reward_mint is not set"),
vault: self.vault.expect("vault is not set"),
distribute_authority: self
.distribute_authority
.expect("distribute_authority is not set"),
Expand All @@ -156,8 +142,6 @@ pub struct DistributeRewardsCpiAccounts<'a, 'b> {
pub reward_pool: &'b solana_program::account_info::AccountInfo<'a>,
/// The address of the reward mint
pub reward_mint: &'b solana_program::account_info::AccountInfo<'a>,
/// The address of the reward vault
pub vault: &'b solana_program::account_info::AccountInfo<'a>,
/// The address of Authority who is eligble for distributiong rewards for users
pub distribute_authority: &'b solana_program::account_info::AccountInfo<'a>,
}
Expand All @@ -170,8 +154,6 @@ pub struct DistributeRewardsCpi<'a, 'b> {
pub reward_pool: &'b solana_program::account_info::AccountInfo<'a>,
/// The address of the reward mint
pub reward_mint: &'b solana_program::account_info::AccountInfo<'a>,
/// The address of the reward vault
pub vault: &'b solana_program::account_info::AccountInfo<'a>,
/// The address of Authority who is eligble for distributiong rewards for users
pub distribute_authority: &'b solana_program::account_info::AccountInfo<'a>,
}
Expand All @@ -185,7 +167,6 @@ impl<'a, 'b> DistributeRewardsCpi<'a, 'b> {
__program: program,
reward_pool: accounts.reward_pool,
reward_mint: accounts.reward_mint,
vault: accounts.vault,
distribute_authority: accounts.distribute_authority,
}
}
Expand Down Expand Up @@ -222,7 +203,7 @@ impl<'a, 'b> DistributeRewardsCpi<'a, 'b> {
bool,
)],
) -> solana_program::entrypoint::ProgramResult {
let mut accounts = Vec::with_capacity(4 + remaining_accounts.len());
let mut accounts = Vec::with_capacity(3 + remaining_accounts.len());
accounts.push(solana_program::instruction::AccountMeta::new(
*self.reward_pool.key,
false,
Expand All @@ -231,10 +212,6 @@ impl<'a, 'b> DistributeRewardsCpi<'a, 'b> {
*self.reward_mint.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new(
*self.vault.key,
false,
));
accounts.push(solana_program::instruction::AccountMeta::new_readonly(
*self.distribute_authority.key,
true,
Expand All @@ -255,11 +232,10 @@ impl<'a, 'b> DistributeRewardsCpi<'a, 'b> {
accounts,
data,
};
let mut account_infos = Vec::with_capacity(4 + 1 + remaining_accounts.len());
let mut account_infos = Vec::with_capacity(3 + 1 + remaining_accounts.len());
account_infos.push(self.__program.clone());
account_infos.push(self.reward_pool.clone());
account_infos.push(self.reward_mint.clone());
account_infos.push(self.vault.clone());
account_infos.push(self.distribute_authority.clone());
remaining_accounts
.iter()
Expand All @@ -279,8 +255,7 @@ impl<'a, 'b> DistributeRewardsCpi<'a, 'b> {
///
/// 0. `[writable]` reward_pool
/// 1. `[]` reward_mint
/// 2. `[writable]` vault
/// 3. `[signer]` distribute_authority
/// 2. `[signer]` distribute_authority
pub struct DistributeRewardsCpiBuilder<'a, 'b> {
instruction: Box<DistributeRewardsCpiBuilderInstruction<'a, 'b>>,
}
Expand All @@ -291,7 +266,6 @@ impl<'a, 'b> DistributeRewardsCpiBuilder<'a, 'b> {
__program: program,
reward_pool: None,
reward_mint: None,
vault: None,
distribute_authority: None,
__remaining_accounts: Vec::new(),
});
Expand All @@ -315,12 +289,6 @@ impl<'a, 'b> DistributeRewardsCpiBuilder<'a, 'b> {
self.instruction.reward_mint = Some(reward_mint);
self
}
/// The address of the reward vault
#[inline(always)]
pub fn vault(&mut self, vault: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
self.instruction.vault = Some(vault);
self
}
/// The address of Authority who is eligble for distributiong rewards for users
#[inline(always)]
pub fn distribute_authority(
Expand Down Expand Up @@ -384,8 +352,6 @@ impl<'a, 'b> DistributeRewardsCpiBuilder<'a, 'b> {
.reward_mint
.expect("reward_mint is not set"),

vault: self.instruction.vault.expect("vault is not set"),

distribute_authority: self
.instruction
.distribute_authority
Expand All @@ -402,7 +368,6 @@ struct DistributeRewardsCpiBuilderInstruction<'a, 'b> {
__program: &'b solana_program::account_info::AccountInfo<'a>,
reward_pool: Option<&'b solana_program::account_info::AccountInfo<'a>>,
reward_mint: Option<&'b solana_program::account_info::AccountInfo<'a>>,
vault: Option<&'b solana_program::account_info::AccountInfo<'a>>,
distribute_authority: Option<&'b solana_program::account_info::AccountInfo<'a>>,
/// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
__remaining_accounts: Vec<(
Expand Down
2 changes: 2 additions & 0 deletions clients/rust/src/generated/types/account_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ use num_derive::FromPrimitive;
pub enum AccountType {
Uninitialized,
RewardPool,
Mining,
RewardVault,
}
Loading

0 comments on commit 2420427

Please sign in to comment.