UNPKG

hypesdk

Version:

A powerful SDK for interacting with the Hype blockchain, featuring advanced routing and token swap capabilities

71 lines (70 loc) 2.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.normalizeAmount = normalizeAmount; exports.calculateGasSettings = calculateGasSettings; exports.logGasSettings = logGasSettings; const ethers_1 = require("ethers"); /** * Normalizes an amount input to a proper decimal string format * @param amount Amount as string or number * @returns Normalized amount string (e.g. "0.001" or ".001") */ function normalizeAmount(amount) { // Convert to string if it's a number const amountStr = amount.toString(); // If it's already in the correct format (starts with decimal), return as is if (amountStr.startsWith(".")) { return amountStr; } // Convert scientific notation if present if (amountStr.includes("e")) { return Number(amountStr).toString(); } // Handle regular decimal numbers const num = parseFloat(amountStr); if (num === 0) { return "0"; } // Format small numbers without leading zero if possible if (num < 1 && num > 0) { const withoutLeadingZero = amountStr.replace(/^0+/, ""); return withoutLeadingZero.startsWith(".") ? withoutLeadingZero : "." + withoutLeadingZero; } return amountStr; } /** * Calculates gas settings based on network conditions and priority multiplier * @param feeData Current network fee data * @param priorityFeeMultiplier Multiplier for priority fee * @param gasLimit Gas limit for the transaction * @returns Gas settings object */ async function calculateGasSettings(feeData, priorityFeeMultiplier = 1, gasLimit = 1000000) { // Calculate adjusted priority fee const basePriorityFee = feeData.maxPriorityFeePerGas || ethers_1.ethers.parseUnits("0.1", "gwei"); const adjustedPriorityFee = (basePriorityFee * BigInt(Math.floor(priorityFeeMultiplier * 100))) / BigInt(100); // Set gas parameters with adjusted priority fee return { gasLimit, maxFeePerGas: feeData.maxFeePerGas || ethers_1.ethers.parseUnits("1", "gwei"), maxPriorityFeePerGas: adjustedPriorityFee, }; } /** * Logs gas settings to console * @param gasSettings Gas settings object * @param priorityFeeMultiplier Priority fee multiplier used */ function logGasSettings(gasSettings, priorityFeeMultiplier) { const basePriorityFee = (gasSettings.maxPriorityFeePerGas / BigInt(Math.floor(priorityFeeMultiplier * 100))) * BigInt(100); console.log("\nGas Settings:"); console.log("-------------"); console.log("Max Fee Per Gas:", ethers_1.ethers.formatUnits(gasSettings.maxFeePerGas, "gwei"), "gwei"); console.log("Base Priority Fee:", ethers_1.ethers.formatUnits(basePriorityFee, "gwei"), "gwei"); console.log("Adjusted Priority Fee:", ethers_1.ethers.formatUnits(gasSettings.maxPriorityFeePerGas, "gwei"), `gwei (${priorityFeeMultiplier}x multiplier)`); }