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)

Approval(address owner, address spender, uint256 value)

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. See a trade flow diagram for details.

changePosition()
ExchangeAddress.changePosition(
    int256 deltaAsset,
    int256 deltaStable,
    int256 stableBound
);

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:

Parameter

Details

deltaStable

If Positive:

This amount of USDC will be taken from your address and added to your collateral.

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. Reducing the collateral and increasing the leverage.

*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:

Buy asset and add to this position. When opening a new position, it would make it into a long position. When updating an existing short position, it could reduce the exposure, close it, or turn it into long, depending on the change.

If Zero:

Can be used to add/remove collateral

If Negative:

Sell asset and add an asset debt to this position. When opening a new position, it would make it into a short position. When updating an existing long position, it could reduce the exposure, close it, or turn it into short, depending on the change.

*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. This value restricts a Uniswap trade. That is, it would be applied after the Uniswap slippage + fee, as Uniswap combines slippage and fees and just returns less of the resulting token.

Note that this restriction applies to the trade on Uniswap, not to the collateral. That is, on the trading flow diagram we are talking about a swap that happens internally between Futureswap and Uniswap.

Assuming that the values of stable payed/received in the trade is called stableSwapped, there are four different cases:

  1. Opening a long trade. stableSwapped will be a negative number (paid by the user). stableBound should be set to a negative number, the absolute value of which is the maximum amount of stable the user is willing to pay for the trade.

  2. Opening a short trade. stableSwapped will be a positive number (received by the user). stableBound should be set to a positive number, which is the minimum of stable the user is willing to receive in this trade.

  3. Closing a long trade. Which is the same as opening a long trade. stableSwapped will be a positive number (received by the user). stableBound should be set to a positive number, which is the minimum of stable the user is willing to receive in this trade.

  4. Closing a short trade. Which is the same as opening a short trade. stableSwapped will be a positive number (received by the user). stableBound should be set to a positive number, which is the minimum of stable the user is willing to receive in this trade.

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()
function getPosition(address _trader)
        external
        view
        returns (
            int256 asset,
            int256 stable,
            uint8 adlTrancheId,
            uint64 adlShareClass
        );

Return Value: Object with the following parameters in order

Return Value

Description

asset

Amount of asset this position owns (if positive) or borrowed (if negative).

A long position would have a positive asset value. As, the asset was purchased and is owned by this position.

A short position would have a negative asset value. As, the asset was sold and this position is this much asset in debt.

stable

Amount of stable this position owns (if positive) or borrowed (if negative).

A long position would have a negative asset value. Asset was purchased using stabled borrowed from the liquidity pool. Provided collateral is included in this sum, reducing the amount of debt.

A short position would have a positive stable value. Asset was borrowed from the pool and sold for stable that this position now owns. Provided collateral is also included in this sum, further increasing the amount of stable a position has.

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

pageTrade Estimate

Get Asset Pool Size

From the exchange address call assetToken().

address public override assetToken;

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.

function balanceOf(address account) public view virtual override returns (uint256) {

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().

address public override stableToken;

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.

function balanceOf(address account) public view virtual override returns (uint256) {

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().

ExchangeConfig2 public override exchangeConfig2;

From this object you will get dfrRate

///range: [0, 1 ether] 
int256 dfrRate;

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.

MainPosition public override longPosition;

and

MainPosition public override shortPosition;

This is what is continuously being paid at the current time. Now you'll want to do the following math...

 int256 assetDifference = longPosition.asset + shortPosition.asset;
 int256 assetToMove = (assetDifference * exchangeConfig2.dfrRate) / 1 ether;
         

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

event TrancheAutoDeleveraged(
        uint8 indexed trancheId,
        uint64 indexed shareClass
    );

Returns

Return Value

Description

trancheId

The tranchId being updated

shareClass

The share class being updated

More details can be found in the ADL section.

pageADL

DFRCharged

Emitted when the dynamic funding rate is charged to all traders

event DFRCharged(
        int256 assetToMove,
        int256 poolAsset,
        int256 totalAssetLong,
        int256 totalAssetShort
    );

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:

const inverseLeverage = (ONE_ETHER * ONE_ETHER) / maxLeverage.toBigInt();

return asset > 0n
  ? (stable * ONE_ETHER * ONE_ETHER) / (inverseLeverage * asset - asset * ONE_ETHER)
  : -((stable * ONE_ETHER) / ((inverseLeverage * asset) / ONE_ETHER + asset));

maxLeverage being 30 ether

Last updated