Gets a wallet’s earnings in a single Earn vault: how much the position has earned (netEarningsAssets) and its return on capital (returnOnCapital).The values are indexed by Morpho rather than read on-chain, so they trail the chain head by a short delay. getYieldPosition stays the source of truth for what a position is worth right now; use this function for earnings and PNL.returnOnCapital is a time-weighted, non-annualized decimal (for example 0.00413 means a 0.41% return). It comes back null when Morpho has too little history on the position to compute one.
This function currently supports vaults from these providers: Morpho. For a vault from any other provider, such as an Aave vault, the call rejects because the API responds with an HTTP 400 error. Check the vault’s provider from getYieldDetails before calling it, and use the assets value from getYieldPosition to show the position’s balance.
netEarningsAssets comes back as a raw string (e.g. "854395278"), not a human-readable number, so values above 2^53 stay exact. Divide by the vault’s assetDecimals, from getYieldDetails, before displaying it, the same way you’d format a position’s assets. See the example below.
import { getYieldDetails, getYieldPerformance } from '@dynamic-labs-sdk/client';import { formatUnits } from 'viem';// Turns the raw `netEarningsAssets` string into a plain number like "854.39",// ready to show a user.async function loadEarnings(vaultId, walletAccount) { const [details, performance] = await Promise.all([ getYieldDetails({ vaultId }), getYieldPerformance({ vaultId, walletAccount }), ]); return { assetSymbol: details.assetSymbol, earnings: formatUnits(BigInt(performance.netEarningsAssets), details.assetDecimals), returnOnCapital: performance.returnOnCapital, };}