Quantcast
Channel: Recent Questions - Solana Stack Exchange
Viewing all articles
Browse latest Browse all 7855

NFT example contract interaction

$
0
0

I've deployed the example: https://github.com/solana-developers/program-examples/blob/main/tokens/create-token/anchor/programs/create-token/src/lib.rs

And been trying to interact with the contract to mint an NFT but failed. Any script example for reference? Thanks.

script I'm using now:

const anchor = require('@project-serum/anchor');const { TOKEN_PROGRAM_ID, getAssociatedTokenAddress } = require('@solana/spl-token');const fs = require('fs');const path = require('path');// Path to the existing keypair fileconst keypairPath = '/Users/.config/solana/id.json';  // Replace with your actual path// Load the keypair from the fileconst secretKey = Uint8Array.from(JSON.parse(fs.readFileSync(keypairPath, 'utf-8')));const payerKeypair = anchor.web3.Keypair.fromSecretKey(secretKey);(async () => {  try {    // Set up provider (connected to devnet)    const connection = new anchor.web3.Connection("https://api.devnet.solana.com", {      commitment: "confirmed"    });    const provider = new anchor.AnchorProvider(connection, new anchor.Wallet(payerKeypair), {});    anchor.setProvider(provider);    // Load the program ID    const programId = new anchor.web3.PublicKey('8tKucvNwQt4jnMgQgZH26SxiV39XeHkjG1wDHzSCZi4H');    // Load the IDL from local file    const idlPath = path.resolve(__dirname, 'target/idl/create_token.json');    const idl = JSON.parse(fs.readFileSync(idlPath, 'utf8'));    // Load the program    const program = new anchor.Program(idl, programId, provider);    // Create a new mint keypair    const mintKeypair = anchor.web3.Keypair.generate();    console.log(`Mint Address: ${mintKeypair.publicKey.toBase58()}`);    // Derive the associated token account for the mint    const associatedTokenAccount = await getAssociatedTokenAddress(      mintKeypair.publicKey,      payerKeypair.publicKey    );    console.log(`Associated Token Account: ${associatedTokenAccount.toBase58()}`);    // Create metadata PDA (Program Derived Address) for the token    const [metadataAccount] = await anchor.web3.PublicKey.findProgramAddress(      [        Buffer.from("metadata"),        new anchor.web3.PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s").toBuffer(),        mintKeypair.publicKey.toBuffer(),      ],      new anchor.web3.PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s") // Metadata program ID    );    console.log(`Metadata Account Address: ${metadataAccount.toBase58()}`);    // Specify accounts for the transaction    const tx = await program.methods.createTokenMint(      9, // Token decimals"ExampleToken", // Token name"EXT", // Token symbol"https://example.com/token-metadata.json" // Token URI    )    .accounts({      payer: payerKeypair.publicKey,      metadata_account: metadataAccount,      mint_account: mintKeypair.publicKey,      token_metadata_program: new anchor.web3.PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),      token_program: TOKEN_PROGRAM_ID,      system_program: anchor.web3.SystemProgram.programId,      rent: anchor.web3.SYSVAR_RENT_PUBKEY    })    .signers([payerKeypair, mintKeypair]) // Mint account and payer must sign    .rpc();    console.log('Token minted successfully! Transaction ID:', tx);  } catch (error) {    console.error('Error minting token:', error);  }})();

Viewing all articles
Browse latest Browse all 7855

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>