-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
token-js: Added support for transfer hook instructions (#5096)
* Update extensionType.ts * Added support for transfer hook in @solana/spl-token js client * Renamed programId to transferHookProgramId for clarity * Added the TransferHookAccount extension type
- Loading branch information
Showing
11 changed files
with
469 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
clusterApiUrl, | ||
sendAndConfirmTransaction, | ||
Connection, | ||
Keypair, | ||
PublicKey, | ||
SystemProgram, | ||
Transaction, | ||
LAMPORTS_PER_SOL, | ||
} from '@solana/web3.js'; | ||
|
||
import { | ||
ExtensionType, | ||
createInitializeMintInstruction, | ||
createInitializeTransferHookInstruction, | ||
getMintLen, | ||
TOKEN_2022_PROGRAM_ID, | ||
updateTransferHook, | ||
} from '../src'; | ||
|
||
(async () => { | ||
const payer = Keypair.generate(); | ||
|
||
const mintAuthority = Keypair.generate(); | ||
const mintKeypair = Keypair.generate(); | ||
const mint = mintKeypair.publicKey; | ||
|
||
const extensions = [ExtensionType.TransferHook]; | ||
const mintLen = getMintLen(extensions); | ||
const decimals = 9; | ||
const transferHookPogramId = new PublicKey('7N4HggYEJAtCLJdnHGCtFqfxcB5rhQCsQTze3ftYstVj'); | ||
const newTransferHookProgramId = new PublicKey('7N4HggYEJAtCLJdnHGCtFqfxcB5rhQCsQTze3ftYstVj'); | ||
|
||
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed'); | ||
|
||
const airdropSignature = await connection.requestAirdrop(payer.publicKey, 2 * LAMPORTS_PER_SOL); | ||
await connection.confirmTransaction({ signature: airdropSignature, ...(await connection.getLatestBlockhash()) }); | ||
|
||
const mintLamports = await connection.getMinimumBalanceForRentExemption(mintLen); | ||
const mintTransaction = new Transaction().add( | ||
SystemProgram.createAccount({ | ||
fromPubkey: payer.publicKey, | ||
newAccountPubkey: mint, | ||
space: mintLen, | ||
lamports: mintLamports, | ||
programId: TOKEN_2022_PROGRAM_ID, | ||
}), | ||
createInitializeTransferHookInstruction(mint, payer.publicKey, transferHookPogramId, TOKEN_2022_PROGRAM_ID), | ||
createInitializeMintInstruction(mint, decimals, mintAuthority.publicKey, null, TOKEN_2022_PROGRAM_ID) | ||
); | ||
await sendAndConfirmTransaction(connection, mintTransaction, [payer, mintKeypair], undefined); | ||
|
||
await updateTransferHook( | ||
connection, | ||
payer, | ||
mint, | ||
newTransferHookProgramId, | ||
payer.publicKey, | ||
[], | ||
undefined, | ||
TOKEN_2022_PROGRAM_ID | ||
); | ||
})(); |
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,67 @@ | ||
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 { createInitializeTransferHookInstruction, createUpdateTransferHookInstruction } from './instructions.js'; | ||
|
||
/** | ||
* Initialize a transfer hook on a mint | ||
* | ||
* @param connection Connection to use | ||
* @param payer Payer of the transaction fees | ||
* @param mint Mint to initialize with extension | ||
* @param authority Transfer hook authority account | ||
* @param transferHookProgramId The transfer hook program account | ||
* @param confirmOptions Options for confirming the transaction | ||
* @param programId SPL Token program account | ||
* | ||
* @return Signature of the confirmed transaction | ||
*/ | ||
export async function initializeTransferHook( | ||
connection: Connection, | ||
payer: Signer, | ||
mint: PublicKey, | ||
authority: PublicKey, | ||
transferHookProgramId: PublicKey, | ||
confirmOptions?: ConfirmOptions, | ||
programId = TOKEN_2022_PROGRAM_ID | ||
): Promise<TransactionSignature> { | ||
const transaction = new Transaction().add( | ||
createInitializeTransferHookInstruction(mint, authority, transferHookProgramId, programId) | ||
); | ||
|
||
return await sendAndConfirmTransaction(connection, transaction, [payer], confirmOptions); | ||
} | ||
|
||
/** | ||
* Update the transfer hook program on a mint | ||
* | ||
* @param connection Connection to use | ||
* @param payer Payer of the transaction fees | ||
* @param mint Mint to modify | ||
* @param transferHookProgramId New transfer hook program account | ||
* @param authority Transfer hook update authority | ||
* @param multiSigners Signing accounts if `freezeAuthority` is a multisig | ||
* @param confirmOptions Options for confirming the transaction | ||
* @param programId SPL Token program account | ||
* | ||
* @return Signature of the confirmed transaction | ||
*/ | ||
export async function updateTransferHook( | ||
connection: Connection, | ||
payer: Signer, | ||
mint: PublicKey, | ||
transferHookProgramId: PublicKey, | ||
authority: PublicKey, | ||
multiSigners: Signer[] = [], | ||
confirmOptions?: ConfirmOptions, | ||
programId = TOKEN_2022_PROGRAM_ID | ||
): Promise<TransactionSignature> { | ||
const [authorityPublicKey, signers] = getSigners(authority, multiSigners); | ||
|
||
const transaction = new Transaction().add( | ||
createUpdateTransferHookInstruction(mint, authorityPublicKey, transferHookProgramId, signers, 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'; |
Oops, something went wrong.