UNPKG

solana-web3-lite

Version:

A lightweight wrapper around @solana/web3.js for simplified Solana blockchain interactions

57 lines (56 loc) 3.68 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const spl_token_1 = require("@solana/spl-token"); const web3_js_1 = require("@solana/web3.js"); const utils_1 = require("./utils"); const helpers_1 = require("@solana-developers/helpers"); const createAndTransferToken = (_a) => __awaiter(void 0, [_a], void 0, function* ({ mintWallet, recipientAddress, amount, decimals = 9, cluster = "devnet", commitment = "confirmed", }) { try { console.log(`Processing token transaction on ${cluster}...`); if (!mintWallet) throw new Error("Mint wallet keypair is required"); if (!recipientAddress) throw new Error("Recipient address is required"); if (amount <= 0) throw new Error("Amount must be greater than 0"); if (decimals < 0) throw new Error("Decimals must be non-negative"); const connection = (0, utils_1.getConnection)({ cluster, commitment }); const recipientPubKey = (0, utils_1.toPublicKey)(recipientAddress); const mintAuthority = mintWallet.publicKey; const mintPublicKeyObj = yield (0, spl_token_1.createMint)(connection, mintWallet, mintAuthority, null, decimals, undefined, { commitment }); console.log("mintPubKey", mintPublicKeyObj); const mintPublicKey = mintPublicKeyObj.toBase58(); const sourceTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(connection, mintWallet, mintPublicKeyObj, mintAuthority, false, commitment); console.log("sourceTokenAccount: ", sourceTokenAccount); const mintAmount = amount * Math.pow(10, decimals); const mint = yield (0, spl_token_1.mintTo)(connection, mintWallet, mintPublicKeyObj, sourceTokenAccount.address, mintAuthority, mintAmount, [], { commitment }); console.log("mint: ", mint); const recipientTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(connection, mintWallet, mintPublicKeyObj, recipientPubKey, false, commitment); console.log("recipientTokenAccount: ", recipientTokenAccount); const transaction = new web3_js_1.Transaction().add((0, spl_token_1.createTransferInstruction)(sourceTokenAccount.address, recipientTokenAccount.address, mintAuthority, mintAmount, [], spl_token_1.TOKEN_PROGRAM_ID)); const signature = yield (0, web3_js_1.sendAndConfirmTransaction)(connection, transaction, [mintWallet], { commitment }); console.log("signature: ", recipientTokenAccount); console.log(`Created token ${mintPublicKey} and transferred ${amount} tokens to ${recipientAddress}`); const explorerLink = (0, helpers_1.getExplorerLink)("tx", signature, cluster); console.log(`✅ Transaction details: ${explorerLink}`); return { signature, mintPublicKey, transaction, }; } catch (error) { console.error("Token transaction failed:", error); throw error; } });