UNPKG

@lighthouse-web3/sdk

Version:

NPM package and CLI tool to interact with lighthouse protocol

55 lines (54 loc) 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ethers_1 = require("ethers"); const lighthouse_config_1 = require("../../../lighthouse.config"); const getTickerPrice = async (symbol) => { try { const response = await fetch(`${lighthouse_config_1.lighthouseConfig.lighthouseAPI}/api/lighthouse/get_ticker?symbol=${symbol}`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = (await response.json()); return data; } catch (error) { throw new Error(`Failed to get ticker price: ${error.message}`); } }; const calculatePrice = async (size, network, token) => { const minFileSizeInBytes = 1048576; // 1MB const chargeableSizeInMB = Math.max(size, minFileSizeInBytes) / 1048576; const dollarPricePerMB = 0.00390625; const cost = dollarPricePerMB * chargeableSizeInMB; if (token === 'usdc' || token === 'usdt' || token === 'dai') { const decimals = lighthouse_config_1.lighthouseConfig[network][`${token}_contract_decimal`]; const priceTODecimals = cost.toFixed(decimals); const priceInSmallestUnits = ethers_1.ethers.parseUnits(priceTODecimals.toString(), decimals); return priceInSmallestUnits; } const tokenPriceInUSD = await getTickerPrice(lighthouse_config_1.lighthouseConfig[network]['symbol']); const priceInToken = cost / tokenPriceInUSD; const priceToDecimals = priceInToken.toFixed(lighthouse_config_1.lighthouseConfig[network].native_decimal); const priceInSmallestUnits = ethers_1.ethers.parseUnits(priceToDecimals.toString(), lighthouse_config_1.lighthouseConfig[network].native_decimal); return priceInSmallestUnits; }; exports.default = async (pathOrSize, network, token) => { try { if (!network) { throw new Error('Token not provided!!!'); } if (typeof pathOrSize === 'string') { const { lstatSync } = eval(`require`)('fs-extra'); const { size } = lstatSync(pathOrSize); const price = calculatePrice(size, network.toLowerCase(), token?.toLowerCase()); return price; } else { const price = calculatePrice(pathOrSize, network.toLowerCase(), token?.toLowerCase()); return price; } } catch (error) { throw new Error(error.message); } };