|
@@ -614,65 +614,117 @@ describe("misc", () => {
|
|
|
assert.equal(account2.idata, 3);
|
|
|
});
|
|
|
|
|
|
- let associatedToken = null;
|
|
|
-
|
|
|
- it("Can create an associated token account", async () => {
|
|
|
- associatedToken = await Token.getAssociatedTokenAddress(
|
|
|
- ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
|
- TOKEN_PROGRAM_ID,
|
|
|
- mint.publicKey,
|
|
|
- program.provider.wallet.publicKey
|
|
|
+ describe("associated_token constraints", () => {
|
|
|
+ let associatedToken = null;
|
|
|
+ // apparently cannot await here so doing it in the 'it' statements
|
|
|
+ let client = Token.createMint(
|
|
|
+ program.provider.connection,
|
|
|
+ program.provider.wallet.payer,
|
|
|
+ program.provider.wallet.publicKey,
|
|
|
+ program.provider.wallet.publicKey,
|
|
|
+ 9,
|
|
|
+ TOKEN_PROGRAM_ID
|
|
|
);
|
|
|
|
|
|
- await program.rpc.testInitAssociatedToken({
|
|
|
- accounts: {
|
|
|
- token: associatedToken,
|
|
|
- mint: mint.publicKey,
|
|
|
- payer: program.provider.wallet.publicKey,
|
|
|
- rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
|
- systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
- tokenProgram: TOKEN_PROGRAM_ID,
|
|
|
- associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
|
- },
|
|
|
+ it("Can create an associated token account", async () => {
|
|
|
+ const localClient = await client;
|
|
|
+ associatedToken = await Token.getAssociatedTokenAddress(
|
|
|
+ ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
|
+ TOKEN_PROGRAM_ID,
|
|
|
+ localClient.publicKey,
|
|
|
+ program.provider.wallet.publicKey
|
|
|
+ );
|
|
|
+
|
|
|
+ await program.rpc.testInitAssociatedToken({
|
|
|
+ accounts: {
|
|
|
+ token: associatedToken,
|
|
|
+ mint: localClient.publicKey,
|
|
|
+ payer: program.provider.wallet.publicKey,
|
|
|
+ rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
|
+ systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
+ tokenProgram: TOKEN_PROGRAM_ID,
|
|
|
+ associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ const account = await localClient.getAccountInfo(associatedToken);
|
|
|
+ assert.ok(account.state === 1);
|
|
|
+ assert.ok(account.amount.toNumber() === 0);
|
|
|
+ assert.ok(account.isInitialized);
|
|
|
+ assert.ok(account.owner.equals(program.provider.wallet.publicKey));
|
|
|
+ assert.ok(account.mint.equals(localClient.publicKey));
|
|
|
});
|
|
|
- const client = new Token(
|
|
|
- program.provider.connection,
|
|
|
- mint.publicKey,
|
|
|
- TOKEN_PROGRAM_ID,
|
|
|
- program.provider.wallet.payer
|
|
|
- );
|
|
|
- const account = await client.getAccountInfo(associatedToken);
|
|
|
- assert.ok(account.state === 1);
|
|
|
- assert.ok(account.amount.toNumber() === 0);
|
|
|
- assert.ok(account.isInitialized);
|
|
|
- assert.ok(account.owner.equals(program.provider.wallet.publicKey));
|
|
|
- assert.ok(account.mint.equals(mint.publicKey));
|
|
|
- });
|
|
|
|
|
|
- it("Can validate associated_token constraints", async () => {
|
|
|
- await program.rpc.testValidateAssociatedToken({
|
|
|
- accounts: {
|
|
|
- token: associatedToken,
|
|
|
- mint: mint.publicKey,
|
|
|
- wallet: program.provider.wallet.publicKey,
|
|
|
- },
|
|
|
+ it("Can validate associated_token constraints", async () => {
|
|
|
+ const localClient = await client;
|
|
|
+ await program.rpc.testValidateAssociatedToken({
|
|
|
+ accounts: {
|
|
|
+ token: associatedToken,
|
|
|
+ mint: localClient.publicKey,
|
|
|
+ wallet: program.provider.wallet.publicKey,
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ let otherMint = await Token.createMint(
|
|
|
+ program.provider.connection,
|
|
|
+ program.provider.wallet.payer,
|
|
|
+ program.provider.wallet.publicKey,
|
|
|
+ program.provider.wallet.publicKey,
|
|
|
+ 9,
|
|
|
+ TOKEN_PROGRAM_ID
|
|
|
+ );
|
|
|
+
|
|
|
+ await assert.rejects(
|
|
|
+ async () => {
|
|
|
+ await program.rpc.testValidateAssociatedToken({
|
|
|
+ accounts: {
|
|
|
+ token: associatedToken,
|
|
|
+ mint: otherMint.publicKey,
|
|
|
+ wallet: program.provider.wallet.publicKey,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ assert.equal(err.code, 2009);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ );
|
|
|
});
|
|
|
|
|
|
- await assert.rejects(
|
|
|
- async () => {
|
|
|
- await program.rpc.testValidateAssociatedToken({
|
|
|
- accounts: {
|
|
|
- token: associatedToken,
|
|
|
- mint: mint.publicKey,
|
|
|
- wallet: anchor.web3.Keypair.generate().publicKey,
|
|
|
- },
|
|
|
- });
|
|
|
- },
|
|
|
- (err) => {
|
|
|
- assert.equal(err.code, 2009);
|
|
|
- return true;
|
|
|
- }
|
|
|
- );
|
|
|
+ it("associated_token constraints check do not allow authority change", async () => {
|
|
|
+ const localClient = await client;
|
|
|
+ await program.rpc.testValidateAssociatedToken({
|
|
|
+ accounts: {
|
|
|
+ token: associatedToken,
|
|
|
+ mint: localClient.publicKey,
|
|
|
+ wallet: program.provider.wallet.publicKey,
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ await localClient.setAuthority(
|
|
|
+ associatedToken,
|
|
|
+ anchor.web3.Keypair.generate().publicKey,
|
|
|
+ "AccountOwner",
|
|
|
+ program.provider.wallet.payer,
|
|
|
+ []
|
|
|
+ );
|
|
|
+
|
|
|
+ await assert.rejects(
|
|
|
+ async () => {
|
|
|
+ await program.rpc.testValidateAssociatedToken({
|
|
|
+ accounts: {
|
|
|
+ token: associatedToken,
|
|
|
+ mint: localClient.publicKey,
|
|
|
+ wallet: program.provider.wallet.publicKey,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ assert.equal(err.code, 2015);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ );
|
|
|
+ });
|
|
|
});
|
|
|
|
|
|
it("Can fetch all accounts of a given type", async () => {
|