-
Notifications
You must be signed in to change notification settings - Fork 0
usd pricing script #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
PASS [ 43.608s] (3/3) lit_node::test toxiproxy::perf_tests::load_with_no_latency |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR introduces a utility script to calculate and display the real-world USD costs for using Naga services by fetching the LITKEY token price from CoinGecko and querying the on-chain PriceFeed contract.
Key changes:
- Added
calculateUSDPricing.tsscript that fetches LITKEY token prices and displays various pricing tiers - Implements price calculations for different product types (PKP Sign, Encrypted Sign, Lit Action, Sign Session Key)
- Shows pricing at different usage percentages (0%, 25%, 50%, 75%, 100%) and individual Lit Action component costs
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| throw new Error( | ||
| 'Unable to fetch LITKEY price. Please check CoinGecko API or set manually.' |
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message references "LITKEY price" but the function attempts to fetch "LIT price" from CoinGecko using the token ID 'lit-protocol'. This inconsistency could lead to confusion. Either update the error message to reflect that it's fetching LIT price (not LITKEY), or clarify the relationship between LIT and LITKEY tokens in the comments.
| const priceFeed = await ethers.getContractAt( | ||
| 'PriceFeedDiamond', | ||
| priceFeedAddress | ||
| ); |
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing error handling for contract interaction failures. If the contract doesn't exist at the specified address, is not deployed on the current network, or if there's an RPC error, the script will crash with an unhelpful error. Consider adding try-catch blocks around contract calls with meaningful error messages.
| // Usage: HARDHAT_NETWORK=litMainnet npx ts-node --files scripts/calculateUSDPricing.ts | ||
|
|
||
| import hre from 'hardhat'; | ||
|
|
||
| const { ethers } = hre; | ||
|
|
||
| // on Lit Chain Mainnet | ||
| const NAGA_PROD_PRICE_FEED_ADDRESS = | ||
| '0x88F5535Fa6dA5C225a3C06489fE4e3405b87608C'; | ||
|
|
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoded contract address may cause issues on different networks. The script hardcodes the Naga production PriceFeed address for Lit Chain Mainnet, but doesn't validate that the current network matches. If run on a different network, the contract call will fail. Consider adding network validation or making the address configurable via environment variables or command-line arguments.
| // Usage: HARDHAT_NETWORK=litMainnet npx ts-node --files scripts/calculateUSDPricing.ts | |
| import hre from 'hardhat'; | |
| const { ethers } = hre; | |
| // on Lit Chain Mainnet | |
| const NAGA_PROD_PRICE_FEED_ADDRESS = | |
| '0x88F5535Fa6dA5C225a3C06489fE4e3405b87608C'; | |
| // Usage: HARDHAT_NETWORK=litMainnet PRICE_FEED_ADDRESS=0xYourAddressHere npx ts-node --files scripts/calculateUSDPricing.ts | |
| import hre from 'hardhat'; | |
| const { ethers, network } = hre; | |
| // Naga PriceFeed address for Lit Chain Mainnet | |
| const DEFAULT_LIT_MAINNET_PRICE_FEED_ADDRESS = '0x88F5535Fa6dA5C225a3C06489fE4e3405b87608C'; | |
| // Get address from env or fallback to default if on Lit Chain Mainnet | |
| function getPriceFeedAddress(): string { | |
| const envAddress = process.env.PRICE_FEED_ADDRESS; | |
| if (envAddress && envAddress !== '') { | |
| return envAddress; | |
| } | |
| // Validate network | |
| if (network.name === 'litMainnet') { | |
| return DEFAULT_LIT_MAINNET_PRICE_FEED_ADDRESS; | |
| } | |
| throw new Error( | |
| `No PRICE_FEED_ADDRESS provided and not on Lit Chain Mainnet (network: ${network.name}). Please set PRICE_FEED_ADDRESS environment variable.` | |
| ); | |
| } |
| // Note: You may need to adjust the token ID if LITKEY is listed differently | ||
| const response = await fetch( | ||
| 'https://api.coingecko.com/api/v3/simple/price?ids=lit-protocol&vs_currencies=usd' | ||
| ); |
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing HTTP response status check. The fetch call should verify the response is successful before parsing JSON. If the API returns a 4xx or 5xx status, response.json() may fail or return an error object instead of the expected data structure. Add if (!response.ok) throw new Error(...) before parsing the JSON.
| ); | |
| ); | |
| if (!response.ok) { | |
| throw new Error(`Failed to fetch LITKEY price: ${response.status} ${response.statusText}`); | |
| } |
| * Get PriceFeed contract address from networkContext.json or use default | ||
| */ | ||
| function getPriceFeedAddress(): string { | ||
| // Naga prod address |
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The getPriceFeedAddress function always returns the hardcoded mainnet address regardless of the actual network being used. This could be misleading since the function name and comment suggest it might retrieve the address from networkContext.json. Either implement the network-specific logic or rename the function to getMainnetPriceFeedAddress to be more explicit.
| * Get PriceFeed contract address from networkContext.json or use default | |
| */ | |
| function getPriceFeedAddress(): string { | |
| // Naga prod address | |
| * Get the PriceFeed contract address for Lit Chain Mainnet. | |
| */ | |
| function getMainnetPriceFeedAddress(): string { | |
| // Naga prod address (Lit Chain Mainnet) |
| async function getLitKeyPrice(): Promise<number> { | ||
| try { | ||
| // Try to get LIT token price from CoinGecko | ||
| // Note: You may need to adjust the token ID if LITKEY is listed differently | ||
| const response = await fetch( | ||
| 'https://api.coingecko.com/api/v3/simple/price?ids=lit-protocol&vs_currencies=usd' | ||
| ); | ||
| const data = await response.json(); | ||
|
|
||
| if (data['lit-protocol'] && data['lit-protocol'].usd) { | ||
| return data['lit-protocol'].usd; | ||
| } | ||
|
|
||
| throw new Error('LIT price not found in CoinGecko response'); | ||
| } catch (error) { | ||
| console.error('Error fetching LITKEY price from CoinGecko:', error); | ||
| console.log('Falling back to manual price input...'); | ||
| // You can set a default price here or throw | ||
| throw new Error( | ||
| 'Unable to fetch LITKEY price. Please check CoinGecko API or set manually.' |
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The script relies on external API (CoinGecko) without rate limiting or caching. For a production script that may be run frequently, consider implementing rate limiting, caching the token price for a reasonable duration, or providing an option to manually specify the price to avoid API rate limits or availability issues.
| async function getLitKeyPrice(): Promise<number> { | |
| try { | |
| // Try to get LIT token price from CoinGecko | |
| // Note: You may need to adjust the token ID if LITKEY is listed differently | |
| const response = await fetch( | |
| 'https://api.coingecko.com/api/v3/simple/price?ids=lit-protocol&vs_currencies=usd' | |
| ); | |
| const data = await response.json(); | |
| if (data['lit-protocol'] && data['lit-protocol'].usd) { | |
| return data['lit-protocol'].usd; | |
| } | |
| throw new Error('LIT price not found in CoinGecko response'); | |
| } catch (error) { | |
| console.error('Error fetching LITKEY price from CoinGecko:', error); | |
| console.log('Falling back to manual price input...'); | |
| // You can set a default price here or throw | |
| throw new Error( | |
| 'Unable to fetch LITKEY price. Please check CoinGecko API or set manually.' | |
| // In-memory cache for LITKEY price | |
| let cachedLitKeyPrice: number | null = null; | |
| let cachedLitKeyPriceTimestamp: number | null = null; | |
| const CACHE_DURATION_MS = 5 * 60 * 1000; // 5 minutes | |
| /** | |
| * Get LITKEY token price in USD from CoinGecko, with caching and manual override | |
| */ | |
| async function getLitKeyPrice(): Promise<number> { | |
| // 1. Check for manual override via environment variable | |
| const manualPrice = process.env.LITKEY_PRICE_USD; | |
| if (manualPrice !== undefined) { | |
| const priceNum = Number(manualPrice); | |
| if (!isNaN(priceNum) && priceNum > 0) { | |
| return priceNum; | |
| } else { | |
| console.warn('Invalid LITKEY_PRICE_USD env var value, falling back to API.'); | |
| } | |
| } | |
| // 2. Check cache | |
| const now = Date.now(); | |
| if ( | |
| cachedLitKeyPrice !== null && | |
| cachedLitKeyPriceTimestamp !== null && | |
| now - cachedLitKeyPriceTimestamp < CACHE_DURATION_MS | |
| ) { | |
| return cachedLitKeyPrice; | |
| } | |
| // 3. Fetch from CoinGecko | |
| try { | |
| const response = await fetch( | |
| 'https://api.coingecko.com/api/v3/simple/price?ids=lit-protocol&vs_currencies=usd' | |
| ); | |
| const data = await response.json(); | |
| if (data['lit-protocol'] && data['lit-protocol'].usd) { | |
| cachedLitKeyPrice = data['lit-protocol'].usd; | |
| cachedLitKeyPriceTimestamp = now; | |
| return cachedLitKeyPrice; | |
| } | |
| throw new Error('LIT price not found in CoinGecko response'); | |
| } catch (error) { | |
| console.error('Error fetching LITKEY price from CoinGecko:', error); | |
| console.log('Falling back to manual price input...'); | |
| throw new Error( | |
| 'Unable to fetch LITKEY price. Please check CoinGecko API or set manually via LITKEY_PRICE_USD env var.' |
A basic script to see the real world USD costs for using Naga