-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab6279b
commit 212de28
Showing
3 changed files
with
190 additions
and
100 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); |