-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 2fa cosigners for distribution claim (#6)
* Add 2fa cosigners for distribution claim * Update * Update distribution seeds
- Loading branch information
1 parent
098b321
commit e21cf2c
Showing
20 changed files
with
1,952 additions
and
71 deletions.
There are no files selected for viewing
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,121 @@ | ||
use crate::{ | ||
distribution_seeds, | ||
errors::CustomError, | ||
id, | ||
states::{Distribution, DistributionClaim, Namespace}, | ||
}; | ||
use anchor_lang::prelude::*; | ||
use anchor_spl::{ | ||
associated_token::AssociatedToken, | ||
token_interface::{Mint, TokenAccount, TokenInterface}, | ||
}; | ||
|
||
#[derive(AnchorSerialize, AnchorDeserialize)] | ||
pub struct ClaimFromDistributionArgs { | ||
amount: u64, | ||
cosigned_msg: [u8; 32], | ||
} | ||
|
||
#[derive(Accounts)] | ||
#[instruction(args:ClaimFromDistributionArgs)] | ||
pub struct ClaimFromDistribution<'info> { | ||
#[account(mut)] | ||
payer: Signer<'info>, | ||
|
||
/// CHECK: claimant is an input parameter, it will be the owner of the claimant_token_account | ||
#[account()] | ||
claimant: UncheckedAccount<'info>, | ||
|
||
#[account()] | ||
cosigner_1: Signer<'info>, | ||
|
||
#[account()] | ||
cosigner_2: Signer<'info>, | ||
|
||
#[account( | ||
init, | ||
seeds=[b"distribution_claim", ns.key().as_ref(), distribution.key().as_ref(), args.cosigned_msg.as_ref()], | ||
payer=payer, | ||
space=8+DistributionClaim::INIT_SPACE, | ||
bump, | ||
)] | ||
distribution_claim: Box<Account<'info, DistributionClaim>>, | ||
|
||
#[account( | ||
seeds=[b"distribution", ns.key().as_ref(), cosigner_1.key().as_ref(), cosigner_2.key().as_ref(), distribution.uuid.as_ref()], | ||
has_one = distribution_token_mint, | ||
has_one = cosigner_1, | ||
has_one = cosigner_2, | ||
constraint = distribution.start_ts <= ns.now() @ CustomError::InvalidTimestamp, | ||
bump, | ||
)] | ||
distribution: Box<Account<'info, Distribution>>, | ||
|
||
#[account()] | ||
distribution_token_mint: Box<InterfaceAccount<'info, Mint>>, | ||
|
||
#[account( | ||
mut, | ||
token::token_program = token_program, | ||
token::mint = distribution_token_mint, | ||
constraint = delegated_token_account.delegate == Some(distribution.key()).into() @ CustomError::InvalidTokenDelegate, | ||
constraint = delegated_token_account.amount >= args.amount @ CustomError::InvalidTokenAmount, | ||
constraint = delegated_token_account.delegated_amount >= args.amount @ CustomError::InvalidTokenDelegate, | ||
)] | ||
delegated_token_account: Box<InterfaceAccount<'info, TokenAccount>>, | ||
|
||
#[account( | ||
init_if_needed, | ||
token::token_program = token_program, | ||
associated_token::token_program = token_program, | ||
associated_token::mint = distribution_token_mint, | ||
associated_token::authority = claimant, | ||
payer = payer, | ||
)] | ||
claimant_token_account: Box<InterfaceAccount<'info, TokenAccount>>, | ||
|
||
#[account( | ||
constraint = *ns.to_account_info().owner == id(), | ||
)] | ||
ns: Box<Account<'info, Namespace>>, | ||
|
||
token_program: Interface<'info, TokenInterface>, | ||
system_program: Program<'info, System>, | ||
associated_token_program: Program<'info, AssociatedToken>, | ||
} | ||
|
||
pub fn handle<'info>( | ||
ctx: Context<'_, '_, '_, 'info, ClaimFromDistribution<'info>>, | ||
args: ClaimFromDistributionArgs, | ||
) -> Result<()> { | ||
let distribution_claim = &mut ctx.accounts.distribution_claim; | ||
let ns = &ctx.accounts.ns; | ||
let cosigner_1 = &ctx.accounts.cosigner_1; | ||
let cosigner_2 = &ctx.accounts.cosigner_2; | ||
let uuid = ctx.accounts.distribution.uuid; | ||
let bump = ctx.bumps.distribution; | ||
|
||
distribution_claim.ns = ctx.accounts.ns.key(); | ||
distribution_claim.claimant = ctx.accounts.claimant.key(); | ||
distribution_claim.distribution = ctx.accounts.distribution.key(); | ||
distribution_claim.amount = args.amount; | ||
distribution_claim.distribution_token_mint = ctx.accounts.distribution_token_mint.key(); | ||
distribution_claim.cosigned_msg = args.cosigned_msg; | ||
|
||
anchor_spl::token_interface::transfer_checked( | ||
CpiContext::new_with_signer( | ||
ctx.accounts.token_program.to_account_info(), | ||
anchor_spl::token_interface::TransferChecked { | ||
from: ctx.accounts.delegated_token_account.to_account_info(), | ||
mint: ctx.accounts.distribution_token_mint.to_account_info(), | ||
to: ctx.accounts.claimant_token_account.to_account_info(), | ||
authority: ctx.accounts.distribution.to_account_info(), | ||
}, | ||
&[distribution_seeds!(ns, cosigner_1, cosigner_2, uuid, bump)], | ||
), | ||
args.amount, | ||
ctx.accounts.distribution_token_mint.decimals, | ||
)?; | ||
|
||
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,57 @@ | ||
use anchor_lang::prelude::*; | ||
use anchor_spl::token_interface::Mint; | ||
|
||
use crate::{ | ||
id, | ||
states::{Distribution, Namespace}, | ||
}; | ||
|
||
#[derive(AnchorSerialize, AnchorDeserialize)] | ||
pub struct InitDistributionArgs { | ||
uuid: Pubkey, | ||
cosigner_1: Pubkey, | ||
cosigner_2: Pubkey, | ||
start_ts: i64, | ||
} | ||
|
||
#[derive(Accounts)] | ||
#[instruction(args:InitDistributionArgs)] | ||
pub struct InitDistribution<'info> { | ||
#[account(mut)] | ||
payer: Signer<'info>, | ||
|
||
#[account( | ||
init, | ||
seeds=[b"distribution", ns.key().as_ref(), args.cosigner_1.as_ref(), args.cosigner_2.as_ref(), args.uuid.as_ref()], | ||
payer=payer, | ||
space=8+Distribution::INIT_SPACE, | ||
bump, | ||
)] | ||
distribution: Account<'info, Distribution>, | ||
|
||
/// CHECK: This is an input for the distribution token mint | ||
#[account()] | ||
distribution_token_mint: Box<InterfaceAccount<'info, Mint>>, | ||
|
||
#[account( | ||
constraint = *ns.to_account_info().owner == id(), | ||
)] | ||
ns: Box<Account<'info, Namespace>>, | ||
|
||
system_program: Program<'info, System>, | ||
} | ||
|
||
pub fn handle<'info>( | ||
ctx: Context<'_, '_, '_, 'info, InitDistribution<'info>>, | ||
args: InitDistributionArgs, | ||
) -> Result<()> { | ||
let distribution = &mut ctx.accounts.distribution; | ||
distribution.ns = ctx.accounts.ns.key(); | ||
distribution.cosigner_1 = args.cosigner_1; | ||
distribution.cosigner_2 = args.cosigner_2; | ||
distribution.uuid = args.uuid; | ||
distribution.start_ts = args.start_ts; | ||
distribution.distribution_token_mint = ctx.accounts.distribution_token_mint.key(); | ||
|
||
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
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
Oops, something went wrong.