From 868c61f454b59505419e40bcc48d233820b746d6 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Tue, 11 Feb 2025 11:12:27 -0800 Subject: [PATCH] prettier --- .../assets/calculate-relative-price.ts | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/.snippets/code/builders/interoperability/xcm/xc-registration/assets/calculate-relative-price.ts b/.snippets/code/builders/interoperability/xcm/xc-registration/assets/calculate-relative-price.ts index b618f3a35..db860659b 100644 --- a/.snippets/code/builders/interoperability/xcm/xc-registration/assets/calculate-relative-price.ts +++ b/.snippets/code/builders/interoperability/xcm/xc-registration/assets/calculate-relative-price.ts @@ -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 { try { // Fetch the native token price from CoinGecko @@ -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 @@ -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 "); - 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 ' + ); + 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() { @@ -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); } @@ -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` @@ -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(); \ No newline at end of file +main();