Skip to content

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
themacexpert committed Feb 11, 2025
1 parent 5cba02d commit 868c61f
Showing 1 changed file with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import axios from "axios";
import axios from 'axios';

// CoinGecko IDs for the networks
const NETWORK_IDS = {
GLMR: "moonbeam",
MOVR: "moonriver",
GLMR: 'moonbeam',
MOVR: 'moonriver',
};

async function calculateRelativePrice(
assetPrice: number,
assetDecimals: number,
network: "GLMR" | "MOVR"
network: 'GLMR' | 'MOVR'
): Promise<bigint> {
try {
// Fetch the native token price from CoinGecko
Expand All @@ -23,7 +23,10 @@ async function calculateRelativePrice(
// Formula: (assetPrice / nativeTokenPrice) * 10^18
// This gives us how many units of the asset we need to equal 1 unit of native token
const relativePrice = BigInt(
0.175 * Math.pow(10, 18 - assetDecimals) * (assetPrice / nativeTokenPrice) * Math.pow(10, 18)
0.175 *
Math.pow(10, 18 - assetDecimals) *
(assetPrice / nativeTokenPrice) *
Math.pow(10, 18)
);

// Return as string to preserve precision
Expand All @@ -40,37 +43,39 @@ function validateInput(
price: string,
decimals: string,
network: string
): { assetPrice: number; assetDecimals: number; network: "GLMR" | "MOVR" } {
): { assetPrice: number; assetDecimals: number; network: 'GLMR' | 'MOVR' } {
// Validate price
const assetPrice = parseFloat(price);
if (isNaN(assetPrice) || assetPrice <= 0) {
throw new Error("Price must be a positive number");
throw new Error('Price must be a positive number');
}

// Validate decimals
const assetDecimals = parseFloat(decimals);
if (isNaN(assetDecimals) || assetDecimals <= 0) {
throw new Error("Decimals must be a positive number");
throw new Error('Decimals must be a positive number');
}

// Validate network
const upperNetwork = network.toUpperCase() as "GLMR" | "MOVR";
if (!["GLMR", "MOVR"].includes(upperNetwork)) {
throw new Error("Network must be either GLMR or MOVR");
const upperNetwork = network.toUpperCase() as 'GLMR' | 'MOVR';
if (!['GLMR', 'MOVR'].includes(upperNetwork)) {
throw new Error('Network must be either GLMR or MOVR');
}

return { assetPrice, assetDecimals, network: upperNetwork };
}

function printUsage() {
console.log("\nUsage:");
console.log("npx ts-node calculate-relative-price.ts <price> <decimals> <network>");
console.log("\nExample:");
console.log("npx ts-node calculate-relative-price.ts 0.25 12 GLMR");
console.log("\nParameters:");
console.log("price - The price of your asset in USD");
console.log("decimals - The decimals of your asset");
console.log("network - Either GLMR or MOVR");
console.log('\nUsage:');
console.log(
'npx ts-node calculate-relative-price.ts <price> <decimals> <network>'
);
console.log('\nExample:');
console.log('npx ts-node calculate-relative-price.ts 0.25 12 GLMR');
console.log('\nParameters:');
console.log('price - The price of your asset in USD');
console.log('decimals - The decimals of your asset');
console.log('network - Either GLMR or MOVR');
}

async function main() {
Expand All @@ -79,14 +84,14 @@ async function main() {
const [, , price, decimals, network] = process.argv;

// Check if help flag is passed
if (price === "--help" || price === "-h") {
if (price === '--help' || price === '-h') {
printUsage();
return;
}

// Check if required arguments are provided
if (!price || !decimals || !network) {
console.error("Error: Missing required arguments");
console.error('Error: Missing required arguments');
printUsage();
process.exit(1);
}
Expand All @@ -101,7 +106,11 @@ async function main() {
console.log(
`\nCalculating relative price for asset worth $${assetPrice} against ${validNetwork}...`
);
const relativePrice = await calculateRelativePrice(assetPrice, assetDecimals, validNetwork);
const relativePrice = await calculateRelativePrice(
assetPrice,
assetDecimals,
validNetwork
);
const nativeTokenPrice = (
await axios.get(
`https://api.coingecko.com/api/v3/simple/price?ids=${NETWORK_IDS[validNetwork]}&vs_currencies=usd`
Expand All @@ -128,9 +137,9 @@ async function main() {
`\nThe relative price you should specify in asset registration steps is ${relativePrice}\n`
);
} catch (error) {
console.error("\nError:", error instanceof Error ? error.message : error);
console.error('\nError:', error instanceof Error ? error.message : error);
process.exit(1);
}
}

main();
main();

0 comments on commit 868c61f

Please sign in to comment.