Convert USD amount to token amount using Chainlink price feed
Viem public client
Network identifier
Amount in USD (e.g., 1.00 for $1)
Token symbol (ETH, LINK, etc.)
Token decimals (18 for ETH, 18 for LINK)
Amount in token's smallest unit (wei for ETH)
// Convert $10 USD to ETH weiconst ethWei = await usdToToken(client, 'eip155:8453', 10.0, 'ETH', 18);console.log(`$10 = ${formatEther(ethWei)} ETH`); Copy
// Convert $10 USD to ETH weiconst ethWei = await usdToToken(client, 'eip155:8453', 10.0, 'ETH', 18);console.log(`$10 = ${formatEther(ethWei)} ETH`);
Convert USD amount to token amount using Chainlink price feed