Fill a limit order

In order to fill an order, a SwappableOrder data is required as an input.

It can be retrieved from API web service or composed by maker.

Parameters

const orderWithSignature: SwappableOrder {
  nonceAndMeta: '1461501637330902918203684832716283019655932542976',
  expiry: 1665587100,
  makerAsset: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
  takerAsset: '0x2b591e99afe9f32eaa6214f7b7629768c40eeb39',
  maker: '0x2bb45fa7c25071ff37a49877A02b5b3986113A3A',
  taker: '0x0000000000000000000000000000000000000000',
  makerAmount: '1000000000000000000',
  takerAmount: '2800000000',
  signature: '0x97166e35e63ecab23a0c4e7ec4ec6863193b48ddcee4f9f8291ac95a05e9545c46037cdcf01445d7a2b5dd0fdce66bc9f8b49cac75be03a3ba07dc0d467387351c'
}

This data can then be supplemented by additional required params and sent to ParaSwap web API to generate Augustus Swapper route for the fulfilment transaction:

const txData = await sdk.buildLimitOrderTx(
  {
    srcDecimals: 18,
    destDecimals: 8,
    userAddress: taker.address,
    orders: [orderWithSignature],
  })

The returned transaction params can be used to send a transaction.

Example

/* eslint-disable @typescript-eslint/no-unused-vars */
import axios from 'axios';
import BigNumber from 'bignumber.js';
import { ethers } from 'ethers';

import {
  constructPartialSDK,
  constructEthersContractCaller,
  constructAxiosFetcher,
  constructBuildLimitOrderTx,
  SwappableOrder,
} from '..';

const taker = ethers.Wallet.createRandom().connect(
  ethers.getDefaultProvider(1)
);

const fetcher = constructAxiosFetcher(axios);

const provider = taker;
const contractCaller = constructEthersContractCaller(
  {
    ethersProviderOrSigner: provider,
    EthersContract: ethers.Contract,
  },
  taker.address
);

const takerSDK = constructPartialSDK(
  {
    chainId: 1,
    contractCaller,
    fetcher,
  },
  constructBuildLimitOrderTx
);

const DAI = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
const HEX = '0x2b591e99afe9f32eaa6214f7b7629768c40eeb39';

const orderWithSignature: SwappableOrder = {
  nonceAndMeta: '1461501637330902918203684832716283019655932542976',
  expiry: 1665584787,
  makerAsset: DAI,
  takerAsset: HEX,
  maker: '0x5ad4346504d1DF55f22091058Ae9Db960E09a6E2',
  taker: '0x0000000000000000000000000000000000000000',
  makerAmount: (1e18).toString(10),
  takerAmount: (28e8).toString(10), // hex has 8 decimals
  signature:
    '0x7a554a9fcd423a2d3110366f8e265c8001049e86f03d9b4e84276496b7713dbb1d394adc38290a7ef231f7f7cc7908f11953cebb5650709e4558271b3203a5c41c',
};

async function run() {
  // build calldata for order fulfilling transaction
  const txData = await takerSDK.buildLimitOrderTx(
    {
      srcDecimals: 18,
      destDecimals: 18,
      userAddress: taker.address,
      orders: [orderWithSignature],
    },
    { ignoreChecks: true }
  );

  const { gas: payloadGas, ...LOPayloadTxParams } = txData;

  // compose ethers transaction out of provided params
  const transaction: ethers.providers.TransactionRequest = {
    ...LOPayloadTxParams,
    gasPrice: '0x' + new BigNumber(LOPayloadTxParams.gasPrice).toString(16),
    gasLimit: '0x' + new BigNumber(payloadGas || 5000000).toString(16),
    value: '0x' + new BigNumber(LOPayloadTxParams.value).toString(16),
  };

  // send
  const takerFillsOrderTx: ethers.providers.TransactionResponse =
    await taker.sendTransaction(transaction);

  console.log('takerFillsOrderTx', takerFillsOrderTx);
}

run();

Last updated