diff --git a/token/js/test/e2e-2022/groupMember.test.ts b/token/js/test/e2e-2022/groupMember.test.ts deleted file mode 100644 index 6a003a04bb2..00000000000 --- a/token/js/test/e2e-2022/groupMember.test.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { expect } from 'chai'; -import type { Connection, Signer } from '@solana/web3.js'; -import { PublicKey } from '@solana/web3.js'; -import { sendAndConfirmTransaction, Keypair, SystemProgram, Transaction } from '@solana/web3.js'; - -import { - ExtensionType, - createInitializeMintInstruction, - getTokenGroupState, - getMint, - getMintLen, - createInitializeGroupMemberPointerInstruction, - createInitializeGroupPointerInstruction, - getTokenGroupMemberState, - tokenGroupInitializeGroupWithRentTransfer, - tokenGroupMemberInitializeWithRentTransfer, -} from '../../src'; -import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common'; - -const TEST_TOKEN_DECIMALS = 2; -const EXTENSIONS = [ExtensionType.GroupMemberPointer]; - -describe('tokenGroupMember', async () => { - let connection: Connection; - let payer: Signer; - let mint: Keypair; - let mintAuthority: Keypair; - let updateAuthority: Keypair; - let groupAddress: PublicKey; - let memberAddress: PublicKey; - - before(async () => { - connection = await getConnection(); - payer = await newAccountWithLamports(connection, 1000000000); - mintAuthority = Keypair.generate(); - updateAuthority = Keypair.generate(); - }); - - beforeEach(async () => { - mint = Keypair.generate(); - groupAddress = PublicKey.unique(); - memberAddress = PublicKey.unique(); - - const mintLen = getMintLen(EXTENSIONS); - const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); - - const transaction = new Transaction().add( - SystemProgram.createAccount({ - fromPubkey: payer.publicKey, - newAccountPubkey: mint.publicKey, - space: mintLen, - lamports, - programId: TEST_PROGRAM_ID, - }), - createInitializeGroupMemberPointerInstruction( - mint.publicKey, - mintAuthority.publicKey, - memberAddress, - TEST_PROGRAM_ID - ), - createInitializeMintInstruction( - mint.publicKey, - TEST_TOKEN_DECIMALS, - mintAuthority.publicKey, - null, - TEST_PROGRAM_ID - ) - ); - - await sendAndConfirmTransaction(connection, transaction, [payer, mint], undefined); - }); - - it('can initialize group member', async () => { - const tokenGroupMember = { - mint: mint.publicKey, - group: groupAddress, - memberNumber: 1, - }; - - await tokenGroupMemberInitializeWithRentTransfer( - connection, - payer, - memberAddress, - mint.publicKey, - mintAuthority.publicKey, - groupAddress, - updateAuthority.publicKey, - [mintAuthority], - undefined, - TEST_PROGRAM_ID - ); - - const mintInfo = await getMint(connection, mint.publicKey, undefined, TEST_PROGRAM_ID); - const member = getTokenGroupMemberState(mintInfo); - expect(member).to.deep.equal(tokenGroupMember); - }); -}); diff --git a/token/js/test/e2e-2022/group.test.ts b/token/js/test/e2e-2022/tokenGroup.test.ts similarity index 98% rename from token/js/test/e2e-2022/group.test.ts rename to token/js/test/e2e-2022/tokenGroup.test.ts index af77e964365..0b302b67095 100644 --- a/token/js/test/e2e-2022/group.test.ts +++ b/token/js/test/e2e-2022/tokenGroup.test.ts @@ -27,7 +27,6 @@ describe('tokenGroup', async () => { let mint: Keypair; let mintAuthority: Keypair; let updateAuthority: Keypair; - let groupAddress: PublicKey; before(async () => { connection = await getConnection(); @@ -38,7 +37,6 @@ describe('tokenGroup', async () => { beforeEach(async () => { mint = Keypair.generate(); - groupAddress = PublicKey.unique(); const mintLen = getMintLen(EXTENSIONS); const lamports = await connection.getMinimumBalanceForRentExemption(mintLen); @@ -54,7 +52,7 @@ describe('tokenGroup', async () => { createInitializeGroupPointerInstruction( mint.publicKey, mintAuthority.publicKey, - groupAddress, + mint.publicKey, TEST_PROGRAM_ID ), createInitializeMintInstruction( diff --git a/token/js/test/e2e-2022/tokenGroupMember.test.ts b/token/js/test/e2e-2022/tokenGroupMember.test.ts new file mode 100644 index 00000000000..e47e52b8f9f --- /dev/null +++ b/token/js/test/e2e-2022/tokenGroupMember.test.ts @@ -0,0 +1,189 @@ +import { expect } from 'chai'; +import type { Connection, Signer } from '@solana/web3.js'; +import { PublicKey } from '@solana/web3.js'; +import { sendAndConfirmTransaction, Keypair, SystemProgram, Transaction } from '@solana/web3.js'; + +import { + ExtensionType, + createInitializeMintInstruction, + createInitializeGroupInstruction, + getTokenGroupState, + getMint, + getMintLen, + createInitializeGroupMemberPointerInstruction, + createInitializeGroupPointerInstruction, + getTokenGroupMemberState, + tokenGroupInitializeGroupWithRentTransfer, + tokenGroupMemberInitializeWithRentTransfer, +} from '../../src'; +import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common'; + +const TEST_TOKEN_DECIMALS = 2; +const TEST_MAX_SIZE = 3; + +describe('tokenGroupMember', async () => { + let connection: Connection; + let payer: Signer; + + let groupMint: Keypair; + let groupUpdateAuthority: Keypair; + + let memberMint: Keypair; + let memberMintAuthority: Keypair; + let memberUpdateAuthority: Keypair; + + before(async () => { + connection = await getConnection(); + payer = await newAccountWithLamports(connection, 1000000000); + + groupMint = Keypair.generate(); + const groupMintAuthority = Keypair.generate(); + groupUpdateAuthority = Keypair.generate(); + + const groupMintLen = getMintLen([ExtensionType.GroupPointer]); + const groupMintLamports = await connection.getMinimumBalanceForRentExemption(groupMintLen); + + // Create the group mint and initialize the group. + await sendAndConfirmTransaction( + connection, + new Transaction().add( + SystemProgram.createAccount({ + fromPubkey: payer.publicKey, + newAccountPubkey: groupMint.publicKey, + space: groupMintLen, + lamports: groupMintLamports, + programId: TEST_PROGRAM_ID, + }), + createInitializeGroupPointerInstruction( + groupMint.publicKey, + groupUpdateAuthority.publicKey, + groupMint.publicKey, + TEST_PROGRAM_ID + ), + createInitializeMintInstruction( + groupMint.publicKey, + TEST_TOKEN_DECIMALS, + groupMintAuthority.publicKey, + null, + TEST_PROGRAM_ID + ) + ), + [payer, groupMint], + undefined + ); + await tokenGroupInitializeGroupWithRentTransfer( + connection, + payer, + groupMint.publicKey, + groupMintAuthority.publicKey, + groupUpdateAuthority.publicKey, + TEST_MAX_SIZE, + [payer, groupMintAuthority], + undefined, + TEST_PROGRAM_ID + ); + }); + + beforeEach(async () => { + memberMint = Keypair.generate(); + memberMintAuthority = Keypair.generate(); + memberUpdateAuthority = Keypair.generate(); + + const memberMintLen = getMintLen([ExtensionType.GroupMemberPointer]); + const memberMintLamports = await connection.getMinimumBalanceForRentExemption(memberMintLen); + + // Create the member mint. + await sendAndConfirmTransaction( + connection, + new Transaction().add( + SystemProgram.createAccount({ + fromPubkey: payer.publicKey, + newAccountPubkey: memberMint.publicKey, + space: memberMintLen, + lamports: memberMintLamports, + programId: TEST_PROGRAM_ID, + }), + createInitializeGroupMemberPointerInstruction( + memberMint.publicKey, + memberUpdateAuthority.publicKey, + memberMint.publicKey, + TEST_PROGRAM_ID + ), + createInitializeMintInstruction( + memberMint.publicKey, + TEST_TOKEN_DECIMALS, + memberMintAuthority.publicKey, + null, + TEST_PROGRAM_ID + ) + ), + [payer, memberMint], + undefined + ); + }); + + it('can initialize a single group member', async () => { + const tokenGroupMember = { + mint: memberMint.publicKey, + group: groupMint.publicKey, + memberNumber: 1, + }; + + await tokenGroupMemberInitializeWithRentTransfer( + connection, + payer, + memberMint.publicKey, + memberMintAuthority.publicKey, + groupMint.publicKey, + groupUpdateAuthority.publicKey, + [memberMintAuthority, groupUpdateAuthority], + undefined, + TEST_PROGRAM_ID + ); + + const mintInfo = await getMint(connection, memberMint.publicKey, undefined, TEST_PROGRAM_ID); + const member = getTokenGroupMemberState(mintInfo); + expect(member).to.deep.equal(tokenGroupMember); + }); + + it('can initialize group members up to the max size', async () => { + for (let memberNumber = 0; memberNumber < TEST_MAX_SIZE; memberNumber++) { + const tokenGroupMember = { + mint: memberMint.publicKey, + group: groupMint.publicKey, + memberNumber, + }; + + await tokenGroupMemberInitializeWithRentTransfer( + connection, + payer, + memberMint.publicKey, + memberMintAuthority.publicKey, + groupMint.publicKey, + groupUpdateAuthority.publicKey, + [memberMintAuthority, groupUpdateAuthority], + undefined, + TEST_PROGRAM_ID + ); + + const mintInfo = await getMint(connection, memberMint.publicKey, undefined, TEST_PROGRAM_ID); + const member = getTokenGroupMemberState(mintInfo); + expect(member).to.deep.equal(tokenGroupMember); + } + + // Try one more, when the group is full. + expect( + tokenGroupMemberInitializeWithRentTransfer( + connection, + payer, + memberMint.publicKey, + memberMintAuthority.publicKey, + groupMint.publicKey, + groupUpdateAuthority.publicKey, + [memberMintAuthority, groupUpdateAuthority], + undefined, + TEST_PROGRAM_ID + ) + ).to.be.rejectedWith('Group is full'); + }); +});