|
| 1 | +import { Account, generateKeyPairSigner, none } from '@solana/kit'; |
| 2 | +import test from 'ava'; |
| 3 | +import { |
| 4 | + AccountState, |
| 5 | + TOKEN_PROGRAM_ADDRESS, |
| 6 | + Token, |
| 7 | + mintToATAInstructionPlan, |
| 8 | + mintToATAInstructionPlanAsync, |
| 9 | + fetchToken, |
| 10 | + findAssociatedTokenPda, |
| 11 | +} from '../src'; |
| 12 | +import { |
| 13 | + createDefaultSolanaClient, |
| 14 | + createDefaultTransactionPlanExecutor, |
| 15 | + createDefaultTransactionPlanner, |
| 16 | + createMint, |
| 17 | + generateKeyPairSignerWithSol, |
| 18 | +} from './_setup'; |
| 19 | + |
| 20 | +test('it creates a new associated token account with an initial balance', async (t) => { |
| 21 | + // Given a mint account, its mint authority, a token owner and the ATA. |
| 22 | + const client = createDefaultSolanaClient(); |
| 23 | + const [payer, mintAuthority, owner] = await Promise.all([ |
| 24 | + generateKeyPairSignerWithSol(client), |
| 25 | + generateKeyPairSigner(), |
| 26 | + generateKeyPairSigner(), |
| 27 | + ]); |
| 28 | + const decimals = 2; |
| 29 | + const mint = await createMint(client, payer, mintAuthority.address, decimals); |
| 30 | + const [ata] = await findAssociatedTokenPda({ |
| 31 | + mint, |
| 32 | + owner: owner.address, |
| 33 | + tokenProgram: TOKEN_PROGRAM_ADDRESS, |
| 34 | + }); |
| 35 | + |
| 36 | + // When we mint to a token account at this address. |
| 37 | + const instructionPlan = mintToATAInstructionPlan({ |
| 38 | + payer, |
| 39 | + ata, |
| 40 | + mint, |
| 41 | + owner: owner.address, |
| 42 | + mintAuthority, |
| 43 | + amount: 1_000n, |
| 44 | + decimals, |
| 45 | + }); |
| 46 | + |
| 47 | + const transactionPlanner = createDefaultTransactionPlanner(client, payer); |
| 48 | + const transactionPlan = await transactionPlanner(instructionPlan); |
| 49 | + const transactionPlanExecutor = createDefaultTransactionPlanExecutor(client); |
| 50 | + await transactionPlanExecutor(transactionPlan); |
| 51 | + |
| 52 | + // Then we expect the token account to exist and have the following data. |
| 53 | + t.like(await fetchToken(client.rpc, ata), <Account<Token>>{ |
| 54 | + address: ata, |
| 55 | + data: { |
| 56 | + mint, |
| 57 | + owner: owner.address, |
| 58 | + amount: 1000n, |
| 59 | + delegate: none(), |
| 60 | + state: AccountState.Initialized, |
| 61 | + isNative: none(), |
| 62 | + delegatedAmount: 0n, |
| 63 | + closeAuthority: none(), |
| 64 | + }, |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +test('it derives a new associated token account with an initial balance', async (t) => { |
| 69 | + // Given a mint account, its mint authority, a token owner and the ATA. |
| 70 | + const client = createDefaultSolanaClient(); |
| 71 | + const [payer, mintAuthority, owner] = await Promise.all([ |
| 72 | + generateKeyPairSignerWithSol(client), |
| 73 | + generateKeyPairSigner(), |
| 74 | + generateKeyPairSigner(), |
| 75 | + ]); |
| 76 | + const decimals = 2; |
| 77 | + const mint = await createMint(client, payer, mintAuthority.address, decimals); |
| 78 | + |
| 79 | + // When we mint to a token account for the mint. |
| 80 | + const instructionPlan = await mintToATAInstructionPlanAsync({ |
| 81 | + payer, |
| 82 | + mint, |
| 83 | + owner: owner.address, |
| 84 | + mintAuthority, |
| 85 | + amount: 1_000n, |
| 86 | + decimals, |
| 87 | + }); |
| 88 | + |
| 89 | + const transactionPlanner = createDefaultTransactionPlanner(client, payer); |
| 90 | + const transactionPlan = await transactionPlanner(instructionPlan); |
| 91 | + const transactionPlanExecutor = createDefaultTransactionPlanExecutor(client); |
| 92 | + await transactionPlanExecutor(transactionPlan); |
| 93 | + |
| 94 | + // Then we expect the token account to exist and have the following data. |
| 95 | + const [ata] = await findAssociatedTokenPda({ |
| 96 | + mint, |
| 97 | + owner: owner.address, |
| 98 | + tokenProgram: TOKEN_PROGRAM_ADDRESS, |
| 99 | + }); |
| 100 | + |
| 101 | + t.like(await fetchToken(client.rpc, ata), <Account<Token>>{ |
| 102 | + address: ata, |
| 103 | + data: { |
| 104 | + mint, |
| 105 | + owner: owner.address, |
| 106 | + amount: 1000n, |
| 107 | + delegate: none(), |
| 108 | + state: AccountState.Initialized, |
| 109 | + isNative: none(), |
| 110 | + delegatedAmount: 0n, |
| 111 | + closeAuthority: none(), |
| 112 | + }, |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +test('it also mints to an existing associated token account', async (t) => { |
| 117 | + // Given a mint account, its mint authority, a token owner and the ATA. |
| 118 | + const client = createDefaultSolanaClient(); |
| 119 | + const [payer, mintAuthority, owner] = await Promise.all([ |
| 120 | + generateKeyPairSignerWithSol(client), |
| 121 | + generateKeyPairSigner(), |
| 122 | + generateKeyPairSigner(), |
| 123 | + ]); |
| 124 | + const decimals = 2; |
| 125 | + const mint = await createMint(client, payer, mintAuthority.address, decimals); |
| 126 | + const [ata] = await findAssociatedTokenPda({ |
| 127 | + mint, |
| 128 | + owner: owner.address, |
| 129 | + tokenProgram: TOKEN_PROGRAM_ADDRESS, |
| 130 | + }); |
| 131 | + |
| 132 | + // When we create and initialize a token account at this address. |
| 133 | + const instructionPlan = mintToATAInstructionPlan({ |
| 134 | + payer, |
| 135 | + ata, |
| 136 | + mint, |
| 137 | + owner: owner.address, |
| 138 | + mintAuthority, |
| 139 | + amount: 1_000n, |
| 140 | + decimals, |
| 141 | + }); |
| 142 | + |
| 143 | + const transactionPlanner = createDefaultTransactionPlanner(client, payer); |
| 144 | + const transactionPlan = await transactionPlanner(instructionPlan); |
| 145 | + const transactionPlanExecutor = createDefaultTransactionPlanExecutor(client); |
| 146 | + await transactionPlanExecutor(transactionPlan); |
| 147 | + |
| 148 | + // And then we mint additional tokens to the same account. |
| 149 | + const instructionPlan2 = mintToATAInstructionPlan({ |
| 150 | + payer, |
| 151 | + ata, |
| 152 | + mint, |
| 153 | + owner: owner.address, |
| 154 | + mintAuthority, |
| 155 | + amount: 1_000n, |
| 156 | + decimals, |
| 157 | + }); |
| 158 | + |
| 159 | + const transactionPlan2 = await transactionPlanner(instructionPlan2); |
| 160 | + await transactionPlanExecutor(transactionPlan2); |
| 161 | + |
| 162 | + // Then we expect the token account to exist and have the following data. |
| 163 | + t.like(await fetchToken(client.rpc, ata), <Account<Token>>{ |
| 164 | + address: ata, |
| 165 | + data: { |
| 166 | + mint, |
| 167 | + owner: owner.address, |
| 168 | + amount: 2000n, |
| 169 | + delegate: none(), |
| 170 | + state: AccountState.Initialized, |
| 171 | + isNative: none(), |
| 172 | + delegatedAmount: 0n, |
| 173 | + closeAuthority: none(), |
| 174 | + }, |
| 175 | + }); |
| 176 | +}); |
0 commit comments