@lighthouse-web3/sdk
Version:
NPM package and CLI tool to interact with lighthouse protocol
58 lines (57 loc) • 2.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/* istanbul ignore file */
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 === 'number') {
const price = calculatePrice(pathOrSize, network.toLowerCase(), token?.toLowerCase());
return price;
}
else {
let totalSize = 0;
for (let i = 0; i < pathOrSize.length; i++) {
totalSize = totalSize + pathOrSize[i]['size'];
}
const price = calculatePrice(totalSize, network.toLowerCase(), token?.toLowerCase());
return price;
}
}
catch (error) {
throw new Error(error.message);
}
};