Fill a limit order

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

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

Parameters

const orderWithSignature: SwappableNFTOrder = {
  expiry: 1665645962,
  nonceAndMeta: '1490585846052014974250870934243084527261268076495',
  maker: '0x7ba594df3161729bf2e68a9d0a11dceb57a2e306',
  taker: '0xdef171fe48cf0115b1d80b88dc8eab59176fee57',
  makerAsset: '0x2953399124f0cbb46d2cbacd8a89cf0599974963',
  makerAssetId:
    '7772759950848685723459796247330971791008072228632493699501910275462086524929',
  makerAssetType: 1,
  takerAsset: '0x6b175474e89094c44da98b954eedeac495271d0f',
  takerAssetId: '0',
  takerAssetType: 0,
  makerAmount: '1',
  takerAmount: '8000000000000000000',
  signature:
    '0x9247734939b4354ce6f99178ad95cbc19635e2ba86395370e29a8ac6a95cf5054203df0ba607717af1541a30d9df00962f16ada5273ef39633f304cc88faac0d1c',
};

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,
    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 * as dotenv from 'dotenv';
import axios from 'axios';
import { ethers } from 'ethers';
import BigNumber from 'bignumber.js';
import {
  constructPartialSDK,
  constructEthersContractCaller,
  constructAxiosFetcher,
  constructBuildNFTOrderTx,
  SwappableNFTOrder,
} from '..';

dotenv.config();

const pk = ethers.Wallet.createRandom().privateKey;

const taker = new ethers.Wallet(process.env.PK || pk).connect(
  ethers.getDefaultProvider(137)
);

const fetcher = constructAxiosFetcher(axios);

// provider must have write access to account
// this would usually be wallet provider (Metamask)
const provider = taker;
const contractCaller = constructEthersContractCaller(
  {
    ethersProviderOrSigner: provider,
    EthersContract: ethers.Contract,
  },
  taker.address
);

const takerSDK = constructPartialSDK(
  {
    chainId: 137,
    contractCaller,
    fetcher,
  },
  constructBuildNFTOrderTx
);

const orderWithSignature: SwappableNFTOrder = {
  expiry: 1665645962,
  nonceAndMeta: '1490585846052014974250870934243084527261268076495',
  maker: '0x7ba594df3161729bf2e68a9d0a11dceb57a2e306',
  taker: '0xdef171fe48cf0115b1d80b88dc8eab59176fee57',
  makerAsset: '0x2953399124f0cbb46d2cbacd8a89cf0599974963',
  makerAssetId:
    '7772759950848685723459796247330971791008072228632493699501910275462086524929',
  makerAssetType: 1,
  takerAsset: '0x6b175474e89094c44da98b954eedeac495271d0f',
  takerAssetId: '0',
  takerAssetType: 0,
  makerAmount: '1',
  takerAmount: '8000000000000000000',
  signature:
    '0x9247734939b4354ce6f99178ad95cbc19635e2ba86395370e29a8ac6a95cf5054203df0ba607717af1541a30d9df00962f16ada5273ef39633f304cc88faac0d1c',
};

async function run() {
  // build calldata for order fulfilling transaction
  const txData = await takerSDK.buildNFTOrderTx(
    {
      srcDecimals: 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 takerFillsNFTOrderTx: ethers.providers.TransactionResponse =
    await taker.sendTransaction(transaction);

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

run();

Last updated