|
| 1 | +import { |
| 2 | + address, |
| 3 | + appendTransactionMessageInstructions, |
| 4 | + assertIsSendableTransaction, |
| 5 | + createKeyPairSignerFromBytes, |
| 6 | + createSolanaRpc, |
| 7 | + createSolanaRpcSubscriptions, |
| 8 | + createTransactionMessage, |
| 9 | + getSignatureFromTransaction, |
| 10 | + pipe, |
| 11 | + sendAndConfirmTransactionFactory, |
| 12 | + setTransactionMessageFeePayerSigner, |
| 13 | + setTransactionMessageLifetimeUsingBlockhash, |
| 14 | + signTransactionMessageWithSigners, |
| 15 | +} from '@solana/kit'; |
| 16 | +import { TOKEN_2022_PROGRAM_ADDRESS } from '@solana-program/token-2022'; |
| 17 | +import { getAddressEncoder, getProgramDerivedAddress, getUtf8Encoder } from '@solana/kit'; |
| 18 | +import { |
| 19 | + findWrappedMintAuthorityPda, |
| 20 | + findWrappedMintPda, |
| 21 | + getSyncMetadataToToken2022Instruction, |
| 22 | +} from '../index'; |
| 23 | + |
| 24 | +// ================================================================= |
| 25 | +// PREREQUISITES: |
| 26 | +// ================================================================= |
| 27 | +// 1. An unwrapped SPL Token mint with Metaplex metadata must exist. |
| 28 | +// 2. The corresponding wrapped Token-2022 mint for it must have been created |
| 29 | +// via the `create-mint` command or `createMint` helper. |
| 30 | +// |
| 31 | +// This example ASSUMES these accounts already exist and focuses only on the |
| 32 | +// transaction to sync the metadata. |
| 33 | + |
| 34 | +// Replace these consts with the addresses from your setup |
| 35 | +const PRIVATE_KEY_PAIR = new Uint8Array([ |
| 36 | + 242, 30, 38, 177, 152, 71, 235, 193, 93, 30, 119, 131, 42, 186, 202, 7, 45, 250, 126, 135, 107, |
| 37 | + 137, 38, 91, 202, 212, 12, 8, 154, 213, 163, 200, 23, 237, 17, 163, 3, 135, 34, 126, 235, 146, |
| 38 | + 251, 18, 199, 101, 153, 249, 134, 88, 219, 68, 167, 136, 234, 195, 12, 34, 184, 85, 234, 25, 125, |
| 39 | + 94, |
| 40 | +]); |
| 41 | + |
| 42 | +// Source Mint: An existing SPL Token mint with Metaplex metadata |
| 43 | +const UNWRAPPED_SPL_TOKEN_MINT = address('8owJWKMiKfMKYbPmobyZAwXibNFcY7Roj6quktaeqxGL'); |
| 44 | + |
| 45 | +async function main() { |
| 46 | + const rpc = createSolanaRpc('http://127.0.0.1:8899'); |
| 47 | + const rpcSubscriptions = createSolanaRpcSubscriptions('ws://127.0.0.1:8900'); |
| 48 | + const sendAndConfirm = sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions }); |
| 49 | + |
| 50 | + const payer = await createKeyPairSignerFromBytes(PRIVATE_KEY_PAIR); |
| 51 | + const { value: blockhash } = await rpc.getLatestBlockhash().send(); |
| 52 | + |
| 53 | + console.log('======== Syncing: SPL Token -> Token-2022 ========'); |
| 54 | + |
| 55 | + // To sync from an SPL Token mint, the client must resolve and provide the |
| 56 | + // Metaplex Metadata PDA as the `sourceMetadata` account. |
| 57 | + // Derive it using the known Metadata program and seeds: ['metadata', programId, mint] |
| 58 | + const TOKEN_METADATA_PROGRAM_ADDRESS = address('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s'); |
| 59 | + const [metaplexMetadataPda] = await getProgramDerivedAddress({ |
| 60 | + programAddress: TOKEN_METADATA_PROGRAM_ADDRESS, |
| 61 | + seeds: [ |
| 62 | + getUtf8Encoder().encode('metadata'), |
| 63 | + getAddressEncoder().encode(TOKEN_METADATA_PROGRAM_ADDRESS), |
| 64 | + getAddressEncoder().encode(UNWRAPPED_SPL_TOKEN_MINT), |
| 65 | + ], |
| 66 | + }); |
| 67 | + const [wrappedMint] = await findWrappedMintPda({ |
| 68 | + unwrappedMint: UNWRAPPED_SPL_TOKEN_MINT, |
| 69 | + wrappedTokenProgram: TOKEN_2022_PROGRAM_ADDRESS, |
| 70 | + }); |
| 71 | + const [wrappedMintAuthority] = await findWrappedMintAuthorityPda({ |
| 72 | + wrappedMint, |
| 73 | + }); |
| 74 | + |
| 75 | + const syncToT22Ix = getSyncMetadataToToken2022Instruction({ |
| 76 | + wrappedMint, |
| 77 | + wrappedMintAuthority, |
| 78 | + unwrappedMint: UNWRAPPED_SPL_TOKEN_MINT, |
| 79 | + // When the source mint is a standard SPL Token, `sourceMetadata` MUST be |
| 80 | + // the address of its Metaplex Metadata PDA. |
| 81 | + sourceMetadata: metaplexMetadataPda, |
| 82 | + }); |
| 83 | + |
| 84 | + const transaction = await pipe( |
| 85 | + createTransactionMessage({ version: 0 }), |
| 86 | + tx => setTransactionMessageFeePayerSigner(payer, tx), |
| 87 | + tx => setTransactionMessageLifetimeUsingBlockhash(blockhash, tx), |
| 88 | + tx => appendTransactionMessageInstructions([syncToT22Ix], tx), |
| 89 | + tx => signTransactionMessageWithSigners(tx), |
| 90 | + ); |
| 91 | + assertIsSendableTransaction(transaction); |
| 92 | + const signature = getSignatureFromTransaction(transaction); |
| 93 | + await sendAndConfirm(transaction, { commitment: 'confirmed' }); |
| 94 | + |
| 95 | + console.log('Successfully synced metadata to Token-2022 mint.'); |
| 96 | + console.log('Signature:', signature); |
| 97 | +} |
| 98 | + |
| 99 | +void main(); |
0 commit comments