Swap Order

You can control and query the status of swap executions using the 'exchange' property of Dexible. The exchange is responsible for managing all things swap related.

Creating Swap Orders

Creating a swap is pretty simple now that you are familiar with the algo types.

const sl = new TrailingStopSwap({
    amountIn:  units.inBNETH(".146"),
    tokenIn: UNI[Networks.EthereumGoerli.chainId],
    tokenOut: WETH[Networks.EthereumGoerli.chainId],
    slippage: new Slippage(.5, false),
    spotPercentage: 10,
});
const o = await sdk.exchange.swap(sl);

Here we are simply using the trailing stop swap details to submit to Dexible for execution.

Querying Swap Orders

You can also use the exchange to query for all your active and historical orders or for a specific order once you know its id. Here is an example:

let orders = await sdk.exchange.allSwaps(
    1, //chainId
    0, //offset is optional
    100, //page size optional
);

This will return the first 100 orders owned by the signer connected to the Dexible instance. The returned information is as follows:

{
  "id": "1:1",
  "state": "CANCELED",
  "token_in": "0xd0a1e359811322d97991e03f863a0c30c2cf029c",
  "token_out": "0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa",
  "amount_in": "5193000000000000000",
  "network_id": 42,
  "quote_id": 8,
  "tags": [],
  "algo": {
    "name": "Market"
  },
  "policies": [
    {
      "name": "Slippage",
      "params": {
        "amount": "0.5"
      }
    },
    {
      "name": "GasCost",
      "params": {
        "amount": "0",
        "gasType": "relative",
        "deviation": "0"
      }
    },
    {
      "name": "FailLimit",
      "params": {
        "maxFailures": 1
      }
    }
  ],
  "quote": {
    "rounds": "1",
    "amount_in_per_round": "1000000000000000000",
    "amount_out_per_round": "661299875487234764089"
  },
  "fill_summary": {
    "fees": "3115800000000000",
    "gas_fees": "1230000000000000",
    "fee_token": "0xd0a1e359811322d97991e03f863a0c30c2cf029c",
    "total_attempts": 2,
    "tokens_spent": "3115800000000000000",
    "tokens_received": "4254057289802560054820",
    "progress": 0.6,
    "has_pending_txn": false,
    "txns": [
      {
        "hash": "0x...",
        "status": true,
        "timestamp": 1626807188637
      },
      {
        "hash": "0x...",
        "status": true,
        "timestamp": 1626807218862
      }
    ]
  },
  "execution_status": [
    {
      "policy": "StopPrice",
      "passed": true,
      "reason": "Price 1370.047003547976 is at or below trigger price 1375"
    },
    {
      "policy": "GasCost",
      "passed": true,
      "reason": "Relative gas price always applies"
    },
    {
      "policy": "FailLimit",
      "passed": true,
      "reason": "Failure count below max failures"
    }
  ],
  "status_changes": [
    {
      "reason": "User changed state",
      "old_status": null,
      "new_status": "CANCELED",
      "timestamp": 1626807204622
    }
  ]
}

There is quite a bit of detail attached to an order. Most of the fields match what was used to create the order (tokens, amounts, etc). But many things get attached to the order as it executes in Dexible. We'll briefly cover those here.

Fill Summary

The fill summary gives you a quick summary of what has been filled with the order so far. In this example, it shows basis points fee (fees) of 0.0031158 WETH and .00123 WETH paid for gas (gas_fees). The fees are expressed in the fee token address, which is WETH in this example. It shows that it's 60% complete (progress) with 2 rounds finished. It shows how much of the input tokens have been spent and how many output tokens have been received. Finally, it shows the txn hash for all transactions that were submitted so far.

Execution Status

As the order executes, Dexible records detailed information about the order. Each policy evaluation records changes and the reason why the pass/fail outcome changed. In this example, we can see three policies: StopPrice, GasCost, and FailLimit. The StopPrice says that it passed and provides the price point it was triggered at to make it pass. The GasCost checks gas prices and in this case, since we used relative/fast gas price, it always passes. Finally, the FailLimit policy did not find more than 30% of the total rounds failing, so it passed as well. If a policy status is "fail", it means the policy conditions prevented the order from proceeding to the next round.

Status Changes

Anytime the order status changes, such as being paused, cancelled, or resumed, a status change log is created to let you know why the order state changed. In this example, the trader canceled the order before it was complete.

Last updated