Skip to content

Commit f0c0b36

Browse files
authored
WIP fix CI for meta data pointer nft (#90)
1 parent 5107274 commit f0c0b36

File tree

5 files changed

+75
-83
lines changed

5 files changed

+75
-83
lines changed

.github/.ghaignore

-1
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ compression/cutils/anchor
3333
compression/cnft-vault/anchor
3434
# builds but need to test on localhost
3535
compression/cnft-burn/anchor
36-
tokens/token-2022/nft-meta-data-pointer/anchor-example/anchor

tokens/token-2022/nft-meta-data-pointer/anchor-example/anchor/Anchor.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[features]
22
seeds = false
33
[programs.localnet]
4-
extension_nft = "MkabCfyUD6rBTaYHpgKBBpBo5qzWA2pK2hrGGKMurJt"
4+
extension_nft = "9aZZ7TJ2fQZxY8hMtWXywp5y6BgqC4N2BPcr9FDT47sW"
55

66
[registry]
77
url = "https://anchor.projectserum.com"
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
pub use crate::errors::GameErrorCode;
22
pub use crate::errors::ProgramErrorCode;
33
pub use crate::state::game_data::GameData;
4-
use anchor_lang::{prelude::*, system_program};
4+
use anchor_lang::{ prelude::*, system_program };
55
use anchor_spl::{
6-
associated_token::{self, AssociatedToken}, token_2022, token_interface::{spl_token_2022::instruction::AuthorityType, Token2022}
6+
associated_token::{ self, AssociatedToken },
7+
token_2022,
8+
token_interface::{ spl_token_2022::instruction::AuthorityType, Token2022 },
79
};
8-
use solana_program::program::{invoke, invoke_signed};
9-
use spl_token_2022::{extension::ExtensionType, state::Mint};
10+
use solana_program::program::{ invoke, invoke_signed };
11+
use spl_token_2022::{ extension::ExtensionType, state::Mint };
1012

1113
pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
1214
msg!("Mint nft with meta data extension and additional meta data");
1315

14-
let space = match ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::MetadataPointer]) {
16+
let space = match
17+
ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::MetadataPointer])
18+
{
1519
Ok(space) => space,
16-
Err(_) => return err!(ProgramErrorCode::InvalidMintAccountSpace)
20+
Err(_) => {
21+
return err!(ProgramErrorCode::InvalidMintAccountSpace);
22+
}
1723
};
1824

19-
// This is the space required for the metadata account.
20-
// We put the meta data into the mint account at the end so we
21-
// don't need to create and additional account.
25+
// This is the space required for the metadata account.
26+
// We put the meta data into the mint account at the end so we
27+
// don't need to create and additional account.
2228
let meta_data_space = 250;
2329

24-
let lamports_required = (Rent::get()?).minimum_balance(space + meta_data_space);
30+
let lamports_required = Rent::get()?.minimum_balance(space + meta_data_space);
2531

2632
msg!(
2733
"Create Mint and metadata account size and cost: {} lamports: {}",
@@ -35,59 +41,52 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
3541
system_program::CreateAccount {
3642
from: ctx.accounts.signer.to_account_info(),
3743
to: ctx.accounts.mint.to_account_info(),
38-
},
44+
}
3945
),
4046
lamports_required,
4147
space as u64,
42-
&ctx.accounts.token_program.key(),
48+
&ctx.accounts.token_program.key()
4349
)?;
4450

4551
// Assign the mint to the token program
4652
system_program::assign(
47-
CpiContext::new(
48-
ctx.accounts.token_program.to_account_info(),
49-
system_program::Assign {
50-
account_to_assign: ctx.accounts.mint.to_account_info(),
51-
},
52-
),
53-
&token_2022::ID,
53+
CpiContext::new(ctx.accounts.token_program.to_account_info(), system_program::Assign {
54+
account_to_assign: ctx.accounts.mint.to_account_info(),
55+
}),
56+
&token_2022::ID
5457
)?;
5558

5659
// Initialize the metadata pointer (Need to do this before initializing the mint)
57-
let init_meta_data_pointer_ix =
58-
match spl_token_2022::extension::metadata_pointer::instruction::initialize(
59-
&Token2022::id(),
60-
&ctx.accounts.mint.key(),
61-
Some(ctx.accounts.nft_authority.key()),
62-
Some(ctx.accounts.mint.key()),
63-
) {
60+
let init_meta_data_pointer_ix = match
61+
spl_token_2022::extension::metadata_pointer::instruction::initialize(
62+
&Token2022::id(),
63+
&ctx.accounts.mint.key(),
64+
Some(ctx.accounts.nft_authority.key()),
65+
Some(ctx.accounts.mint.key())
66+
)
67+
{
6468
Ok(ix) => ix,
65-
Err(_) => return err!(ProgramErrorCode::CantInitializeMetadataPointer)
69+
Err(_) => {
70+
return err!(ProgramErrorCode::CantInitializeMetadataPointer);
71+
}
6672
};
67-
73+
6874
invoke(
6975
&init_meta_data_pointer_ix,
70-
&[
71-
ctx.accounts.mint.to_account_info(),
72-
ctx.accounts.nft_authority.to_account_info()
73-
],
76+
&[ctx.accounts.mint.to_account_info(), ctx.accounts.nft_authority.to_account_info()]
7477
)?;
75-
78+
7679
// Initialize the mint cpi
7780
let mint_cpi_ix = CpiContext::new(
7881
ctx.accounts.token_program.to_account_info(),
7982
token_2022::InitializeMint2 {
8083
mint: ctx.accounts.mint.to_account_info(),
81-
},
84+
}
8285
);
8386

84-
token_2022::initialize_mint2(
85-
mint_cpi_ix,
86-
0,
87-
&ctx.accounts.nft_authority.key(),
88-
None).unwrap();
89-
90-
// We use a PDA as a mint authority for the metadata account because
87+
token_2022::initialize_mint2(mint_cpi_ix, 0, &ctx.accounts.nft_authority.key(), None).unwrap();
88+
89+
// We use a PDA as a mint authority for the metadata account because
9190
// we want to be able to update the NFT from the program.
9291
let seeds = b"nft_authority";
9392
let bump = ctx.bumps.nft_authority;
@@ -96,22 +95,24 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
9695
msg!("Init metadata {0}", ctx.accounts.nft_authority.to_account_info().key);
9796

9897
// Init the metadata account
99-
let init_token_meta_data_ix =
100-
&spl_token_metadata_interface::instruction::initialize(
98+
let init_token_meta_data_ix = &spl_token_metadata_interface::instruction::initialize(
10199
&spl_token_2022::id(),
102100
ctx.accounts.mint.key,
103101
ctx.accounts.nft_authority.to_account_info().key,
104102
ctx.accounts.mint.key,
105103
ctx.accounts.nft_authority.to_account_info().key,
106104
"Beaver".to_string(),
107105
"BVA".to_string(),
108-
"https://arweave.net/MHK3Iopy0GgvDoM7LkkiAdg7pQqExuuWvedApCnzfj0".to_string(),
106+
"https://arweave.net/MHK3Iopy0GgvDoM7LkkiAdg7pQqExuuWvedApCnzfj0".to_string()
109107
);
110108

111109
invoke_signed(
112110
init_token_meta_data_ix,
113-
&[ctx.accounts.mint.to_account_info().clone(), ctx.accounts.nft_authority.to_account_info().clone()],
114-
signer,
111+
&[
112+
ctx.accounts.mint.to_account_info().clone(),
113+
ctx.accounts.nft_authority.to_account_info().clone(),
114+
],
115+
signer
115116
)?;
116117

117118
// Update the metadata account with an additional metadata field in this case the player level
@@ -121,7 +122,7 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
121122
ctx.accounts.mint.key,
122123
ctx.accounts.nft_authority.to_account_info().key,
123124
spl_token_metadata_interface::state::Field::Key("level".to_string()),
124-
"1".to_string(),
125+
"1".to_string()
125126
),
126127
&[
127128
ctx.accounts.mint.to_account_info().clone(),
@@ -133,16 +134,17 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
133134
// Create the associated token account
134135
associated_token::create(
135136
CpiContext::new(
136-
ctx.accounts.associated_token_program.to_account_info(),
137-
associated_token::Create {
138-
payer: ctx.accounts.signer.to_account_info(),
139-
associated_token: ctx.accounts.token_account.to_account_info(),
140-
authority: ctx.accounts.signer.to_account_info(),
141-
mint: ctx.accounts.mint.to_account_info(),
142-
system_program: ctx.accounts.system_program.to_account_info(),
143-
token_program: ctx.accounts.token_program.to_account_info(),
144-
},
145-
))?;
137+
ctx.accounts.associated_token_program.to_account_info(),
138+
associated_token::Create {
139+
payer: ctx.accounts.signer.to_account_info(),
140+
associated_token: ctx.accounts.token_account.to_account_info(),
141+
authority: ctx.accounts.signer.to_account_info(),
142+
mint: ctx.accounts.mint.to_account_info(),
143+
system_program: ctx.accounts.system_program.to_account_info(),
144+
token_program: ctx.accounts.token_program.to_account_info(),
145+
}
146+
)
147+
)?;
146148

147149
// Mint one token to the associated token account of the player
148150
token_2022::mint_to(
@@ -155,7 +157,7 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
155157
},
156158
signer
157159
),
158-
1,
160+
1
159161
)?;
160162

161163
// Freeze the mint authority so no more tokens can be minted to make it an NFT
@@ -169,7 +171,7 @@ pub fn mint_nft(ctx: Context<MintNft>) -> Result<()> {
169171
signer
170172
),
171173
AuthorityType::MintTokens,
172-
None,
174+
None
173175
)?;
174176

175177
Ok(())
@@ -188,16 +190,9 @@ pub struct MintNft<'info> {
188190
pub mint: Signer<'info>,
189191
pub rent: Sysvar<'info, Rent>,
190192
pub associated_token_program: Program<'info, AssociatedToken>,
191-
#[account(
192-
init_if_needed,
193-
seeds = [b"nft_authority".as_ref()],
194-
bump,
195-
space = 8,
196-
payer = signer,
197-
)]
198-
pub nft_authority: Account<'info, NftAuthority >
193+
#[account(init_if_needed, seeds = [b"nft_authority".as_ref()], bump, space = 8, payer = signer)]
194+
pub nft_authority: Account<'info, NftAuthority>,
199195
}
200196

201197
#[account]
202-
pub struct NftAuthority {
203-
}
198+
pub struct NftAuthority {}

tokens/token-2022/nft-meta-data-pointer/anchor-example/anchor/programs/extension_nft/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
pub use crate::errors::GameErrorCode;
22
pub use anchor_lang::prelude::*;
3-
pub use session_keys::{session_auth_or, Session, SessionError};
3+
pub use session_keys::{ session_auth_or, Session, SessionError };
44
pub mod constants;
55
pub mod errors;
66
pub mod instructions;
77
pub mod state;
88
use instructions::*;
99

10-
declare_id!("H31ofLpWqeAzF2Pg54HSPQGYifJad843tTJg8vCYVoh3");
10+
declare_id!("9aZZ7TJ2fQZxY8hMtWXywp5y6BgqC4N2BPcr9FDT47sW");
1111

1212
#[program]
1313
pub mod extension_nft {
14-
1514
use super::*;
1615

1716
pub fn init_player(ctx: Context<InitPlayer>, _level_seed: String) -> Result<()> {

tokens/token-2022/nft-meta-data-pointer/anchor-example/anchor/tests/lumberjack.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import * as anchor from '@coral-xyz/anchor';
22
import type { Program } from '@coral-xyz/anchor';
33
import { ASSOCIATED_PROGRAM_ID } from '@coral-xyz/anchor/dist/cjs/utils/token';
4-
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, getAssociatedTokenAddressSync } from '@solana/spl-token';
4+
import {
5+
ASSOCIATED_TOKEN_PROGRAM_ID,
6+
TOKEN_2022_PROGRAM_ID,
7+
getAssociatedTokenAddressSync,
8+
getOrCreateAssociatedTokenAccount,
9+
} from '@solana/spl-token';
510
import { Keypair, PublicKey } from '@solana/web3.js';
611
import type { ExtensionNft } from '../target/types/extension_nft';
712

@@ -30,22 +35,16 @@ describe('extension_nft', () => {
3035
ASSOCIATED_TOKEN_PROGRAM_ID,
3136
);
3237

33-
const nft_authority = await PublicKey.findProgramAddress([Buffer.from('nft_authority')], program.programId);
34-
38+
getOrCreateAssociatedTokenAccount;
3539
const tx = await program.methods
3640
.mintNft()
3741
.accounts({
3842
signer: payer.publicKey,
39-
systemProgram: anchor.web3.SystemProgram.programId,
40-
tokenProgram: TOKEN_2022_PROGRAM_ID,
41-
associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
4243
tokenAccount: destinationTokenAccount,
4344
mint: mint.publicKey,
44-
nftAuthority: nft_authority[0],
45-
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
4645
})
4746
.signers([mint])
48-
.rpc({ skipPreflight: true });
47+
.rpc();
4948

5049
console.log('Mint nft tx', tx);
5150
await anchor.getProvider().connection.confirmTransaction(tx, 'confirmed');

0 commit comments

Comments
 (0)