From fd775b5945ab7e5006b412a441cd1a845408b498 Mon Sep 17 00:00:00 2001 From: Will <82029448+wjthieme@users.noreply.github.com> Date: Fri, 25 Aug 2023 00:04:43 +0200 Subject: [PATCH] token-js: added the missing AuthorityTypes and tests (#5102) * Added the missing AuthorityTypes and tests * Added a test for TransferHookProgramId setAuthority and fixed failing tests * Update token/js/test/e2e/setAuthority.test.ts Co-authored-by: Jon Cinque --------- Co-authored-by: Jon Cinque --- .../js/src/extensions/transferHook/actions.ts | 2 +- token/js/src/instructions/setAuthority.ts | 9 ++++ token/js/test/e2e-2022/closeMint.test.ts | 26 ++++++++++- .../test/e2e-2022/interestBearingMint.test.ts | 24 ++++++++++- .../test/e2e-2022/permanentDelegate.test.ts | 26 ++++++++++- token/js/test/e2e-2022/transferFee.test.ts | 43 ++++++++++++++++++- token/js/test/e2e-2022/transferHook.test.ts | 43 ++++++++++++++++--- token/js/test/e2e/setAuthority.test.ts | 19 +++++++- 8 files changed, 179 insertions(+), 13 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/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..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, @@ -12,6 +13,10 @@ import { mintTo, getMintLen, ExtensionType, + AuthorityType, + getMint, + 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.eql(PublicKey.default); + } + }); }); diff --git a/token/js/test/e2e-2022/interestBearingMint.test.ts b/token/js/test/e2e-2022/interestBearingMint.test.ts index 2edd20ec92f..6da2514771e 100644 --- a/token/js/test/e2e-2022/interestBearingMint.test.ts +++ b/token/js/test/e2e-2022/interestBearingMint.test.ts @@ -2,12 +2,15 @@ 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, + 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.eql(PublicKey.default); + } + }); }); diff --git a/token/js/test/e2e-2022/permanentDelegate.test.ts b/token/js/test/e2e-2022/permanentDelegate.test.ts index 5cbbc8ac5b9..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, @@ -13,6 +14,10 @@ import { createInitializePermanentDelegateInstruction, burn, transferChecked, + AuthorityType, + 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.eql(PublicKey.default); + } + }); }); diff --git a/token/js/test/e2e-2022/transferFee.test.ts b/token/js/test/e2e-2022/transferFee.test.ts index ce0bae69410..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 { @@ -16,6 +17,8 @@ import { getAccount, getMint, getMintLen, + setAuthority, + AuthorityType, } from '../../src'; import { @@ -226,4 +229,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.eql(PublicKey.default); + } + }); + 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.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); } }); }); diff --git a/token/js/test/e2e/setAuthority.test.ts b/token/js/test/e2e/setAuthority.test.ts index 1700d82b2f9..f5c931907df 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.freezeAuthority).to.be.null; + }); });