Skip to content

Commit

Permalink
Rust formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
blockiosaurus committed Jun 23, 2024
1 parent 24a972f commit b6419a5
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 205 deletions.
8 changes: 8 additions & 0 deletions programs/mpl-hybrid/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
max_width = 100
imports_indent = "Block"
imports_layout = "Mixed"
imports_granularity = "Crate"
group_imports = "Preserve"
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
101 changes: 48 additions & 53 deletions programs/mpl-hybrid/src/instructions/capture.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@

use crate::constants::*;
use crate::error::MplHybridError;
use crate::state::*;
use crate::constants::*;
use anchor_lang::prelude::*;
use anchor_lang::{accounts::{unchecked_account::UncheckedAccount, signer::Signer, program::Program}, system_program::System};
use anchor_lang::{
accounts::{program::Program, signer::Signer, unchecked_account::UncheckedAccount},
system_program::System,
};
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token;
use anchor_spl::token::Mint;
use anchor_spl::token::{Token, TokenAccount, Transfer};
use arrayref::array_ref;
use mpl_core::accounts::BaseAssetV1;
use mpl_core::instructions::{
TransferV1Cpi, TransferV1InstructionArgs, UpdateV1Cpi, UpdateV1InstructionArgs,
};
use mpl_core::types::UpdateAuthority;
use solana_program::program::invoke;
use arrayref::array_ref;
use mpl_core::instructions::{UpdateV1Cpi,UpdateV1InstructionArgs,TransferV1Cpi,TransferV1InstructionArgs};
use anchor_spl::token::{TokenAccount,Token,Transfer};
use anchor_spl::token;

#[derive(Accounts)]
pub struct CaptureV1Ctx<'info> {
Expand Down Expand Up @@ -40,7 +44,7 @@ pub struct CaptureV1Ctx<'info> {
#[account(mut,
address = escrow.collection
)]
collection: AccountInfo<'info>,
collection: AccountInfo<'info>,

#[account(init_if_needed,
payer = owner,
Expand All @@ -60,13 +64,13 @@ pub struct CaptureV1Ctx<'info> {
#[account(
address = escrow.token @MplHybridError::InvalidMintAccount
)]
token: Account<'info, Mint>,
token: Account<'info, Mint>,

#[account(init_if_needed,
payer = owner,
associated_token::mint = token,
associated_token::authority = fee_project_account)]
fee_token_account: Account<'info,TokenAccount>,
fee_token_account: Account<'info, TokenAccount>,

/// CHECK: We check against constant
#[account(mut,
Expand Down Expand Up @@ -98,25 +102,24 @@ pub struct CaptureV1Ctx<'info> {
}

pub fn handler_capture_v1(ctx: Context<CaptureV1Ctx>) -> Result<()> {

let owner = &mut ctx.accounts.owner;
let escrow = &mut ctx.accounts.escrow;
let asset = &mut ctx.accounts.asset;
let authority = &mut ctx.accounts.authority;
let collection = &mut ctx.accounts.collection;
let mpl_core =&mut ctx.accounts.mpl_core;
let user_token_account =&mut ctx.accounts.user_token_account;
let escrow_token_account =&mut ctx.accounts.escrow_token_account;
let fee_token_account =&mut ctx.accounts.fee_token_account;
let fee_sol_account =&mut ctx.accounts.fee_sol_account;
let fee_project_account =&mut ctx.accounts.fee_project_account;
let system_program =&mut ctx.accounts.system_program;
let token_program =&mut ctx.accounts.token_program;
let mpl_core = &mut ctx.accounts.mpl_core;
let user_token_account = &mut ctx.accounts.user_token_account;
let escrow_token_account = &mut ctx.accounts.escrow_token_account;
let fee_token_account = &mut ctx.accounts.fee_token_account;
let fee_sol_account = &mut ctx.accounts.fee_sol_account;
let fee_project_account = &mut ctx.accounts.fee_project_account;
let system_program = &mut ctx.accounts.system_program;
let token_program = &mut ctx.accounts.token_program;

let recent_slothashes = &ctx.accounts.recent_blockhashes;
let data = recent_slothashes.data.borrow();
let most_recent = array_ref![data, 12, 8];

let collection_info = &collection.to_account_info();
let authority_info = &authority.to_account_info();
let escrow_info = &escrow.to_account_info();
Expand All @@ -131,12 +134,15 @@ pub fn handler_capture_v1(ctx: Context<CaptureV1Ctx>) -> Result<()> {

//If the path is 0, we need to update the metadata onchain
if Path::RerollMetadata.check(escrow.path) {

let clock = Clock::get()?;
// seed for the random number is a combination of the slot_hash - timestamp
let seed = u64::from_le_bytes(*most_recent).saturating_sub(clock.unix_timestamp as u64)*escrow.count;
let seed = u64::from_le_bytes(*most_recent).saturating_sub(clock.unix_timestamp as u64)
* escrow.count;
// remainder is the random number between the min and max
let remainder = seed.checked_rem(escrow.max-escrow.min).ok_or(MplHybridError::RandomnessError)?+escrow.min;
let remainder = seed
.checked_rem(escrow.max - escrow.min)
.ok_or(MplHybridError::RandomnessError)?
+ escrow.min;

//construct the new uri
let mut uri = escrow.uri.clone();
Expand All @@ -153,19 +159,19 @@ pub fn handler_capture_v1(ctx: Context<CaptureV1Ctx>) -> Result<()> {
collection: Some(collection_info),
payer: &owner.to_account_info(),
authority: Some(authority_info),
system_program: &system_program.to_account_info(),
system_program: &system_program.to_account_info(),
log_wrapper: None,
__args: UpdateV1InstructionArgs{
__args: UpdateV1InstructionArgs {
new_name: Some(name),
new_uri: Some(uri),
new_update_authority:None
new_update_authority: None,
},
};
};

//invoke the update instruction
let _update_result = update_ix.invoke();
}

//create transfer instruction
let transfer_nft_ix = TransferV1Cpi {
__program: &mpl_core.to_account_info(),
Expand All @@ -174,27 +180,20 @@ pub fn handler_capture_v1(ctx: Context<CaptureV1Ctx>) -> Result<()> {
payer: &owner.to_account_info(),
authority: Some(escrow_info),
new_owner: &owner.to_account_info(),
system_program: Some(system_info),
system_program: Some(system_info),
log_wrapper: None,
__args: TransferV1InstructionArgs{
compression_proof:None
}
};
__args: TransferV1InstructionArgs {
compression_proof: None,
},
};

//invoke the transfer instruction with seeds
let _transfer_nft_result = transfer_nft_ix.invoke_signed(
&[
&[
b"escrow",
collection.key.as_ref(),
&[escrow.bump],
]
]
);
let _transfer_nft_result =
transfer_nft_ix.invoke_signed(&[&[b"escrow", collection.key.as_ref(), &[escrow.bump]]]);

let cpi_program = token_program.to_account_info();
let cpi_program = token_program.to_account_info();

//create transfer token instruction
//create transfer token instruction
let cpi_accounts_transfer = Transfer {
from: user_token_account.to_account_info(),
to: escrow_token_account.to_account_info(),
Expand All @@ -216,21 +215,17 @@ pub fn handler_capture_v1(ctx: Context<CaptureV1Ctx>) -> Result<()> {

token::transfer(transfer_fees_cpi_ctx, escrow.fee_amount)?;


//create protocol transfer fee sol instruction
let sol_fee_ix = anchor_lang::solana_program::system_instruction::transfer(
&owner.key(),
&fee_sol_account.key(),
PROTOCOL_FEE,
);

//invoke protocol the transfer fee sol instruction
let _sol_fee_result = invoke(
&sol_fee_ix,
&[
owner.to_account_info(),
fee_sol_account.to_account_info(),
],
&[owner.to_account_info(), fee_sol_account.to_account_info()],
);

//create project transfer fee sol instruction for project
Expand All @@ -239,18 +234,18 @@ pub fn handler_capture_v1(ctx: Context<CaptureV1Ctx>) -> Result<()> {
&fee_project_account.key(),
escrow.sol_fee_amount,
);

//invoke project the transfer fee sol instruction for project
let _sol_fee_project_result = invoke(
&sol_fee_project_ix,
&[
owner.to_account_info(),
fee_project_account.to_account_info(),
],
);
);

//increment the swap count
escrow.count += 1;

Ok(())
}
}
51 changes: 26 additions & 25 deletions programs/mpl-hybrid/src/instructions/init_escrow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::constants::MPL_CORE;
use crate::state::*;
use crate::error::MplHybridError;
use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token::{Mint, Token, TokenAccount};
Expand All @@ -10,8 +10,8 @@ use mpl_core::types::Key as MplCoreKey;

#[derive(AnchorSerialize, AnchorDeserialize)]
pub struct InitEscrowV1Ix {
name: String,
uri: String,
name: String,
uri: String,
max: u64,
min: u64,
amount: u64,
Expand All @@ -38,13 +38,13 @@ pub struct InitEscrowV1Ctx<'info> {
authority: Signer<'info>,

/// CHECK: We check the collection bellow and with escrow seeds
collection: UncheckedAccount<'info>,
collection: UncheckedAccount<'info>,

/// CHECK: This is a user defined account
token: Account<'info, Mint>,
token: Account<'info, Mint>,

/// CHECK: This is a user defined account
fee_location: UncheckedAccount<'info>,
fee_location: UncheckedAccount<'info>,

/// The ATA for token fees to be stored
#[account(
Expand All @@ -60,8 +60,7 @@ pub struct InitEscrowV1Ctx<'info> {
associated_token_program: Program<'info, AssociatedToken>,
}

pub fn handler_init_escrow_v1(ctx: Context<InitEscrowV1Ctx>, ix:InitEscrowV1Ix) -> Result<()> {

pub fn handler_init_escrow_v1(ctx: Context<InitEscrowV1Ctx>, ix: InitEscrowV1Ix) -> Result<()> {
let escrow = &mut ctx.accounts.escrow;
let collection = &mut ctx.accounts.collection;
let authority = &mut ctx.accounts.authority;
Expand All @@ -73,35 +72,37 @@ pub fn handler_init_escrow_v1(ctx: Context<InitEscrowV1Ctx>, ix:InitEscrowV1Ix)
return Err(MplHybridError::MaxMustBeGreaterThanMin.into());
}

if *collection.owner != MPL_CORE || load_key(&collection.to_account_info(), 0)? != MplCoreKey::CollectionV1 {
if *collection.owner != MPL_CORE
|| load_key(&collection.to_account_info(), 0)? != MplCoreKey::CollectionV1
{
return Err(MplHybridError::InvalidCollectionAccount.into());
}

// We only fetch the Base collection to check authority.
let collection_data = BaseCollectionV1::from_bytes(&collection.to_account_info().data.borrow())?;
let collection_data =
BaseCollectionV1::from_bytes(&collection.to_account_info().data.borrow())?;

// Check that the collection authority is the same as the escrow authority.
if collection_data.update_authority != authority.key() {
return Err(MplHybridError::InvalidCollectionAuthority.into());
}

//initialize with input data

escrow.collection=collection.key();
escrow.authority=authority.key();
escrow.token=token.key();
escrow.collection = collection.key();
escrow.authority = authority.key();
escrow.token = token.key();
escrow.fee_location = fee_location.key();
escrow.name=ix.name;
escrow.uri=ix.uri;
escrow.max=ix.max;
escrow.min=ix.min;
escrow.amount=ix.amount;
escrow.fee_amount=ix.fee_amount;
escrow.sol_fee_amount=ix.sol_fee_amount;
escrow.count=1;
escrow.path=ix.path;
escrow.bump=ctx.bumps.escrow;
escrow.name = ix.name;
escrow.uri = ix.uri;
escrow.max = ix.max;
escrow.min = ix.min;
escrow.amount = ix.amount;
escrow.fee_amount = ix.fee_amount;
escrow.sol_fee_amount = ix.sol_fee_amount;
escrow.count = 1;
escrow.path = ix.path;
escrow.bump = ctx.bumps.escrow;

Ok(())
}

Loading

0 comments on commit b6419a5

Please sign in to comment.