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:
1
const assetTokenDecimals = 18n;
2
const stableTokenDecimals = 6n;
3
const assetTokenDivisor = 10n ** (18n - assetTokenDecimals);
4
const stableTokenDivisor = 10n ** (18n - stableTokenDecimals);
5
6
const tradeValues = await exchange.callStatic.changePosition(
7
toWei("5") / assetTokenDivisor,
8
toWei("100") / stableTokenDivisor,
9
"0",
10
{}
11
);
Copied!
Below is an example return value:
Example Return Value
1
[
2
BigNumber { _hex: '0x00', _isBigNumber: true },
3
BigNumber { _hex: '0x00', _isBigNumber: true },
4
BigNumber { _hex: '0x00', _isBigNumber: true },
5
BigNumber { _hex: '0x00', _isBigNumber: true },
6
BigNumber { _hex: '0x4563918244f40000', _isBigNumber: true },
7
BigNumber { _hex: '-0x17d78400', _isBigNumber: true },
8
startAsset: BigNumber { _hex: '0x00', _isBigNumber: true },
9
startStable: BigNumber { _hex: '0x00', _isBigNumber: true },
10
totalAsset: BigNumber { _hex: '0x4563918244f40000', _isBigNumber: true },
11
totalStable: BigNumber { _hex: '-0x17d78400', _isBigNumber: true },
12
tradeFee: BigNumber { _hex: '0x00', _isBigNumber: true },
13
traderPayout: BigNumber { _hex: '0x00', _isBigNumber: true },
14
]
Copied!
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 modified 1mo ago