From 4946d64650f389df12b9e3b42d54542eb122212e Mon Sep 17 00:00:00 2001 From: Wilhelm Thieme Date: Thu, 24 Aug 2023 15:12:33 +0200 Subject: [PATCH 1/3] Added the missing AuthorityTypes and tests --- token/js/src/instructions/setAuthority.ts | 9 +++++ token/js/test/e2e-2022/closeMint.test.ts | 24 +++++++++++ .../test/e2e-2022/interestBearingMint.test.ts | 22 ++++++++++ .../test/e2e-2022/permanentDelegate.test.ts | 24 +++++++++++ token/js/test/e2e-2022/transferFee.test.ts | 40 +++++++++++++++++++ token/js/test/e2e/setAuthority.test.ts | 19 ++++++++- 6 files changed, 137 insertions(+), 1 deletion(-) diff --git a/token/js/src/instructions/setAuthority.ts b/token/js/src/instructions/setAuthority.ts index 71722d451fb..4301f57c4ca 100644 --- a/token/js/src/instructions/setAuthority.ts +++ b/token/js/src/instructions/setAuthority.ts @@ -18,6 +18,15 @@ export enum AuthorityType { FreezeAccount = 1, AccountOwner = 2, CloseAccount = 3, + TransferFeeConfig = 4, + WithheldWithdraw = 5, + CloseMint = 6, + InterestRate = 7, + PermanentDelegate = 8, + ConfidentialTransferMint = 9, + TransferHookProgramId = 10, + ConfidentialTransferFeeConfig = 11, + MetadataPointer = 12, } /** TODO: docs */ diff --git a/token/js/test/e2e-2022/closeMint.test.ts b/token/js/test/e2e-2022/closeMint.test.ts index f5351f65c80..2aea08bd19f 100644 --- a/token/js/test/e2e-2022/closeMint.test.ts +++ b/token/js/test/e2e-2022/closeMint.test.ts @@ -12,6 +12,11 @@ import { mintTo, getMintLen, ExtensionType, + AuthorityType, + getMint, + getTransferFeeConfig, + setAuthority, + getMintCloseAuthority, } from '../../src'; import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common'; @@ -80,4 +85,23 @@ describe('closeMint', () => { expect(destinationInfo.lamports).to.eql(rentExemptAmount); } }); + it('authority', async () => { + await setAuthority( + connection, + payer, + mint, + closeAuthority, + AuthorityType.CloseMint, + null, + [], + undefined, + TEST_PROGRAM_ID + ); + const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); + const mintCloseAuthority = getMintCloseAuthority(mintInfo); + expect(mintCloseAuthority).to.not.be.null; + if (mintCloseAuthority !== null) { + expect(mintCloseAuthority.closeAuthority).to.be.null; + } + }); }); diff --git a/token/js/test/e2e-2022/interestBearingMint.test.ts b/token/js/test/e2e-2022/interestBearingMint.test.ts index 2edd20ec92f..fdcc8363bb7 100644 --- a/token/js/test/e2e-2022/interestBearingMint.test.ts +++ b/token/js/test/e2e-2022/interestBearingMint.test.ts @@ -5,9 +5,12 @@ chai.use(chaiAsPromised); import type { Connection, PublicKey, Signer } from '@solana/web3.js'; import { Keypair } from '@solana/web3.js'; import { + AuthorityType, createInterestBearingMint, getInterestBearingMintConfigState, getMint, + getMintCloseAuthority, + setAuthority, updateRateInterestBearingMint, } from '../../src'; import { getConnection, newAccountWithLamports, TEST_PROGRAM_ID } from '../common'; @@ -81,4 +84,23 @@ describe('interestBearingMint', () => { expect(updatedRateConfigState.initializationTimestamp).to.be.greaterThan(0); } }); + it('authority', async () => { + await setAuthority( + connection, + payer, + mint, + rateAuthority, + AuthorityType.InterestRate, + null, + [], + undefined, + TEST_PROGRAM_ID + ); + const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); + const rateConfigState = getInterestBearingMintConfigState(mintInfo); + expect(rateConfigState).to.not.be.null; + if (rateConfigState !== null) { + expect(rateConfigState.rateAuthority).to.be.null; + } + }); }); diff --git a/token/js/test/e2e-2022/permanentDelegate.test.ts b/token/js/test/e2e-2022/permanentDelegate.test.ts index 5cbbc8ac5b9..13a7dbcd705 100644 --- a/token/js/test/e2e-2022/permanentDelegate.test.ts +++ b/token/js/test/e2e-2022/permanentDelegate.test.ts @@ -13,6 +13,11 @@ import { createInitializePermanentDelegateInstruction, burn, transferChecked, + AuthorityType, + getInterestBearingMintConfigState, + getMint, + setAuthority, + getPermanentDelegate, } from '../../src'; import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common'; @@ -100,4 +105,23 @@ describe('permanentDelegate', () => { expect(destination_info.value.uiAmount).to.eql(2); } }); + it('authority', async () => { + await setAuthority( + connection, + payer, + mint, + permanentDelegate, + AuthorityType.PermanentDelegate, + null, + [], + undefined, + TEST_PROGRAM_ID + ); + const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); + const permanentDelegateConfig = getPermanentDelegate(mintInfo); + expect(permanentDelegateConfig).to.not.be.null; + if (permanentDelegateConfig !== null) { + expect(permanentDelegateConfig.delegate).to.be.null; + } + }); }); diff --git a/token/js/test/e2e-2022/transferFee.test.ts b/token/js/test/e2e-2022/transferFee.test.ts index ce0bae69410..9acb1b65103 100644 --- a/token/js/test/e2e-2022/transferFee.test.ts +++ b/token/js/test/e2e-2022/transferFee.test.ts @@ -16,6 +16,8 @@ import { getAccount, getMint, getMintLen, + setAuthority, + AuthorityType, } from '../../src'; import { @@ -226,4 +228,42 @@ describe('transferFee', () => { expect(transferFeeConfig.withheldAmount).to.eql(BigInt(0)); } }); + it('transferFeeConfigAuthority', async () => { + await setAuthority( + connection, + payer, + mint, + transferFeeConfigAuthority, + AuthorityType.TransferFeeConfig, + null, + [], + undefined, + TEST_PROGRAM_ID + ); + const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); + const transferFeeConfig = getTransferFeeConfig(mintInfo); + expect(transferFeeConfig).to.not.be.null; + if (transferFeeConfig !== null) { + expect(transferFeeConfig.transferFeeConfigAuthority).to.be.null; + } + }); + it('withdrawWithheldAuthority', async () => { + await setAuthority( + connection, + payer, + mint, + withdrawWithheldAuthority, + AuthorityType.WithheldWithdraw, + null, + [], + undefined, + TEST_PROGRAM_ID + ); + const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); + const transferFeeConfig = getTransferFeeConfig(mintInfo); + expect(transferFeeConfig).to.not.be.null; + if (transferFeeConfig !== null) { + expect(transferFeeConfig.transferFeeConfigAuthority).to.be.null; + } + }); }); diff --git a/token/js/test/e2e/setAuthority.test.ts b/token/js/test/e2e/setAuthority.test.ts index 1700d82b2f9..80f55fac1c0 100644 --- a/token/js/test/e2e/setAuthority.test.ts +++ b/token/js/test/e2e/setAuthority.test.ts @@ -15,18 +15,20 @@ describe('setAuthority', () => { let payer: Signer; let mint: PublicKey; let mintAuthority: Keypair; + let freezeAuthority: Keypair; let owner: Keypair; let account: PublicKey; before(async () => { connection = await getConnection(); payer = await newAccountWithLamports(connection, 1000000000); mintAuthority = Keypair.generate(); + freezeAuthority = Keypair.generate(); const mintKeypair = Keypair.generate(); mint = await createMint( connection, payer, mintAuthority.publicKey, - mintAuthority.publicKey, + freezeAuthority.publicKey, TEST_TOKEN_DECIMALS, mintKeypair, undefined, @@ -116,4 +118,19 @@ describe('setAuthority', () => { const accountInfo = await getAccount(connection, account, undefined, TEST_PROGRAM_ID); expect(accountInfo.closeAuthority).to.eql(closeAuthority.publicKey); }); + it('FreezeAuthority', async () => { + await setAuthority( + connection, + payer, + mint, + freezeAuthority, + AuthorityType.FreezeAccount, + null, + [], + undefined, + TEST_PROGRAM_ID + ); + const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); + expect(mintInfo.mintAuthority).to.be.null; + }); }); From ba08a2956df2859089660e9432e27ab62612baf4 Mon Sep 17 00:00:00 2001 From: Wilhelm Thieme Date: Thu, 24 Aug 2023 15:56:57 +0200 Subject: [PATCH 2/3] Added a test for TransferHookProgramId setAuthority and fixed failing tests --- .../js/src/extensions/transferHook/actions.ts | 2 +- token/js/test/e2e-2022/closeMint.test.ts | 6 +-- .../test/e2e-2022/interestBearingMint.test.ts | 6 +-- .../test/e2e-2022/permanentDelegate.test.ts | 6 +-- token/js/test/e2e-2022/transferFee.test.ts | 7 +-- token/js/test/e2e-2022/transferHook.test.ts | 43 ++++++++++++++++--- 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/token/js/src/extensions/transferHook/actions.ts b/token/js/src/extensions/transferHook/actions.ts index 6610c72416d..ac87727050d 100644 --- a/token/js/src/extensions/transferHook/actions.ts +++ b/token/js/src/extensions/transferHook/actions.ts @@ -52,7 +52,7 @@ export async function updateTransferHook( payer: Signer, mint: PublicKey, transferHookProgramId: PublicKey, - authority: PublicKey, + authority: Signer | PublicKey, multiSigners: Signer[] = [], confirmOptions?: ConfirmOptions, programId = TOKEN_2022_PROGRAM_ID diff --git a/token/js/test/e2e-2022/closeMint.test.ts b/token/js/test/e2e-2022/closeMint.test.ts index 2aea08bd19f..f6610a9c251 100644 --- a/token/js/test/e2e-2022/closeMint.test.ts +++ b/token/js/test/e2e-2022/closeMint.test.ts @@ -2,7 +2,8 @@ import chai, { expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; chai.use(chaiAsPromised); -import type { Connection, PublicKey, Signer } from '@solana/web3.js'; +import type { Connection, Signer } from '@solana/web3.js'; +import { PublicKey } from '@solana/web3.js'; import { sendAndConfirmTransaction, Keypair, SystemProgram, Transaction } from '@solana/web3.js'; import { createAccount, @@ -14,7 +15,6 @@ import { ExtensionType, AuthorityType, getMint, - getTransferFeeConfig, setAuthority, getMintCloseAuthority, } from '../../src'; @@ -101,7 +101,7 @@ describe('closeMint', () => { const mintCloseAuthority = getMintCloseAuthority(mintInfo); expect(mintCloseAuthority).to.not.be.null; if (mintCloseAuthority !== null) { - expect(mintCloseAuthority.closeAuthority).to.be.null; + expect(mintCloseAuthority.closeAuthority).to.eql(PublicKey.default); } }); }); diff --git a/token/js/test/e2e-2022/interestBearingMint.test.ts b/token/js/test/e2e-2022/interestBearingMint.test.ts index fdcc8363bb7..6da2514771e 100644 --- a/token/js/test/e2e-2022/interestBearingMint.test.ts +++ b/token/js/test/e2e-2022/interestBearingMint.test.ts @@ -2,14 +2,14 @@ import chai, { expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; chai.use(chaiAsPromised); -import type { Connection, PublicKey, Signer } from '@solana/web3.js'; +import type { Connection, Signer } from '@solana/web3.js'; +import { PublicKey } from '@solana/web3.js'; import { Keypair } from '@solana/web3.js'; import { AuthorityType, createInterestBearingMint, getInterestBearingMintConfigState, getMint, - getMintCloseAuthority, setAuthority, updateRateInterestBearingMint, } from '../../src'; @@ -100,7 +100,7 @@ describe('interestBearingMint', () => { const rateConfigState = getInterestBearingMintConfigState(mintInfo); expect(rateConfigState).to.not.be.null; if (rateConfigState !== null) { - expect(rateConfigState.rateAuthority).to.be.null; + expect(rateConfigState.rateAuthority).to.eql(PublicKey.default); } }); }); diff --git a/token/js/test/e2e-2022/permanentDelegate.test.ts b/token/js/test/e2e-2022/permanentDelegate.test.ts index 13a7dbcd705..802518a51fe 100644 --- a/token/js/test/e2e-2022/permanentDelegate.test.ts +++ b/token/js/test/e2e-2022/permanentDelegate.test.ts @@ -2,7 +2,8 @@ import chai, { expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; chai.use(chaiAsPromised); -import type { Connection, PublicKey, Signer } from '@solana/web3.js'; +import type { Connection, Signer } from '@solana/web3.js'; +import { PublicKey } from '@solana/web3.js'; import { sendAndConfirmTransaction, Keypair, SystemProgram, Transaction } from '@solana/web3.js'; import { createAccount, @@ -14,7 +15,6 @@ import { burn, transferChecked, AuthorityType, - getInterestBearingMintConfigState, getMint, setAuthority, getPermanentDelegate, @@ -121,7 +121,7 @@ describe('permanentDelegate', () => { const permanentDelegateConfig = getPermanentDelegate(mintInfo); expect(permanentDelegateConfig).to.not.be.null; if (permanentDelegateConfig !== null) { - expect(permanentDelegateConfig.delegate).to.be.null; + expect(permanentDelegateConfig.delegate).to.eql(PublicKey.default); } }); }); diff --git a/token/js/test/e2e-2022/transferFee.test.ts b/token/js/test/e2e-2022/transferFee.test.ts index 9acb1b65103..8e5cad6266d 100644 --- a/token/js/test/e2e-2022/transferFee.test.ts +++ b/token/js/test/e2e-2022/transferFee.test.ts @@ -2,7 +2,8 @@ import chai, { expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; chai.use(chaiAsPromised); -import type { Connection, PublicKey, Signer } from '@solana/web3.js'; +import type { Connection, Signer } from '@solana/web3.js'; +import { PublicKey } from '@solana/web3.js'; import { Keypair, SystemProgram, Transaction, sendAndConfirmTransaction } from '@solana/web3.js'; import { @@ -244,7 +245,7 @@ describe('transferFee', () => { const transferFeeConfig = getTransferFeeConfig(mintInfo); expect(transferFeeConfig).to.not.be.null; if (transferFeeConfig !== null) { - expect(transferFeeConfig.transferFeeConfigAuthority).to.be.null; + expect(transferFeeConfig.transferFeeConfigAuthority).to.eql(PublicKey.default); } }); it('withdrawWithheldAuthority', async () => { @@ -263,7 +264,7 @@ describe('transferFee', () => { const transferFeeConfig = getTransferFeeConfig(mintInfo); expect(transferFeeConfig).to.not.be.null; if (transferFeeConfig !== null) { - expect(transferFeeConfig.transferFeeConfigAuthority).to.be.null; + expect(transferFeeConfig.withdrawWithheldAuthority).to.eql(PublicKey.default); } }); }); diff --git a/token/js/test/e2e-2022/transferHook.test.ts b/token/js/test/e2e-2022/transferHook.test.ts index f88511d1910..f9a5520c51f 100644 --- a/token/js/test/e2e-2022/transferHook.test.ts +++ b/token/js/test/e2e-2022/transferHook.test.ts @@ -2,7 +2,8 @@ import chai, { expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; chai.use(chaiAsPromised); -import type { Connection, PublicKey, Signer } from '@solana/web3.js'; +import type { Connection, Signer } from '@solana/web3.js'; +import { PublicKey } from '@solana/web3.js'; import { sendAndConfirmTransaction, Keypair, SystemProgram, Transaction } from '@solana/web3.js'; import { createInitializeMintInstruction, @@ -12,6 +13,8 @@ import { createInitializeTransferHookInstruction, getTransferHook, updateTransferHook, + AuthorityType, + setAuthority, } from '../../src'; import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common'; @@ -20,12 +23,14 @@ const EXTENSIONS = [ExtensionType.TransferHook]; describe('transferHook', () => { let connection: Connection; let payer: Signer; + let transferHookAuthority: Keypair; let mint: PublicKey; let transferHookProgramId: PublicKey; let newTransferHookProgramId: PublicKey; before(async () => { connection = await getConnection(); payer = await newAccountWithLamports(connection, 1000000000); + transferHookAuthority = Keypair.generate(); transferHookProgramId = Keypair.generate().publicKey; newTransferHookProgramId = Keypair.generate().publicKey; }); @@ -43,7 +48,12 @@ describe('transferHook', () => { lamports, programId: TEST_PROGRAM_ID, }), - createInitializeTransferHookInstruction(mint, payer.publicKey, transferHookProgramId, TEST_PROGRAM_ID), + createInitializeTransferHookInstruction( + mint, + transferHookAuthority.publicKey, + transferHookProgramId, + TEST_PROGRAM_ID + ), createInitializeMintInstruction(mint, TEST_TOKEN_DECIMALS, payer.publicKey, null, TEST_PROGRAM_ID) ); @@ -54,8 +64,8 @@ describe('transferHook', () => { const transferHook = getTransferHook(mintInfo); expect(transferHook).to.not.be.null; if (transferHook !== null) { - expect(transferHook.authority.toString()).to.eql(payer.publicKey.toString()); - expect(transferHook.programId.toString()).to.eql(transferHookProgramId.toString()); + expect(transferHook.authority).to.eql(transferHookAuthority.publicKey); + expect(transferHook.programId).to.eql(transferHookProgramId); } }); it('can be updated', async () => { @@ -64,7 +74,7 @@ describe('transferHook', () => { payer, mint, newTransferHookProgramId, - payer.publicKey, + transferHookAuthority, [], undefined, TEST_PROGRAM_ID @@ -73,8 +83,27 @@ describe('transferHook', () => { const transferHook = getTransferHook(mintInfo); expect(transferHook).to.not.be.null; if (transferHook !== null) { - expect(transferHook.authority.toString()).to.eql(payer.publicKey.toString()); - expect(transferHook.programId.toString()).to.eql(newTransferHookProgramId.toString()); + expect(transferHook.authority).to.eql(transferHookAuthority.publicKey); + expect(transferHook.programId).to.eql(newTransferHookProgramId); + } + }); + it('authority', async () => { + await setAuthority( + connection, + payer, + mint, + transferHookAuthority, + AuthorityType.TransferHookProgramId, + null, + [], + undefined, + TEST_PROGRAM_ID + ); + const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); + const transferHook = getTransferHook(mintInfo); + expect(transferHook).to.not.be.null; + if (transferHook !== null) { + expect(transferHook.authority).to.eql(PublicKey.default); } }); }); From c7ecc9a4909eed68479c0ec6bf67708972f36809 Mon Sep 17 00:00:00 2001 From: Will <82029448+wjthieme@users.noreply.github.com> Date: Thu, 24 Aug 2023 19:22:10 +0200 Subject: [PATCH 3/3] Update token/js/test/e2e/setAuthority.test.ts Co-authored-by: Jon Cinque --- token/js/test/e2e/setAuthority.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/token/js/test/e2e/setAuthority.test.ts b/token/js/test/e2e/setAuthority.test.ts index 80f55fac1c0..f5c931907df 100644 --- a/token/js/test/e2e/setAuthority.test.ts +++ b/token/js/test/e2e/setAuthority.test.ts @@ -131,6 +131,6 @@ describe('setAuthority', () => { TEST_PROGRAM_ID ); const mintInfo = await getMint(connection, mint, undefined, TEST_PROGRAM_ID); - expect(mintInfo.mintAuthority).to.be.null; + expect(mintInfo.freezeAuthority).to.be.null; }); });