-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pausable extension to js-legacy client
- Loading branch information
1 parent
7644591
commit f2685ec
Showing
9 changed files
with
380 additions
and
0 deletions.
There are no files selected for viewing
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
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,63 @@ | ||
import type { ConfirmOptions, Connection, PublicKey, Signer, TransactionSignature } from '@solana/web3.js'; | ||
import { sendAndConfirmTransaction, Transaction } from '@solana/web3.js'; | ||
import { getSigners } from '../../actions/internal.js'; | ||
import { TOKEN_2022_PROGRAM_ID } from '../../constants.js'; | ||
import { createPauseInstruction, createResumeInstruction } from './instructions.js'; | ||
|
||
/** | ||
* Pause a pausable mint | ||
* | ||
* @param connection Connection to use | ||
* @param payer Payer of the transaction fees | ||
* @param mint Public key of the mint | ||
* @param owner The pausable config authority | ||
* @param multiSigners Signing accounts if `owner` is a multisig | ||
* @param confirmOptions Options for confirming the transaction | ||
* @param programId SPL Token program account | ||
* | ||
* @return Public key of the mint | ||
*/ | ||
export async function pause( | ||
connection: Connection, | ||
payer: Signer, | ||
mint: PublicKey, | ||
owner: Signer | PublicKey, | ||
multiSigners: Signer[] = [], | ||
confirmOptions?: ConfirmOptions, | ||
programId = TOKEN_2022_PROGRAM_ID, | ||
): Promise<TransactionSignature> { | ||
const [ownerPublicKey, signers] = getSigners(owner, multiSigners); | ||
|
||
const transaction = new Transaction().add(createPauseInstruction(mint, ownerPublicKey, multiSigners, programId)); | ||
|
||
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); | ||
} | ||
|
||
/** | ||
* Resume a pausable mint | ||
* | ||
* @param connection Connection to use | ||
* @param payer Payer of the transaction fees | ||
* @param mint Public key of the mint | ||
* @param owner The pausable config authority | ||
* @param multiSigners Signing accounts if `owner` is a multisig | ||
* @param confirmOptions Options for confirming the transaction | ||
* @param programId SPL Token program account | ||
* | ||
* @return Public key of the mint | ||
*/ | ||
export async function resume( | ||
connection: Connection, | ||
payer: Signer, | ||
mint: PublicKey, | ||
owner: Signer | PublicKey, | ||
multiSigners: Signer[] = [], | ||
confirmOptions?: ConfirmOptions, | ||
programId = TOKEN_2022_PROGRAM_ID, | ||
): Promise<TransactionSignature> { | ||
const [ownerPublicKey, signers] = getSigners(owner, multiSigners); | ||
|
||
const transaction = new Transaction().add(createResumeInstruction(mint, ownerPublicKey, multiSigners, programId)); | ||
|
||
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); | ||
} |
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,3 @@ | ||
export * from './actions.js'; | ||
export * from './instructions.js'; | ||
export * from './state.js'; |
132 changes: 132 additions & 0 deletions
132
clients/js-legacy/src/extensions/pausable/instructions.ts
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,132 @@ | ||
import { struct, u8 } from '@solana/buffer-layout'; | ||
import { publicKey } from '@solana/buffer-layout-utils'; | ||
import type { Signer } from '@solana/web3.js'; | ||
import { PublicKey, TransactionInstruction } from '@solana/web3.js'; | ||
import { TOKEN_2022_PROGRAM_ID, programSupportsExtensions } from '../../constants.js'; | ||
import { TokenUnsupportedInstructionError } from '../../errors.js'; | ||
import { TokenInstruction } from '../../instructions/types.js'; | ||
import { addSigners } from '../../instructions/internal.js'; | ||
|
||
export enum PausableInstruction { | ||
Initialize = 0, | ||
Pause = 1, | ||
Resume = 2, | ||
} | ||
|
||
export interface InitializePausableConfigInstructionData { | ||
instruction: TokenInstruction.PausableExtension; | ||
pausableInstruction: PausableInstruction.Initialize; | ||
authority: PublicKey; | ||
} | ||
|
||
export const initializePausableConfigInstructionData = struct<InitializePausableConfigInstructionData>([ | ||
u8('instruction'), | ||
u8('pausableInstruction'), | ||
publicKey('authority'), | ||
]); | ||
|
||
/** | ||
* Construct a InitializePausableConfig instruction | ||
* | ||
* @param mint Token mint account | ||
* @param authority Optional authority that can pause or resume mint | ||
* @param programId SPL Token program account | ||
*/ | ||
export function createInitializePausableConfigInstruction( | ||
mint: PublicKey, | ||
authority: PublicKey | null, | ||
programId: PublicKey = TOKEN_2022_PROGRAM_ID, | ||
): TransactionInstruction { | ||
if (!programSupportsExtensions(programId)) { | ||
throw new TokenUnsupportedInstructionError(); | ||
} | ||
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }]; | ||
|
||
const data = Buffer.alloc(initializePausableConfigInstructionData.span); | ||
initializePausableConfigInstructionData.encode( | ||
{ | ||
instruction: TokenInstruction.PausableExtension, | ||
pausableInstruction: PausableInstruction.Initialize, | ||
authority: authority ?? PublicKey.default, | ||
}, | ||
data, | ||
); | ||
|
||
return new TransactionInstruction({ keys, programId, data: data }); | ||
} | ||
|
||
export interface PauseInstructionData { | ||
instruction: TokenInstruction.PausableExtension; | ||
pausableInstruction: PausableInstruction.Pause; | ||
} | ||
|
||
export const pauseInstructionData = struct<PauseInstructionData>([u8('instruction'), u8('pausableInstruction')]); | ||
|
||
/** | ||
* Construct a Pause instruction | ||
* | ||
* @param mint Token mint account | ||
* @param authority The pausable mint's authority | ||
* @param multiSigners Signing accounts if authority is a multisig | ||
* @param programId SPL Token program account | ||
*/ | ||
export function createPauseInstruction( | ||
mint: PublicKey, | ||
authority: PublicKey, | ||
multiSigners: (Signer | PublicKey)[] = [], | ||
programId: PublicKey = TOKEN_2022_PROGRAM_ID, | ||
): TransactionInstruction { | ||
if (!programSupportsExtensions(programId)) { | ||
throw new TokenUnsupportedInstructionError(); | ||
} | ||
const keys = addSigners([{ pubkey: mint, isSigner: false, isWritable: true }], authority, multiSigners); | ||
|
||
const data = Buffer.alloc(pauseInstructionData.span); | ||
pauseInstructionData.encode( | ||
{ | ||
instruction: TokenInstruction.PausableExtension, | ||
pausableInstruction: PausableInstruction.Pause, | ||
}, | ||
data, | ||
); | ||
|
||
return new TransactionInstruction({ keys, programId, data: data }); | ||
} | ||
|
||
export interface ResumeInstructionData { | ||
instruction: TokenInstruction.PausableExtension; | ||
pausableInstruction: PausableInstruction.Resume; | ||
} | ||
|
||
export const resumeInstructionData = struct<ResumeInstructionData>([u8('instruction'), u8('pausableInstruction')]); | ||
|
||
/** | ||
* Construct a Resume instruction | ||
* | ||
* @param mint Token mint account | ||
* @param authority The pausable mint's authority | ||
* @param multiSigners Signing accounts if authority is a multisig | ||
* @param programId SPL Token program account | ||
*/ | ||
export function createResumeInstruction( | ||
mint: PublicKey, | ||
authority: PublicKey, | ||
multiSigners: (Signer | PublicKey)[] = [], | ||
programId: PublicKey = TOKEN_2022_PROGRAM_ID, | ||
): TransactionInstruction { | ||
if (!programSupportsExtensions(programId)) { | ||
throw new TokenUnsupportedInstructionError(); | ||
} | ||
const keys = addSigners([{ pubkey: mint, isSigner: false, isWritable: true }], authority, multiSigners); | ||
|
||
const data = Buffer.alloc(resumeInstructionData.span); | ||
resumeInstructionData.encode( | ||
{ | ||
instruction: TokenInstruction.PausableExtension, | ||
pausableInstruction: PausableInstruction.Resume, | ||
}, | ||
data, | ||
); | ||
|
||
return new TransactionInstruction({ keys, programId, data: data }); | ||
} |
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,45 @@ | ||
import { struct } from '@solana/buffer-layout'; | ||
import { publicKey, bool } from '@solana/buffer-layout-utils'; | ||
import type { PublicKey } from '@solana/web3.js'; | ||
import type { Account } from '../../state/account.js'; | ||
import type { Mint } from '../../state/mint.js'; | ||
import { ExtensionType, getExtensionData } from '../extensionType.js'; | ||
|
||
/** PausableConfig as stored by the program */ | ||
export interface PausableConfig { | ||
/** Authority that can pause or resume activity on the mint */ | ||
authority: PublicKey; | ||
/** Whether minting / transferring / burning tokens is paused */ | ||
paused: boolean; | ||
} | ||
|
||
/** Buffer layout for de/serializing a pausable config */ | ||
export const PausableConfigLayout = struct<PausableConfig>([publicKey('authority'), bool('paused')]); | ||
|
||
export const PAUSABLE_CONFIG_SIZE = PausableConfigLayout.span; | ||
|
||
export function getPausableConfig(mint: Mint): PausableConfig | null { | ||
const extensionData = getExtensionData(ExtensionType.PausableConfig, mint.tlvData); | ||
if (extensionData !== null) { | ||
return PausableConfigLayout.decode(extensionData); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** Pausable token account state as stored by the program */ | ||
export interface PausableAccount {} // eslint-disable-line | ||
|
||
/** Buffer layout for de/serializing a pausable account */ | ||
export const PausableAccountLayout = struct<PausableAccount>([]); // esline-disable-line | ||
|
||
export const PAUSABLE_ACCOUNT_SIZE = PausableAccountLayout.span; | ||
|
||
export function getPausableAccount(account: Account): PausableAccount | null { | ||
const extensionData = getExtensionData(ExtensionType.PausableAccount, account.tlvData); | ||
if (extensionData !== null) { | ||
return PausableAccountLayout.decode(extensionData); | ||
} else { | ||
return null; | ||
} | ||
} |
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
Oops, something went wrong.