Skip to content

Commit 0f3ee0c

Browse files
committed
refactor: reduce type conversions in earn
1 parent eb4e792 commit 0f3ee0c

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

programs/earn/src/instructions/admin/initialize.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
constants::{ANCHOR_DISCRIMINATOR_SIZE, PORTAL_PROGRAM},
2121
errors::EarnError,
2222
state::{EarnGlobal, GLOBAL_SEED, TOKEN_AUTHORITY_SEED},
23-
utils::{conversion::update_multiplier, token::thaw_token_account},
23+
utils::{conversion::{update_multiplier, index_to_multiplier}, token::thaw_token_account},
2424
};
2525

2626
cfg_if::cfg_if!(
@@ -177,20 +177,20 @@ impl Initialize<'_> {
177177

178178
// Set the multiplier on the m_mint to the current index and timestamp on the old earn program
179179
update_multiplier(
180-
&mut ctx.accounts.m_mint, // mint
181-
&ctx.accounts.global_account.to_account_info(), // authority
182-
&[&[GLOBAL_SEED, &[ctx.bumps.global_account]]], // authority seeds
183-
&ctx.accounts.token_program, // token program
184-
ctx.accounts.old_global_account.index, // index
185-
ctx.accounts.old_global_account.timestamp as i64, // timestamp
180+
&mut ctx.accounts.m_mint, // mint
181+
&ctx.accounts.global_account.to_account_info(), // authority
182+
&[&[GLOBAL_SEED, &[ctx.bumps.global_account]]], // authority seeds
183+
&ctx.accounts.token_program, // token program
184+
index_to_multiplier(ctx.accounts.old_global_account.index)?, // index
185+
ctx.accounts.old_global_account.timestamp as i64, // timestamp
186186
)?;
187187
} else {
188188
update_multiplier(
189189
&mut ctx.accounts.m_mint, // mint
190190
&ctx.accounts.global_account.to_account_info(), // authority
191191
&[&[GLOBAL_SEED, &[ctx.bumps.global_account]]], // authority seeds
192192
&ctx.accounts.token_program, // token program
193-
_current_index, // index
193+
index_to_multiplier(_current_index)?, // index
194194
Clock::get()?.unix_timestamp, // timestamp
195195
)?;
196196
}

programs/earn/src/instructions/portal/propagate_index.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use anchor_spl::token_interface::{Mint, Token2022};
44

55
// local dependencies
66
use crate::{
7-
constants::INDEX_SCALE_F64,
87
errors::EarnError,
98
state::{EarnGlobal, GLOBAL_SEED},
10-
utils::conversion::{get_scaled_ui_config, update_multiplier},
9+
utils::conversion::{get_scaled_ui_config, update_multiplier, index_to_multiplier},
1110
};
1211

1312
#[derive(Accounts)]
@@ -40,10 +39,10 @@ impl PropagateIndex<'_> {
4039
) -> Result<()> {
4140
let scaled_ui_config = get_scaled_ui_config(&ctx.accounts.m_mint)?;
4241
let current_multiplier: f64 = scaled_ui_config.new_multiplier.into();
43-
let current_index = (INDEX_SCALE_F64 * current_multiplier).trunc() as u64;
42+
let new_multiplier = index_to_multiplier(new_index)?;
4443

45-
// Check if the new index is greater than or equal to the previously seen index.
46-
if new_index >= current_index {
44+
// Check if the new multiplier is greater than or equal to the previously seen multiplier.
45+
if new_multiplier >= current_multiplier {
4746
// If so, update the merkle root if it is non-zero.
4847
// We don't necessarily need the second check if we know updates only come
4948
// from mainnet. However, it provides some protection against staleness
@@ -52,16 +51,16 @@ impl PropagateIndex<'_> {
5251
ctx.accounts.global_account.earner_merkle_root = earner_merkle_root;
5352
}
5453

55-
// If the new index is strictly greater than the current one, update the multiplier.
56-
if new_index > current_index {
54+
// If the new multiplier is strictly greater than the current one, update the multiplier.
55+
if new_multiplier > current_multiplier {
5756
let timestamp = Clock::get()?.unix_timestamp;
5857

5958
update_multiplier(
6059
&mut ctx.accounts.m_mint,
6160
&ctx.accounts.global_account.to_account_info(),
6261
&[&[GLOBAL_SEED, &[ctx.accounts.global_account.bump]]],
6362
&ctx.accounts.token_program,
64-
new_index,
63+
new_multiplier,
6564
timestamp,
6665
)?;
6766

programs/earn/src/utils/conversion.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ pub fn update_multiplier<'info>(
1414
authority: &AccountInfo<'info>,
1515
authority_seeds: &[&[&[u8]]],
1616
token_program: &Program<'info, Token2022>,
17-
index: u64,
17+
multiplier: f64,
1818
timestamp: i64,
1919
) -> Result<()> {
20-
let multiplier = (index as f64) / INDEX_SCALE_F64;
21-
2220
// Only update multiplier if the new multiplier is greater than the current multiplier
2321
// Indices in the M protocol are monotonically increasing, but we may receive a stale update
2422
// from another chain.
@@ -137,6 +135,23 @@ pub fn principal_to_amount_up(principal: u64, multiplier: f64) -> Result<u64> {
137135
Ok(amount)
138136
}
139137

138+
pub fn multiplier_to_index(multiplier: f64) -> Result<u64> {
139+
let index: f64 = (INDEX_SCALE_F64 * multiplier).trunc();
140+
141+
if index < 0.0 {
142+
err!(EarnError::MathUnderflow)
143+
} else if index > u64::MAX as f64 {
144+
err!(EarnError::MathOverflow)
145+
} else {
146+
// Convert the f64 index to u64
147+
Ok(index as u64)
148+
}
149+
}
150+
151+
pub fn index_to_multiplier(index: u64) -> Result<f64> {
152+
Ok(index as f64 / INDEX_SCALE_F64)
153+
}
154+
140155
pub fn get_mint_extensions<'info>(
141156
mint: &InterfaceAccount<'info, Mint>,
142157
) -> Result<Vec<spl_token_2022::extension::ExtensionType>> {

0 commit comments

Comments
 (0)