-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added init, update and emit token metadata actions
- Loading branch information
1 parent
c8b42ed
commit 5d22bf8
Showing
9 changed files
with
369 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,133 @@ | ||
import type { ConfirmOptions, Connection, PublicKey, Signer, TransactionSignature } from '@solana/web3.js'; | ||
import type { Field } from '@solana/spl-token-metadata'; | ||
import { | ||
createInitializeInstruction, | ||
createEmitInstruction, | ||
createUpdateFieldInstruction, | ||
} from '@solana/spl-token-metadata'; | ||
import { sendAndConfirmTransaction, Transaction } from '@solana/web3.js'; | ||
|
||
import { TOKEN_2022_PROGRAM_ID } from '../../constants.js'; | ||
import { getSigners } from '../../actions/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 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 tokenMetadataInitialize( | ||
connection: Connection, | ||
payer: Signer, | ||
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: mint, | ||
updateAuthority, | ||
mint, | ||
mintAuthority: mintAuthorityPublicKey, | ||
name, | ||
symbol, | ||
uri, | ||
}) | ||
); | ||
|
||
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); | ||
} | ||
|
||
/** | ||
* Updates a field in a token-metadata account. | ||
* If the field does not exist on the account, it will be created. | ||
* If the field does exist, it will be overwritten. | ||
* | ||
* The field can be one of the required fields (name, symbol, URI), or a | ||
* totally new field denoted by a "key" string. | ||
* @param connection Connection to use | ||
* @param payer Payer of the transaction fees | ||
* @param updateAuthority Update Authority | ||
* @param mint Mint Account | ||
* @param field Longer name of token | ||
* @param value Shortened symbol of token | ||
* @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 tokenMetadataUpdateField( | ||
connection: Connection, | ||
payer: Signer, | ||
updateAuthority: PublicKey | Signer, | ||
mint: PublicKey, | ||
field: string | Field, | ||
value: string, | ||
multiSigners: Signer[] = [], | ||
confirmOptions?: ConfirmOptions, | ||
programId = TOKEN_2022_PROGRAM_ID | ||
): Promise<TransactionSignature> { | ||
const [updateAuthorityPublicKey, signers] = getSigners(updateAuthority, multiSigners); | ||
|
||
const transaction = new Transaction().add( | ||
createUpdateFieldInstruction({ | ||
programId, | ||
metadata: mint, | ||
updateAuthority: updateAuthorityPublicKey, | ||
field, | ||
value, | ||
}) | ||
); | ||
|
||
return await sendAndConfirmTransaction(connection, transaction, [payer, ...signers], confirmOptions); | ||
} | ||
|
||
/** | ||
* Emits the token-metadata as return data | ||
* | ||
* @param connection Connection to use | ||
* @param payer Payer of the transaction fees | ||
* @param mint Mint Account | ||
* @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 tokenMetadataEmit( | ||
connection: Connection, | ||
payer: Signer, | ||
mint: PublicKey, | ||
confirmOptions?: ConfirmOptions, | ||
programId = TOKEN_2022_PROGRAM_ID | ||
): Promise<TransactionSignature> { | ||
const transaction = new Transaction().add( | ||
createEmitInstruction({ | ||
programId, | ||
metadata: mint, | ||
}) | ||
); | ||
|
||
return await sendAndConfirmTransaction(connection, transaction, [payer], 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,2 @@ | ||
export * from './actions.js'; | ||
export * from './state.js'; |
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,62 @@ | ||
import type { Commitment, Connection, Finality, PublicKey } from '@solana/web3.js'; | ||
import type { TokenMetadata } from '@solana/spl-token-metadata'; | ||
import { unpack } from '@solana/spl-token-metadata'; | ||
|
||
import { TOKEN_2022_PROGRAM_ID } from '../../constants.js'; | ||
import { ExtensionType, getExtensionData } from '../extensionType.js'; | ||
import { getMint } from '../../state/mint.js'; | ||
|
||
/** | ||
* Retrieve Token Metadata Information | ||
* | ||
* @param connection Connection to use | ||
* @param address Mint account | ||
* @param commitment Desired level of commitment for querying the state | ||
* @param programId SPL Token program account | ||
* | ||
* @return Token Metadata information | ||
*/ | ||
export async function getTokenMetadata( | ||
connection: Connection, | ||
address: PublicKey, | ||
commitment?: Commitment, | ||
programId = TOKEN_2022_PROGRAM_ID | ||
): Promise<TokenMetadata | null> { | ||
const mintInfo = await getMint(connection, address, commitment, programId); | ||
const data = getExtensionData(ExtensionType.TokenMetadata, mintInfo.tlvData); | ||
|
||
if (data === null) { | ||
return null; | ||
} | ||
|
||
return unpack(data); | ||
} | ||
|
||
/** | ||
* Retrieve Token Metadata Information emitted in transaction | ||
* | ||
* @param connection Connection to use | ||
* @param signature Transaction signature | ||
* @param commitment Desired level of commitment for querying the state | ||
* | ||
* @return Token Metadata information | ||
*/ | ||
export async function getEmittedTokenMetadata( | ||
connection: Connection, | ||
signature: string, | ||
commitment?: Finality, | ||
programId = TOKEN_2022_PROGRAM_ID | ||
): Promise<TokenMetadata | null> { | ||
const tx: any = await connection.getTransaction(signature, { | ||
commitment: commitment, | ||
maxSupportedTransactionVersion: 2, | ||
}); | ||
|
||
const data = Buffer.from(tx?.meta?.returnData?.data?.[0], 'base64'); | ||
|
||
if (data === null) { | ||
return null; | ||
} | ||
|
||
return unpack(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
Oops, something went wrong.