Order ERC20

Demo for making an immediate swap

Mini-order

To support small quantity trading, we introduce an additional concept tradeCost, which is the minimum gas fee when a trade transaction is uplink to Ethereum.

Let's take LRC-ETH trading as an example.

Below are the steps:

Query api/v3/exchange/tokens to get the dust value of orderAmounts for both LRC and ETH

The dust value is the minimum value to pass Relayer check. Any amount less than "dust" can't be traded.

In this case, we will get both minTokenLRC and minTokenETH after getting dust value.

If a user wants to convert LRC to ETH, the set LRC amount can't be less than minTokenLRC and the converted ETH amount can't be less than minTokenETH.

Caculate the cost by considering slippage

minCostLRCSlip = minCostLRC/(1-slippage)
minCostETHSlip = minCostETH/(1-slippage)

Price impact update

  1. sellTokenMinAmount = baseOrderInfo.minAmount from LoopringAPI.userAPI.getMinimumTokenAmt({accountId,marke}, apiKey)

  2. {output} from sdk.getOutputAmount(input: sellTokenMinAmount, isAtoB: isAtoB,…}).output

  3. PriceBase = output / sellTokenMinAmount

  4. tradePrice = calcTradeParams.minReceive / userInputSell

  5. priceImpact = 1 - tradePrice/PriceBase - 0.005

  6. If priceImpact < 0 priceImpact = 0 Else priceImpact

LRC-ETH : for Base to Quote

An example swap of ETH into LRC.

Calculate swap function

const calculateSwap = (
  sellSymbol = "LRC",
  buySymbol = "ETH",
  isInputSellToBuy: boolean,
  inputValue: number, // user Input value no decimal,
  _slippage = 0.1,
  // MOCK value
  amountMap: { [key: string]: any } = userAmount,
  market: string = deepMock.symbol,
  // close = ticker.tickers[7],
  depth: any = deepMock,
  ammPoolSnapshot: sdk.AmmPoolSnapshot = ammPoolSnapshotMock,
  tokenMap: sdk.LoopringMap<sdk.TokenInfo> = TokenMapMockSwap,
  ammMap: { [key: string]: any } = AMM_MAP
) => {
  let calcFor100USDAmount, calcForMinCost, calcForPriceImpact;
  if (depth && market && tokenMap) {
    const sellToken = tokenMap[sellSymbol];
    const buyToken = tokenMap[buySymbol];
    const isInputSellOutputBuy = isInputSellToBuy;
    let input: any = inputValue;

    console.log(
      "sellToken: Symbol ",
      sellSymbol,
      "buyToken: Symbol",
      buySymbol,
      "is Input Sell Output Buy:",
      isInputSellOutputBuy,
      "input value",
      input
    );
    input = input === undefined || isNaN(Number(input)) ? 0 : Number(input);
    let slippage = sdk.toBig(_slippage).times(100).toString();
    let totalFee = undefined;
    let feeTakerRate = undefined;
    let feeBips = undefined;
    let takerRate = undefined;
    let buyMinAmtInfo = undefined;
    let sellMinAmtInfo = undefined;
    let tradeCost = undefined;
    let basePrice = undefined;
    let maxFeeBips = MAPFEEBIPS;
    let minAmt = undefined;

    if (amountMap && amountMap[market] && ammMap) {
      console.log(`amountMap[${market}]:`, amountMap[market]);

      const ammMarket = `AMM-${market}`;

      const amountMarket = amountMap[market]; // userAmount from  LRC-ETH(Market)

      buyMinAmtInfo = amountMarket[buySymbol];
      sellMinAmtInfo = amountMarket[sellSymbol];
      console.log(
        `buyMinAmtInfo: ${market}, ${buySymbol}`,
        buyMinAmtInfo,
        `sellMinAmtInfo: ${market}, ${sellSymbol}`,
        sellMinAmtInfo
      );

      feeBips = ammMap[ammMarket] ? ammMap[ammMarket].feeBips : 1;

      feeTakerRate =
        amountMarket[buySymbol] &&
        amountMarket[buySymbol].userOrderInfo.takerRate;
      tradeCost = amountMarket[buySymbol].tradeCost;

      /** @description for charge fee calc, calcFor100USDAmount
       *  Loopring market consider buyToken value small then  max(buyMinAmtInfo.userOrderInfo.minAmount,buyToken.orderAmounts.dust) is a small order,
       * the fee will take the Max(tradeCost,userTakeRate)
       * use the buyMinAmount Input calc the selltoken value,
       * please read Line:321
       * **/
      const minAmountInput = BigNumber.max(
        buyMinAmtInfo.userOrderInfo.minAmount,
        buyToken.orderAmounts.dust
      )
        .div(sdk.toBig(1).minus(sdk.toBig(slippage).div(10000)))
        .div("1e" + buyToken.decimals)
        .toString();
      calcFor100USDAmount = sdk.getOutputAmount({
        input: minAmountInput,
        sell: sellSymbol,
        buy: buySymbol,
        isAtoB: false,
        marketArr: marketArray as string[],
        tokenMap: tokenMap as any,
        marketMap: marketMap as any,
        depth,
        ammPoolSnapshot: ammPoolSnapshot,
        feeBips: feeBips ? feeBips.toString() : 1,
        takerRate: "0",
        slipBips: slippage,
      });

      console.log(
        "buyMinAmtInfo.userOrderInfo.minAmount:",
        buyMinAmtInfo.userOrderInfo.minAmount,
        `buyMinAmtInfo.userOrderInfo.minAmount, with slippage:${slippage}`,
        sdk
          .toBig(buyMinAmtInfo.userOrderInfo.minAmount)
          .div(sdk.toBig(1).minus(sdk.toBig(slippage).div(10000)))
          .toString()
      );

      /*** calc for Price Impact ****/
      const sellMinAmtInput = sdk
        .toBig(sellMinAmtInfo.baseOrderInfo.minAmount)
        .div("1e" + sellToken.decimals)
        .toString();

      calcForPriceImpact = sdk.getOutputAmount({
        input: sellMinAmtInput,
        sell: sellSymbol,
        buy: buySymbol,
        isAtoB: true,
        marketArr: marketArray as string[],
        tokenMap: tokenMap as any,
        marketMap: marketMap as any,
        depth,
        ammPoolSnapshot: ammPoolSnapshot,
        feeBips: feeBips ? feeBips.toString() : 1,
        takerRate: "0",
        slipBips: "10",
      });

      basePrice = sdk.toBig(calcForPriceImpact?.output).div(sellMinAmtInput);

      console.log(
        "calcForPriceImpact input: ",
        sellMinAmtInput,
        ", output: ",
        sdk.toBig(calcForPriceImpact?.output).div(sellMinAmtInput).toNumber(),
        ", calcForPriceImpact:",
        calcForPriceImpact?.amountBOutSlip?.minReceivedVal,
        ", calcForPriceImpact basePrice: ",
        basePrice.toNumber()
      );

      /**** calc for mini Cost ****/

      //minCostBuyToken = max(dustBuyToken, tradeCostETH/maxAllowBips)
      const dustToken = buyToken;
      let minCostBuyTokenInput = BigNumber.max(
        sdk.toBig(tradeCost).times(2), //maxAllowBips = 50% tradeCostETH/50%
        dustToken.orderAmounts.dust
      );

      const tradeCostInput = sdk
        .toBig(minCostBuyTokenInput)
        .div(sdk.toBig(1).minus(sdk.toBig(slippage).div(10000)))
        .div("1e" + dustToken.decimals)
        .toString();

      console.log(
        `tradeCost: ${tradeCost}*2:`,
        sdk.toBig(tradeCost).times(2).toString(),
        "buyToken.orderAmounts.dust",
        buyToken.orderAmounts.dust,
        "minCostBuyToken:",
        minCostBuyTokenInput.toString(),
        `calcForMinCostInput, with slippage:${slippage}`,
        sdk
          .toBig(minCostBuyTokenInput ?? 0)
          .div(sdk.toBig(1).minus(sdk.toBig(slippage).div(10000)))
          .toString(),
        "calcForMinCost, Input",
        tradeCostInput
      );

      calcForMinCost = sdk.getOutputAmount({
        input: tradeCostInput,
        sell: sellSymbol,
        buy: buySymbol,
        isAtoB: false,
        marketArr: marketArray as string[],
        tokenMap: tokenMap as any,
        marketMap: marketMap as any,
        depth,
        ammPoolSnapshot: ammPoolSnapshot,
        feeBips: feeBips ? feeBips.toString() : 1,
        takerRate: "0",
        slipBips: slippage,
      });

      //add additionally 10% tolerance for minimum quantity user has to set on sell Token
      /**
       * @output: minAmt for UI
       * this value mini-order Sell token amount (show on the UI for available order check)
       * setSellMinAmt(minAmt.toString());
       */
      minAmt = BigNumber.max(
        sellToken.orderAmounts.dust,
        calcForMinCost?.amountS ?? 0
      ).times(1.1);

      console.log(
        "UI show mini-order Sell token amount:",
        minAmt.toString(),
        sdk
          .toBig(minAmt)
          .div("1e" + sellToken.decimals)
          .toString()
      );

      console.log(
        `calcFor100USDAmount.amountS`,
        sdk
          .toBig(calcFor100USDAmount?.amountS ?? 0)
          .div("1e" + sellToken.decimals)
          .toString(),
        "calcForMinCost.amountS",
        sdk
          .toBig(calcForMinCost?.amountS ?? 0)
          .div("1e" + sellToken.decimals)
          .toString()
      );
    }
    const calcTradeParams = sdk.getOutputAmount({
      input: input.toString(),
      sell: sellSymbol,
      buy: buySymbol,
      isAtoB: isInputSellOutputBuy,
      marketArr: marketArray as string[],
      tokenMap: tokenMap as any,
      marketMap: marketMap as any,
      depth,
      ammPoolSnapshot: ammPoolSnapshot,
      feeBips: feeBips ? feeBips.toString() : 1,
      takerRate: "0", // for new calc miniReceive will minus fee, so takeRate can fix as 0
      slipBips: slippage,
    });

    const minSymbol = buySymbol;
    const tradePrice = sdk
      .toBig(calcTradeParams?.amountBOutSlip?.minReceivedVal ?? 0)
      .div(isInputSellOutputBuy ? input.toString() : calcTradeParams?.output);
    const priceImpact = sdk
      .toBig(1)
      .minus(sdk.toBig(tradePrice).div(basePrice ?? 1))
      .minus(0.001);
    if (calcTradeParams && priceImpact.gte(0)) {
      calcTradeParams.priceImpact = priceImpact.toFixed(4, 1);
    } else {
      calcTradeParams && (calcTradeParams.priceImpact = "0");
    }

    console.log(
      "calcTradeParams input:",
      input.toString(),
      ", calcTradeParams Price: ",
      sdk
        .toBig(calcTradeParams?.amountBOutSlip?.minReceivedVal ?? 0)
        .div(input.toString())
        .toNumber(),
      `isAtoB mean isInputSellOutputBuy:${isInputSellOutputBuy}, ${
        isInputSellOutputBuy ? input.toString() : calcTradeParams?.output
      } tradePrice: `,
      tradePrice.toString(),
      "basePrice: ",
      basePrice?.toString(),
      "toBig(tradePrice).div(basePrice)",
      sdk
        .toBig(tradePrice)
        .div(basePrice ?? 1)
        .toNumber(),
      "priceImpact (1-tradePrice/basePrice) - 0.001",
      priceImpact.toNumber(),
      "priceImpact view",
      calcTradeParams?.priceImpact
    );

    if (
      tradeCost &&
      calcTradeParams &&
      calcTradeParams.amountBOutSlip?.minReceived &&
      feeTakerRate
    ) {
      let value = sdk
        .toBig(calcTradeParams.amountBOutSlip?.minReceived)
        .times(feeTakerRate)
        .div(10000);

      console.log(
        "input Accounts",
        calcTradeParams?.amountS,
        "100 U Amount Sell:",
        calcFor100USDAmount?.amountS
      );

      let validAmt = !!(
        calcTradeParams?.amountS &&
        calcFor100USDAmount?.amountS &&
        sdk.toBig(calcTradeParams?.amountS).gte(calcFor100USDAmount.amountS)
      );
      let totalFeeRaw;

      console.log(
        `${minSymbol} tradeCost:`,
        tradeCost,
        "useTakeRate Fee:",
        value.toString(),
        "calcFor100USDAmount?.amountS:",
        calcFor100USDAmount?.amountS,
        `is setup minTrade amount, ${calcFor100USDAmount?.amountS}:`,
        validAmt
      );

      if (!validAmt) {
        if (sdk.toBig(tradeCost).gte(value)) {
          totalFeeRaw = sdk.toBig(tradeCost);
        } else {
          totalFeeRaw = value;
        }
        console.log(
          "maxFeeBips update for tradeCost before value:",
          maxFeeBips,
          "totalFeeRaw",
          totalFeeRaw.toString()
        );
        maxFeeBips = Math.ceil(
          totalFeeRaw
            .times(10000)
            .div(calcTradeParams.amountBOutSlip?.minReceived)
            .toNumber()
        );
        console.log("maxFeeBips update for tradeCost after value:", maxFeeBips);
      } else {
        totalFeeRaw = sdk.toBig(value);
      }

      /**
       * totalFee
       */
      totalFee = totalFeeRaw
        .div("1e" + tokenMap[minSymbol].decimals)
        .toString();
      /** @output: UI
       *   getValuePrecisionThousand(
       *   totalFeeRaw.div("1e" + tokenMap[minSymbol].decimals).toString(),
       *   tokenMap[minSymbol].precision,
       *   tokenMap[minSymbol].precision,
       *   tokenMap[minSymbol].precision,
       *   false,
       *   { floor: true }
       * );
       */

      tradeCost = sdk
        .toBig(tradeCost)
        // @ts-ignore
        .div("1e" + tokenMap[minSymbol].decimals)
        .toString();

      /** @output:  UI code with precision
       *   getValuePrecisionThousand(
       *   sdk
       *     .toBig(tradeCost)
       *     .div("1e" + tokenMap[minSymbol].decimals)
       *     .toString(),
       *   tokenMap[minSymbol].precision,
       *   tokenMap[minSymbol].precision,
       *   tokenMap[minSymbol].precision,
       *   false,
       *   { floor: true }
       * );
       */

      console.log("totalFee view value:", totalFee + " " + minSymbol);
      console.log("tradeCost view value:", tradeCost + " " + minSymbol);
    }

    const minimumReceived = sdk
      .toBig(calcTradeParams?.amountBOutSlip?.minReceivedVal ?? 0)
      .minus(totalFee ?? 0)
      .toString();
    console.log("minimumReceived:", minimumReceived);

    /** @output:   UI code with precision
     *   getValuePrecisionThousand(
     *   toBig(calcTradeParams?.amountBOutSlip?.minReceivedVal ?? 0)
     *     .minus(totalFee)
     *     .toString(),
     *   tokenMap[minSymbol].precision,
     *   tokenMap[minSymbol].precision,
     *   tokenMap[minSymbol].precision,
     *   false,
     *   { floor: true }
     * );
     */

    let priceImpactView: any = calcTradeParams?.priceImpact
      ? parseFloat(calcTradeParams?.priceImpact) * 100
      : undefined;
    console.log("priceImpact view:", priceImpactView + "%");
    //  @output:   UI code with color alert
    // const priceImpactObj = getPriceImpactInfo(calcTradeParams);
    // const _tradeCalcData: Partial<TradeCalcData<C>> = {
    //   priceImpact: priceImpactObj.value.toString(),
    //   priceImpactColor: priceImpactObj.priceImpactColor,
    //   minimumReceived: !minimumReceived?.toString().startsWith("-")
    //     ? minimumReceived
    //     : undefined,
    //   fee: totalFee,
    //   feeTakerRate,
    //   tradeCost,
    // };

    console.log(
      `isInputSellOutputBuy:${isInputSellOutputBuy}`,
      `output ${isInputSellOutputBuy ? "Buy" : "Sell"}`,
      calcTradeParams?.output
    );

    return {
      market,
      feeBips,
      takerRate,
      sellMinAmtInfo: sellMinAmtInfo as any,
      buyMinAmtInfo: buyMinAmtInfo as any,
      totalFee,
      maxFeeBips,
      feeTakerRate,
      tradeCost,
      minimumReceived,
      calcTradeParams,
      minAmt,
    };
  }
};

Get apikey & eddsaKey

const {accInfo} = await LoopringAPI.exchangeAPI.getAccount({
  owner: LOOPRING_EXPORTED_ACCOUNT.address,
});
const eddsaKey = await signatureKeyPairMock(accInfo);
apiKey = (
  await LoopringAPI.userAPI.getUserApiKey(
    {
      accountId: accInfo.accountId,
    },
    eddsaKey.sk
  )
).apiKey;
Mock Swap Data
import * as sdk from "../index";

export const marketArray = ["LRC-ETH"];
export const marketMap = {
  "LRC-ETH": {
    baseTokenId: 1,
    enabled: true,
    market: "LRC-ETH",
    orderbookAggLevels: 5,
    precisionForPrice: 6,
    quoteTokenId: 0,
    status: 3,
    isSwapEnabled: true,
    createdAt: 1617967800000,
  },
};
//v3/mix/depth?level=0&limit=50&market=LRC-ETH
export const deepMock = {
  symbol: "LRC-ETH",
  version: 23249677,
  timestamp: 1655719492365,
  mid_price: 0.00033248,
  bids: [
    {
      price: 0.00030689,
      amt: "12041160324514792497908",
      vol: "3695372332571085210",
      amtTotal: "618450503644320209925641",
      volTotal: "198539605794234049017",
    },
    {
      price: 0.00030752,
      amt: "12016302126785109160251",
      vol: "3695372332571085210",
      amtTotal: "606409343319805417427733",
      volTotal: "194844233461662963807",
    },
    {
      price: 0.00030816,
      amt: "11991520826895479525387",
      vol: "3695372332571085210",
      amtTotal: "594393041193020308267482",
      volTotal: "191148861129091878597",
    },
    {
      price: 0.00030881,
      amt: "12329062048917727073625",
      vol: "3807353312345966580",
      amtTotal: "582401520366124828742095",
      volTotal: "187453488796520793387",
    },
    {
      price: 0.00030945,
      amt: "11941442525358097768419",
      vol: "3695372332571085210",
      amtTotal: "570072458317207101668470",
      volTotal: "183646135484174826807",
    },
    {
      price: 0.0003102,
      amt: "3223726000000000000000",
      vol: "999999805200000000",
      amtTotal: "558131015791849003900051",
      volTotal: "179950763151603741597",
    },
    {
      price: 0.0003104,
      amt: "11904963012947201084062",
      vol: "3695372332571085210",
      amtTotal: "554907289791849003900051",
      volTotal: "178950763346403741597",
    },
    {
      price: 0.00031072,
      amt: "11892800074855146120151",
      vol: "3695372332571085210",
      amtTotal: "543002326778901802815989",
      volTotal: "175255391013832656387",
    },
    {
      price: 0.00031137,
      amt: "12227667622887455762012",
      vol: "3807353312345966580",
      amtTotal: "531109526704046656695838",
      volTotal: "171560018681261571177",
    },
    {
      price: 0.00031202,
      amt: "11843337524732768607817",
      vol: "3695372332571085210",
      amtTotal: "518881859081159200933826",
      volTotal: "167752665368915604597",
    },
    {
      price: 0.00031266,
      amt: "11819088718260537718160",
      vol: "3695372332571085210",
      amtTotal: "507038521556426432326009",
      volTotal: "164057293036344519387",
    },
    {
      price: 0.0003133,
      amt: "11794914308461194855590",
      vol: "3695372332571085210",
      amtTotal: "495219432838165894607849",
      volTotal: "160361920703773434177",
    },
    {
      price: 0.00031395,
      amt: "12127129883064173669497",
      vol: "3807353312345966580",
      amtTotal: "483424518529704699752259",
      volTotal: "156666548371202348967",
    },
    {
      price: 0.0003146,
      amt: "11746060536480763829870",
      vol: "3695372332571085210",
      amtTotal: "471297388646640526082762",
      volTotal: "152859195058856382387",
    },
    {
      price: 0.00031524,
      amt: "11722109721002274877424",
      vol: "3695372332571085210",
      amtTotal: "459551328110159762252892",