Skip to content
function useRiskFactor(params): UseRiskFactorResult;

Scores how close a Dynamic LOC is to its pair’s collateral factor threshold, for an open LOC or a creation preview. The pair’s current configuration applies to both: there is no per-LOC threshold, so changing a pair’s configuration changes an already-open LOC’s risk.

Fails closed: until every input is present and the pair configuration is fresh, status explains why there is no score instead of showing a healthy-looking one.

Parameters

ParameterTypeDescription
paramsUseRiskFactorParamsThe dynamic LOC’s amounts, token pair, and which configured factor to score against. Every field is optional so the hook can be mounted before a form is complete; incomplete inputs report incomplete and fetch nothing.
params.basis?CollateralFactorBasisWhich configured factor to score against. Defaults to liquidation, the threshold at which an open LOC becomes liquidatable; pass creation to score against the creation threshold a new LOC must meet, for a preview of a LOC that does not exist yet.
params.collateralAmount?bigintCollateral the Creator has reserved (or is about to reserve) for this Dynamic LOC, in the collateral token’s smallest unit. Together with creditedAmount and the live pair price this fixes the position’s current collateral factor; the result stays incomplete until both amounts are positive.
params.collateralToken?`0x${string}`ERC-20 the LOC is collateralized in. With creditedToken it selects the oracle pair price and the pair’s configured collateral factors.
params.creditedAmount?bigintAmount the LOC promises the Beneficiary, in the credited token’s smallest unit. The denominator of the collateral factor: more credit against the same collateral means a higher risk factor.
params.creditedToken?`0x${string}`ERC-20 the LOC credits to the Beneficiary. Must differ from collateralToken: a Static LOC (same token both sides) has no oracle price and no risk factor.

Returns

UseRiskFactorResult

The score and the inputs it was derived from. Read status first: only ready means riskFactorBasisPoints and riskLevel hold values. collateralFactor and pairPrice are exposed so a caller can show the same numbers without re-reading them, and error carries the first failure from either leg.

Throws

Error when rendered outside <AnvilProvider>, by way of useAnvilSDK.

Example

export function RiskBadge({ collateral }: { collateral: bigint }) {
const risk = useRiskFactor({
collateralAmount: collateral,
creditedAmount: 1_000_000_000n,
collateralToken: WETH,
creditedToken: USDC,
});
// Four distinct facts, deliberately not collapsed: `incomplete` waits on
// the form, `loading` on the first read, `unavailable` on a pair
// configuration or price the hook will not guess at.
switch (risk.status) {
case 'incomplete':
return <span>Enter amounts</span>;
case 'loading':
return <span>Calculating…</span>;
case 'unavailable':
return <span>Risk unavailable</span>;
case 'ready':
return <span>{risk.riskLevel}</span>;
}
}