Skip to content

Commit

Permalink
Invalidation types and use non-usable items
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbogle committed Feb 5, 2022
1 parent e955447 commit 1392a4b
Show file tree
Hide file tree
Showing 25 changed files with 1,614 additions and 1,313 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cardinal/token-manager",
"version": "0.0.9",
"version": "0.0.10",
"description": "Cardinal token manager SDK",
"keywords": [
"solana",
Expand Down
6 changes: 3 additions & 3 deletions programs/cardinal-paid-claim-approver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-paid-claim-approver"
version = "0.0.6"
version = "0.0.10"
description = "Cardinal paid claim approver"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,8 +25,8 @@ 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.6", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-payment-manager = { version = "^0.0.6", path = "../cardinal-payment-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.10", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-payment-manager = { version = "^0.0.10", path = "../cardinal-payment-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
4 changes: 2 additions & 2 deletions programs/cardinal-payment-manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-payment-manager"
version = "0.0.6"
version = "0.0.10"
description = "Cardinal paid claim approver"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,7 +25,7 @@ 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.6", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.10", path = "../cardinal-token-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
4 changes: 2 additions & 2 deletions programs/cardinal-rent-receipt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-rent-receipt"
version = "0.0.6"
version = "0.0.10"
description = "Cardinal paid claim approver"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,7 +25,7 @@ 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.6", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.10", path = "../cardinal-token-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
4 changes: 2 additions & 2 deletions programs/cardinal-rent-receipt/src/instructions/claim.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
crate::{state::*},
anchor_lang::{prelude::*},
cardinal_token_manager::{program::CardinalTokenManager, state::{TokenManagerKind, TokenManager}, instructions::IssueIx},
cardinal_token_manager::{program::CardinalTokenManager, state::{TokenManagerKind, TokenManager, InvalidationType}, instructions::IssueIx},
anchor_spl::{token::{Token}}
};

Expand Down Expand Up @@ -67,7 +67,7 @@ pub fn handler(ctx: Context<ClaimCtx>, bump: u8, receipt_token_manager_bump: u8)
system_program: ctx.accounts.system_program.to_account_info(),
};
let issue_ctx = CpiContext::new(ctx.accounts.cardinal_token_manager.to_account_info(), cpi_accounts).with_signer(rent_receipt_signer);
cardinal_token_manager::cpi::issue(issue_ctx, IssueIx{amount: 1, kind: TokenManagerKind::Managed as u8})?;
cardinal_token_manager::cpi::issue(issue_ctx, IssueIx{amount: 1, kind: TokenManagerKind::Managed as u8, invalidation_type: InvalidationType::Return as u8 })?;

let cpi_accounts = cardinal_token_manager::cpi::accounts::ClaimCtx {
token_manager: ctx.accounts.receipt_token_manager.to_account_info(),
Expand Down
4 changes: 2 additions & 2 deletions programs/cardinal-rental-counter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-rental-counter"
version = "0.0.6"
version = "0.0.10"
description = "Cardinal paid claim approver"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,7 +25,7 @@ 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.6", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.10", path = "../cardinal-token-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
4 changes: 2 additions & 2 deletions programs/cardinal-time-invalidator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-time-invalidator"
version = "0.0.6"
version = "0.0.10"
description = "Program for a invalidating token managers based on time"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,7 +25,7 @@ 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.6", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.10", path = "../cardinal-token-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
2 changes: 1 addition & 1 deletion programs/cardinal-token-manager/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-token-manager"
version = "0.0.6"
version = "0.0.10"
description = "Cardinal token manager"
edition = "2021"
homepage = "https://cardinal.so"
Expand Down
2 changes: 2 additions & 0 deletions programs/cardinal-token-manager/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum ErrorCode {
InvalidInvalidatorTokenAccount,
#[msg("Token manager kind is not valid")]
InvalidTokenManagerKind,
#[msg("Invalid invalidation type")]
InvalidInvalidationType,
#[msg("Invalid claim authority")]
InvalidClaimAuthority,
#[msg("Invalid transfer authority")]
Expand Down
66 changes: 29 additions & 37 deletions programs/cardinal-token-manager/src/instructions/invalidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ pub fn handler<'key, 'accounts, 'remaining, 'info>(ctx: Context<'key, 'accounts,
let token_manager_seeds = &[TOKEN_MANAGER_SEED.as_bytes(), mint.as_ref(), &[token_manager.bump]];
let token_manager_signer = &[&token_manager_seeds[..]];

if token_manager.kind == TokenManagerKind::Managed as u8 {
// if claimed we need to thaw
if token_manager.state == TokenManagerState::Claimed as u8 {
if token_manager.state == TokenManagerState::Claimed as u8 {
if token_manager.kind == TokenManagerKind::Managed as u8 {
// thaw recipient account
let cpi_accounts = ThawAccount {
account: ctx.accounts.recipient_token_account.to_account_info(),
Expand All @@ -58,42 +57,35 @@ pub fn handler<'key, 'accounts, 'remaining, 'info>(ctx: Context<'key, 'accounts,
let cpi_program = ctx.accounts.token_program.to_account_info();
let cpi_context = CpiContext::new(cpi_program, cpi_accounts).with_signer(token_manager_signer);
token::thaw_account(cpi_context)?;

} else if token_manager.kind == TokenManagerKind::Edition as u8 {
let remaining_accs = &mut ctx.remaining_accounts.iter();
let edition_info = next_account_info(remaining_accs)?;
let metadata_program = next_account_info(remaining_accs)?;

// edition will be validated by metadata_program
assert_keys_eq!(metadata_program.key, mpl_token_metadata::id());

invoke_signed(
&freeze_delegated_account(
*metadata_program.key,
token_manager.key(),
ctx.accounts.recipient_token_account.key(),
*edition_info.key,
ctx.accounts.mint.key(),
),
&vec![
token_manager.to_account_info(),
ctx.accounts.recipient_token_account.to_account_info(),
edition_info.to_account_info(),
ctx.accounts.mint.to_account_info(),
],
&[token_manager_seeds],
)?;
}
// transfer back to issuer
let cpi_accounts = Transfer {
from: ctx.accounts.recipient_token_account.to_account_info(),
to: ctx.accounts.issuer_token_account.to_account_info(),
authority: token_manager.to_account_info(),
};
let cpi_program = ctx.accounts.token_program.to_account_info();
let cpi_context = CpiContext::new(cpi_program, cpi_accounts).with_signer(token_manager_signer);
token::transfer(cpi_context, token_manager.amount)?;
} else if token_manager.kind == TokenManagerKind::Edition as u8 {
let remaining_accs = &mut ctx.remaining_accounts.iter();
let edition_info = next_account_info(remaining_accs)?;
let metadata_program = next_account_info(remaining_accs)?;

// edition will be validated by metadata_program
assert_keys_eq!(metadata_program.key, mpl_token_metadata::id());

invoke_signed(
&freeze_delegated_account(
*metadata_program.key,
token_manager.key(),
ctx.accounts.recipient_token_account.key(),
*edition_info.key,
ctx.accounts.mint.key(),
),
&vec![
token_manager.to_account_info(),
ctx.accounts.recipient_token_account.to_account_info(),
edition_info.to_account_info(),
ctx.accounts.mint.to_account_info(),
],
&[token_manager_seeds],
)?;
}

if token_manager.invalidation_type == InvalidationType::Return as u8 {
// transfer back to issuer
let cpi_accounts = Transfer {
from: ctx.accounts.recipient_token_account.to_account_info(),
Expand All @@ -104,7 +96,7 @@ pub fn handler<'key, 'accounts, 'remaining, 'info>(ctx: Context<'key, 'accounts,
let cpi_context = CpiContext::new(cpi_program, cpi_accounts).with_signer(token_manager_signer);
token::transfer(cpi_context, token_manager.amount)?;
}

token_manager.state = TokenManagerState::Invalidated as u8;

// close token_manager_token_account
Expand Down
7 changes: 7 additions & 0 deletions programs/cardinal-token-manager/src/instructions/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
pub struct IssueIx {
pub amount: u64,
pub kind: u8,
pub invalidation_type: u8,
}

#[derive(Accounts)]
Expand Down Expand Up @@ -40,11 +41,17 @@ pub fn handler(ctx: Context<IssueCtx>, ix: IssueIx) -> ProgramResult {
&& ix.kind != TokenManagerKind::Edition as u8 {
return Err(ErrorCode::InvalidTokenManagerKind.into());
}
if ix.invalidation_type != InvalidationType::Return as u8
&& ix.invalidation_type != InvalidationType::Invalidate as u8 {
return Err(ErrorCode::InvalidInvalidationType.into());
}

// set token manager data
let token_manager = &mut ctx.accounts.token_manager;
token_manager.mint = ctx.accounts.mint.key();
token_manager.amount = ix.amount;
token_manager.kind = ix.kind;
token_manager.invalidation_type = ix.invalidation_type;
token_manager.state = TokenManagerState::Issued as u8;

// transfer token to token manager token account
Expand Down
12 changes: 11 additions & 1 deletion programs/cardinal-token-manager/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ pub enum TokenManagerKind {
Edition = 3,
}

#[derive(Clone, Debug, PartialEq, AnchorSerialize, AnchorDeserialize)]
#[repr(u8)]
pub enum InvalidationType {
/// Upon invalidation it will be returned to the issuer
Return = 1,
/// Upon invalidation it will remain marked as invalid
Invalidate = 2,
}

pub fn token_manager_size(num_invalidators: usize) -> usize {
return (8 + 32 + 1 + 1 + 32 + 32 + 8 + 1 + 1 + 32 + 32 + 32 + 32 + num_invalidators * 32) + 8 as usize
return (8 + 32 + 1 + 1 + 32 + 32 + 8 + 1 + 1 + 1 + 32 + 32 + 32 + 32 + num_invalidators * 32) + 8 as usize
}

pub const MAX_INVALIDATORS: u8 = 5;
Expand All @@ -40,6 +49,7 @@ pub struct TokenManager {
pub amount: u64,
pub kind: u8,
pub state: u8,
pub invalidation_type: u8,
pub recipient_token_account: Pubkey,
pub payment_manager: Option<Pubkey>,
pub claim_approver: Option<Pubkey>,
Expand Down
4 changes: 2 additions & 2 deletions programs/cardinal-use-invalidator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cardinal-use-invalidator"
version = "0.0.6"
version = "0.0.10"
description = "Cardinal usage invalidator and counter"
edition = "2021"
homepage = "https://cardinal.so"
Expand All @@ -25,7 +25,7 @@ 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.6", path = "../cardinal-token-manager", features = ["cpi"] }
cardinal-token-manager = { version = "^0.0.10", path = "../cardinal-token-manager", features = ["cpi"] }

[dev-dependencies]
proptest = { version = "1.0" }
Loading

0 comments on commit 1392a4b

Please sign in to comment.