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:

const assetTokenDecimals = 18n;
const stableTokenDecimals = 6n;
const assetTokenDivisor = 10n ** (18n - assetTokenDecimals);
const stableTokenDivisor = 10n ** (18n - stableTokenDecimals);

const tradeValues = await exchange.callStatic.changePosition(
      toWei("5") / assetTokenDivisor,
      toWei("100") / stableTokenDivisor,
      "0",
      {}
 );

Below is an example return value:

Example Return Value
[
  BigNumber { _hex: '0x00', _isBigNumber: true },
  BigNumber { _hex: '0x00', _isBigNumber: true },
  BigNumber { _hex: '0x00', _isBigNumber: true },
  BigNumber { _hex: '0x00', _isBigNumber: true },
  BigNumber { _hex: '0x4563918244f40000', _isBigNumber: true },
  BigNumber { _hex: '-0x17d78400', _isBigNumber: true },
  startAsset: BigNumber { _hex: '0x00', _isBigNumber: true },
  startStable: BigNumber { _hex: '0x00', _isBigNumber: true },
  totalAsset: BigNumber { _hex: '0x4563918244f40000', _isBigNumber: true },
  totalStable: BigNumber { _hex: '-0x17d78400', _isBigNumber: true },
  tradeFee: BigNumber { _hex: '0x00', _isBigNumber: true },
  traderPayout: BigNumber { _hex: '0x00', _isBigNumber: true },
]

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