# Trade Estimate

## Getting a trade estimate

Since trades are routed through Uniswap V3 there is no easy view function that uniswap has to estimate execution prices. To overcome this you can use EthersJS `callStatic()`

method that simulates the call in a node and returns the details. Below is an example of using `callStatic()`

for a long 5 ETH trade with 100 ether of USDC collateral and 0 bounds:

Below is an example return value:

For more details on `staticCall()`

Link

## Trade Price Query

To calculate the price the trade would receive you will need to understand what asset and stable mean for a trade.

For example, if no trade was open and changePosition returned the below for a **Long**:

totalAsset: 2 ether (2000000000000000000)

totalStable: -150 mWei (-150000000)

This would mean that you have a debt of 150 stable (if USDC this would be 150 USDC) and a positive balance of 2 ether of the asset. Let's pretend that the asset is at $100 right now. That means that this position would have $200 of positive value (2 * 100) and a negative value(debt) of - $150. This means that you must have sent in +50 USDC because otherwise your debt would be -200 rather than -150 (because your positive value is +200).

**To get the execution price** that trade received you would subtract the collateral from the negative stable value of -150 to get -200. Now it is just -200 / 2 asset = 100 USDC per asset.

The **short** version of the example of above is:

If no trade was open and changePosition returned:

totalAsset: -2 ether (-2000000000000000000)

totalStable: 250 mWei (250000000)

This would mean that you have a debt of 2 asset(you sold asset to go short) and a positive balance of 250 stable. Let's pretend that the asset is at $100 right now. That means that this position would have -$200 of negative(debt) value (-2 * 100) and a positive value of $250. This means that you must have sent in +50 USDC because otherwise your stable would be only 200 rather than 250.

**To get the execution price** that trade received, you would subtract the collateral from the stable value of 250 to get 200. Now it is just 200 / 2 asset = 100 USDC per asset.

Last updated