Estimate Clipper Prices
In order to assist in integrating Clipper with DEX Aggregators & Resolvers, offchain state information that is used to calculate Clipper prices is made available in a read-only endpoint that can be queried frequently. This state information can then be used to accurately estimate Clipper price quotes when combined with onchain state information (pulled and maintained by the aggregators themselves).
You can view code to see how to implement it, in our section Complete Swap Flow
1. Get pool data
Make a GET call to https://api.clipper.exchange/rfq/pool?chain_id={CHAIN_ID}&fieldset=offchain-data
This endpoint returns a JSON blob that holds Clipper's offchain state information. Note that this response is modified from the standard response to the endpoint by removing any calls to the onchain contracts for response speed.
The API response will look like the following:
💡 To obtain more details about the endpoint, its parameters, and the response, please refer to the API Reference in the pool section.
2. Estimate Clipper Prices using On- and Off- chain Data
Convert (using
decimals
) the onchain balances of the Clipper Pool - the quantity vectorq
- into human-readable terms (i.e., a balance of 100 ETH instead of 1e20 ETH.Calculate the fee-adjustment multiplier for the swapping pair
M = (10000-fee_in_basis_points)/10000
(10,000 = number of basis points in 100%).Solve for a swap from asset X to asset Y through root-finding or a closed form solution. Let
pX
,qX
,wX
, andinX
represent the current price, quantity, listing weight, and swap input of asset X, and similarlypY
,qY
,wY
, andoutY
represent the current price, quantity, listing weight, and swap output of asset Y. Exactly one ofinX
oroutY
should be unknown. That unknown quantity is then found by solving an indifference pricing equation relating current utility from the assets X and Y to the utility from those assets after the swap:
Checks should be done to ensure an intermediate solution to this equation before attempting to solve:
If
outY
is specified:outY
should be no larger thanqY
.The LHS of the formula (current utility) should be larger than the RHS (utility after swap) evaluated at
inX=0
The LHS of the formula should be smaller than the RHS when evaluated at very large
inX
If
inX
is specified:The LHS of the formula (current utility) should not be smaller than the RHS (utility after swap) evaluated at
outY=qY
The LHS of the formula should be smaller than the RHS when evaluated at
outY=0
Closed Formed Solution
In practice, this is the preferred way to solve the equation because it tends to be faster and more consistent. It is more numerically unstable in the worst case since it requires 1/(1-k) exponentiation, but in practice Clipper’s k tends to be small, making 1/(1-k) ≈ 1
If
inX
is specified, then:
If
outY
is specified, then:
Root Finding
A good initial guess (for a Newton method solver) for inX
or outY
is the FMV of the other value.
If
inX
is specified, then try:
If
outY
is specified, try:
In practice, the actual Clipper price quote will also depend on the amount of time the quote is alive for, as well as the most recent price update. These should result in only small changes from the estimated quotes produced by this process.
Final Check: Output Value No Larger than Input Value
Finally, we enforce that the FMV (according to our price_in_usd
values) of the contributed input is at least the FMV of the output. This constraint will typically only bind when the Clipper pool is very far off target allocations, since in general fees plus slippage will be sufficient to ensure the condition.
If inX
was specified and outY
has been calculated to solve the formula, then as a final check:
If outY
is specified and inX
has been calculated to solve the formula, then as a final check:
Note that these tests are done without regard to fees.
Last updated