|
1 | 1 | import should from 'should'; |
2 | 2 | import * as testData from '../resources/sol'; |
3 | 3 | import { solInstructionFactory } from '../../src/lib/solInstructionFactory'; |
| 4 | +import { getToken2022Config } from '../../src/lib/token2022Config'; |
4 | 5 | import { InstructionBuilderTypes, MEMO_PROGRAM_PK } from '../../src/lib/constants'; |
5 | 6 | import { InstructionParams } from '../../src/lib/iface'; |
6 | 7 | import { PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js'; |
@@ -149,6 +150,75 @@ describe('Instruction Builder Tests: ', function () { |
149 | 150 | ]); |
150 | 151 | }); |
151 | 152 |
|
| 153 | + it('Token Transfer - Token-2022 with transfer hook config', () => { |
| 154 | + const tokenConfig = getToken2022Config('4MmJVdwYN8LwvbGeCowYjSx7KoEi6BJWg8XXnW4fDDp6'); |
| 155 | + should.exist(tokenConfig); |
| 156 | + should.exist(tokenConfig?.transferHook); |
| 157 | + const transferHook = tokenConfig!.transferHook!; |
| 158 | + |
| 159 | + const fromAddress = testData.authAccount.pub; |
| 160 | + const toAddress = testData.nonceAccount.pub; |
| 161 | + const sourceAddress = testData.associatedTokenAccounts.accounts[0].ata; |
| 162 | + const amount = '500000'; |
| 163 | + |
| 164 | + const transferParams: InstructionParams = { |
| 165 | + type: InstructionBuilderTypes.TokenTransfer, |
| 166 | + params: { |
| 167 | + fromAddress, |
| 168 | + toAddress, |
| 169 | + amount, |
| 170 | + tokenName: tokenConfig!.symbol, |
| 171 | + sourceAddress, |
| 172 | + tokenAddress: tokenConfig!.mintAddress, |
| 173 | + decimalPlaces: tokenConfig!.decimals, |
| 174 | + programId: tokenConfig!.programId, |
| 175 | + }, |
| 176 | + }; |
| 177 | + |
| 178 | + const result = solInstructionFactory(transferParams); |
| 179 | + result.should.have.length(1); |
| 180 | + |
| 181 | + const builtInstruction = result[0]; |
| 182 | + builtInstruction.programId.equals(TOKEN_2022_PROGRAM_ID).should.be.true(); |
| 183 | + |
| 184 | + const baseInstruction = createTransferCheckedInstruction( |
| 185 | + new PublicKey(sourceAddress), |
| 186 | + new PublicKey(tokenConfig!.mintAddress), |
| 187 | + new PublicKey(toAddress), |
| 188 | + new PublicKey(fromAddress), |
| 189 | + BigInt(amount), |
| 190 | + tokenConfig!.decimals, |
| 191 | + [], |
| 192 | + TOKEN_2022_PROGRAM_ID |
| 193 | + ); |
| 194 | + |
| 195 | + const baseKeyCount = baseInstruction.keys.length; |
| 196 | + builtInstruction.keys.slice(0, baseKeyCount).should.deepEqual(baseInstruction.keys); |
| 197 | + |
| 198 | + const extraKeys = builtInstruction.keys.slice(baseKeyCount); |
| 199 | + const expectedExtraKeys = [ |
| 200 | + ...transferHook.extraAccountMetas.map((meta) => ({ |
| 201 | + pubkey: new PublicKey(meta.pubkey), |
| 202 | + isSigner: meta.isSigner, |
| 203 | + isWritable: meta.isWritable, |
| 204 | + })), |
| 205 | + { pubkey: new PublicKey(transferHook.programId), isSigner: false, isWritable: false }, |
| 206 | + ]; |
| 207 | + |
| 208 | + if (transferHook.extraAccountMetasPDA) { |
| 209 | + expectedExtraKeys.push({ |
| 210 | + pubkey: new PublicKey(transferHook.extraAccountMetasPDA), |
| 211 | + isSigner: false, |
| 212 | + isWritable: false, |
| 213 | + }); |
| 214 | + } |
| 215 | + extraKeys.should.deepEqual(expectedExtraKeys); |
| 216 | + |
| 217 | + for (const expectedMeta of expectedExtraKeys) { |
| 218 | + builtInstruction.keys.filter((meta) => meta.pubkey.equals(expectedMeta.pubkey)).should.have.length(1); |
| 219 | + } |
| 220 | + }); |
| 221 | + |
152 | 222 | it('Mint To - Standard SPL Token', () => { |
153 | 223 | const mintAddress = testData.tokenTransfers.mintUSDC; |
154 | 224 | const destinationAddress = testData.tokenTransfers.sourceUSDC; |
|
0 commit comments