Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

token-js: added the missing AuthorityTypes and tests #5102

Merged
merged 4 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion token/js/src/extensions/transferHook/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions token/js/src/instructions/setAuthority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
26 changes: 25 additions & 1 deletion token/js/test/e2e-2022/closeMint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,6 +13,10 @@ import {
mintTo,
getMintLen,
ExtensionType,
AuthorityType,
getMint,
setAuthority,
getMintCloseAuthority,
} from '../../src';
import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common';

Expand Down Expand Up @@ -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);
}
});
});
24 changes: 23 additions & 1 deletion token/js/test/e2e-2022/interestBearingMint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
});
});
26 changes: 25 additions & 1 deletion token/js/test/e2e-2022/permanentDelegate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -13,6 +14,10 @@ import {
createInitializePermanentDelegateInstruction,
burn,
transferChecked,
AuthorityType,
getMint,
setAuthority,
getPermanentDelegate,
} from '../../src';
import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common';

Expand Down Expand Up @@ -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);
}
});
});
43 changes: 42 additions & 1 deletion token/js/test/e2e-2022/transferFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,6 +17,8 @@ import {
getAccount,
getMint,
getMintLen,
setAuthority,
AuthorityType,
} from '../../src';

import {
Expand Down Expand Up @@ -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);
}
});
});
43 changes: 36 additions & 7 deletions token/js/test/e2e-2022/transferHook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,6 +13,8 @@ import {
createInitializeTransferHookInstruction,
getTransferHook,
updateTransferHook,
AuthorityType,
setAuthority,
} from '../../src';
import { TEST_PROGRAM_ID, newAccountWithLamports, getConnection } from '../common';

Expand All @@ -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;
});
Expand All @@ -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)
);

Expand All @@ -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 () => {
Expand All @@ -64,7 +74,7 @@ describe('transferHook', () => {
payer,
mint,
newTransferHookProgramId,
payer.publicKey,
transferHookAuthority,
[],
undefined,
TEST_PROGRAM_ID
Expand All @@ -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);
}
});
});
19 changes: 18 additions & 1 deletion token/js/test/e2e/setAuthority.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
wjthieme marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading