Skip to content

Commit

Permalink
refactor: drop unused errors and few unused methods (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
n00m4d authored Jul 1, 2024
1 parent ed67be2 commit e37c96c
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 199 deletions.
88 changes: 8 additions & 80 deletions clients/js/src/generated/errors/mplxRewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,109 +41,37 @@ export class MathOverflowError extends ProgramError {
codeToErrorMap.set(0x1, MathOverflowError);
nameToErrorMap.set('MathOverflow', MathOverflowError);

/** ZeroAmount: Zero amount */
export class ZeroAmountError extends ProgramError {
override readonly name: string = 'ZeroAmount';

readonly code: number = 0x2; // 2

constructor(program: Program, cause?: Error) {
super('Zero amount', program, cause);
}
}
codeToErrorMap.set(0x2, ZeroAmountError);
nameToErrorMap.set('ZeroAmount', ZeroAmountError);

/** RewardsInvalidVault: Rewards: Invalid vault */
export class RewardsInvalidVaultError extends ProgramError {
override readonly name: string = 'RewardsInvalidVault';

readonly code: number = 0x3; // 3

constructor(program: Program, cause?: Error) {
super('Rewards: Invalid vault', program, cause);
}
}
codeToErrorMap.set(0x3, RewardsInvalidVaultError);
nameToErrorMap.set('RewardsInvalidVault', RewardsInvalidVaultError);

/** RewardsNoDeposits: Rewards: No deposits */
export class RewardsNoDepositsError extends ProgramError {
override readonly name: string = 'RewardsNoDeposits';

readonly code: number = 0x4; // 4
readonly code: number = 0x2; // 2

constructor(program: Program, cause?: Error) {
super('Rewards: No deposits', program, cause);
}
}
codeToErrorMap.set(0x4, RewardsNoDepositsError);
codeToErrorMap.set(0x2, RewardsNoDepositsError);
nameToErrorMap.set('RewardsNoDeposits', RewardsNoDepositsError);

/** RebalanceLiquidityCheckFailed: Rebalancing: liquidity check failed */
export class RebalanceLiquidityCheckFailedError extends ProgramError {
override readonly name: string = 'RebalanceLiquidityCheckFailed';

readonly code: number = 0x5; // 5

constructor(program: Program, cause?: Error) {
super('Rebalancing: liquidity check failed', program, cause);
}
}
codeToErrorMap.set(0x5, RebalanceLiquidityCheckFailedError);
nameToErrorMap.set(
'RebalanceLiquidityCheckFailed',
RebalanceLiquidityCheckFailedError
);

/** IndexMustExist: Rewards: index receiving failed */
export class IndexMustExistError extends ProgramError {
override readonly name: string = 'IndexMustExist';

readonly code: number = 0x6; // 6

constructor(program: Program, cause?: Error) {
super('Rewards: index receiving failed', program, cause);
}
}
codeToErrorMap.set(0x6, IndexMustExistError);
nameToErrorMap.set('IndexMustExist', IndexMustExistError);

/** InvalidLockupPeriod: Rewards: lockup period invalid */
export class InvalidLockupPeriodError extends ProgramError {
override readonly name: string = 'InvalidLockupPeriod';

readonly code: number = 0x7; // 7
readonly code: number = 0x3; // 3

constructor(program: Program, cause?: Error) {
super('Rewards: lockup period invalid', program, cause);
}
}
codeToErrorMap.set(0x7, InvalidLockupPeriodError);
codeToErrorMap.set(0x3, InvalidLockupPeriodError);
nameToErrorMap.set('InvalidLockupPeriod', InvalidLockupPeriodError);

/** InvalidCpiCaller: Rewards: only Staking contract is allowed to do CPI calls */
export class InvalidCpiCallerError extends ProgramError {
override readonly name: string = 'InvalidCpiCaller';

readonly code: number = 0x8; // 8

constructor(program: Program, cause?: Error) {
super(
'Rewards: only Staking contract is allowed to do CPI calls',
program,
cause
);
}
}
codeToErrorMap.set(0x8, InvalidCpiCallerError);
nameToErrorMap.set('InvalidCpiCaller', InvalidCpiCallerError);

/** DistributionInThePast: Rewards: distribution_ends_at date is lower than current date */
export class DistributionInThePastError extends ProgramError {
override readonly name: string = 'DistributionInThePast';

readonly code: number = 0x9; // 9
readonly code: number = 0x4; // 4

constructor(program: Program, cause?: Error) {
super(
Expand All @@ -153,14 +81,14 @@ export class DistributionInThePastError extends ProgramError {
);
}
}
codeToErrorMap.set(0x9, DistributionInThePastError);
codeToErrorMap.set(0x4, DistributionInThePastError);
nameToErrorMap.set('DistributionInThePast', DistributionInThePastError);

/** InvalidPrimitiveTypesConversion: Rewards: distribution_ends_at date is lower than current date */
export class InvalidPrimitiveTypesConversionError extends ProgramError {
override readonly name: string = 'InvalidPrimitiveTypesConversion';

readonly code: number = 0xa; // 10
readonly code: number = 0x5; // 5

constructor(program: Program, cause?: Error) {
super(
Expand All @@ -170,7 +98,7 @@ export class InvalidPrimitiveTypesConversionError extends ProgramError {
);
}
}
codeToErrorMap.set(0xa, InvalidPrimitiveTypesConversionError);
codeToErrorMap.set(0x5, InvalidPrimitiveTypesConversionError);
nameToErrorMap.set(
'InvalidPrimitiveTypesConversion',
InvalidPrimitiveTypesConversionError
Expand Down
23 changes: 4 additions & 19 deletions clients/rust/src/generated/errors/mplx_rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,16 @@ pub enum MplxRewardsError {
/// 1 (0x1) - Math operation overflow
#[error("Math operation overflow")]
MathOverflow,
/// 2 (0x2) - Zero amount
#[error("Zero amount")]
ZeroAmount,
/// 3 (0x3) - Rewards: Invalid vault
#[error("Rewards: Invalid vault")]
RewardsInvalidVault,
/// 4 (0x4) - Rewards: No deposits
/// 2 (0x2) - Rewards: No deposits
#[error("Rewards: No deposits")]
RewardsNoDeposits,
/// 5 (0x5) - Rebalancing: liquidity check failed
#[error("Rebalancing: liquidity check failed")]
RebalanceLiquidityCheckFailed,
/// 6 (0x6) - Rewards: index receiving failed
#[error("Rewards: index receiving failed")]
IndexMustExist,
/// 7 (0x7) - Rewards: lockup period invalid
/// 3 (0x3) - Rewards: lockup period invalid
#[error("Rewards: lockup period invalid")]
InvalidLockupPeriod,
/// 8 (0x8) - Rewards: only Staking contract is allowed to do CPI calls
#[error("Rewards: only Staking contract is allowed to do CPI calls")]
InvalidCpiCaller,
/// 9 (0x9) - Rewards: distribution_ends_at date is lower than current date
/// 4 (0x4) - Rewards: distribution_ends_at date is lower than current date
#[error("Rewards: distribution_ends_at date is lower than current date ")]
DistributionInThePast,
/// 10 (0xA) - Rewards: distribution_ends_at date is lower than current date
/// 5 (0x5) - Rewards: distribution_ends_at date is lower than current date
#[error("Rewards: distribution_ends_at date is lower than current date ")]
InvalidPrimitiveTypesConversion,
}
Expand Down
31 changes: 3 additions & 28 deletions idls/mplx_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -671,46 +671,21 @@
},
{
"code": 2,
"name": "ZeroAmount",
"msg": "Zero amount"
},
{
"code": 3,
"name": "RewardsInvalidVault",
"msg": "Rewards: Invalid vault"
},
{
"code": 4,
"name": "RewardsNoDeposits",
"msg": "Rewards: No deposits"
},
{
"code": 5,
"name": "RebalanceLiquidityCheckFailed",
"msg": "Rebalancing: liquidity check failed"
},
{
"code": 6,
"name": "IndexMustExist",
"msg": "Rewards: index receiving failed"
},
{
"code": 7,
"code": 3,
"name": "InvalidLockupPeriod",
"msg": "Rewards: lockup period invalid"
},
{
"code": 8,
"name": "InvalidCpiCaller",
"msg": "Rewards: only Staking contract is allowed to do CPI calls"
},
{
"code": 9,
"code": 4,
"name": "DistributionInThePast",
"msg": "Rewards: distribution_ends_at date is lower than current date "
},
{
"code": 10,
"code": 5,
"name": "InvalidPrimitiveTypesConversion",
"msg": "Rewards: distribution_ends_at date is lower than current date "
}
Expand Down
9 changes: 0 additions & 9 deletions programs/rewards/src/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,3 @@ pub fn assert_rent_exempt(account_info: &AccountInfo) -> ProgramResult {
Err(ProgramError::AccountNotRentExempt)
}
}

/// Assert a non-zero amount
pub fn assert_non_zero_amount(amount: u64) -> ProgramResult {
if amount == 0 {
return Err(MplxRewardsError::ZeroAmount.into());
}

Ok(())
}
31 changes: 3 additions & 28 deletions programs/rewards/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,21 @@ pub enum MplxRewardsError {
MathOverflow,

/// 2
/// Zero amount
#[error("Zero amount")]
ZeroAmount,

/// 3
/// Invalid vault
#[error("Rewards: Invalid vault")]
RewardsInvalidVault,

/// 4
/// No deposits
#[error("Rewards: No deposits")]
RewardsNoDeposits,

/// 5
/// Check for liquidity amount in rebalance
#[error("Rebalancing: liquidity check failed")]
RebalanceLiquidityCheckFailed,

/// 6
/// Non existing reward's index
#[error("Rewards: index receiving failed")]
IndexMustExist,

/// 7
/// 3
/// Invalid lockup period
#[error("Rewards: lockup period invalid")]
InvalidLockupPeriod,

/// 8
/// Invalid CPI caller
#[error("Rewards: only Staking contract is allowed to do CPI calls")]
InvalidCpiCaller,

/// 9
/// 4
/// Invalid distribution_ends_at data
#[error("Rewards: distribution_ends_at date is lower than current date ")]
DistributionInThePast,

/// 10
/// 5
/// Invalid math conversion between types
#[error("Rewards: distribution_ends_at date is lower than current date ")]
InvalidPrimitiveTypesConversion,
Expand Down
35 changes: 0 additions & 35 deletions programs/rewards/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,30 +197,6 @@ impl AccountLoader {
Err(ProgramError::MissingRequiredSignature)
}

/// Checks if account is initialized and then checks it's owner
pub fn next_optional<'a, 'b, I: Iterator<Item = &'a AccountInfo<'b>>>(
iter: &mut Enumerate<I>,
owner: &Pubkey,
) -> Result<I::Item, ProgramError> {
let (idx, acc) = iter.next().ok_or(ProgramError::NotEnoughAccountKeys)?;
if acc.owner.eq(&Pubkey::default()) {
return Ok(acc);
}

if acc.owner.eq(owner) {
return Ok(acc);
}

msg!(
"Account #{}:{} owner error. Got {} Expected unitialized or {}",
idx,
acc.key,
acc.owner,
owner
);
Err(MplxRewardsError::InvalidAccountOwner.into())
}

/// Load the account without any checks
pub fn next_unchecked<'a, 'b, I: Iterator<Item = &'a AccountInfo<'b>>>(
iter: &mut Enumerate<I>,
Expand Down Expand Up @@ -278,17 +254,6 @@ impl LockupPeriod {
LockupPeriod::Flex => Ok(0)
}
}

/// Return number of days plain numbers to make them appliable for the self.weighted_stake_diff
pub fn days(&self) -> Result<u64, MplxRewardsError> {
match self {
LockupPeriod::None => Err(MplxRewardsError::InvalidLockupPeriod),
LockupPeriod::ThreeMonths => Ok(90),
LockupPeriod::SixMonths => Ok(180),
LockupPeriod::OneYear => Ok(365),
LockupPeriod::Flex => Ok(0),
}
}
}

/// Get current unix time
Expand Down

0 comments on commit e37c96c

Please sign in to comment.