solana-web3-lite
Version:
A lightweight wrapper around @solana/web3.js for simplified Solana blockchain interactions
34 lines (33 loc) • 1.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.lamportsToSol = exports.solToLamports = exports.toPublicKey = exports.getConnection = void 0;
const web3_js_1 = require("@solana/web3.js");
/**
* Creates a Solana connection based on cluster or localnet RPC URL
* @param params Connection parameters
* @returns Connection object
*/
const getConnection = ({ cluster, commitment, }) => {
const rpcUrl = cluster === "localnet" ? "http://localhost:8899" : (0, web3_js_1.clusterApiUrl)(cluster);
return new web3_js_1.Connection(rpcUrl, commitment);
};
exports.getConnection = getConnection;
/**
* Validates and converts a string address to PublicKey
* @param address String address to validate
* @returns PublicKey object
* @throws Error if address is invalid
*/
const toPublicKey = (address) => {
try {
return new web3_js_1.PublicKey(address);
}
catch (error) {
throw new Error("Invalid public key address provided");
}
};
exports.toPublicKey = toPublicKey;
const solToLamports = (sol) => sol * web3_js_1.LAMPORTS_PER_SOL;
exports.solToLamports = solToLamports;
const lamportsToSol = (lamports) => lamports / web3_js_1.LAMPORTS_PER_SOL;
exports.lamportsToSol = lamportsToSol;