@gelatonetwork/limit-orders-lib

中文文档
4.5.0发布于 2022/08/22GPL-3.0-onlyTypeScriptCommonJS近 30 天下载 8413

Library for creating limit orders via Gelato

原始使用文档
文档目录6 节
  1. Demo - Sorbet Finance
  2. Installation
  3. Getting Started
  4. Examples
  5. Types
  6. Need help?

npm version

Gelato Limit Order SDK

Place limit buy and sell orders on Ethereum, Polygon and Fantom using Gelato Network.

:warning: :warning: :warning: Warning :warning: :warning: :warning: : Version 2.0.0 introduced new features and our system changed to an approval/transferFrom flow. You should use the latest version available (>= 2.0.0). If you are using an old version you should update to the latest version immediately. Versions below 2.0.0 are being deprecated.

Demo - Sorbet Finance

Installation

yarn add -D @gelatonetwork/limit-orders-lib

or

npm install --save-dev @gelatonetwork/limit-orders-lib

Getting Started

Instantiate GelatoLimitOrders

import {
  GelatoLimitOrders,
  GelatoStopLimitOrders,
  utils,
} from "@gelatonetwork/limit-orders-lib";

// Supported networks: Mainnet = 1; Ropsten = 3; Polygon = 137; Fantom = 250; Avalanche = 43114;  BSC = 56
const chainId = 1;
const signerOrProvider = await provider.getSigner();
const handler = "uniswap";
// | "spookyswap"
// | "spookyswap_stoplimit"
// | "uniswap"
// | "uniswap_stoplimit"
// | "quickswap"
// | "quickswap_stoplimit"
// | "spiritswap"
// | "spiritswap_stoplimit"
// | "bombswap"
// | "polydex"
// | "cafeswap"
// | "pancakeswap"
// | "pancakeswap_stoplimit"
// | "traderjoe"
// | "traderjoe_stoplimit"
// | "defyswap"
// | "pangolin"
// | "pangolin_stoplimit";

const gelatoLimitOrders = new GelatoLimitOrders(
  chainId as ChainId,
  signerOrProvider, // optional
  handler // optional
);

const gelatoStopLimitOrders = new GelatoStopLimitOrders(
  chainId as ChainId,
  signerOrProvider, // optional
  handler // required
);

Note: Use 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE when referring to native blockchain asset (i.e MATIC, FTM or ETH)

Examples

  1. Submit an order

Note: To submit a order with an ERC20 as input you must first approve the erc20OrderRouter. You can get its address via gelatoLimitOrders.erc20OrderRouter.address or call gelatoLimitOrders.approveTokenAmount(inputToken, inputAmount).

// Token to sell
const inputToken = "0x6b175474e89094c44da98b954eedeac495271d0f"; // DAI

// Token to buy
const outputToken = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; // ETH

// Amount to sell
const inputAmount = ethers.utils.parseUnits("2000", "18");

// Minimum amount of outToken which the users wants to receive back
const minReturn = ethers.utils.parseEther("1");

// Address of user who places the order (must be same as signer address)
const userAddress = "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B";

await gelatoLimitOrders.approveTokenAmount(inputToken, inputAmount);

const tx = await gelatoLimitOrders.submitLimitOrder(
  inputToken,
  outputToken,
  inputAmount,
  minReturn
);

// Maximum amount of outToken which the users wants to receive back => stop-limit price
const maxReturn = ethers.utils.parseEther("1");

const tx = await gelatoStopLimitOrders.submitStopLimitOrder(
  inputToken,
  outputToken,
  inputAmount,
  maxReturn
);
  1. Cancel an order
const tx = await gelatoLimitOrders.cancelLimitOrder(
  order,
  true // Optional: checkIsActiveOrder = true, to check if order to be cancelled actually exists. It is recommended to use this check before trying to cancel an order to avoid faulty cancellations and gas waste.
);
const tx = await gelatoStopLimitOrders.cancelStopLimitOrder(
  order,
  true // Optional: checkIsActiveOrder = true, to check if order to be cancelled actually exists. It is recommended to use this check before trying to cancel an order to avoid faulty cancellations and gas waste.
);
  1. Fetch orders

Note: to display the minimum amount returned (i.e the output amount of the order) you should always use the adjustedMinReturn field of the order.

Note 2: If you instantiated the library by passing an handler, all orders fetched will be filtered accordingly. Otherwise, orders will not be filtered.

const allOrders = await gelatoLimitOrders.getOrders(userAddress);

Types

export class GelatoLimitOrders {
  get gelatoFeeBPS(): number;
  get slippageBPS(): number;
  get chainId(): ChainId;
  get signer(): Signer | undefined;
  get provider(): Provider | undefined;
  get subgraphUrl(): string;
  get handler(): Handler | undefined;
  get handlerAddress(): string | undefined;
  get moduleAddress(): string;
  get contract(): GelatoLimitOrdersContract;
  get erc20OrderRouter(): ERC20OrderRouter;
  constructor(chainId: ChainId, signerOrProvider?: Signer, handler?: Handler);
  encodeLimitOrderSubmission(
    inputToken: string,
    outputToken: string,
    inputAmount: BigNumberish,
    minReturn: BigNumberish,
    owner: string,
    // If inputToken is an ERC20, compare allowance with inputAmount. defaults to `true`
    checkAllowance?: boolean
  ): Promise<TransactionData>;
  encodeLimitOrderSubmissionWithSecret(
    inputToken: string,
    outputToken: string,
    inputAmount: BigNumberish,
    minReturn: BigNumberish,
    owner: string,
    // If inputToken is an ERC20, compare allowance with inputAmount. defaults to `true`
    checkAllowance?: boolean
  ): Promise<TransactionDataWithSecret>;
  submitLimitOrder(
    inputToken: string,
    outputToken: string,
    inputAmount: BigNumberish,
    minReturn: BigNumberish,
    // If inputToken is an ERC20, compare allowance with inputAmount. defaults to `true`
    checkAllowance?: boolean,
    overrides?: Overrides
  ): Promise<ContractTransaction>;
  encodeLimitOrderCancellation(
    order: Order,
    checkIsActiveOrder?: boolean
  ): Promise<TransactionData>;
  cancelLimitOrder(
    order: Order,
    checkIsActiveOrder?: boolean,
    overrides?: Overrides
  ): Promise<ContractTransaction>;
  approveTokenAmount(
    inputToken: string,
    amount: BigNumberish,
    overrides?: Overrides
  ): Promise<ContractTransaction>;
  isActiveOrder(order: Order): Promise<boolean>;
  getExchangeRate(
    inputValue: BigNumberish,
    inputDecimals: number,
    outputValue: BigNumberish,
    outputDecimals: number,
    invert?: boolean
  ): string;
  getFeeAndSlippageAdjustedMinReturn(
    outputAmount: BigNumberish,
    extraSlippageBPS?: number
  ): {
    minReturn: string;
    slippage: string;
    gelatoFee: string;
  };
  getAdjustedMinReturn(
    minReturn: BigNumberish,
    extraSlippageBPS?: number
  ): string;
  getExecutionPrice(
    inputAmount: BigNumberish,
    inputDecimals: number,
    outputAmount: BigNumberish,
    outputDecimals: number,
    isInverted?: boolean
  ): string;
  getOrders(
    owner: string,
    // includeOrdersWithNullHandler defaults to `false`
    includeOrdersWithNullHandler?: boolean
  ): Promise<Order[]>;
  getOpenOrders(
    owner: string,
    // includeOrdersWithNullHandler defaults to `false`
    includeOrdersWithNullHandler?: boolean
  ): Promise<Order[]>;
  getPastOrders(
    owner: string,
    // includeOrdersWithNullHandler defaults to `false`
    includeOrdersWithNullHandler?: boolean
  ): Promise<Order[]>;
  getExecutedOrders(
    owner: string,
    // includeOrdersWithNullHandler defaults to `false`
    includeOrdersWithNullHandler?: boolean
  ): Promise<Order[]>;
  getCancelledOrders(
    owner: string,
    // includeOrdersWithNullHandler defaults to `false`
    includeOrdersWithNullHandler?: boolean
  ): Promise<Order[]>;
}

export declare class GelatoBase {
  get gelatoFeeBPS(): number;
  get slippageBPS(): number;
  get chainId(): ChainId;
  get signer(): Signer | undefined;
  get provider(): Provider | undefined;
  get subgraphUrl(): string;
  get handler(): Handler | undefined;
  get handlerAddress(): string | undefined;
  get moduleAddress(): string;
  get contract(): GelatoBaseContract;
  get erc20OrderRouter(): ERC20OrderRouter;
  get abiEncoder(): any;
  constructor(
    chainId: ChainId,
    moduleAddress: string,
    signerOrProvider?: Signer | Provider,
    handler?: Handler,
    handlerAddress?: string
  );
  encodeLimitOrderCancellation(
    order: StopLimitOrder,
    checkIsActiveOrder?: boolean
  ): Promise<TransactionData>;
  cancelStopLimitOrder(
    order: StopLimitOrder,
    checkIsActiveOrder?: boolean,
    overrides?: Overrides
  ): Promise<ContractTransaction>;
  approveTokenAmount(
    inputToken: string,
    amount: BigNumberish,
    overrides?: Overrides
  ): Promise<ContractTransaction>;
  isActiveOrder(order: StopLimitOrder): Promise<boolean>;
  getExchangeRate(
    inputValue: BigNumberish,
    inputDecimals: number,
    outputValue: BigNumberish,
    outputDecimals: number,
    invert?: boolean
  ): string;
  getFeeAndSlippageAdjustedMinReturn(
    outputAmount: BigNumberish,
    extraSlippageBPS?: number
  ): {
    minReturn: string;
    slippage: string;
    gelatoFee: string;
  };
  getAdjustedMinReturn(
    minReturn: BigNumberish,
    extraSlippageBPS?: number
  ): string;
  getExecutionPrice(
    inputAmount: BigNumberish,
    inputDecimals: number,
    outputAmount: BigNumberish,
    outputDecimals: number,
    isInverted?: boolean
  ): string;
  getPastStopLimitOrders(owner: string): Promise<StopLimitOrder[]>;
}

class GelatoStopLimitOrders extends GelatoBase {
  constructor(
    chainId: ChainId,
    signerOrProvider?: Signer | Provider,
    handler?: Handler
  );
  submitStopLimitOrder(
    inputToken: string,
    outputToken: string,
    inputAmount: BigNumberish,
    maxReturn: BigNumberish,
    checkAllowance?: boolean,
    overrides?: Overrides
  ): Promise<ContractTransaction>;
  encodeStopLimitOrderSubmission(
    inputToken: string,
    outputToken: string,
    inputAmount: BigNumberish,
    maxReturn: BigNumberish,
    owner: string,
    checkAllowance?: boolean
  ): Promise<TransactionData>;
  encodeStopLimitOrderSubmissionWithSecret(
    inputToken: string,
    outputToken: string,
    inputAmount: BigNumberish,
    maxReturnToBeParsed: BigNumberish,
    owner: string,
    checkAllowance?: boolean
  ): Promise<TransactionDataWithSecret>;
  getOpenStopLimitOrders(owner: string): Promise<StopLimitOrder[]>;
  getStopLimitOrders(owner: string): Promise<StopLimitOrder[]>;
  getExecutedStopLimitOrders(owner: string): Promise<StopLimitOrder[]>;
  getCancelledStopLimitOrders(owner: string): Promise<StopLimitOrder[]>;
}

export declare type ChainId = 1 | 3 | 56 | 137 | 250 | 43114;

export type Handler =
  | "spookyswap"
  | "spookyswap_stoplimit"
  | "uniswap"
  | "uniswap_stoplimit"
  | "quickswap"
  | "quickswap_stoplimit"
  | "spiritswap"
  | "spiritswap_stoplimit"
  | "bombswap"
  | "polydex"
  | "cafeswap"
  | "pancakeswap"
  | "pancakeswap_stoplimit"
  | "traderjoe"
  | "traderjoe_stoplimit"
  | "defyswap"
  | "pangolin"
  | "pangolin_stoplimit";

export interface TransactionData {
  to: string;
  data: BytesLike;
  value: BigNumberish;
}

export interface TransactionDataWithSecret {
  payload: TransactionData;
  secret: string;
  witness: string;
  order: PartialOrder;
}

export interface Order {
  id: string;
  owner: string;
  inputToken: string;
  outputToken: string;
  minReturn: string;
  maxReturn?: string;
  adjustedMinReturn: string;
  module: string;
  witness: string;
  secret: string;
  inputAmount: string;
  vault: string;
  bought: string | null;
  auxData: string | null;
  status: string;
  createdTxHash: string;
  executedTxHash: string | null;
  cancelledTxHash: string | null;
  blockNumber: string;
  createdAt: string;
  updatedAt: string;
  updatedAtBlock: string;
  updatedAtBlockHash: string;
  data: string;
  inputData: string;
  handler: string | null;
  isExpired: boolean;
}

export interface PartialOrder {
  owner: string;
  inputToken: string;
  outputToken: string;
  minReturn: string;
  adjustedMinReturn: string;
  module: string;
  witness: string;
  secret: string;
  inputAmount: string;
  data: string;
  inputData: string;
  handler?: string;
}

Need help?

Reach out to us on Telegram, Discord or Twitter

版本与发布记录

@gelatonetwork/limit-orders-lib 共发布 112 个版本,首次发布于 2021/04/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.5.0latest2022/08/224 年前
4.4.02022/07/294 年前
4.3.02022/07/204 年前
4.2.2-4.3.0-alpha.7.0next2022/06/054 年前
4.3.0-alpha.62022/05/054 年前
4.3.0-alpha.52022/05/044 年前

其他发布通道:next → 4.2.2-4.3.0-alpha.7.0,beta → 2.0.0-beta-3,paraswap-beta-0 → 2.1.0-0。

关于 @gelatonetwork/limit-orders-lib 的常见问题

@gelatonetwork/limit-orders-lib 是什么?

Library for creating limit orders via Gelato

如何安装 @gelatonetwork/limit-orders-lib?

在项目目录运行 npm install @gelatonetwork/limit-orders-lib,或使用 pnpm add / yarn add / bun add。

@gelatonetwork/limit-orders-lib 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@gelatonetwork/limit-orders-lib 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@gelatonetwork/limit-orders-lib 可以商用吗?

@gelatonetwork/limit-orders-lib 使用 GPL-3.0-only 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以 GPL-3.0 兼容许可开源,并附带专利与反锁定条款。仅内部使用不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@gelatonetwork/limit-orders-lib 的下载量有多少?

根据 npm 官方下载统计,@gelatonetwork/limit-orders-lib 在 2026/10/16 – 2026/11/14 这 30 天内被下载 8,413 次,日均约 280 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gelatonetwork/limit-orders-lib 还在维护吗?

最新版本 4.5.0 发布于 2022/08/22(4 年前);至今共发布 112 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/04/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。