Trade

The Core Set of Functions to Place a Trade

If on testnet, you can acquire test USDC and Arb ETH on Arbitrum by joining the Futureswap Discord and sending your address in the #testnet-usdc channel.

Approve USDC

Open Zeppelin Docs (link)
1
Approval(address owner, address spender, uint256 value)
Copied!

Testnet

Exchange: 0xfcD6da3Ea74309905Baa5F3BAbDdE630FccCcBD1
Testnet USDC: 0xb0Ad46bD50b44cBE47E2d83143E0E415d6A842F6

Mainnet

Exchange: 0xF7CA7384cc6619866749955065f17beDD3ED80bC
Arbitrum USDC: 0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8

Trading

Change Position

changePosition( ) is used to open and close trades as well as add and remove collateral. To fully close a trade, set both collateral and asset to zero.
changePosition()
1
ExchangeAddress.changePosition(
2
int256 deltaAsset,
3
int256 deltaStable,
4
int256 stableBound
5
);
Copied!

Definitions:

deltaAsset: The amount of asset to long(if positive) or short(if negative) to change/create your position. Denomination: Same as Asset currency's denomination
Example #1: To go long 5 ETH from not having a positions open deltaAsset would be: 5 ether (5000000000000000000).
Example #2: To go short 5 ETH from not having a positions open deltaAsset would be: -5 ether (-5000000000000000000).
Example #3: To go short 5 ETH from being long 12 ETH, a positions open deltaAsset would be: -17 ether (-17000000000000000000).
deltaStable: Is the amount of collateral to add if positive, no change if zero, or remove if negative. Denomination: Same as Stable currency's denomination
Example #1: To add 123 stable collateral, deltaStable would be: 123 mWei(6 decimals) (123000000).
Example #2: To remove 65 stable collateral, deltaStable would be: -65 mWei(6 decimals) (-65000000).
stableBound: This bound is for the final execution quantity inclusive of slippage, and any fees. If this is violated the trade will be reverted and collateral returned. Denomination: Same as stable currency's denomination
Example #1: If trying to go long 2 ETH at 100 USDC per ETH or lower, your stableBound would be -200 * 10^6 (-200000000). Negative because you are selling(losing) a maximum of 200 USDC to receive 2 ETH long.
Example #2: If trying to go short 2 ETH at 110 USDC per ETH or higher, your stableBound would be 220 *10^6 (220000000). Positive because you are selling(losing) 2 ETH for a minimum gain of 220 of USDC.
To fully close a position it is recommended to set deltaStable and deltaAsset both to zero. This is a unique identifier that will fully close the trade and return the collateral + profit/loss.

Inputs:

Parameters
Details
deltaStable
If Positive:
This amount of USDC will be taken from your address
If Zero:
No USDC will be removed from your trade. USDC may be received by you if this is fully closing your trade
If Negative:
To withdraw this amount of USDC from a position
*Combination:
If zero collateral and zero asset are combined this
is recognized as intent to fully close to zero and
receive 100% of collateral +/- P/L
deltaAsset
The amount of asset the position should be changed by.
If Positive:
Goes long(or less short) this amount of asset, added to any currently open position
If Zero:
Can be used to add/remove collateral
If Negative:
To withdraw this amount of USDC from a position
*Combination:
If zero asset and zero collateral are combined this
is recognized as intent to fully close to zero and
receive 100% of collateral +/- P/L
stableBound
The maximum/minimum amount of stable that the user is willing to pay/receive for the _deltaAsset change.
If the user is buying asset (_deltaAsset > 0) the user will have to choose a maximum negative number that he is going to be in dept for.
If the user is selling asset (_deltaAsset < 0) the user will have to choose a minimum positive number /// of that that he wants to be credited with.

Outputs:

Name
Description
startAsset
The amount of asset the position had before changePosition( ) was called
startStable
The amount of stable the position had before changePosition( ) was called
totalAsset
The amount of asset the position had after execution of changePosition()
totalStable
The amount of stable the position had after execution of changePosition()
tradeFee
The trade fee in stable the trade was charged
traderPayout
The amount of stable sent to the trader

Get Position

Exchange specific trade position details for a user
getPosition()
1
function getPosition(address _trader)
2
external
3
view
4
returns (
5
int256 asset,
6
int256 stable,
7
uint8 adlTrancheId,
8
uint64 adlShareClass
9
);
Copied!

Return Value: Object with the following parameters in order

Return Value
Description
asset
Amount of asset in the position
stable
Amount of collateral in the position
adlTrancheId
Tranche ID, used to monitor ADL events
adlShareClass
Share class of the tranche trade is in. Used to monitor ADL events

Estimate Execution Price

Get Asset Pool Size

From the exchange address call assetToken().
1
address public override assetToken;
Copied!
This will return the address of the asset token for this exchange. Once you have the address of the token you can use the balanceOf() to find the asset pool size using the token address balanceOf and the account as the exchange.
1
function balanceOf(address account) public view virtual override returns (uint256) {
Copied!
This return the amount of asset token available in the Futureswap pool for trading against the uniswap exchange.

Get Stable Pool Size

From the exchange address call stableToken().
1
address public override stableToken;
Copied!
This will return the address of the stable token for this exchange. Once you have the address of the token you can use the balanceOf() to find the stable pool size.
1
function balanceOf(address account) public view virtual override returns (uint256) {
Copied!
This return the amount of stable token available in the Futureswap pool for trading against the uniswap exchange. This value is also the max trade size.

Get Funding Rate

To get the funding rate of an exchange first pull the current funding rate constant from the exchange config exchangeConfig2().
1
ExchangeConfig2 public override exchangeConfig2;
Copied!
From this object you will get dfrRate
1
///range: [0, 1 ether]
2
int256 dfrRate;
Copied!
Futureswap's funding rate is based on the balance between longs and shorts in an exchange. Next, you'll need to get the total long and short trade amounts. Call the following.
1
MainPosition public override longPosition;
Copied!
and
1
MainPosition public override shortPosition;
Copied!
This is what is continuously being paid at the current time. Now you'll want to do the following math...
1
int256 assetDifference = longPosition.asset + shortPosition.asset;
2
int256 assetToMove = (assetDifference * exchangeConfig2.dfrRate) / 1 ether;
3
Copied!
From this you get the Pay Asset Rate (the current funding rate). If you multiple this by a period of time (timeDelta) you'll get how much asset needs to be paid over that period of time.

Events

Auto Deleveraging (ADL) events

Exchanges emit a TrancheAutoDeleveraged() event every time ADL occurs. Traders should subscribe to this event to see if their position got ADL'd.
Traders can get their trancheId and shareClass from IExchange.getPosition.
If this event is emitted a trader's position with a matching tranceId and shareClass will have changed
1
event TrancheAutoDeleveraged(
2
uint8 indexed trancheId,
3
uint64 indexed shareClass
4
);
Copied!

Returns

Return Value
Description
trancheId
The tranchId being updated
shareClass
The share class being updated

More details can be found in the ADL section.

DFRCharged

Emitted when the dynamic funding rate is charged to all traders
1
event DFRCharged(
2
int256 assetToMove,
3
int256 poolAsset,
4
int256 totalAssetLong,
5
int256 totalAssetShort
6
);
Copied!

Returns

Return Value
Description
assetToMove
Total amount of asset paid by the more popular trade (longs or shorts)
poolAsset
The amount of asset earned by the liquidity providers (assetToMove - poolAsset gives one the asset paid to the less popular side)
totalAssetLong
The new amount of asset that is now long
totalAssetShort
The new amount of asset that is now short

Calc Liquidation Price:

This will be available from the Graph but while that is down due to Arbitrum (they are working on a fix) you will need to calculate it manually:
1
const inverseLeverage = (ONE_ETHER * ONE_ETHER) / maxLeverage.toBigInt();
2
3
return asset > 0n
4
? (stable * ONE_ETHER * ONE_ETHER) / (inverseLeverage * asset - asset * ONE_ETHER)
5
: -((stable * ONE_ETHER) / ((inverseLeverage * asset) / ONE_ETHER + asset));
Copied!
maxLeverage being 30 ether
Last modified 1mo ago