Skip to content

Commit

Permalink
Use percentages as a smooth
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Jan 8, 2025
1 parent a2f8701 commit f5924a2
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 28 deletions.
16 changes: 16 additions & 0 deletions packages/helium-sub-daos-sdk/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,22 @@ export const subDaoEpochInfoResolver = resolveIndividual(
if (subDao) {
const [key] = await subDaoEpochInfoKey(subDao, unixTime - EPOCH_LENGTH, PROGRAM_ID);

return key;
}
}
if (path[path.length - 1] === "prevSubDaoEpochInfo" && args && args[0] && args[0].epoch) {
const unixTime = args[0].epoch.toNumber() * EPOCH_LENGTH;
const subDao = get(accounts, [
...path.slice(0, path.length - 1),
"subDao",
]) as PublicKey;
if (subDao) {
const [key] = await subDaoEpochInfoKey(
subDao,
unixTime - EPOCH_LENGTH,
PROGRAM_ID
);

return key;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ pub fn handler(
// burned hnt since last supply setting.
let curr_supply = ctx.accounts.hnt_mint.supply;
let mut prev_supply = curr_supply;
let mut prev_total_utility_score = 0;
if ctx.accounts.prev_dao_epoch_info.lamports() > 0 {
let info: Account<DaoEpochInfoV0> = Account::try_from(&ctx.accounts.prev_dao_epoch_info)?;
prev_supply = info.current_hnt_supply;
prev_total_utility_score = info.total_utility_score;
}

ctx.accounts.dao_epoch_info.total_rewards = ctx
Expand Down Expand Up @@ -152,40 +154,40 @@ pub fn handler(
.checked_div(&PreciseNumber::new(100000000_u128).unwrap()) // vehnt has 8 decimals
.unwrap();

// Apply a 90 day smooth
let utility_score_prec = vehnt_staked
// Add 12 decimals of precision
.checked_mul(&PreciseNumber::new(1000000000000_u128).unwrap()) // First convert vehnt to 12 decimals
.unwrap()
.checked_div(&PreciseNumber::new(90_u128).unwrap())
.unwrap()
.checked_add(
&PreciseNumber::new(89_u128)
.unwrap()
.checked_mul(
&ctx
.accounts
.prev_sub_dao_epoch_info
.utility_score
.and_then(PreciseNumber::new)
.unwrap_or_else(|| {
vehnt_staked
// Add 12 decimals of precision
.checked_mul(&PreciseNumber::new(1000000000000_u128).unwrap())
.unwrap()
}),
)
.unwrap()
.checked_div(&PreciseNumber::new(90_u128).unwrap())
.unwrap(),
)
.unwrap();

let utility_score = utility_score_prec.to_imprecise().unwrap();

// Store utility scores
epoch_info.utility_score = Some(utility_score);

let prev_epoch_info = &ctx.accounts.prev_sub_dao_epoch_info;
let previous_percentage = prev_epoch_info.previous_percentage;

// Initialize previous percentage if it's not already set
ctx.accounts.prev_sub_dao_epoch_info.previous_percentage = match previous_percentage {
0 => match prev_epoch_info.utility_score {
Some(prev_score) => {
if prev_total_utility_score == 0 {
0
} else {
prev_score
.checked_mul(u32::MAX as u128)
.and_then(|x| x.checked_div(prev_total_utility_score))
.map(|x| x as u32)
.unwrap_or(0)
}
}
None => u32::MAX
.checked_div(ctx.accounts.dao.num_sub_daos)
.unwrap_or(0),
},
_ => previous_percentage,
};

// Only increment utility scores when either (a) in prod or (b) testing and we haven't already over-calculated utility scores.
// TODO: We can remove this after breakpoint demo
if !(TESTING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ pub fn handler(ctx: Context<DelegateV0>) -> Result<()> {
epoch: genesis_end_epoch,
bump_seed: ctx.bumps["genesis_end_sub_dao_epoch_info"],
sub_dao: sub_dao.key(),
previous_percentage: 0,
dc_burned: 0,
vehnt_at_epoch_start: 0,
vehnt_in_closing_positions: genesis_end_vehnt_correction,
Expand Down
37 changes: 36 additions & 1 deletion programs/helium-sub-daos/src/instructions/issue_rewards_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ pub struct IssueRewardsV0<'info> {
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub circuit_breaker_program: Program<'info, CircuitBreaker>,
#[account(
seeds = ["sub_dao_epoch_info".as_bytes(), sub_dao.key().as_ref(), &(args.epoch - 1).to_le_bytes()],
bump = prev_sub_dao_epoch_info.bump_seed,
)]
pub prev_sub_dao_epoch_info: Box<Account<'info, SubDaoEpochInfoV0>>,
}

fn to_prec(n: Option<u128>) -> Option<PreciseNumber> {
Expand Down Expand Up @@ -118,9 +123,39 @@ pub fn handler(ctx: Context<IssueRewardsV0>, args: IssueRewardsArgsV0) -> Result
let total_utility_score = to_prec(Some(ctx.accounts.dao_epoch_info.total_utility_score))
.ok_or_else(|| error!(ErrorCode::NoUtilityScore))?;

let percent_share = utility_score
let percent_share_pre_smooth = utility_score
.checked_div(&total_utility_score)
.or_arith_error()?;

// Convert previous percentage from u32 to PreciseNumber (divide by u32::MAX)
let prev_percentage =
PreciseNumber::new(ctx.accounts.prev_sub_dao_epoch_info.previous_percentage as u128)
.or_arith_error()?
.checked_div(&PreciseNumber::new(u32::MAX as u128).or_arith_error()?)
.or_arith_error()?;

let percent_share = prev_percentage
.checked_mul(&PreciseNumber::new(29).or_arith_error()?)
.or_arith_error()?
.checked_div(&PreciseNumber::new(30).or_arith_error()?)
.or_arith_error()?
.checked_add(
&percent_share_pre_smooth
.checked_mul(&PreciseNumber::new(1).or_arith_error()?)
.or_arith_error()?
.checked_div(&PreciseNumber::new(30).or_arith_error()?)
.or_arith_error()?,
)
.or_arith_error()?;

ctx.accounts.sub_dao_epoch_info.previous_percentage = prev_percentage
.checked_mul(&PreciseNumber::new(u32::MAX as u128).or_arith_error()?)
.or_arith_error()?
.to_imprecise()
.ok_or_else(|| error!(ErrorCode::ArithmeticError))?
.try_into()
.unwrap();

let total_emissions = ctx.accounts.dao_epoch_info.total_rewards;
let hst_percent = ctx
.accounts
Expand Down
3 changes: 2 additions & 1 deletion programs/helium-sub-daos/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,11 @@ pub struct SubDaoEpochInfoV0 {
pub dc_onboarding_fees_paid: u64,
/// The number of hnt rewards issued to the reward escrow this epoch
pub hnt_rewards_issued: u64,
pub previous_percentage: u32,
}

impl SubDaoEpochInfoV0 {
pub const SIZE: usize = 60 + 8 + std::mem::size_of::<SubDaoEpochInfoV0>() - 8 - 8 - 8; // subtract 8 the extra u64 we added to vehnt, dc onboarding fees paid, and hnt rewards issued
pub const SIZE: usize = 60 + 8 + std::mem::size_of::<SubDaoEpochInfoV0>() - 8 - 8 - 8 - 4; // subtract 8 the extra u64 we added to vehnt, dc onboarding fees paid, hnt rewards issued, and prev percentage
}
impl SubDaoEpochInfoV0 {
pub fn start_ts(&self) -> i64 {
Expand Down
8 changes: 6 additions & 2 deletions tests/helium-sub-daos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,14 +879,18 @@ describe("helium-sub-daos", () => {
const preMobileBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(rewardsEscrow))?.data!
).amount;
await program.methods
const { pubkeys: { prevSubDaoEpochInfo, daoEpochInfo } } = await program.methods
.issueRewardsV0({
epoch,
})
.accounts({
subDao,
})
.rpc({ skipPreflight: true });
.rpcAndKeys({ skipPreflight: true });

console.log("subDaoEpochInfo", await program.account.subDaoEpochInfoV0.fetch(subDaoEpochInfo!));
console.log("prevSubDaoEpochInfo", await program.account.subDaoEpochInfoV0.fetch(prevSubDaoEpochInfo!));
console.log("daoEpochInfo", await program.account.daoEpochInfoV0.fetch(daoEpochInfo!));

const postBalance = AccountLayout.decode(
(await provider.connection.getAccountInfo(treasury))?.data!
Expand Down

0 comments on commit f5924a2

Please sign in to comment.