-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
227 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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 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 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,31 @@ | ||
[package] | ||
name = "cardinal-use-invalidator" | ||
version = "0.0.3" | ||
description = "Cardinal usage invalidator and counter" | ||
edition = "2021" | ||
homepage = "https://cardinal.so" | ||
repository = "https://github.com/cardinal-labs/cardinal-token-manager/tree/main/programs/cardinal-time-invalidator" | ||
authors = ["Jeremy Bogle <[email protected]>"] | ||
license = "AGPL-3.0" | ||
keywords = ["solana", "cardinal"] | ||
|
||
[lib] | ||
crate-type = ["cdylib", "lib"] | ||
name = "cardinal_use_invalidator" | ||
|
||
[features] | ||
no-entrypoint = [] | ||
no-idl = [] | ||
cpi = ["no-entrypoint"] | ||
default = [] | ||
|
||
[dependencies] | ||
anchor-lang = "0.20.1" | ||
anchor-spl = "0.20.1" | ||
spl-associated-token-account = "1.0.2" | ||
spl-token = { version = "3.1.1", features = ["no-entrypoint"] } | ||
solana-program = "1.8.1" | ||
cardinal-token-manager = { version = "^0.0.3", path = "../cardinal-token-manager", features = ["cpi"] } | ||
|
||
[dev-dependencies] | ||
proptest = { version = "1.0" } |
This file contains 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,5 @@ | ||
# Use invalidator | ||
|
||
Program for a invalidating token managers based on usages | ||
|
||
More in-depth documentation pending. |
This file contains 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,2 @@ | ||
[target.bpfel-unknown-unknown.dependencies.std] | ||
features = [] |
This file contains 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,13 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
#[error] | ||
pub enum ErrorCode { | ||
#[msg("Token account not owned by the claim approver")] | ||
InvalidPaymentTokenAccount, | ||
#[msg("Token account not owned by the issuer")] | ||
InvalidIssuerTokenAccount, | ||
#[msg("Invalid token manager for this claim approver")] | ||
InvalidTokenManager, | ||
#[msg("Usages not at the maximum")] | ||
InvalidUsages | ||
} |
24 changes: 24 additions & 0 deletions
24
programs/cardinal-use-invalidator/src/instructions/increment_usages.rs
This file contains 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,24 @@ | ||
use { | ||
crate::{state::*, errors::*}, | ||
anchor_lang::{prelude::*}, | ||
cardinal_token_manager::{state::TokenManager}, | ||
}; | ||
|
||
#[derive(Accounts)] | ||
#[instruction(num_usages: u64)] | ||
pub struct IncrementUsagesCtx<'info> { | ||
token_manager: Box<Account<'info, TokenManager>>, | ||
|
||
#[account(mut, constraint = use_invalidator.usages + num_usages <= use_invalidator.max_usages @ ErrorCode::InvalidUsages)] | ||
use_invalidator: Box<Account<'info, UseInvalidator>>, | ||
|
||
#[account(mut)] | ||
payer: Signer<'info>, | ||
system_program: Program<'info, System>, | ||
} | ||
|
||
pub fn handler(ctx: Context<IncrementUsagesCtx>, num_usages: u64) -> ProgramResult { | ||
let use_invalidator = &mut ctx.accounts.use_invalidator; | ||
use_invalidator.usages += num_usages; | ||
return Ok(()) | ||
} |
30 changes: 30 additions & 0 deletions
30
programs/cardinal-use-invalidator/src/instructions/init.rs
This file contains 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,30 @@ | ||
use { | ||
crate::{state::*}, | ||
anchor_lang::{prelude::*}, | ||
cardinal_token_manager::{state::TokenManager}, | ||
}; | ||
|
||
#[derive(Accounts)] | ||
#[instruction(bump: u8, _max_usages: u64)] | ||
pub struct InitCtx<'info> { | ||
token_manager: Box<Account<'info, TokenManager>>, | ||
|
||
#[account( | ||
init, | ||
payer = payer, | ||
space = USE_INVALIDATOR_SIZE, | ||
seeds = [USE_INVALIDATOR_SEED.as_bytes(), token_manager.key().as_ref()], bump = bump, | ||
)] | ||
use_invalidator: Box<Account<'info, UseInvalidator>>, | ||
|
||
#[account(mut)] | ||
payer: Signer<'info>, | ||
system_program: Program<'info, System>, | ||
} | ||
|
||
pub fn handler(ctx: Context<InitCtx>, bump: u8, max_usages: u64) -> ProgramResult { | ||
let use_invalidator = &mut ctx.accounts.use_invalidator; | ||
use_invalidator.bump = bump; | ||
use_invalidator.max_usages = max_usages; | ||
return Ok(()) | ||
} |
50 changes: 50 additions & 0 deletions
50
programs/cardinal-use-invalidator/src/instructions/invalidate.rs
This file contains 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,50 @@ | ||
use { | ||
crate::{state::*, errors::*}, | ||
anchor_lang::{prelude::*}, | ||
cardinal_token_manager::{program::CardinalTokenManager, state::{TokenManager}}, | ||
}; | ||
|
||
#[derive(Accounts)] | ||
pub struct InvalidateCtx<'info> { | ||
#[account(mut)] | ||
token_manager: Box<Account<'info, TokenManager>>, | ||
|
||
#[account(mut, | ||
constraint = use_invalidator.usages >= use_invalidator.max_usages @ ErrorCode::InvalidUsages, | ||
close = invalidator | ||
)] | ||
use_invalidator: Box<Account<'info, UseInvalidator>>, | ||
|
||
#[account(mut)] | ||
invalidator: Signer<'info>, | ||
|
||
cardinal_token_manager: Program<'info, CardinalTokenManager>, | ||
|
||
// cpi accounts | ||
token_manager_token_account: UncheckedAccount<'info>, | ||
token_program: UncheckedAccount<'info>, | ||
mint: UncheckedAccount<'info>, | ||
recipient_token_account: UncheckedAccount<'info>, | ||
issuer_token_account: UncheckedAccount<'info>, | ||
} | ||
|
||
pub fn handler(ctx: Context<InvalidateCtx>) -> ProgramResult { | ||
let token_manager_key = ctx.accounts.token_manager.key(); | ||
let use_invalidator_seeds = &[USE_INVALIDATOR_SEED.as_bytes(), token_manager_key.as_ref(), &[ctx.accounts.use_invalidator.bump]]; | ||
let use_invalidator_signer = &[&use_invalidator_seeds[..]]; | ||
|
||
// invalidate | ||
let cpi_accounts = cardinal_token_manager::cpi::accounts::InvalidateCtx { | ||
token_manager: ctx.accounts.token_manager.to_account_info(), | ||
token_manager_token_account: ctx.accounts.token_manager_token_account.to_account_info(), | ||
mint: ctx.accounts.mint.to_account_info(), | ||
recipient_token_account: ctx.accounts.recipient_token_account.to_account_info(), | ||
issuer_token_account: ctx.accounts.issuer_token_account.to_account_info(), | ||
invalidator: ctx.accounts.invalidator.to_account_info(), | ||
token_program: ctx.accounts.token_program.to_account_info(), | ||
}; | ||
let cpi_ctx = CpiContext::new(ctx.accounts.cardinal_token_manager.to_account_info(), cpi_accounts).with_signer(use_invalidator_signer); | ||
cardinal_token_manager::cpi::invalidate(cpi_ctx)?; | ||
|
||
return Ok(()) | ||
} |
This file contains 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,7 @@ | ||
pub mod init; | ||
pub mod increment_usages; | ||
pub mod invalidate; | ||
|
||
pub use init::*; | ||
pub use increment_usages::*; | ||
pub use invalidate::*; |
This file contains 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,24 @@ | ||
pub mod instructions; | ||
pub mod state; | ||
pub mod errors; | ||
|
||
use {anchor_lang::prelude::*, instructions::*}; | ||
|
||
declare_id!("useB5qbYZgjE14qXxWx17Zm4JS5bzWrDcWXt3uBq62L"); | ||
|
||
#[program] | ||
pub mod cardinal_use_invalidator { | ||
use super::*; | ||
|
||
pub fn init(ctx: Context<InitCtx>, bump: u8, max_usages: u64) -> ProgramResult { | ||
init::handler(ctx, bump, max_usages) | ||
} | ||
|
||
pub fn increment_usages(ctx: Context<IncrementUsagesCtx>, num_usages: u64) -> ProgramResult { | ||
increment_usages::handler(ctx, num_usages) | ||
} | ||
|
||
pub fn invalidate(ctx: Context<InvalidateCtx>) -> ProgramResult { | ||
invalidate::handler(ctx) | ||
} | ||
} |
This file contains 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,11 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
pub const USE_INVALIDATOR_SEED: &str = "use-invalidator"; | ||
pub const USE_INVALIDATOR_SIZE: usize = 8 + std::mem::size_of::<UseInvalidator>(); | ||
#[account] | ||
pub struct UseInvalidator { | ||
pub bump: u8, | ||
pub usages: u64, | ||
pub max_usages: u64, | ||
pub use_authority: Pubkey, | ||
} |
This file contains 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