123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- import {
- PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID
- } from '@metaplex-foundation/mpl-token-metadata';
- import * as anchor from "@project-serum/anchor";
- import { ASSOCIATED_PROGRAM_ID } from '@project-serum/anchor/dist/cjs/utils/token';
- import { TransferTokens } from "../target/types/transfer_tokens";
- describe("Transfer Tokens", () => {
-
- const provider = anchor.AnchorProvider.env();
- anchor.setProvider(provider);
- const payer = provider.wallet as anchor.Wallet;
- const program = anchor.workspace.TransferTokens as anchor.Program<TransferTokens>;
- const tokenMintKeypair: anchor.web3.Keypair = anchor.web3.Keypair.generate();
- const nftMintKeypair: anchor.web3.Keypair = anchor.web3.Keypair.generate();
-
- const recipientWallet: anchor.web3.Keypair = anchor.web3.Keypair.generate();
- it("Create an SPL Token!", async () => {
- const metadataAddress = anchor.web3.PublicKey.findProgramAddressSync(
- [
- Buffer.from("metadata"),
- TOKEN_METADATA_PROGRAM_ID.toBuffer(),
- tokenMintKeypair.publicKey.toBuffer(),
- ],
- TOKEN_METADATA_PROGRAM_ID
- )[0];
- const sx = await program.methods.createToken(
- "Solana Gold",
- "GOLDSOL",
- "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json",
- 9,
- )
- .accounts({
- metadataAccount: metadataAddress,
- mintAccount: tokenMintKeypair.publicKey,
- mintAuthority: payer.publicKey,
- payer: payer.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
- tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
- })
- .signers([tokenMintKeypair, payer.payer])
- .rpc();
- console.log("Success!");
- console.log(` Mint Address: ${tokenMintKeypair.publicKey}`);
- console.log(` Tx Signature: ${sx}`);
- });
- it("Create an NFT!", async () => {
- const metadataAddress = anchor.web3.PublicKey.findProgramAddressSync(
- [
- Buffer.from("metadata"),
- TOKEN_METADATA_PROGRAM_ID.toBuffer(),
- nftMintKeypair.publicKey.toBuffer(),
- ],
- TOKEN_METADATA_PROGRAM_ID
- )[0];
- const sx = await program.methods.createToken(
- "Homer NFT",
- "HOMR",
- "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json",
- 0,
- )
- .accounts({
- metadataAccount: metadataAddress,
- mintAccount: nftMintKeypair.publicKey,
- mintAuthority: payer.publicKey,
- payer: payer.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
- tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
- })
- .signers([nftMintKeypair, payer.payer])
- .rpc();
- console.log("Success!");
- console.log(` Mint Address: ${nftMintKeypair.publicKey}`);
- console.log(` Tx Signature: ${sx}`);
- });
- it("Mint some tokens to your wallet!", async () => {
- const associatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
- mint: tokenMintKeypair.publicKey,
- owner: payer.publicKey,
- });
- const sx = await program.methods.mintSpl(
- new anchor.BN(150)
- )
- .accounts({
- associatedTokenAccount: associatedTokenAccountAddress,
- mintAccount: tokenMintKeypair.publicKey,
- mintAuthority: payer.publicKey,
- payer: payer.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
- })
- .signers([payer.payer])
- .rpc();
- console.log("Success!");
- console.log(` Mint Address: ${tokenMintKeypair.publicKey}`);
- console.log(` Tx Signature: ${sx}`);
- });
- it("Mint the NFT to your wallet!", async () => {
- const metadataAddress = (anchor.web3.PublicKey.findProgramAddressSync(
- [
- Buffer.from("metadata"),
- TOKEN_METADATA_PROGRAM_ID.toBuffer(),
- nftMintKeypair.publicKey.toBuffer(),
- ],
- TOKEN_METADATA_PROGRAM_ID
- ))[0];
- const editionAddress = (anchor.web3.PublicKey.findProgramAddressSync(
- [
- Buffer.from("metadata"),
- TOKEN_METADATA_PROGRAM_ID.toBuffer(),
- nftMintKeypair.publicKey.toBuffer(),
- Buffer.from("edition"),
- ],
- TOKEN_METADATA_PROGRAM_ID
- ))[0];
- const associatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
- mint: nftMintKeypair.publicKey,
- owner: payer.publicKey,
- });
- const sx = await program.methods.mintNft()
- .accounts({
- associatedTokenAccount: associatedTokenAccountAddress,
- editionAccount: editionAddress,
- metadataAccount: metadataAddress,
- mintAccount: nftMintKeypair.publicKey,
- mintAuthority: payer.publicKey,
- payer: payer.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
- tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
- })
- .signers([payer.payer])
- .rpc();
- console.log("Success!");
- console.log(` ATA Address: ${associatedTokenAccountAddress}`);
- console.log(` Tx Signature: ${sx}`);
- });
- it("Prep a new test wallet for transfers", async () => {
-
- await provider.connection.confirmTransaction(
- await provider.connection.requestAirdrop(
- recipientWallet.publicKey,
- await provider.connection.getMinimumBalanceForRentExemption(0),
- )
- );
- console.log(`Recipient Pubkey: ${recipientWallet.publicKey}`);
- });
- it("Transfer some tokens to another wallet!", async () => {
- const fromAssociatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
- mint: tokenMintKeypair.publicKey,
- owner: payer.publicKey,
- });
- const toAssociatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
- mint: tokenMintKeypair.publicKey,
- owner: recipientWallet.publicKey,
- });
- const sx = await program.methods.transferTokens(
- new anchor.BN(150)
- )
- .accounts({
- mintAccount: tokenMintKeypair.publicKey,
- fromAssociatedTokenAccount: fromAssociatedTokenAccountAddress,
- owner: payer.publicKey,
- toAssociatedTokenAccount: toAssociatedTokenAccountAddress,
- recipient: recipientWallet.publicKey,
- payer: payer.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
- })
- .signers([payer.payer])
- .rpc();
- console.log("Success!");
- console.log(` Mint Address: ${tokenMintKeypair.publicKey}`);
- console.log(` Tx Signature: ${sx}`);
- });
- it("Transfer the NFT to another wallet!", async () => {
- const fromAssociatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
- mint: nftMintKeypair.publicKey,
- owner: payer.publicKey,
- });
- const toAssociatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
- mint: nftMintKeypair.publicKey,
- owner: recipientWallet.publicKey,
- });
- const sx = await program.methods.transferTokens(
- new anchor.BN(1)
- )
- .accounts({
- mintAccount: nftMintKeypair.publicKey,
- fromAssociatedTokenAccount: fromAssociatedTokenAccountAddress,
- owner: payer.publicKey,
- toAssociatedTokenAccount: toAssociatedTokenAccountAddress,
- recipient: recipientWallet.publicKey,
- payer: payer.publicKey,
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
- systemProgram: anchor.web3.SystemProgram.programId,
- tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
- associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
- })
- .signers([payer.payer])
- .rpc();
- console.log("Success!");
- console.log(` Mint Address: ${nftMintKeypair.publicKey}`);
- console.log(` Tx Signature: ${sx}`);
- });
- });
|