test.ts 2.9 KB

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