Skip to content

Commit

Permalink
added initialize metadata action
Browse files Browse the repository at this point in the history
  • Loading branch information
mistersimon committed Oct 26, 2023
1 parent 506ed92 commit e9390d8
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 2 deletions.
147 changes: 147 additions & 0 deletions token/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions token/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@
"dependencies": {
"@solana/buffer-layout": "^4.0.0",
"@solana/buffer-layout-utils": "^0.2.0",
"@solana/spl-token-metadata": "^0.1.0",
"buffer": "^6.0.3"
},
"devDependencies": {
"@solana/spl-memo": "^0.2.2",
"@solana/web3.js": "^1.47.4",
"@types/chai-as-promised": "^7.1.4",
"@types/chai": "^4.3.3",
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^10.0.0",
"@types/node": "^20.1.1",
"@types/node-fetch": "^2.6.2",
Expand All @@ -80,8 +81,8 @@
"process": "^0.11.10",
"shx": "^0.3.4",
"start-server-and-test": "^2.0.0",
"tslib": "^2.3.1",
"ts-node": "^10.9.1",
"tslib": "^2.3.1",
"typedoc": "^0.25.0",
"typescript": "^5.0.4"
}
Expand Down
1 change: 1 addition & 0 deletions token/js/src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './createNativeMint.js';
export * from './createWrappedNativeAccount.js';
export * from './freezeAccount.js';
export * from './getOrCreateAssociatedTokenAccount.js';
export * from "./initializeMetadata.js";
export * from './mintTo.js';
export * from './mintToChecked.js';
export * from './recoverNested.js';
Expand Down
56 changes: 56 additions & 0 deletions token/js/src/actions/initializeMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { ConfirmOptions, Connection, PublicKey, Signer, TransactionSignature } from '@solana/web3.js';
import { createInitializeInstruction } from '@solana/spl-token-metadata';
import { sendAndConfirmTransaction, Transaction } from '@solana/web3.js';

import { TOKEN_2022_PROGRAM_ID } from '../constants.js';
import { getSigners } from './internal.js';

/**
* Initializes a TLV entry with the basic token-metadata fields.
*
* @param connection Connection to use
* @param payer Payer of the transaction fees
* @param metadata Metadata account
* @param updateAuthority Update Authority
* @param mint Mint Account
* @param mintAuthority Mint Authority
* @param name Longer name of token
* @param symbol Shortened symbol of token
* @param uri URI pointing to more metadata (image, video, etc)
* @param multiSigners Signing accounts if `authority` 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 initializeMetadata(
connection: Connection,
payer: Signer,
metadata: PublicKey,
updateAuthority: PublicKey,
mint: PublicKey,
mintAuthority: PublicKey | Signer,
name: string,
symbol: string,
uri: string,
multiSigners: Signer[] = [],
confirmOptions?: ConfirmOptions,
programId = TOKEN_2022_PROGRAM_ID
): Promise<TransactionSignature> {
const [mintAuthorityPublicKey, signers] = getSigners(mintAuthority, multiSigners);

const transaction = new Transaction().add(
createInitializeInstruction({
programId,
metadata,
updateAuthority,
mint,
mintAuthority: mintAuthorityPublicKey,
name,
symbol,
uri,
})
);

return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions);
}
8 changes: 8 additions & 0 deletions token/js/src/instructions/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export {
createInitializeInstruction,
createUpdateFieldInstruction,
createRemoveKeyInstruction,
createUpdateAuthorityInstruction,
createEmitInstruction,
} from '@solana/spl-token-metadata';

export * from './associatedTokenAccount.js';
export * from './decode.js';
export * from './types.js';
Expand Down

0 comments on commit e9390d8

Please sign in to comment.