Skip to content

Commit a8065e2

Browse files
authored
Merge pull request #14 from sunrise-stake/feature/yield-router-program-documentation
Add documentation to yield-router program
2 parents 1e9ec1d + 80c3957 commit a8065e2

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

programs/yield-router/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub mod yield_router {
1616
sunrise_state: Pubkey,
1717
state_in: GenericStateInput,
1818
) -> Result<()> {
19+
// register state account on chain, only need to ever be done once
1920
let state = &mut ctx.accounts.state;
2021
state.sunrise_state = sunrise_state;
2122
state.update_authority = state_in.update_authority;
@@ -24,6 +25,7 @@ pub mod yield_router {
2425
state.spend_proportions = state_in.spend_proportions;
2526
state.input_yield_account_bump = *ctx.bumps.get("input_yield_account").unwrap();
2627

28+
// make sure the input proportions sum up to 100
2729
check_proportions(&state.spend_proportions)?;
2830

2931
state.total_spent = 0;
@@ -32,12 +34,14 @@ pub mod yield_router {
3234
}
3335

3436
pub fn update_state(ctx: Context<UpdateState>, state_in: GenericStateInput) -> Result<()> {
37+
// update state account parameters
3538
let state = &mut ctx.accounts.state;
3639
state.update_authority = state_in.update_authority;
3740
state.output_yield_accounts = state_in.output_yield_accounts;
3841
state.spend_threshold = state_in.spend_threshold;
3942
state.spend_proportions = state_in.spend_proportions;
4043

44+
// make sure the new proportions sum up to 100
4145
check_proportions(&state.spend_proportions)?;
4246

4347
Ok(())
@@ -47,15 +51,18 @@ pub mod yield_router {
4751
ctx: Context<'_, '_, '_, 'info, AllocateYield<'info>>,
4852
amount: u64,
4953
) -> Result<()> {
54+
// send yield to output_yield_accounts with specified proportions
5055
let state = &mut ctx.accounts.state;
5156
let input_yield_account = &mut ctx.accounts.input_yield_account;
5257

5358
// check output yield accounts
59+
// Question: why do we need in additional input `remaining_accounts`?
60+
// Why not just loop through the `output_yield_accounts`?
5461
if ctx.remaining_accounts.len() != state.output_yield_accounts.len() {
5562
return Err(ErrorCode::IncorrectOutputYieldAccount.into());
5663
}
5764

58-
// send to each output yield account
65+
// loop through all output yield accounts
5966
for i in 0..ctx.remaining_accounts.len() {
6067
let output_yield_account = &ctx.remaining_accounts[i];
6168
let proportion = state.spend_proportions[i];
@@ -64,8 +71,10 @@ pub mod yield_router {
6471
return Err(ErrorCode::IncorrectOutputYieldAccount.into());
6572
}
6673

74+
// compute the amount to be send to this output_yield_account based on the specifed proportion
6775
let amount_to_send = (amount as f64 * (proportion as f64 / 100.0)) as u64;
6876

77+
// send appropriate fund to this output_yield_account
6978
transfer_native_cpi(
7079
&state.key(),
7180
&input_yield_account.to_account_info(),

programs/yield-router/src/utils/spend.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anchor_lang::prelude::*;
44
use anchor_lang::system_program;
55

66
pub fn check_proportions(spend_proportions: &[u8]) -> Result<()> {
7-
// check proportions sum to 100
7+
// check proportions to be spend on different output yield accounts sum to 100
88
let mut sum = 0;
99
for proportion in spend_proportions.iter() {
1010
sum += proportion;
@@ -23,9 +23,11 @@ pub fn transfer_native_cpi<'a>(
2323
source_bump: u8,
2424
system_program: &Program<'a, System>,
2525
) -> Result<()> {
26+
// transfer `amount` (in lamports) from `source` account to `dest` account
2627
let state_bytes = state.to_bytes();
2728
let bump_bytes = &[source_bump];
2829
let seeds = &[INPUT_YIELD_ACCOUNT, &state_bytes[..], bump_bytes][..];
30+
// Question: how many signers are here? why are they signing?
2931
let signer_seeds = &[seeds];
3032
let cpi_ctx = CpiContext::new(
3133
system_program.to_account_info(),

programs/yield-router/src/utils/state.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@ use anchor_lang::prelude::*;
55
/* This struct will be used for both registering and updating the state account */
66
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
77
pub struct GenericStateInput {
8+
// an account that can update the `output_yield_accounts` and `spend_proportions`
89
pub update_authority: Pubkey,
10+
// a vector of accounts to which we will send yields to
911
pub output_yield_accounts: Vec<Pubkey>,
10-
pub spend_proportions: Vec<u8>, // sum to 100
12+
// proportions of sum to send to each of the accounts in `output_yield_accounts` (sum to 100)
13+
pub spend_proportions: Vec<u8>,
14+
// minimum threshold of yield in input_yield_account before it is allowed to send funds
1115
pub spend_threshold: u64,
1216
}
1317

1418
#[account]
1519
pub struct State {
20+
// the state account holding all the configs from GenericStateInput and the info of total yields spent
1621
pub sunrise_state: Pubkey,
1722
pub update_authority: Pubkey,
1823
pub output_yield_accounts: Vec<Pubkey>,
@@ -24,6 +29,7 @@ pub struct State {
2429

2530
impl State {
2631
pub fn space(output_yield_account_count: u8) -> usize {
32+
// find space needed for state account for current config
2733
32 + 32
2834
+ (32 * output_yield_account_count as usize)
2935
+ 4
@@ -39,6 +45,7 @@ impl State {
3945
#[derive(Accounts)]
4046
#[instruction(sunrise_state: Pubkey, state_in: GenericStateInput)]
4147
pub struct RegisterState<'info> {
48+
// to be used for registering state account on chain, only need to ever be done once
4249
#[account(mut)]
4350
pub payer: Signer<'info>,
4451
#[account(
@@ -61,11 +68,13 @@ pub struct RegisterState<'info> {
6168
#[derive(Accounts)]
6269
#[instruction(state_in: GenericStateInput)]
6370
pub struct UpdateState<'info> {
71+
// to be used for updating parameters of state account
6472
#[account(mut)]
6573
pub payer: Signer<'info>,
6674
#[account(
6775
mut,
6876
constraint = state.update_authority == payer.key() @ ErrorCode::Unauthorized,
77+
// resize the state account if necessary
6978
realloc = State::space(state_in.output_yield_accounts.len() as u8),
7079
realloc::payer = payer,
7180
realloc::zero = false,
@@ -77,6 +86,7 @@ pub struct UpdateState<'info> {
7786
#[derive(Accounts)]
7887
#[instruction(amount: u64)]
7988
pub struct AllocateYield<'info> {
89+
// to allocate correct yield proportion to various output_yield_accounts
8090
#[account(mut)]
8191
pub payer: Signer<'info>,
8292
pub state: Account<'info, State>,

0 commit comments

Comments
 (0)