bankrun.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import fs from 'node:fs';
  2. import { describe, it } from 'node:test';
  3. import * as anchor from '@coral-xyz/anchor';
  4. import { getAssociatedTokenAddressSync } from '@solana/spl-token';
  5. import { Keypair } from '@solana/web3.js';
  6. import { PublicKey } from '@solana/web3.js';
  7. import { BankrunProvider } from 'anchor-bankrun';
  8. import { startAnchor } from 'solana-bankrun';
  9. import type { NftMinter } from '../target/types/nft_minter';
  10. const IDL = require('../target/idl/nft_minter.json');
  11. const PROGRAM_ID = new PublicKey(IDL.address);
  12. const METADATA_PROGRAM_ID = new PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');
  13. describe('NFT bankrun Minter', async () => {
  14. const context = await startAnchor(
  15. '',
  16. [
  17. { name: 'nft_minter', programId: PROGRAM_ID },
  18. { name: 'token_metadata', programId: METADATA_PROGRAM_ID },
  19. ],
  20. [],
  21. );
  22. const provider = new BankrunProvider(context);
  23. anchor.setProvider(provider);
  24. const payer = provider.wallet as anchor.Wallet;
  25. const program = new anchor.Program<NftMinter>(IDL, provider);
  26. // The metadata for our NFT
  27. const metadata = {
  28. name: 'Homer NFT',
  29. symbol: 'HOMR',
  30. uri: 'https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json',
  31. };
  32. it('Create an NFT!', async () => {
  33. // Generate a keypair to use as the address of our mint account
  34. const mintKeypair = new Keypair();
  35. // Derive the associated token address account for the mint and payer.
  36. const associatedTokenAccountAddress = getAssociatedTokenAddressSync(mintKeypair.publicKey, payer.publicKey);
  37. const transactionSignature = await program.methods
  38. .mintNft(metadata.name, metadata.symbol, metadata.uri)
  39. .accounts({
  40. payer: payer.publicKey,
  41. mintAccount: mintKeypair.publicKey,
  42. associatedTokenAccount: associatedTokenAccountAddress,
  43. })
  44. .signers([mintKeypair])
  45. .rpc({ skipPreflight: true });
  46. console.log('Success!');
  47. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  48. console.log(` Transaction Signature: ${transactionSignature}`);
  49. });
  50. });