|
| 1 | +import * as anchor from "@project-serum/anchor"; |
| 2 | +import { Native, Spl } from "@project-serum/anchor"; |
| 3 | +import { Keypair, PublicKey } from "@solana/web3.js"; |
| 4 | +import * as assert from "assert"; |
| 5 | +import BN from "bn.js"; |
| 6 | + |
| 7 | +describe("spl-associated-token-coder", () => { |
| 8 | + // Configure the client to use the local cluster. |
| 9 | + const provider = anchor.AnchorProvider.env(); |
| 10 | + anchor.setProvider(provider); |
| 11 | + |
| 12 | + // Client. |
| 13 | + const program = Spl.associatedToken(); |
| 14 | + const systemProgram = Native.system(); |
| 15 | + const tokenProgram = Spl.token(); |
| 16 | + |
| 17 | + it("Creates an account", async () => { |
| 18 | + // arrange |
| 19 | + const mintKeypair = Keypair.generate(); |
| 20 | + const mintDecimals = 6; |
| 21 | + const mintSize = tokenProgram.coder.accounts.size( |
| 22 | + tokenProgram.idl.accounts[0] |
| 23 | + ); |
| 24 | + const mintRentExemption = |
| 25 | + await provider.connection.getMinimumBalanceForRentExemption(mintSize); |
| 26 | + const [associatedToken] = await PublicKey.findProgramAddress( |
| 27 | + [ |
| 28 | + provider.publicKey.toBuffer(), |
| 29 | + tokenProgram.programId.toBuffer(), |
| 30 | + mintKeypair.publicKey.toBuffer(), |
| 31 | + ], |
| 32 | + program.programId |
| 33 | + ); |
| 34 | + |
| 35 | + // act |
| 36 | + await program.methods |
| 37 | + .create() |
| 38 | + .accounts({ |
| 39 | + authority: provider.wallet.publicKey, |
| 40 | + mint: mintKeypair.publicKey, |
| 41 | + owner: provider.wallet.publicKey, |
| 42 | + associatedAccount: associatedToken, |
| 43 | + }) |
| 44 | + .preInstructions( |
| 45 | + await Promise.all([ |
| 46 | + systemProgram.methods |
| 47 | + .createAccount( |
| 48 | + new BN(mintRentExemption), |
| 49 | + new BN(mintSize), |
| 50 | + tokenProgram.programId |
| 51 | + ) |
| 52 | + .accounts({ |
| 53 | + from: provider.wallet.publicKey, |
| 54 | + to: mintKeypair.publicKey, |
| 55 | + }) |
| 56 | + .instruction(), |
| 57 | + tokenProgram.methods |
| 58 | + .initializeMint(mintDecimals, provider.wallet.publicKey, null) |
| 59 | + .accounts({ |
| 60 | + mint: mintKeypair.publicKey, |
| 61 | + }) |
| 62 | + .instruction(), |
| 63 | + ]) |
| 64 | + ) |
| 65 | + .signers([mintKeypair]) |
| 66 | + .rpc(); |
| 67 | + // assert |
| 68 | + const tokenAccount = await tokenProgram.account.token.fetch( |
| 69 | + associatedToken |
| 70 | + ); |
| 71 | + assert.ok(tokenAccount.mint.equals(mintKeypair.publicKey)); |
| 72 | + }); |
| 73 | +}); |
0 commit comments