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:

Outputs:

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

Estimate Execution Price

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

More details can be found in the ADL section.

DFRCharged

Emitted when the dynamic funding rate is charged to all traders

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

Returns

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