forked from jito-foundation/distributor
-
Notifications
You must be signed in to change notification settings - Fork 20
Storage subset merkle tree onchain #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
defi0x1
wants to merge
13
commits into
jup-ag:master
Choose a base branch
from
defi0x1:add_partial_merkle_tree
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
501b248
fixed potential aligment issues
defi0x1 f28fd2b
add parent vault
defi0x1 d968a51
store subset of merkle tree onchain
defi0x1 0000595
add unit-test merkle tree
defi0x1 e5432ef
removed unused error code
defi0x1 ff8e49b
add unit-test distribution vault
defi0x1 f4ddcca
use math
defi0x1 513f51c
remove unused param
defi0x1 f463e20
canopy tree & distributor root setup
defi0x1 a2dfe7b
update unit test
defi0x1 e685b2f
update unit test
defi0x1 a764f5d
testcase canopy is root
defi0x1 f9211c6
clean code
defi0x1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
programs/merkle-distributor/src/instructions/admin/create_canopy_tree.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| use crate::{ | ||
| error::ErrorCode, | ||
| state::{canopy_tree::CanopyTree, merkle_distributor::MerkleDistributor}, | ||
| }; | ||
| use anchor_lang::prelude::*; | ||
|
|
||
| #[derive(Accounts)] | ||
| #[instruction(depth: u8)] | ||
| pub struct CreateCanopyTree<'info> { | ||
| /// [CanopyTree] | ||
| #[account( | ||
| init, | ||
| seeds = [ | ||
| b"CanopyTree".as_ref(), | ||
| distributor.key().to_bytes().as_ref(), | ||
| ], | ||
| bump, | ||
| space = CanopyTree::space(depth as usize), | ||
| payer = payer | ||
| )] | ||
| pub canopy_tree: Account<'info, CanopyTree>, | ||
|
|
||
| /// The [MerkleDistributor]. | ||
| pub distributor: AccountLoader<'info, MerkleDistributor>, | ||
|
|
||
| /// Payer wallet, responsible for creating the distributor and paying for the transaction. | ||
| #[account(mut)] | ||
| pub payer: Signer<'info>, | ||
|
|
||
| /// The [System] program. | ||
| pub system_program: Program<'info, System>, | ||
| } | ||
|
|
||
| pub fn handle_create_canopy_tree( | ||
| ctx: Context<CreateCanopyTree>, | ||
| depth: u8, | ||
| root: [u8; 32], | ||
| canopy_nodes: Vec<[u8; 32]>, | ||
| ) -> Result<()> { | ||
| let canopy_tree = &mut ctx.accounts.canopy_tree; | ||
|
|
||
| let verify_canopy_root = canopy_tree.verify_canopy_root(root, canopy_nodes.clone()); | ||
| require!(verify_canopy_root, ErrorCode::CanopyRootMissMatch); | ||
|
|
||
| canopy_tree.root = root; | ||
| canopy_tree.depth = depth; | ||
| canopy_tree.nodes = canopy_nodes; | ||
| canopy_tree.distributor = ctx.accounts.distributor.key(); | ||
|
|
||
| Ok(()) | ||
| } |
73 changes: 73 additions & 0 deletions
73
programs/merkle-distributor/src/instructions/admin/fund_distributor_root.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| use anchor_lang::{ | ||
| accounts::{account::Account, program::Program}, | ||
| context::Context, | ||
| prelude::*, | ||
| Accounts, Result, ToAccountInfo, | ||
| }; | ||
| use anchor_spl::{ | ||
| associated_token::AssociatedToken, | ||
| token::{self, Mint, Token, TokenAccount}, | ||
| }; | ||
|
|
||
| use crate::state::distributor_root::DistributorRoot; | ||
|
|
||
| /// Accounts required for distributing tokens from the parent vault to distributor vaults. | ||
| #[derive(Accounts)] | ||
| pub struct FundDistributorRoot<'info> { | ||
| /// The [DistributorRoot] | ||
| #[account(mut, has_one = mint)] | ||
| pub distributor_root: AccountLoader<'info, DistributorRoot>, | ||
|
|
||
| /// Distributor root vault | ||
| #[account( | ||
| init_if_needed, | ||
| associated_token::mint = mint, | ||
| associated_token::authority = distributor_root, | ||
| payer = payer | ||
| )] | ||
| pub distributor_root_vault: Account<'info, TokenAccount>, | ||
|
|
||
| /// The mint to distribute. | ||
| pub mint: Account<'info, Mint>, | ||
|
|
||
| /// Payer. | ||
| #[account(mut)] | ||
| pub payer: Signer<'info>, | ||
|
|
||
| /// Payer Token Account. | ||
| #[account(mut)] | ||
| pub payer_token: Account<'info, TokenAccount>, | ||
|
|
||
| /// The [System] program. | ||
| pub system_program: Program<'info, System>, | ||
|
|
||
| /// The [Token] program. | ||
| pub token_program: Program<'info, Token>, | ||
|
|
||
| // Associated token program. | ||
| pub associated_token_program: Program<'info, AssociatedToken>, | ||
| } | ||
|
|
||
| pub fn handle_fund_distributor_root( | ||
| ctx: Context<FundDistributorRoot>, | ||
| max_amount: u64, | ||
| ) -> Result<()> { | ||
| let fund_amount = { | ||
| let mut distributor_root = ctx.accounts.distributor_root.load_mut()?; | ||
| distributor_root.get_and_set_fund_amount(max_amount)? | ||
| }; | ||
|
|
||
| token::transfer( | ||
| CpiContext::new( | ||
| ctx.accounts.token_program.to_account_info(), | ||
| token::Transfer { | ||
| from: ctx.accounts.payer_token.to_account_info(), | ||
| to: ctx.accounts.distributor_root_vault.to_account_info(), | ||
| authority: ctx.accounts.payer.to_account_info(), | ||
| }, | ||
| ), | ||
| fund_amount, | ||
| )?; | ||
|
|
||
| Ok(()) | ||
| } |
78 changes: 78 additions & 0 deletions
78
programs/merkle-distributor/src/instructions/admin/fund_merkle_distributor_from_root.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| use anchor_lang::{ | ||
| accounts::{account::Account, program::Program}, | ||
| context::{Context, CpiContext}, | ||
| prelude::*, | ||
| Accounts, Result, ToAccountInfo, | ||
| }; | ||
| use anchor_spl::token::{self, Token, TokenAccount}; | ||
|
|
||
| use crate::state::{distributor_root::DistributorRoot, merkle_distributor::MerkleDistributor}; | ||
|
|
||
| /// Accounts required for distributing tokens from the parent vault to distributor vaults. | ||
| #[derive(Accounts)] | ||
| pub struct FundMerkleDisitributorFromRoot<'info> { | ||
| /// The [DistributorRoot]. | ||
| pub distributor_root: AccountLoader<'info, DistributorRoot>, | ||
|
|
||
| /// Distributor root vault containing the tokens to distribute to distributor vault. | ||
| #[account( | ||
| mut, | ||
| associated_token::mint = distributor_root.load()?.mint, | ||
| associated_token::authority = distributor_root.key(), | ||
| address = distributor_root.load()?.distributor_root_vault, | ||
| )] | ||
| pub distributor_root_vault: Account<'info, TokenAccount>, | ||
|
|
||
| /// The [MerkleDistributor]. | ||
| #[account(mut, constraint = distributor.load()?.distributor_root == distributor_root.key())] | ||
| pub distributor: AccountLoader<'info, MerkleDistributor>, | ||
|
|
||
| /// Distributor vault | ||
| #[account( | ||
| mut, | ||
| associated_token::mint = distributor.load()?.mint, | ||
| associated_token::authority = distributor.key(), | ||
| )] | ||
| pub distributor_vault: Account<'info, TokenAccount>, | ||
|
|
||
| /// SPL [Token] program. | ||
| pub token_program: Program<'info, Token>, | ||
| } | ||
|
|
||
| /// Handles the distribution of tokens from the parent vault to multiple distributor vaults. | ||
| pub fn handle_fund_merkle_distributor_from_root<'info>( | ||
| ctx: Context<'_, '_, '_, 'info, FundMerkleDisitributorFromRoot<'info>>, | ||
| ) -> Result<()> { | ||
| let distributor_root = ctx.accounts.distributor_root.load()?; | ||
| let signer = distributor_root.signer(); | ||
| let seeds = signer.seeds(); | ||
|
|
||
| let mut distributor_state = ctx.accounts.distributor.load_mut()?; | ||
|
|
||
| // Check distributor has been funded token | ||
| if distributor_state.funded_amount == 0 { | ||
| let fund_amount = distributor_state.max_total_claim; | ||
| token::transfer( | ||
| CpiContext::new( | ||
| ctx.accounts.token_program.to_account_info(), | ||
| token::Transfer { | ||
| from: ctx.accounts.distributor_root_vault.to_account_info(), | ||
| to: ctx.accounts.distributor_vault.to_account_info(), | ||
| authority: ctx.accounts.distributor_root.to_account_info(), | ||
| }, | ||
| ) | ||
| .with_signer(&[&seeds[..]]), | ||
| fund_amount, | ||
| )?; | ||
|
|
||
| distributor_state.accumulate_funded_amount(fund_amount)?; | ||
|
|
||
| msg!( | ||
| "Funded {} tokens to distributor version {}.", | ||
| fund_amount, | ||
| distributor_state.version | ||
| ); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| use crate::error::ErrorCode::ArithmeticError; | ||
| use crate::state::distributor_root::DistributorRoot; | ||
| use crate::state::merkle_distributor::{ActivationType, ClaimType}; | ||
| use crate::{ | ||
| error::ErrorCode, | ||
| state::merkle_distributor::{AirdropBonus, MerkleDistributor}, | ||
| }; | ||
| use anchor_lang::{account, context::Context, prelude::*, Accounts, Key, ToAccountInfo}; | ||
| use anchor_spl::associated_token::AssociatedToken; | ||
| use anchor_spl::token::{Mint, Token, TokenAccount}; | ||
|
|
||
| #[cfg(feature = "localnet")] | ||
|
|
@@ -13,10 +15,9 @@ const SECONDS_PER_DAY: i64 = 0; | |
| #[cfg(not(feature = "localnet"))] | ||
| const SECONDS_PER_DAY: i64 = 24 * 3600; // 24 hours * 3600 seconds | ||
|
|
||
| #[derive(AnchorSerialize, AnchorDeserialize, InitSpace)] | ||
| #[derive(AnchorSerialize, AnchorDeserialize, Debug)] | ||
| pub struct NewDistributorParams { | ||
| pub version: u64, | ||
| pub root: [u8; 32], | ||
| pub total_claim: u64, | ||
| pub max_num_nodes: u64, | ||
| pub start_vesting_ts: i64, | ||
|
|
@@ -29,7 +30,7 @@ pub struct NewDistributorParams { | |
| pub bonus_vesting_duration: u64, | ||
| pub claim_type: u8, | ||
| pub operator: Pubkey, | ||
| pub locker: Pubkey, | ||
| pub locker: Pubkey | ||
| } | ||
|
|
||
| impl NewDistributorParams { | ||
|
|
@@ -123,10 +124,14 @@ pub struct NewDistributor<'info> { | |
| ], | ||
| bump, | ||
| space = 8 + MerkleDistributor::INIT_SPACE, | ||
| payer = admin | ||
| payer = payer | ||
| )] | ||
| pub distributor: AccountLoader<'info, MerkleDistributor>, | ||
|
|
||
| /// The [DistributorRoot]. | ||
| #[account(mut)] | ||
| pub distributor_root: AccountLoader<'info, DistributorRoot>, | ||
|
|
||
| /// Base key of the distributor. | ||
| pub base: Signer<'info>, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use admin as unchecked_account, and payer to pay for rent There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
|
|
@@ -140,21 +145,28 @@ pub struct NewDistributor<'info> { | |
| /// Token vault | ||
| /// Should create previously | ||
| #[account( | ||
| init_if_needed, | ||
| associated_token::mint = mint, | ||
| associated_token::authority=distributor, | ||
| payer = payer | ||
| )] | ||
| pub token_vault: Account<'info, TokenAccount>, | ||
|
|
||
| /// Admin wallet, responsible for creating the distributor and paying for the transaction. | ||
| /// Also has the authority to set the clawback receiver and change itself. | ||
| /// CHECK: This account is not use to read or write | ||
| pub admin: UncheckedAccount<'info>, | ||
|
|
||
| /// Payer wallet, responsible for creating the distributor and paying for the transaction. | ||
| #[account(mut)] | ||
| pub admin: Signer<'info>, | ||
| pub payer: Signer<'info>, | ||
|
|
||
| /// The [System] program. | ||
| pub system_program: Program<'info, System>, | ||
|
|
||
| /// The [Token] program. | ||
| pub token_program: Program<'info, Token>, | ||
|
|
||
| // Associated token program. | ||
| pub associated_token_program: Program<'info, AssociatedToken>, | ||
| } | ||
|
|
||
| /// Creates a new [MerkleDistributor]. | ||
|
|
@@ -172,12 +184,9 @@ pub fn handle_new_distributor( | |
| params: &NewDistributorParams, | ||
| ) -> Result<()> { | ||
| params.validate()?; | ||
|
|
||
| let mut distributor = ctx.accounts.distributor.load_init()?; | ||
|
|
||
| distributor.bump = *ctx.bumps.get("distributor").unwrap(); | ||
| distributor.version = params.version; | ||
| distributor.root = params.root; | ||
| distributor.mint = ctx.accounts.mint.key(); | ||
| distributor.token_vault = ctx.accounts.token_vault.key(); | ||
| distributor.max_total_claim = params.get_max_total_claim()?; | ||
|
|
@@ -200,6 +209,7 @@ pub fn handle_new_distributor( | |
| distributor.activation_type = params.activation_type; | ||
| distributor.operator = params.operator; | ||
| distributor.locker = params.locker; | ||
| distributor.distributor_root = ctx.accounts.distributor_root.key(); | ||
|
|
||
| // Note: might get truncated, do not rely on | ||
| msg! { | ||
|
|
@@ -220,5 +230,11 @@ pub fn handle_new_distributor( | |
| distributor.claim_type, | ||
| }; | ||
|
|
||
| drop(distributor); | ||
|
|
||
| // increase total distributor created | ||
| let mut distributor_root = ctx.accounts.distributor_root.load_mut()?; | ||
| distributor_root.update_new_distributor()?; | ||
|
|
||
| Ok(()) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use init_if_needed for
token_vaultThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done