|
| 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 { |
| 15 | + createInitializeMintInstruction, |
| 16 | + createInitializeNonTransferableMintInstruction, |
| 17 | + createInitializeImmutableOwnerInstruction, |
| 18 | + createInitializeAccountInstruction, |
| 19 | + mintTo, |
| 20 | + getAccountLen, |
| 21 | + getMint, |
| 22 | + getMintLen, |
| 23 | + getNonTransferable, |
| 24 | + transfer, |
| 25 | + ExtensionType, |
| 26 | +} from '../../src'; |
| 27 | +import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common'; |
| 28 | + |
| 29 | +const TEST_TOKEN_DECIMALS = 2; |
| 30 | +const EXTENSIONS = [ExtensionType.NonTransferable]; |
| 31 | +describe('nonTransferable', () => { |
| 32 | + let connection: Connection; |
| 33 | + let payer: Signer; |
| 34 | + let mint: PublicKey; |
| 35 | + let mintAuthority: Keypair; |
| 36 | + before(async () => { |
| 37 | + connection = await getConnection(); |
| 38 | + payer = await newAccountWithLamports(connection, 1000000000); |
| 39 | + mintAuthority = Keypair.generate(); |
| 40 | + }); |
| 41 | + beforeEach(async () => { |
| 42 | + const mintKeypair = Keypair.generate(); |
| 43 | + mint = mintKeypair.publicKey; |
| 44 | + const mintLen = getMintLen(EXTENSIONS); |
| 45 | + const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); |
| 46 | + |
| 47 | + const transaction = new Transaction().add( |
| 48 | + SystemProgram.createAccount({ |
| 49 | + fromPubkey: payer.publicKey, |
| 50 | + newAccountPubkey: mint, |
| 51 | + space: mintLen, |
| 52 | + lamports, |
| 53 | + programId: TEST_PROGRAM_ID, |
| 54 | + }), |
| 55 | + createInitializeNonTransferableMintInstruction(mint, TEST_PROGRAM_ID), |
| 56 | + createInitializeMintInstruction(mint, TEST_TOKEN_DECIMALS, mintAuthority.publicKey, null, TEST_PROGRAM_ID) |
| 57 | + ); |
| 58 | + |
| 59 | + await sendAndConfirmTransaction(connection, transaction, [payer, mintKeypair], undefined); |
| 60 | + }); |
| 61 | + it('fails transfer', async () => { |
| 62 | + const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); |
| 63 | + const nonTransferable = getNonTransferable(mintInfo); |
| 64 | + expect(nonTransferable).to.not.be.null; |
| 65 | + |
| 66 | + const owner = Keypair.generate(); |
| 67 | + const accountLen = getAccountLen([ExtensionType.ImmutableOwner]); |
| 68 | + const lamports = await connection.getMinimumBalanceForRentExemption(accountLen); |
| 69 | + |
| 70 | + const sourceKeypair = Keypair.generate(); |
| 71 | + const source = sourceKeypair.publicKey; |
| 72 | + let transaction = new Transaction().add( |
| 73 | + SystemProgram.createAccount({ |
| 74 | + fromPubkey: payer.publicKey, |
| 75 | + newAccountPubkey: source, |
| 76 | + space: accountLen, |
| 77 | + lamports, |
| 78 | + programId: TEST_PROGRAM_ID, |
| 79 | + }), |
| 80 | + createInitializeImmutableOwnerInstruction(source, TEST_PROGRAM_ID), |
| 81 | + createInitializeAccountInstruction(source, mint, owner.publicKey, TEST_PROGRAM_ID) |
| 82 | + ); |
| 83 | + await sendAndConfirmTransaction(connection, transaction, [payer, sourceKeypair], undefined); |
| 84 | + |
| 85 | + const destinationKeypair = Keypair.generate(); |
| 86 | + const destination = destinationKeypair.publicKey; |
| 87 | + transaction = new Transaction().add( |
| 88 | + SystemProgram.createAccount({ |
| 89 | + fromPubkey: payer.publicKey, |
| 90 | + newAccountPubkey: destination, |
| 91 | + space: accountLen, |
| 92 | + lamports, |
| 93 | + programId: TEST_PROGRAM_ID, |
| 94 | + }), |
| 95 | + createInitializeImmutableOwnerInstruction(destination, TEST_PROGRAM_ID), |
| 96 | + createInitializeAccountInstruction(destination, mint, owner.publicKey, TEST_PROGRAM_ID) |
| 97 | + ); |
| 98 | + await sendAndConfirmTransaction(connection, transaction, [payer, destinationKeypair], undefined); |
| 99 | + |
| 100 | + const amount = BigInt(1000); |
| 101 | + await mintTo(connection, payer, mint, source, mintAuthority, amount, [], undefined, TEST_PROGRAM_ID); |
| 102 | + |
| 103 | + expect(transfer(connection, payer, source, destination, owner, amount, [], undefined, TEST_PROGRAM_ID)).to.be |
| 104 | + .rejected; |
| 105 | + }); |
| 106 | +}); |
0 commit comments