-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add interest bearing mint extension #4464
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
jamie-osec
wants to merge
8
commits into
otter-sec:master
Choose a base branch
from
jamie-osec:interest-bearing-mint
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
8 commits
Select commit
Hold shift + click to select a range
d8cb842
add interest bearing mint rate extension
Bhargavamacha 1d1a6fc
add test
Bhargavamacha de1bb5d
fix(lang/syn): correct error message in interest-bearing authority he…
jamie-osec 6937688
fix(lang/syn): allocate InterestBearingConfig when only authority is set
jamie-osec c4ad64b
fix(lang/syn): scope-check interest-bearing rate and authority exprs
jamie-osec f396e66
feat(lang/syn): validate interest-bearing extension on non-init mints
jamie-osec fd78c99
Merge branch 'master' into interest-bearing-mint
0x4ka5h a677986
Merge remote-tracking branch 'otter-sec/master' into interest-bearing…
jamie-osec 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
Some comments aren't visible on the classic Files Changed page.
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
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 |
|---|---|---|
|
|
@@ -784,6 +784,8 @@ fn generate_constraint_init_group( | |
| metadata_pointer_metadata_address, | ||
| close_authority, | ||
| permanent_delegate, | ||
| interest_bearing_mint_rate, | ||
| interest_bearing_mint_authority, | ||
| transfer_hook_authority, | ||
| transfer_hook_program_id, | ||
| pausable_authority, | ||
|
|
@@ -856,6 +858,24 @@ fn generate_constraint_init_group( | |
| None => quote! {}, | ||
| }; | ||
|
|
||
| let interest_bearing_mint_rate_check = match interest_bearing_mint_rate { | ||
| Some(r) => check_scope.generate_check(r), | ||
| None => quote! {}, | ||
| }; | ||
|
|
||
| let interest_bearing_mint_authority_check = match interest_bearing_mint_authority { | ||
| Some(a) => check_scope.generate_check(a), | ||
| None => quote! {}, | ||
| }; | ||
|
|
||
| let if_needed_interest_bearing_check = generate_interest_bearing_extension_check( | ||
| &account_ref, | ||
| &name_str, | ||
| interest_bearing_mint_rate.as_ref(), | ||
| interest_bearing_mint_authority.as_ref(), | ||
| &mut check_scope, | ||
| ); | ||
|
|
||
| let system_program_optional_check = check_scope.generate_check(system_program); | ||
| let token_program_optional_check = check_scope.generate_check(&token_program); | ||
| let rent_optional_check = check_scope.generate_check(rent); | ||
|
|
@@ -877,6 +897,8 @@ fn generate_constraint_init_group( | |
| #transfer_hook_program_id_check | ||
| #permanent_delegate_check | ||
| #pausable_authority_check | ||
| #interest_bearing_mint_rate_check | ||
| #interest_bearing_mint_authority_check | ||
| }; | ||
|
|
||
| let payer_optional_check = check_scope.generate_check(payer); | ||
|
|
@@ -912,6 +934,10 @@ fn generate_constraint_init_group( | |
| extensions.push(quote! {::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::Pausable}) | ||
| } | ||
|
|
||
| if interest_bearing_mint_rate.is_some() || interest_bearing_mint_authority.is_some() { | ||
|
Collaborator
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. if I understand it correctly if only |
||
| extensions.push(quote! {::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::InterestBearingConfig}); | ||
| } | ||
|
|
||
| let mint_space = if extensions.is_empty() { | ||
| quote! { ::anchor_spl::token::Mint::LEN } | ||
| } else { | ||
|
|
@@ -971,6 +997,18 @@ fn generate_constraint_init_group( | |
| None => quote! { Option::<&anchor_lang::prelude::Pubkey>::None }, | ||
| }; | ||
|
|
||
| let interest_bearing_mint_rate = match interest_bearing_mint_rate { | ||
| Some(ibmr) => quote! { #ibmr }, | ||
| None => quote! { 0i16 }, | ||
| }; | ||
|
|
||
| let interest_bearing_mint_authority = match interest_bearing_mint_authority { | ||
| Some(ibma) => { | ||
| quote! { Option::<anchor_lang::prelude::Pubkey>::Some(#ibma.key()) } | ||
| } | ||
| None => quote! { Option::<anchor_lang::prelude::Pubkey>::None }, | ||
| }; | ||
|
|
||
| let transfer_hook_authority = match transfer_hook_authority { | ||
| Some(tha) => quote! { Option::<anchor_lang::prelude::Pubkey>::Some(#tha.key()) }, | ||
| None => quote! { Option::<anchor_lang::prelude::Pubkey>::None }, | ||
|
|
@@ -1068,6 +1106,12 @@ fn generate_constraint_init_group( | |
| mint: #field.to_account_info(), | ||
| }), #pausable_authority.unwrap())?; | ||
| }, | ||
| ::anchor_spl::token_interface::spl_token_2022::extension::ExtensionType::InterestBearingConfig => { | ||
| ::anchor_spl::token_interface::interest_bearing_mint_initialize(anchor_lang::context::CpiContext::new(cpi_program_id, ::anchor_spl::token_interface::InterestBearingMintInitialize { | ||
| token_program_id: #token_program.to_account_info(), | ||
| mint: #field.to_account_info(), | ||
| }), #interest_bearing_mint_authority, #interest_bearing_mint_rate)?; | ||
| }, | ||
| // All extensions specified by the user should be implemented. | ||
| // If this line runs, it means there is a bug in the codegen. | ||
| _ => unimplemented!("{e:?}"), | ||
|
|
@@ -1100,6 +1144,7 @@ fn generate_constraint_init_group( | |
| if owner_program != &#token_program.key() { | ||
| return Err(anchor_lang::error::Error::from(anchor_lang::error::ErrorCode::ConstraintMintTokenProgram).with_account_name(#name_str).with_pubkeys((*owner_program, #token_program.key()))); | ||
| } | ||
| #if_needed_interest_bearing_check | ||
| } | ||
| Ok(pa) | ||
| }})()?; | ||
|
|
@@ -1647,6 +1692,14 @@ fn generate_constraint_mint( | |
| None => quote! {}, | ||
| }; | ||
|
|
||
| let interest_bearing_check = generate_interest_bearing_extension_check( | ||
| &account_ref, | ||
| &name.to_string(), | ||
| c.interest_bearing_mint_rate.as_ref(), | ||
| c.interest_bearing_mint_authority.as_ref(), | ||
| &mut optional_check_scope, | ||
| ); | ||
|
|
||
| quote! { | ||
| { | ||
| #decimal_check | ||
|
|
@@ -1664,10 +1717,56 @@ fn generate_constraint_mint( | |
| #transfer_hook_authority_check | ||
| #transfer_hook_program_id_check | ||
| #pausable_authority_check | ||
| #interest_bearing_check | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn generate_interest_bearing_extension_check( | ||
| account_ref: &proc_macro2::TokenStream, | ||
| name_str: &str, | ||
| rate: Option<&Expr>, | ||
| authority: Option<&Expr>, | ||
| optional_check_scope: &mut OptionalCheckScope, | ||
| ) -> proc_macro2::TokenStream { | ||
| if rate.is_none() && authority.is_none() { | ||
| return quote! {}; | ||
| } | ||
| let rate_check = match rate { | ||
| Some(rate) => quote! { | ||
| if i16::from(interest_bearing.current_rate) != #rate { | ||
| return Err(anchor_lang::error::Error::from( | ||
| anchor_lang::error::ErrorCode::ConstraintMintInterestBearingRate, | ||
| ).with_account_name(#name_str)); | ||
| } | ||
| }, | ||
| None => quote! {}, | ||
| }; | ||
| let authority_check = match authority { | ||
| Some(authority) => { | ||
| let authority_optional_check = optional_check_scope.generate_check(authority); | ||
| quote! { | ||
| #authority_optional_check | ||
| if interest_bearing.rate_authority != ::anchor_spl::token_2022_extensions::spl_pod::optional_keys::OptionalNonZeroPubkey::try_from(Some(#authority.key()))? { | ||
| return Err(anchor_lang::error::Error::from( | ||
| anchor_lang::error::ErrorCode::ConstraintMintInterestBearingAuthority, | ||
| ).with_account_name(#name_str)); | ||
| } | ||
| } | ||
| } | ||
| None => quote! {}, | ||
| }; | ||
| quote! { | ||
| let interest_bearing = ::anchor_spl::token_interface::get_mint_extension_data::< | ||
| ::anchor_spl::token_interface::spl_token_2022::extension::interest_bearing_mint::InterestBearingConfig, | ||
| >(#account_ref).map_err(|_| anchor_lang::error::Error::from( | ||
| anchor_lang::error::ErrorCode::ConstraintMintInterestBearingExtension, | ||
| ).with_account_name(#name_str))?; | ||
| #rate_check | ||
| #authority_check | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Debug)] | ||
| pub struct OptionalCheckScope<'a> { | ||
| seen: HashSet<String>, | ||
|
|
||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.