Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update token metadata lib #77

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 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 programs/mmm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ anchor-test = []
anchor-lang = { version = "0.27.0", features = ["init-if-needed"] }
anchor-spl = "0.27.0"
solana-program = "=1.14.17"
mpl-token-metadata = { version = "=1.10.0", features = ["no-entrypoint"] }
mpl-token-metadata = { version = "3.2.3" }
mpl-token-auth-rules = { version = "1.0.0", features = ["no-entrypoint"] }
spl-token = { version = "3.5.0", features = ["no-entrypoint"] }
spl-associated-token-account = { version = "1.1.1", features = [
Expand Down
108 changes: 45 additions & 63 deletions programs/mmm/src/instructions/mip1/mip1_deposit_sell.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use anchor_lang::{
prelude::*,
solana_program::{program::invoke, sysvar},
AnchorDeserialize,
};
use std::collections::HashMap;

use anchor_lang::{prelude::*, solana_program::sysvar, AnchorDeserialize};
use anchor_spl::{
associated_token::AssociatedToken,
token::{Mint, Token, TokenAccount},
};
use mpl_token_auth_rules::payload::{Payload, PayloadType, SeedsVec};
use mpl_token_metadata::{
instruction::{builders::TransferBuilder, InstructionBuilder, TransferArgs},
processor::AuthorizationData,
instructions::TransferCpiBuilder,
types::{AuthorizationData, Payload, PayloadType, SeedsVec, TransferArgs},
};

use crate::{
Expand Down Expand Up @@ -84,7 +81,7 @@ pub struct Mip1DepositSell<'info> {
pub authorization_rules: UncheckedAccount<'info>,

/// CHECK: checked by address and in cpi
#[account(address = mpl_token_metadata::id())]
#[account(address = mpl_token_metadata::ID)]
pub token_metadata_program: UncheckedAccount<'info>,
/// CHECK: checked by address and in cpi
#[account(address = mpl_token_auth_rules::id())]
Expand Down Expand Up @@ -115,6 +112,7 @@ pub fn handler(ctx: Context<Mip1DepositSell>, args: DepositSellArgs) -> Result<(
let associated_token_program = &ctx.accounts.associated_token_program;
let authorization_rules = &ctx.accounts.authorization_rules;
let authorization_rules_program = &ctx.accounts.authorization_rules_program;
let token_metadata_program_ai = &ctx.accounts.token_metadata_program.to_account_info();

let parsed_metadata = check_allowlists_for_mint(
&pool.allowlists,
Expand All @@ -125,61 +123,45 @@ pub fn handler(ctx: Context<Mip1DepositSell>, args: DepositSellArgs) -> Result<(
)?;
assert_is_programmable(&parsed_metadata)?;

let payload = Payload::from([(
"DestinationSeeds".to_owned(),
PayloadType::Seeds(SeedsVec {
seeds: vec![
POOL_PREFIX.as_bytes().to_vec(),
owner.key().to_bytes().to_vec(),
pool.uuid.to_bytes().to_vec(),
],
}),
)]);
let ins = TransferBuilder::new()
.token(asset_token_account.key())
.token_owner(owner.key())
.destination(sellside_escrow_token_account.key())
.destination_owner(pool.key())
.mint(asset_mint.key())
.metadata(asset_metadata.key())
.edition(asset_master_edition.key())
.owner_token_record(owner_token_record.key())
.destination_token_record(destination_token_record.key())
.authority(owner.key())
.payer(owner.key())
.system_program(system_program.key())
.sysvar_instructions(instructions.key())
.spl_token_program(token_program.key())
.spl_ata_program(associated_token_program.key())
.authorization_rules(authorization_rules.key())
.authorization_rules_program(authorization_rules_program.key())
.build(TransferArgs::V1 {
authorization_data: Some(AuthorizationData { payload }),
amount: args.asset_amount,
})
.unwrap()
.instruction();
let payload = Payload {
map: HashMap::from([(
"DestinationSeeds".to_owned(),
PayloadType::Seeds(SeedsVec {
seeds: vec![
POOL_PREFIX.as_bytes().to_vec(),
owner.key().to_bytes().to_vec(),
pool.uuid.to_bytes().to_vec(),
],
}),
)]),
};

invoke(
&ins,
&[
asset_token_account.to_account_info(),
owner.to_account_info(),
sellside_escrow_token_account.to_account_info(),
pool.to_account_info(),
asset_mint.to_account_info(),
asset_metadata.to_account_info(),
asset_master_edition.to_account_info(),
owner_token_record.to_account_info(),
destination_token_record.to_account_info(),
system_program.to_account_info(),
instructions.to_account_info(),
token_program.to_account_info(),
associated_token_program.to_account_info(),
authorization_rules.to_account_info(),
authorization_rules_program.to_account_info(),
],
)?;
let transfer_args = TransferArgs::V1 {
authorization_data: Some(AuthorizationData { payload }),
amount: args.asset_amount,
};

let mut transfer_cpi = TransferCpiBuilder::new(token_metadata_program_ai);
transfer_cpi
.token(&asset_token_account.to_account_info())
.token_owner(&owner.to_account_info())
.destination_token(&sellside_escrow_token_account.to_account_info())
.destination_owner(&pool.to_account_info())
.mint(&asset_mint.to_account_info())
.metadata(&asset_metadata.to_account_info())
.edition(Some(&asset_master_edition.to_account_info()))
.token_record(Some(&owner_token_record.to_account_info()))
.destination_token_record(Some(&destination_token_record.to_account_info()))
.authority(&owner.to_account_info())
.payer(&owner.to_account_info())
.system_program(&system_program.to_account_info())
.sysvar_instructions(&instructions.to_account_info())
.spl_token_program(&token_program.to_account_info())
.spl_ata_program(&associated_token_program.to_account_info())
.authorization_rules(Some(&authorization_rules.to_account_info()))
.authorization_rules_program(Some(&authorization_rules_program.to_account_info()))
.transfer_args(transfer_args)
.invoke()?;

if asset_token_account.amount == args.asset_amount {
anchor_spl::token::close_account(CpiContext::new(
Expand Down
105 changes: 43 additions & 62 deletions programs/mmm/src/instructions/mip1/mip1_withdraw_sell.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use anchor_lang::{
prelude::*,
solana_program::{program::invoke_signed, sysvar},
AnchorDeserialize,
};
use std::collections::HashMap;

use anchor_lang::{prelude::*, solana_program::sysvar, AnchorDeserialize};
use anchor_spl::{
associated_token::AssociatedToken,
token::{Mint, Token, TokenAccount},
};
use mpl_token_auth_rules::payload::{Payload, PayloadType, SeedsVec};
use mpl_token_metadata::{
instruction::{builders::TransferBuilder, InstructionBuilder, TransferArgs},
processor::AuthorizationData,
state::{Metadata, TokenMetadataAccount},
accounts::Metadata,
instructions::TransferCpiBuilder,
types::{AuthorizationData, Payload, PayloadType, SeedsVec, TransferArgs},
};

use crate::{
Expand Down Expand Up @@ -89,7 +86,7 @@ pub struct Mip1WithdrawSell<'info> {
pub authorization_rules: UncheckedAccount<'info>,

/// CHECK: checked by address and in cpi
#[account(address = mpl_token_metadata::id())]
#[account(address = mpl_token_metadata::ID)]
pub token_metadata_program: UncheckedAccount<'info>,
/// CHECK: checked by address and in cpi
#[account(address = mpl_token_auth_rules::id())]
Expand Down Expand Up @@ -121,6 +118,7 @@ pub fn handler(ctx: Context<Mip1WithdrawSell>, args: WithdrawSellArgs) -> Result
let associated_token_program = &ctx.accounts.associated_token_program;
let authorization_rules = &ctx.accounts.authorization_rules;
let authorization_rules_program = &ctx.accounts.authorization_rules_program;
let token_metadata_program_ai = &ctx.accounts.token_metadata_program.to_account_info();

let pool_seeds: &[&[&[u8]]] = &[&[
POOL_PREFIX.as_bytes(),
Expand All @@ -129,60 +127,43 @@ pub fn handler(ctx: Context<Mip1WithdrawSell>, args: WithdrawSellArgs) -> Result
&[*ctx.bumps.get("pool").unwrap()],
]];

assert_is_programmable(&Metadata::from_account_info(asset_metadata)?)?;
assert_is_programmable(&Metadata::safe_deserialize(&asset_metadata.data.borrow())?)?;

let payload = Payload::from([(
"SourceSeeds".to_owned(),
PayloadType::Seeds(SeedsVec {
seeds: pool_seeds[0][0..3].iter().map(|v| v.to_vec()).collect(),
}),
)]);
let ins = TransferBuilder::new()
.token(sellside_escrow_token_account.key())
.token_owner(pool.key())
.destination(asset_token_account.key())
.destination_owner(owner.key())
.mint(asset_mint.key())
.metadata(asset_metadata.key())
.edition(asset_master_edition.key())
.owner_token_record(owner_token_record.key())
.destination_token_record(destination_token_record.key())
.authority(pool.key())
.payer(owner.key())
.system_program(system_program.key())
.sysvar_instructions(instructions.key())
.spl_token_program(token_program.key())
.spl_ata_program(associated_token_program.key())
.authorization_rules(authorization_rules.key())
.authorization_rules_program(authorization_rules_program.key())
.build(TransferArgs::V1 {
authorization_data: Some(AuthorizationData { payload }),
amount: args.asset_amount,
})
.unwrap()
.instruction();
let payload = Payload {
map: HashMap::from([(
"SourceSeeds".to_owned(),
PayloadType::Seeds(SeedsVec {
seeds: pool_seeds[0][0..3].iter().map(|v| v.to_vec()).collect(),
}),
)]),
};

invoke_signed(
&ins,
&[
sellside_escrow_token_account.to_account_info(),
pool.to_account_info(),
asset_token_account.to_account_info(),
owner.to_account_info(),
asset_mint.to_account_info(),
asset_metadata.to_account_info(),
asset_master_edition.to_account_info(),
owner_token_record.to_account_info(),
destination_token_record.to_account_info(),
system_program.to_account_info(),
instructions.to_account_info(),
token_program.to_account_info(),
associated_token_program.to_account_info(),
authorization_rules.to_account_info(),
authorization_rules_program.to_account_info(),
],
pool_seeds,
)?;
let transfer_args = TransferArgs::V1 {
authorization_data: Some(AuthorizationData { payload }),
amount: args.asset_amount,
};

let mut transfer_cpi = TransferCpiBuilder::new(token_metadata_program_ai);
transfer_cpi
.token(&sellside_escrow_token_account.to_account_info())
.token_owner(&pool.to_account_info())
.destination_token(&asset_token_account.to_account_info())
.destination_owner(&owner.to_account_info())
.mint(&asset_mint.to_account_info())
.metadata(&asset_metadata.to_account_info())
.edition(Some(&asset_master_edition.to_account_info()))
.token_record(Some(&owner_token_record.to_account_info()))
.destination_token_record(Some(&destination_token_record.to_account_info()))
.authority(&pool.to_account_info())
.payer(&owner.to_account_info())
.system_program(&system_program.to_account_info())
.sysvar_instructions(&instructions.to_account_info())
.spl_token_program(&token_program.to_account_info())
.spl_ata_program(&associated_token_program.to_account_info())
.authorization_rules(Some(&authorization_rules.to_account_info()))
.authorization_rules_program(Some(&authorization_rules_program.to_account_info()))
.transfer_args(transfer_args)
.invoke_signed(pool_seeds)?;

if sellside_escrow_token_account.amount == args.asset_amount {
anchor_spl::token::close_account(CpiContext::new_with_signer(
Expand Down
Loading
Loading