Skip to content

Commit

Permalink
Merge pull request #79 from cardinal-labs/jeremy/crd-50
Browse files Browse the repository at this point in the history
CRD-50, CRD-24: Remove payment_mint and add direct payouts
  • Loading branch information
jpbogle authored Mar 6, 2022
2 parents 89358e1 + de369b6 commit cbeb969
Show file tree
Hide file tree
Showing 27 changed files with 609 additions and 545 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use {

#[derive(Accounts)]
pub struct InitCtx<'info> {
#[account(constraint =
token_manager.payment_mint != None
&& token_manager.state == TokenManagerState::Initialized as u8
@ ErrorCode::InvalidTokenManager
)]
#[account(constraint = token_manager.state == TokenManagerState::Initialized as u8 @ ErrorCode::InvalidTokenManager)]
token_manager: Box<Account<'info, TokenManager>>,

#[account(
Expand Down
14 changes: 6 additions & 8 deletions programs/cardinal-paid-claim-approver/src/instructions/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ use {
crate::{state::*, errors::ErrorCode},
anchor_lang::{prelude::*},
anchor_spl::{token::{self, Token, TokenAccount, Transfer}},
cardinal_token_manager::{program::CardinalTokenManager, state::TokenManager},
cardinal_token_manager::{program::CardinalTokenManager, state::TokenManager, utils::assert_payment_token_account},
};

#[derive(Accounts)]
pub struct PayCtx<'info> {
#[account(constraint = claim_approver.key() == token_manager.claim_approver.unwrap() @ ErrorCode::InvalidTokenManager)]
token_manager: Box<Account<'info, TokenManager>>,

#[account(mut, constraint =
token_manager.payment_mint != None
&& payment_token_account.owner == token_manager.key()
&& payment_token_account.mint == token_manager.payment_mint.unwrap()
@ ErrorCode::InvalidPaymentTokenAccount,
)]
#[account(mut, constraint = payment_token_account.mint == claim_approver.payment_mint @ ErrorCode::InvalidPaymentTokenAccount)]
payment_token_account: Box<Account<'info, TokenAccount>>,

#[account(mut)]
Expand All @@ -25,7 +20,7 @@ pub struct PayCtx<'info> {
payer: Signer<'info>,
#[account(mut, constraint =
payer_token_account.owner == payer.key()
&& payer_token_account.mint == token_manager.payment_mint.unwrap()
&& payer_token_account.mint == claim_approver.payment_mint
@ ErrorCode::InvalidPayerTokenAccount
)]
payer_token_account: Box<Account<'info, TokenAccount>>,
Expand All @@ -40,6 +35,9 @@ pub struct PayCtx<'info> {
}

pub fn handler(ctx: Context<PayCtx>) -> Result<()> {
let remaining_accs = &mut ctx.remaining_accounts.iter();
assert_payment_token_account(&ctx.accounts.payment_token_account, &ctx.accounts.token_manager, remaining_accs)?;

let cpi_accounts = Transfer {
from: ctx.accounts.payer_token_account.to_account_info(),
to: ctx.accounts.payment_token_account.to_account_info(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
crate::{errors::ErrorCode, state::*},
anchor_lang::prelude::*,
anchor_spl::token::{self, Token, TokenAccount, Transfer},
cardinal_token_manager::state::{TokenManager, TokenManagerState},
cardinal_token_manager::{state::{TokenManager, TokenManagerState}, utils::assert_payment_token_account},
};

#[derive(Accounts)]
Expand All @@ -13,18 +13,14 @@ pub struct ExtendExpirationCtx<'info> {
#[account(mut, constraint = time_invalidator.token_manager == token_manager.key() @ ErrorCode::InvalidTimeInvalidator)]
time_invalidator: Box<Account<'info, TimeInvalidator>>,

#[account(mut, constraint =
payment_token_account.owner == token_manager.key()
&& payment_token_account.mint == time_invalidator.payment_mint.unwrap()
@ ErrorCode::InvalidPaymentTokenAccount,
)]
#[account(mut, constraint = payment_token_account.mint == time_invalidator.extension_payment_mint.unwrap() @ ErrorCode::InvalidPaymentTokenAccount)]
payment_token_account: Box<Account<'info, TokenAccount>>,

#[account(mut)]
payer: Signer<'info>,
#[account(mut, constraint =
payer_token_account.owner == payer.key()
&& payer_token_account.mint == time_invalidator.payment_mint.unwrap()
&& payer_token_account.mint == time_invalidator.extension_payment_mint.unwrap()
@ ErrorCode::InvalidPayerTokenAccount
)]
payer_token_account: Box<Account<'info, TokenAccount>>,
Expand All @@ -33,11 +29,13 @@ pub struct ExtendExpirationCtx<'info> {
}

pub fn handler(ctx: Context<ExtendExpirationCtx>, payment_amount: u64) -> Result<()> {
let remaining_accs = &mut ctx.remaining_accounts.iter();
assert_payment_token_account(&ctx.accounts.payment_token_account, &ctx.accounts.token_manager, remaining_accs)?;

let time_invalidator = &mut ctx.accounts.time_invalidator;

if time_invalidator.extension_payment_amount == None
|| time_invalidator.extension_duration_seconds == None
|| time_invalidator.payment_mint == None
|| time_invalidator.extension_payment_mint == None
{
return Err(error!(ErrorCode::InvalidTimeInvalidator));
}
Expand Down
11 changes: 3 additions & 8 deletions programs/cardinal-time-invalidator/src/instructions/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct InitIx {
pub expiration: Option<i64>,
pub extension_payment_amount: Option<u64>,
pub extension_duration_seconds: Option<u64>,
pub payment_mint: Option<Pubkey>,
pub extension_payment_mint: Option<Pubkey>,
pub max_expiration: Option<i64>,
pub disable_partial_extension: Option<bool>
}
Expand Down Expand Up @@ -40,13 +40,8 @@ pub fn handler(ctx: Context<InitCtx>, ix: InitIx) -> Result<()> {
|| (ix.extension_payment_amount != None && ix.extension_duration_seconds == None)
{
return Err(error!(ErrorCode::InvalidInstruction));
} else if ix.extension_payment_amount != None && ix.payment_mint == None {
} else if ix.extension_payment_amount != None && ix.extension_payment_mint == None {
return Err(error!(ErrorCode::InvalidInstruction));
} else if ix.payment_mint != None
&& ctx.accounts.token_manager.payment_mint != None
&& ctx.accounts.token_manager.payment_mint.unwrap() != ix.payment_mint.unwrap()
{
return Err(error!(ErrorCode::InvalidPaymentMint));
}
let time_invalidator = &mut ctx.accounts.time_invalidator;
time_invalidator.bump = *ctx.bumps.get("time_invalidator").unwrap();
Expand All @@ -55,7 +50,7 @@ pub fn handler(ctx: Context<InitCtx>, ix: InitIx) -> Result<()> {
time_invalidator.expiration = ix.expiration;
time_invalidator.extension_payment_amount = ix.extension_payment_amount;
time_invalidator.extension_duration_seconds = ix.extension_duration_seconds;
time_invalidator.payment_mint = ix.payment_mint;
time_invalidator.extension_payment_mint = ix.extension_payment_mint;
time_invalidator.max_expiration = ix.max_expiration;
time_invalidator.disable_partial_extension = ix.disable_partial_extension;
return Ok(());
Expand Down
2 changes: 1 addition & 1 deletion programs/cardinal-time-invalidator/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct TimeInvalidator {
pub duration_seconds: Option<i64>,
pub extension_payment_amount: Option<u64>,
pub extension_duration_seconds: Option<u64>,
pub payment_mint: Option<Pubkey>,
pub extension_payment_mint: Option<Pubkey>,
pub max_expiration: Option<i64>,
pub disable_partial_extension: Option<bool>,
}
6 changes: 3 additions & 3 deletions programs/cardinal-token-manager/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ pub enum ErrorCode {
InvalidClaimAuthority,
#[msg("Invalid transfer authority")]
InvalidTransferAuthority,
#[msg("Invalid payment manager")]
InvalidPaymentManager,
#[msg("Invalid issuer")]
InvalidIssuer,
#[msg("Invalid invalidator")]
Expand All @@ -45,5 +43,7 @@ pub enum ErrorCode {
#[msg("Invalid receipt mint account")]
InvalidReceiptMintAccount,
#[msg("Invalid receipt mint owner")]
InvalidReceiptMintOwner
InvalidReceiptMintOwner,
#[msg("Invalid receipt mint")]
InvalidReceiptMint
}
39 changes: 1 addition & 38 deletions programs/cardinal-token-manager/src/instructions/invalidate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
crate::{state::*, errors::ErrorCode},
anchor_lang::{prelude::*, solana_program::program::invoke_signed, AccountsClose},
anchor_spl::{token::{self, Token, TokenAccount, Mint, Transfer, ThawAccount, CloseAccount}},
mpl_token_metadata::{instruction::thaw_delegated_account, utils::{assert_derivation,assert_initialized}},
mpl_token_metadata::{instruction::thaw_delegated_account, utils::{assert_derivation, assert_initialized}},
};

#[derive(Accounts)]
Expand Down Expand Up @@ -41,43 +41,6 @@ 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.payment_mint != None {
let payment_token_account_info = next_account_info(remaining_accs)?;
let payment_token_account = Account::<TokenAccount>::try_from(payment_token_account_info)?;

if payment_token_account.mint != token_manager.payment_mint.unwrap() { return Err(error!(ErrorCode::PublicKeyMismatch)); }
if payment_token_account.owner != token_manager.key() { return Err(error!(ErrorCode::PublicKeyMismatch)); }
// assert_keys_eq!(payment_token_account.mint, token_manager.payment_mint.unwrap());
// assert_keys_eq!(payment_token_account.owner, token_manager.key());

let issuer_payment_token_account_info = next_account_info(remaining_accs)?;
let issuer_payment_token_account = Account::<TokenAccount>::try_from(issuer_payment_token_account_info)?;

if issuer_payment_token_account.mint != token_manager.payment_mint.unwrap() { return Err(error!(ErrorCode::PublicKeyMismatch)); }
if issuer_payment_token_account.owner != token_manager.issuer { return Err(error!(ErrorCode::PublicKeyMismatch)); }
// assert_keys_eq!(issuer_payment_token_account.mint, token_manager.payment_mint.unwrap());
// assert_keys_eq!(issuer_payment_token_account.owner, token_manager.issuer);

let cpi_accounts = Transfer {
from: payment_token_account_info.clone(),
to: issuer_payment_token_account_info.clone(),
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, payment_token_account.amount)?;

// close token account
let cpi_accounts = CloseAccount {
account: payment_token_account_info.to_account_info(),
destination: ctx.accounts.collector.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::close_account(cpi_context)?;
}

if token_manager.state == TokenManagerState::Claimed as u8 {
if token_manager.kind == TokenManagerKind::Managed as u8 {
let mint_manager_info = next_account_info(remaining_accs)?;
Expand Down
2 changes: 0 additions & 2 deletions programs/cardinal-token-manager/src/instructions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod init;
pub mod uninit;
pub mod init_mint_counter;
pub mod set_payment_mint;
pub mod set_claim_approver;
pub mod set_transfer_authority;
pub mod add_invalidator;
Expand All @@ -18,7 +17,6 @@ pub mod close_mint_manager;
pub use init::*;
pub use uninit::*;
pub use init_mint_counter::*;
pub use set_payment_mint::*;
pub use set_claim_approver::*;
pub use set_transfer_authority::*;
pub use add_invalidator::*;
Expand Down

This file was deleted.

5 changes: 1 addition & 4 deletions programs/cardinal-token-manager/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod instructions;
pub mod state;
pub mod utils;
pub mod errors;

use {anchor_lang::prelude::*, instructions::*};
Expand All @@ -22,10 +23,6 @@ pub mod cardinal_token_manager {
init_mint_counter::handler(ctx, mint)
}

pub fn set_payment_mint(ctx: Context<SetPaymentMintCtx>, payment_mint: Pubkey) -> Result<()> {
set_payment_mint::handler(ctx, payment_mint)
}

pub fn set_claim_approver(ctx: Context<SetClaimApproverCtx>, claim_approver: Pubkey) -> Result<()> {
set_claim_approver::handler(ctx, claim_approver)
}
Expand Down
3 changes: 1 addition & 2 deletions programs/cardinal-token-manager/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub enum InvalidationType {
}

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

pub const MAX_INVALIDATORS: u8 = 5;
Expand All @@ -57,7 +57,6 @@ pub struct TokenManager {
pub invalidation_type: u8,
pub recipient_token_account: Pubkey,
pub receipt_mint: Option<Pubkey>,
pub payment_mint: Option<Pubkey>,
pub claim_approver: Option<Pubkey>,
pub transfer_authority: Option<Pubkey>,
pub invalidators: Vec<Pubkey>,
Expand Down
21 changes: 21 additions & 0 deletions programs/cardinal-token-manager/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use {
crate::{errors::ErrorCode, state::*},
anchor_lang::prelude::*,
anchor_spl::token::{TokenAccount},
};

pub fn assert_payment_token_account(
token_account: &Account<TokenAccount>,
token_manager: &Account<TokenManager>,
remaining_accounts: &mut std::slice::Iter<AccountInfo>,
) -> Result<()> {
if token_manager.receipt_mint == None {
if token_account.owner != token_manager.issuer { return Err(error!(ErrorCode::InvalidIssuer))}
} else {
let receipt_token_account_info = next_account_info(remaining_accounts)?;
let receipt_token_account = Account::<TokenAccount>::try_from(receipt_token_account_info)?;
if !(receipt_token_account.mint == token_manager.receipt_mint.unwrap() && receipt_token_account.amount > 0) { return Err(error!(ErrorCode::InvalidReceiptMint))}
if receipt_token_account.owner != token_account.owner { return Err(error!(ErrorCode::InvalidReceiptMintOwner))}
}
Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use {
crate::{errors::ErrorCode, state::*},
anchor_lang::prelude::*,
anchor_spl::token::{self, Token, TokenAccount, Transfer},
cardinal_token_manager::state::{TokenManager, TokenManagerState},
};
cardinal_token_manager::{state::{TokenManager, TokenManagerState}, utils::assert_payment_token_account},
};

#[derive(Accounts)]
pub struct ExtendUsagesCtx<'info> {
Expand All @@ -13,11 +13,7 @@ use {
#[account(mut, constraint = use_invalidator.token_manager == token_manager.key() @ ErrorCode::InvalidUseInvalidator)]
use_invalidator: Box<Account<'info, UseInvalidator>>,

#[account(mut, constraint =
payment_token_account.owner == token_manager.key()
&& payment_token_account.mint == use_invalidator.extension_payment_mint.unwrap()
@ ErrorCode::InvalidPaymentTokenAccount,
)]
#[account(mut, constraint = payment_token_account.mint == use_invalidator.extension_payment_mint.unwrap() @ ErrorCode::InvalidPaymentTokenAccount)]
payment_token_account: Box<Account<'info, TokenAccount>>,

#[account(mut)]
Expand All @@ -33,8 +29,10 @@ use {
}

pub fn handler(ctx: Context<ExtendUsagesCtx>, payment_amount: u64) -> Result<()> {
let remaining_accs = &mut ctx.remaining_accounts.iter();
assert_payment_token_account(&ctx.accounts.payment_token_account, &ctx.accounts.token_manager, remaining_accs)?;

let use_invalidator = &mut ctx.accounts.use_invalidator;

if use_invalidator.extension_payment_amount == None
|| use_invalidator.extension_usages == None
|| use_invalidator.extension_payment_mint == None
Expand Down
2 changes: 1 addition & 1 deletion programs/cardinal-use-invalidator/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub struct UseInvalidator {
pub token_manager: Pubkey,
pub use_authority: Option<Pubkey>,
pub total_usages: Option<u64>,
pub max_usages: Option<u64>,
pub extension_payment_amount: Option<u64>,
pub extension_payment_mint: Option<Pubkey>,
pub extension_usages: Option<u64>,
pub max_usages: Option<u64>,
}
8 changes: 4 additions & 4 deletions src/idl/cardinal_time_invalidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export type CardinalTimeInvalidator = {
};
},
{
name: "paymentMint";
name: "extensionPaymentMint";
type: {
option: "publicKey";
};
Expand Down Expand Up @@ -251,7 +251,7 @@ export type CardinalTimeInvalidator = {
};
},
{
name: "paymentMint";
name: "extensionPaymentMint";
type: {
option: "publicKey";
};
Expand Down Expand Up @@ -532,7 +532,7 @@ export const IDL: CardinalTimeInvalidator = {
},
},
{
name: "paymentMint",
name: "extensionPaymentMint",
type: {
option: "publicKey",
},
Expand Down Expand Up @@ -584,7 +584,7 @@ export const IDL: CardinalTimeInvalidator = {
},
},
{
name: "paymentMint",
name: "extensionPaymentMint",
type: {
option: "publicKey",
},
Expand Down
Loading

0 comments on commit cbeb969

Please sign in to comment.