Skip to content
Merged
1 change: 1 addition & 0 deletions program-libs/account-checks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ solana = [
"solana-account-info",
"solana-pubkey",
"msg",
"std"
]
msg = ["dep:solana-msg"]
pinocchio = ["dep:pinocchio"]
Expand Down
6 changes: 6 additions & 0 deletions program-libs/compressed-account/src/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ pub trait AsPubkey {
fn to_pubkey_bytes(&self) -> [u8; 32];
#[cfg(feature = "anchor")]
fn to_anchor_pubkey(&self) -> anchor_lang::prelude::Pubkey;
fn to_light_pubkey(&self) -> Pubkey {
self.to_pubkey_bytes().into()
}
}

impl AsPubkey for Pubkey {
Expand All @@ -220,6 +223,9 @@ impl AsPubkey for Pubkey {
fn to_anchor_pubkey(&self) -> anchor_lang::prelude::Pubkey {
self.into()
}
fn to_light_pubkey(&self) -> Pubkey {
*self
}
}

#[cfg(feature = "anchor")]
Expand Down
161 changes: 161 additions & 0 deletions program-libs/ctoken-types/src/instructions/mint_action/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
use light_compressed_account::instruction_data::{
compressed_proof::CompressedProof,
traits::{InstructionDiscriminator, LightInstructionData},
};

use crate::instructions::mint_action::{
Action, CompressedMintInstructionData, CompressedMintWithContext, CpiContext, CreateMint,
MintActionCompressedInstructionData, MintToCTokenAction, MintToCompressedAction,
RemoveMetadataKeyAction, UpdateAuthority, UpdateMetadataAuthorityAction,
UpdateMetadataFieldAction,
};

/// Discriminator for MintAction instruction
pub const MINT_ACTION_DISCRIMINATOR: u8 = 103;

impl InstructionDiscriminator for MintActionCompressedInstructionData {
fn discriminator(&self) -> &'static [u8] {
&[MINT_ACTION_DISCRIMINATOR]
}
}

impl LightInstructionData for MintActionCompressedInstructionData {}

impl MintActionCompressedInstructionData {
pub fn new(
mint_with_context: CompressedMintWithContext,
proof: Option<CompressedProof>,
) -> Self {
Self {
leaf_index: mint_with_context.leaf_index,
prove_by_index: mint_with_context.prove_by_index,
root_index: mint_with_context.root_index,
compressed_address: mint_with_context.address,
token_pool_bump: 0,
token_pool_index: 0,
create_mint: None,
actions: Vec::new(),
proof,
cpi_context: None,
mint: mint_with_context.mint,
}
}

pub fn new_mint(
compressed_address: [u8; 32],
root_index: u16,
proof: CompressedProof,
mint: CompressedMintInstructionData,
) -> Self {
Self {
leaf_index: 0,
prove_by_index: false,
root_index,
compressed_address,
token_pool_bump: 0,
token_pool_index: 0,
create_mint: Some(CreateMint::default()),
actions: Vec::new(),
proof: Some(proof),
cpi_context: None,
mint,
}
}

pub fn new_mint_write_to_cpi_context(
compressed_address: [u8; 32],
root_index: u16,
mint: CompressedMintInstructionData,
cpi_context: CpiContext,
) -> Self {
Self {
leaf_index: 0,
prove_by_index: false,
root_index,
compressed_address,
token_pool_bump: 0,
token_pool_index: 0,
create_mint: Some(CreateMint::default()),
actions: Vec::new(),
proof: None, // Proof is verified with execution not write
cpi_context: Some(cpi_context),
mint,
}
}

#[must_use = "with_mint_to_compressed returns a new value"]
pub fn with_mint_to_compressed(mut self, action: MintToCompressedAction) -> Self {
self.actions.push(Action::MintToCompressed(action));
self
}

#[must_use = "with_mint_to_ctoken returns a new value"]
pub fn with_mint_to_ctoken(mut self, action: MintToCTokenAction) -> Self {
self.actions.push(Action::MintToCToken(action));
self
}

#[must_use = "with_update_mint_authority returns a new value"]
pub fn with_update_mint_authority(mut self, authority: UpdateAuthority) -> Self {
self.actions.push(Action::UpdateMintAuthority(authority));
self
}

#[must_use = "with_update_freeze_authority returns a new value"]
pub fn with_update_freeze_authority(mut self, authority: UpdateAuthority) -> Self {
self.actions.push(Action::UpdateFreezeAuthority(authority));
self
}

#[must_use = "with_update_metadata_field returns a new value"]
pub fn with_update_metadata_field(mut self, action: UpdateMetadataFieldAction) -> Self {
self.actions.push(Action::UpdateMetadataField(action));
self
}

#[must_use = "with_update_metadata_authority returns a new value"]
pub fn with_update_metadata_authority(mut self, action: UpdateMetadataAuthorityAction) -> Self {
self.actions.push(Action::UpdateMetadataAuthority(action));
self
}

#[must_use = "with_remove_metadata_key returns a new value"]
pub fn with_remove_metadata_key(mut self, action: RemoveMetadataKeyAction) -> Self {
self.actions.push(Action::RemoveMetadataKey(action));
self
}

#[must_use = "with_cpi_context returns a new value"]
pub fn with_cpi_context(mut self, cpi_context: CpiContext) -> Self {
self.cpi_context = Some(cpi_context);
self
}

#[must_use = "write_to_cpi_context_first returns a new value"]
pub fn write_to_cpi_context_first(mut self) -> Self {
if let Some(ref mut ctx) = self.cpi_context {
ctx.first_set_context = true;
ctx.set_context = false;
} else {
self.cpi_context = Some(CpiContext {
first_set_context: true,
..Default::default()
});
}
self
}

#[must_use = "write_to_cpi_context_set returns a new value"]
pub fn write_to_cpi_context_set(mut self) -> Self {
if let Some(ref mut ctx) = self.cpi_context {
ctx.set_context = true;
ctx.first_set_context = false;
} else {
self.cpi_context = Some(CpiContext {
set_context: true,
..Default::default()
});
}
self
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use light_compressed_account::Pubkey;
use light_compressed_account::{pubkey::AsPubkey, Pubkey};
use light_zero_copy::ZeroCopy;

use crate::{AnchorDeserialize, AnchorSerialize};
Expand All @@ -10,9 +10,27 @@ pub struct Recipient {
pub amount: u64,
}

impl Recipient {
pub fn new(recipient: impl AsPubkey, amount: u64) -> Self {
Self {
recipient: recipient.to_light_pubkey(),
amount,
}
}
}

#[repr(C)]
#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize, ZeroCopy)]
pub struct MintToCompressedAction {
pub token_account_version: u8,
pub recipients: Vec<Recipient>,
}

impl MintToCompressedAction {
pub fn new(recipients: Vec<Recipient>) -> Self {
Self {
token_account_version: 3,
recipients,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ pub use mint_to_compressed::*;
pub use mint_to_ctoken::*;
pub use update_metadata::*;
pub use update_mint::*;
mod builder;
Loading
Loading