UNPKG

solana-web3-lite

Version:

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

52 lines (51 loc) 2.67 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 }); exports.airdrop = void 0; const utils_1 = require("./utils"); const helpers_1 = require("@solana-developers/helpers"); const airdrop = (_a) => __awaiter(void 0, [_a], void 0, function* ({ address, amount, cluster = "devnet", commitment = "confirmed", }) { try { console.log(`Initiating airdrop on ${cluster}...`); if (!address) throw new Error("Address string is required."); if (amount <= 0) throw new Error("Airdrop amount must be greater than 0."); if (cluster === "mainnet-beta") { throw new Error("Airdrops are not available on Mainnet Beta unless using the localnet or a custom RPC URL."); } if (cluster === "devnet" && amount > 100) { throw new Error("Airdrop amount exceeds typical devnet limit of 100 SOL"); } const publicKey = (0, utils_1.toPublicKey)(address); const connection = (0, utils_1.getConnection)({ cluster, commitment }); const signature = yield connection.requestAirdrop(publicKey, (0, utils_1.solToLamports)(amount)); const { blockhash, lastValidBlockHeight } = yield connection.getLatestBlockhash(); const timeoutMs = 30000; const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error("Airdrop confirmation timeout")), timeoutMs)); yield Promise.race([ connection.confirmTransaction({ signature, blockhash, lastValidBlockHeight, }, "finalized"), timeoutPromise, ]); const explorerLink = (0, helpers_1.getExplorerLink)("transaction", signature, cluster); console.log(`✅ Airdrop confirmed, visit the explorer: ${explorerLink}`); return signature; } catch (error) { console.error("Airdrop failed:", error); throw error; } }); exports.airdrop = airdrop;