-
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.
Extensions doc metadata js example (#6396)
* doc(metadata-extension): add js example to create mint with metadata * doc(metadata-extensions): add js example for updating a field * doc(metadata-extensions): add js example for adding a custom field * doc(metadata-extensions): add js example for removing a field * doc(metadata-extension): add the js example to `token/js/examples`` * style: running prettier
- Loading branch information
1 parent
ebb1b91
commit eaeb220
Showing
2 changed files
with
229 additions
and
4 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,109 @@ | ||
import { | ||
clusterApiUrl, | ||
Connection, | ||
Keypair, | ||
LAMPORTS_PER_SOL, | ||
sendAndConfirmTransaction, | ||
SystemProgram, | ||
Transaction, | ||
} from '@solana/web3.js'; | ||
import { | ||
createInitializeMetadataPointerInstruction, | ||
createInitializeMintInstruction, | ||
ExtensionType, | ||
getMintLen, | ||
LENGTH_SIZE, | ||
TOKEN_2022_PROGRAM_ID, | ||
TYPE_SIZE, | ||
} from '@solana/spl-token'; | ||
import type { TokenMetadata } from '@solana/spl-token-metadata'; | ||
import { | ||
createInitializeInstruction, | ||
pack, | ||
createUpdateFieldInstruction, | ||
createRemoveKeyInstruction, | ||
} from '@solana/spl-token-metadata'; | ||
|
||
(async () => { | ||
const payer = Keypair.generate(); | ||
|
||
const mint = Keypair.generate(); | ||
const decimals = 9; | ||
|
||
const metadata: TokenMetadata = { | ||
mint: mint.publicKey, | ||
name: 'TOKEN_NAME', | ||
symbol: 'SMBL', | ||
uri: 'URI', | ||
additionalMetadata: [['new-field', 'new-value']], | ||
}; | ||
|
||
const mintLen = getMintLen([ExtensionType.MetadataPointer]); | ||
|
||
const metadataLen = TYPE_SIZE + LENGTH_SIZE + pack(metadata).length; | ||
|
||
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 + metadataLen); | ||
const mintTransaction = new Transaction().add( | ||
SystemProgram.createAccount({ | ||
fromPubkey: payer.publicKey, | ||
newAccountPubkey: mint.publicKey, | ||
space: mintLen, | ||
lamports: mintLamports, | ||
programId: TOKEN_2022_PROGRAM_ID, | ||
}), | ||
createInitializeMetadataPointerInstruction( | ||
mint.publicKey, | ||
payer.publicKey, | ||
mint.publicKey, | ||
TOKEN_2022_PROGRAM_ID | ||
), | ||
createInitializeMintInstruction(mint.publicKey, decimals, payer.publicKey, null, TOKEN_2022_PROGRAM_ID), | ||
createInitializeInstruction({ | ||
programId: TOKEN_2022_PROGRAM_ID, | ||
mint: mint.publicKey, | ||
metadata: mint.publicKey, | ||
name: metadata.name, | ||
symbol: metadata.symbol, | ||
uri: metadata.uri, | ||
mintAuthority: payer.publicKey, | ||
updateAuthority: payer.publicKey, | ||
}), | ||
|
||
// add a custom field | ||
createUpdateFieldInstruction({ | ||
metadata: mint.publicKey, | ||
updateAuthority: payer.publicKey, | ||
programId: TOKEN_2022_PROGRAM_ID, | ||
field: metadata.additionalMetadata[0][0], | ||
value: metadata.additionalMetadata[0][1], | ||
}), | ||
|
||
// update a field | ||
createUpdateFieldInstruction({ | ||
metadata: mint.publicKey, | ||
updateAuthority: payer.publicKey, | ||
programId: TOKEN_2022_PROGRAM_ID, | ||
field: 'name', | ||
value: 'YourToken', | ||
}), | ||
|
||
// remove a field | ||
createRemoveKeyInstruction({ | ||
programId: TOKEN_2022_PROGRAM_ID, | ||
metadata: mint.publicKey, | ||
updateAuthority: payer.publicKey, | ||
key: 'new-field', | ||
idempotent: true, // If false the operation will fail if the field does not exist in the metadata | ||
}) | ||
); | ||
const sig = await sendAndConfirmTransaction(connection, mintTransaction, [payer, mint]); | ||
console.log('Signature:', sig); | ||
})(); |