|
| 1 | +import { SolanaAgentKit } from "../../index"; |
| 2 | +import { PublicKey as LegacyPublicKey } from "@solana/web3.js"; |
| 3 | +import { createUmi } from "@metaplex-foundation/umi-bundle-defaults"; |
| 4 | +import { |
| 5 | + generateSigner, |
| 6 | + keypairIdentity, |
| 7 | + publicKey, |
| 8 | +} from "@metaplex-foundation/umi"; |
| 9 | +import { |
| 10 | + createFungible, |
| 11 | + mintV1, |
| 12 | + TokenStandard, |
| 13 | +} from "@metaplex-foundation/mpl-token-metadata"; |
| 14 | +import { |
| 15 | + fromWeb3JsKeypair, |
| 16 | + fromWeb3JsPublicKey, |
| 17 | + toWeb3JsPublicKey, |
| 18 | +} from "@metaplex-foundation/umi-web3js-adapters"; |
| 19 | +import { |
| 20 | + mplToolbox, |
| 21 | + findAssociatedTokenPda, |
| 22 | +} from "@metaplex-foundation/mpl-toolbox"; |
| 23 | +import { TOKEN_2022_PROGRAM_ID } from "@solana/spl-token"; |
| 24 | +import type { PublicKey } from "@metaplex-foundation/umi"; |
| 25 | + |
| 26 | +/** |
| 27 | + * Deploy a new SPL token |
| 28 | + * @param agent SolanaAgentKit instance |
| 29 | + * @param name Name of the token |
| 30 | + * @param uri URI for the token metadata |
| 31 | + * @param symbol Symbol of the token |
| 32 | + * @param decimals Number of decimals for the token (default: 9) |
| 33 | + * @param initialSupply Initial supply to mint (optional) |
| 34 | + * @returns Object containing token mint address and initial account (if supply was minted) |
| 35 | + */ |
| 36 | + |
| 37 | +export async function deploy_token2022( |
| 38 | + agent: SolanaAgentKit, |
| 39 | + name: string, |
| 40 | + uri: string, |
| 41 | + symbol: string, |
| 42 | + decimals: number = 9, |
| 43 | + initialSupply?: number, |
| 44 | +): Promise<{ mint: LegacyPublicKey }> { |
| 45 | + try { |
| 46 | + // Create UMI instance from agent |
| 47 | + const umi = createUmi(agent.connection.rpcEndpoint).use(mplToolbox()); |
| 48 | + umi.use(keypairIdentity(fromWeb3JsKeypair(agent.wallet))); |
| 49 | + |
| 50 | + const SPL_TOKEN_2022_PROGRAM_ID: PublicKey = publicKey( |
| 51 | + TOKEN_2022_PROGRAM_ID.toBase58(), |
| 52 | + ); |
| 53 | + // Create new token2022 mint |
| 54 | + const mint = generateSigner(umi); |
| 55 | + |
| 56 | + let builder = createFungible(umi, { |
| 57 | + name, |
| 58 | + uri, |
| 59 | + symbol, |
| 60 | + splTokenProgram: SPL_TOKEN_2022_PROGRAM_ID, |
| 61 | + sellerFeeBasisPoints: { |
| 62 | + basisPoints: 0n, |
| 63 | + identifier: "%", |
| 64 | + decimals: 2, |
| 65 | + }, |
| 66 | + decimals, |
| 67 | + mint, |
| 68 | + }); |
| 69 | + |
| 70 | + if (initialSupply) { |
| 71 | + const token = findAssociatedTokenPda(umi, { |
| 72 | + mint: mint.publicKey, |
| 73 | + owner: fromWeb3JsPublicKey(agent.wallet_address), |
| 74 | + tokenProgramId: SPL_TOKEN_2022_PROGRAM_ID, |
| 75 | + }); |
| 76 | + |
| 77 | + builder = builder.add( |
| 78 | + mintV1(umi, { |
| 79 | + mint: mint.publicKey, |
| 80 | + token, |
| 81 | + splTokenProgram: SPL_TOKEN_2022_PROGRAM_ID, |
| 82 | + tokenStandard: TokenStandard.Fungible, |
| 83 | + tokenOwner: fromWeb3JsPublicKey(agent.wallet_address), |
| 84 | + amount: initialSupply * Math.pow(10, decimals), |
| 85 | + }), |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + await builder.sendAndConfirm(umi, { |
| 90 | + confirm: { commitment: "processed" }, |
| 91 | + }); |
| 92 | + |
| 93 | + return { |
| 94 | + mint: toWeb3JsPublicKey(mint.publicKey), |
| 95 | + }; |
| 96 | + } catch (error: any) { |
| 97 | + throw new Error(`Token deployment failed: ${error.message}`); |
| 98 | + } |
| 99 | +} |
0 commit comments