|
| 1 | +--- |
| 2 | +title: GetTokenPrices |
| 3 | +openapi: ../trails-api.gen.json post /rpc/Trails/GetTokenPrices |
| 4 | +--- |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The `GetTokenPrices` endpoint retrieves current USD prices for one or more tokens. This is useful for displaying real-time token values, calculating transaction costs, and showing users estimated amounts in fiat currency. |
| 9 | + |
| 10 | +## Use Cases |
| 11 | + |
| 12 | +- Display token values in USD in your UI |
| 13 | +- Calculate total transaction costs in fiat |
| 14 | +- Show users how much they'll receive in USD |
| 15 | +- Build portfolio value displays |
| 16 | +- Create price alerts or notifications |
| 17 | +- Compare token values across chains |
| 18 | + |
| 19 | +## Request Parameters |
| 20 | + |
| 21 | +### Required Fields |
| 22 | + |
| 23 | +- **tokens** (Token[]): Array of token objects to get prices for |
| 24 | + |
| 25 | +### Token Object Structure |
| 26 | + |
| 27 | +Each token object should include: |
| 28 | +- **chainId** (number): The chain ID where the token exists |
| 29 | +- **tokenAddress** (string): The token contract address |
| 30 | +- **tokenSymbol** (string, optional): Token symbol for reference |
| 31 | + |
| 32 | +## Response |
| 33 | + |
| 34 | +The response includes: |
| 35 | + |
| 36 | +- **tokenPrices** (TokenPrice[]): Array of token price objects |
| 37 | + |
| 38 | +### TokenPrice Object Structure |
| 39 | + |
| 40 | +Each price object contains: |
| 41 | +- **token** (Token): The token information |
| 42 | + - **chainId** (number): Chain ID |
| 43 | + - **tokenAddress** (string): Token contract address |
| 44 | + - **tokenSymbol** (string): Token symbol |
| 45 | +- **priceUsd** (number): Current price in USD |
| 46 | +- **updatedAt** (string): Timestamp of last price update |
| 47 | + |
| 48 | +## Examples |
| 49 | + |
| 50 | +### Single Token Price |
| 51 | + |
| 52 | +```typescript |
| 53 | +const response = await fetch('https://trails-api.sequence.app/rpc/Trails/GetTokenPrices', { |
| 54 | + method: 'POST', |
| 55 | + headers: { |
| 56 | + 'Content-Type': 'application/json' |
| 57 | + }, |
| 58 | + body: JSON.stringify({ |
| 59 | + tokens: [ |
| 60 | + { |
| 61 | + chainId: 1, |
| 62 | + tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC on Ethereum |
| 63 | + tokenSymbol: 'USDC' |
| 64 | + } |
| 65 | + ] |
| 66 | + }) |
| 67 | +}); |
| 68 | + |
| 69 | +const { tokenPrices } = await response.json(); |
| 70 | + |
| 71 | +console.log('USDC Price:', tokenPrices[0].priceUsd); |
| 72 | +console.log('Updated:', tokenPrices[0].updatedAt); |
| 73 | +``` |
| 74 | + |
| 75 | +### Multiple Token Prices |
| 76 | + |
| 77 | +```typescript |
| 78 | +const response = await fetch('https://trails-api.sequence.app/rpc/Trails/GetTokenPrices', { |
| 79 | + method: 'POST', |
| 80 | + headers: { |
| 81 | + 'Content-Type': 'application/json' |
| 82 | + }, |
| 83 | + body: JSON.stringify({ |
| 84 | + tokens: [ |
| 85 | + { |
| 86 | + chainId: 1, |
| 87 | + tokenAddress: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', // WETH on Ethereum |
| 88 | + tokenSymbol: 'WETH' |
| 89 | + }, |
| 90 | + { |
| 91 | + chainId: 1, |
| 92 | + tokenAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC on Ethereum |
| 93 | + tokenSymbol: 'USDC' |
| 94 | + }, |
| 95 | + { |
| 96 | + chainId: 137, |
| 97 | + tokenAddress: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // USDC on Polygon |
| 98 | + tokenSymbol: 'USDC' |
| 99 | + } |
| 100 | + ] |
| 101 | + }) |
| 102 | +}); |
| 103 | + |
| 104 | +const { tokenPrices } = await response.json(); |
| 105 | + |
| 106 | +tokenPrices.forEach(price => { |
| 107 | + console.log(`${price.token.tokenSymbol} on Chain ${price.token.chainId}:`, |
| 108 | + `$${price.priceUsd}`); |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +## Calculate Transaction Cost |
| 113 | + |
| 114 | +```typescript |
| 115 | +async function calculateTransactionCost(intent: Intent) { |
| 116 | + // Get price for the fee token |
| 117 | + const response = await fetch('https://trails-api.sequence.app/rpc/Trails/GetTokenPrices', { |
| 118 | + method: 'POST', |
| 119 | + headers: { |
| 120 | + 'Content-Type': 'application/json' |
| 121 | + }, |
| 122 | + body: JSON.stringify({ |
| 123 | + tokens: [ |
| 124 | + { |
| 125 | + chainId: intent.quoteRequest.originChainId, |
| 126 | + tokenAddress: intent.fees.feeTokenAddress |
| 127 | + } |
| 128 | + ] |
| 129 | + }) |
| 130 | + }); |
| 131 | + |
| 132 | + const { tokenPrices } = await response.json(); |
| 133 | + |
| 134 | + if (tokenPrices.length > 0) { |
| 135 | + const feeInUsd = intent.fees.totalFeeAmount * tokenPrices[0].priceUsd; |
| 136 | + console.log(`Total Fee: ${intent.fees.totalFeeAmount} tokens`); |
| 137 | + console.log(`Total Fee (USD): $${feeInUsd.toFixed(2)}`); |
| 138 | + return feeInUsd; |
| 139 | + } |
| 140 | + |
| 141 | + return null; |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +## Common Token Addresses |
| 146 | + |
| 147 | +### Ethereum (chainId: 1) |
| 148 | +- **WETH**: `0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2` |
| 149 | +- **USDC**: `0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48` |
| 150 | +- **USDT**: `0xdAC17F958D2ee523a2206206994597C13D831ec7` |
| 151 | +- **DAI**: `0x6B175474E89094C44Da98b954EedeAC495271d0F` |
| 152 | + |
| 153 | +### Polygon (chainId: 137) |
| 154 | +- **WMATIC**: `0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270` |
| 155 | +- **USDC**: `0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174` |
| 156 | +- **USDT**: `0xc2132D05D31c914a87C6611C10748AEb04B58e8F` |
| 157 | + |
| 158 | +### Base (chainId: 8453) |
| 159 | +- **WETH**: `0x4200000000000000000000000000000000000006` |
| 160 | +- **USDC**: `0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913` |
| 161 | + |
| 162 | +### Arbitrum (chainId: 42161) |
| 163 | +- **WETH**: `0x82aF49447D8a07e3bd95BD0d56f35241523fBab1` |
| 164 | +- **USDC**: `0xaf88d065e77c8cC2239327C5EDb3A432268e5831` |
| 165 | + |
| 166 | + |
| 167 | +## Notes |
| 168 | + |
| 169 | +- Not all tokens have price data available |
| 170 | +- The `updatedAt` timestamp indicates when the price was last refreshed |
| 171 | +- Prices are returned in USD |
| 172 | + |
| 173 | +## Next Steps |
| 174 | + |
| 175 | +<CardGroup cols={2}> |
| 176 | + <Card title="QuoteIntent" icon="dollar-sign" href="/api-reference/endpoints/quote-intent"> |
| 177 | + Use token prices to display quote values in USD |
| 178 | + </Card> |
| 179 | + |
| 180 | + <Card title="GetIntent" icon="file-invoice-dollar" href="/api-reference/endpoints/get-intent"> |
| 181 | + Calculate total transaction costs with token prices |
| 182 | + </Card> |
| 183 | +</CardGroup> |
| 184 | + |
0 commit comments