|
| 1 | +import chai, { expect } from 'chai'; |
| 2 | +import chaiAsPromised from 'chai-as-promised'; |
| 3 | +chai.use(chaiAsPromised); |
| 4 | + |
| 5 | +import { |
| 6 | + sendAndConfirmTransaction, |
| 7 | + Connection, |
| 8 | + Keypair, |
| 9 | + PublicKey, |
| 10 | + Signer, |
| 11 | + SystemProgram, |
| 12 | + Transaction, |
| 13 | +} from '@solana/web3.js'; |
| 14 | +import { createMemoInstruction } from '@solana/spl-memo'; |
| 15 | +import { |
| 16 | + createAccount, |
| 17 | + createMint, |
| 18 | + createEnableRequiredMemoTransfersInstruction, |
| 19 | + createInitializeAccountInstruction, |
| 20 | + createTransferInstruction, |
| 21 | + getAccount, |
| 22 | + getMemoTransfer, |
| 23 | + disableRequiredMemoTransfers, |
| 24 | + enableRequiredMemoTransfers, |
| 25 | + mintTo, |
| 26 | + transfer, |
| 27 | + getAccountLen, |
| 28 | + ExtensionType, |
| 29 | +} from '../../src'; |
| 30 | +import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common'; |
| 31 | + |
| 32 | +const TEST_TOKEN_DECIMALS = 2; |
| 33 | +const TRANSFER_AMOUNT = 1_000; |
| 34 | +const EXTENSIONS = [ExtensionType.MemoTransfer]; |
| 35 | +describe('memoTransfer', () => { |
| 36 | + let connection: Connection; |
| 37 | + let payer: Signer; |
| 38 | + let owner: Keypair; |
| 39 | + let mint: PublicKey; |
| 40 | + let mintAuthority: Keypair; |
| 41 | + let source: PublicKey; |
| 42 | + let destination: PublicKey; |
| 43 | + before(async () => { |
| 44 | + connection = await getConnection(); |
| 45 | + payer = await newAccountWithLamports(connection, 1000000000); |
| 46 | + mintAuthority = Keypair.generate(); |
| 47 | + owner = Keypair.generate(); |
| 48 | + }); |
| 49 | + beforeEach(async () => { |
| 50 | + const mintKeypair = Keypair.generate(); |
| 51 | + mint = await createMint( |
| 52 | + connection, |
| 53 | + payer, |
| 54 | + mintAuthority.publicKey, |
| 55 | + mintAuthority.publicKey, |
| 56 | + TEST_TOKEN_DECIMALS, |
| 57 | + mintKeypair, |
| 58 | + undefined, |
| 59 | + TEST_PROGRAM_ID |
| 60 | + ); |
| 61 | + |
| 62 | + source = await createAccount( |
| 63 | + connection, |
| 64 | + payer, |
| 65 | + mint, |
| 66 | + owner.publicKey, |
| 67 | + undefined, // uses ATA by default |
| 68 | + undefined, |
| 69 | + TEST_PROGRAM_ID |
| 70 | + ); |
| 71 | + |
| 72 | + const destinationKeypair = Keypair.generate(); |
| 73 | + destination = destinationKeypair.publicKey; |
| 74 | + const accountLen = getAccountLen(EXTENSIONS); |
| 75 | + const lamports = await connection.getMinimumBalanceForRentExemption(accountLen); |
| 76 | + |
| 77 | + const transaction = new Transaction().add( |
| 78 | + SystemProgram.createAccount({ |
| 79 | + fromPubkey: payer.publicKey, |
| 80 | + newAccountPubkey: destination, |
| 81 | + space: accountLen, |
| 82 | + lamports, |
| 83 | + programId: TEST_PROGRAM_ID, |
| 84 | + }), |
| 85 | + createInitializeAccountInstruction(destination, mint, owner.publicKey, TEST_PROGRAM_ID), |
| 86 | + createEnableRequiredMemoTransfersInstruction(destination, owner.publicKey, [], TEST_PROGRAM_ID) |
| 87 | + ); |
| 88 | + |
| 89 | + await sendAndConfirmTransaction(connection, transaction, [payer, owner, destinationKeypair], undefined); |
| 90 | + await mintTo( |
| 91 | + connection, |
| 92 | + payer, |
| 93 | + mint, |
| 94 | + source, |
| 95 | + mintAuthority, |
| 96 | + TRANSFER_AMOUNT * 10, |
| 97 | + [], |
| 98 | + undefined, |
| 99 | + TEST_PROGRAM_ID |
| 100 | + ); |
| 101 | + }); |
| 102 | + it('fails without memo when enabled', async () => { |
| 103 | + const accountInfo = await getAccount(connection, destination, undefined, TEST_PROGRAM_ID); |
| 104 | + const memoTransfer = getMemoTransfer(accountInfo); |
| 105 | + expect(memoTransfer).to.not.be.null; |
| 106 | + if (memoTransfer !== null) { |
| 107 | + expect(memoTransfer.requireIncomingTransferMemos).to.be.true; |
| 108 | + } |
| 109 | + expect(transfer(connection, payer, source, destination, owner, TRANSFER_AMOUNT, [], undefined, TEST_PROGRAM_ID)) |
| 110 | + .to.be.rejected; |
| 111 | + }); |
| 112 | + it('works without memo when disabled', async () => { |
| 113 | + await disableRequiredMemoTransfers(connection, payer, destination, owner, [], undefined, TEST_PROGRAM_ID); |
| 114 | + await transfer(connection, payer, source, destination, owner, TRANSFER_AMOUNT, [], undefined, TEST_PROGRAM_ID); |
| 115 | + await enableRequiredMemoTransfers(connection, payer, destination, owner, [], undefined, TEST_PROGRAM_ID); |
| 116 | + expect(transfer(connection, payer, source, destination, owner, TRANSFER_AMOUNT, [], undefined, TEST_PROGRAM_ID)) |
| 117 | + .to.be.rejected; |
| 118 | + }); |
| 119 | + it('works with memo when enabled', async () => { |
| 120 | + const transaction = new Transaction().add( |
| 121 | + createMemoInstruction('transfer with a memo', [payer.publicKey, owner.publicKey]), |
| 122 | + createTransferInstruction(source, destination, owner.publicKey, TRANSFER_AMOUNT, [], TEST_PROGRAM_ID) |
| 123 | + ); |
| 124 | + await sendAndConfirmTransaction(connection, transaction, [payer, owner], { |
| 125 | + preflightCommitment: 'confirmed', |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments