Token amounts and USD
The market underlying is the asset borrowed from the pool. A position reports its total value and debt in that asset so you can subtract them to get equity. Individual collateral balances use each held token’s own units.
| Field | How to read it |
|---|---|
Current or preview TokenAmount, such as totalDebt | Use viem’s formatUnits(amount.value, amount.token.decimals) to display whole tokens. amount.valueUsd is already in dollars. A null value means the dollar valuation is unavailable. |
Current position quota, such as quota.value | Use the market underlying’s decimals and price. The same entry’s collateral.token identifies the collateral the quota applies to. |
| Chart series | With unit: "token", values are already in whole tokens of the series’ base. With unit: "usd", values are already in dollars. |
| Rate in basis points | Divide by 100 to display a percentage. For example, 520 means 5.2%. |
Multiply whole tokens by USD per token to get dollars. Divide dollars by the
same positive price to get whole tokens. For example, 125000000n base units
of a six-decimal token is 125 tokens. At 0.98 USD per token, the balance
is worth 122.50 USD.
Current balances
If you have a raw token balance, use the market’s SDK
price oracle from the same snapshot. priceOracle.convertToUSD(tokenAddress, amountRaw)
returns USD with eight decimals. Display it with formatUnits(usdRaw, 8).
The reverse call, priceOracle.convertFromUSD(underlyingAddress, usdRaw),
returns underlying base units.
Historical values
Use the position’s chainId and creditManager to request the underlying’s
USD price for the same range as the position chart.
const prices = await sdk.opportunities.charts( { kind: "strategy", chainId, creditManager }, ["underlyingUsdPrice"], range, );
Match price and balance observations by Unix timestamp. Multiply whole underlying tokens by the matching price and keep missing observations as gaps. For a deposit in the underlying, that same calculation gives its dollar value if the chart has a price at the deposit timestamp. For a USD gain since opening, value current equity and subtract net deposits valued at their respective times.
See the worked WETH and USD comparison for a position with an opening deposit and a later top-up.
Price a deposit at its block
Use this fallback when the transfer has no dated valuation in the currency
you need. From the position, take
creditManager and the underlying decimals (position.totalDebt.token.decimals).
Set archiveRpcUrl to an archive RPC for the selected chain.
For each asset in the SDK transaction’s assets array, use tx.txHash as
txHash, asset.token.address as tokenAddress, and asset.value as
amountRaw (the amount in token base units).
import { createPublicClient, formatUnits, http, parseAbi } from "viem"; const rpc = createPublicClient({ transport: http(archiveRpcUrl) }); const { blockNumber } = await rpc.getTransactionReceipt({ hash: txHash }); const manager = { address: creditManager, abi: parseAbi([ "function priceOracle() view returns (address)", "function underlying() view returns (address)", ]), blockNumber, }; const oracle = await rpc.readContract({ ...manager, functionName: "priceOracle" }); const underlying = await rpc.readContract({ ...manager, functionName: "underlying" }); // Pin both conversions to the transfer block. const pricing = { address: oracle, abi: parseAbi([ "function convert(uint256,address,address) view returns (uint256)", "function convertToUSD(uint256,address) view returns (uint256)", ]), blockNumber, }; const underlyingRaw = await rpc.readContract({ ...pricing, functionName: "convert", args: [amountRaw, tokenAddress, underlying], }); const usdRaw = await rpc.readContract({ ...pricing, functionName: "convertToUSD", args: [amountRaw, tokenAddress], });
Use underlyingRaw with the account’s raw equity. For dollars, use
formatUnits(usdRaw, 8) before comparing with the SDK’s valueUsd.
Keep full precision until display. If a conversion fails, leave that
valuation unavailable. For a native deposit wrapped on entry, price the
wrapped token credited to the account and count the deposit once.