bankrun.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { getAssociatedTokenAddressSync } from '@solana/spl-token';
  4. import { Keypair } from '@solana/web3.js';
  5. import { PublicKey } from '@solana/web3.js';
  6. import { BankrunProvider } from 'anchor-bankrun';
  7. import { startAnchor } from 'solana-bankrun';
  8. import type { TransferTokens } from '../target/types/transfer_tokens';
  9. const IDL = require('../target/idl/transfer_tokens.json');
  10. const PROGRAM_ID = new PublicKey(IDL.address);
  11. const METADATA_PROGRAM_ID = new PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');
  12. describe('Transfer Tokens Bankrun', async () => {
  13. const context = await startAnchor(
  14. '',
  15. [
  16. { name: 'transfer_tokens', programId: PROGRAM_ID },
  17. { name: 'token_metadata', programId: METADATA_PROGRAM_ID },
  18. ],
  19. [],
  20. );
  21. const provider = new BankrunProvider(context);
  22. anchor.setProvider(provider);
  23. const payer = provider.wallet as anchor.Wallet;
  24. const program = new anchor.Program<TransferTokens>(IDL, provider);
  25. const metadata = {
  26. name: 'Solana Gold',
  27. symbol: 'GOLDSOL',
  28. uri: 'https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json',
  29. };
  30. // Generate new keypair to use as address for mint account.
  31. const mintKeypair = new Keypair();
  32. // Generate new keypair to use as address for recipient wallet.
  33. const recipient = new Keypair();
  34. // Derive the associated token address account for the mint and payer.
  35. const senderTokenAddress = getAssociatedTokenAddressSync(mintKeypair.publicKey, payer.publicKey);
  36. // Derive the associated token address account for the mint and recipient.
  37. const recepientTokenAddress = getAssociatedTokenAddressSync(mintKeypair.publicKey, recipient.publicKey);
  38. it('Create an SPL Token!', async () => {
  39. const transactionSignature = await program.methods
  40. .createToken(metadata.name, metadata.symbol, metadata.uri)
  41. .accounts({
  42. payer: payer.publicKey,
  43. mintAccount: mintKeypair.publicKey,
  44. })
  45. .signers([mintKeypair])
  46. .rpc();
  47. console.log('Success!');
  48. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  49. console.log(` Transaction Signature: ${transactionSignature}`);
  50. });
  51. it('Mint tokens!', async () => {
  52. // Amount of tokens to mint.
  53. const amount = new anchor.BN(100);
  54. // Mint the tokens to the associated token account.
  55. const transactionSignature = await program.methods
  56. .mintToken(amount)
  57. .accounts({
  58. mintAuthority: payer.publicKey,
  59. recipient: payer.publicKey,
  60. mintAccount: mintKeypair.publicKey,
  61. associatedTokenAccount: senderTokenAddress,
  62. })
  63. .rpc();
  64. console.log('Success!');
  65. console.log(` Associated Token Account Address: ${senderTokenAddress}`);
  66. console.log(` Transaction Signature: ${transactionSignature}`);
  67. });
  68. it('Transfer tokens!', async () => {
  69. // Amount of tokens to transfer.
  70. const amount = new anchor.BN(50);
  71. const transactionSignature = await program.methods
  72. .transferTokens(amount)
  73. .accounts({
  74. sender: payer.publicKey,
  75. recipient: recipient.publicKey,
  76. mintAccount: mintKeypair.publicKey,
  77. senderTokenAccount: senderTokenAddress,
  78. recipientTokenAccount: recepientTokenAddress,
  79. })
  80. .rpc();
  81. console.log('Success!');
  82. console.log(` Transaction Signature: ${transactionSignature}`);
  83. });
  84. });