GithubHelp home page GithubHelp logo

chouandy / limit-orders-lib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gelatodigital/limit-orders-lib

0.0 1.0 0.0 1.92 MB

License: GNU General Public License v3.0

TypeScript 99.17% Shell 0.83%

limit-orders-lib's Introduction

npm version gelatodigital

Gelato Limit Order SDK

Place limit buy and sell orders on Uniswap v2 using Gelato Network.

Mainnet & Ropsten Demo

Gelato Limit orders

Installation

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

or

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

Getting Started (using ethers.js, but also works with web3.js)

  1. Create the limit order payload
import { getLimitOrderPayload } from "@gelatonetwork/limit-orders-lib";

// Supported networks: Mainnet = 1; Ropsten = 3
const chainId = 1;

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

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

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

// Minimum amount of outTOken which the users wants to receive back
const minimumReturn = ethers.utils.parseEther("1", "18");

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

// If 1 ETH is above 2000 DAI, then the user will buy 1 ETH if it is less than 2000 DAI
// If 1 ETH is below 2000 DAI, then the user will buy 1 ETH if it is greater than 2000 DAI
const txData = await getLimitOrderPayload(
  chainId,
  inToken,
  outToken,
  inputAmount,
  minimumReturn,
  userAddress
);
  1. Place the order by sending the transaction
// Use Front End web3 provider library of your choice
import Web3Modal from "web3modal";
import { useWeb3React } from "@web3-react/core";

// If using Web3Modal
const web3Modal = new Web3Modal();
const web3ModalProvider = await web3Modal.connect();
const provider = new ethers.providers.Web3Provider(web3ModalProvider);

// If using Web3React
const { library } = useWeb3React();
const provider = new ethers.providers.Web3Provider(library.provider);

// Submit Limit Order
const tx = await provider.getSigner().sendTransaction(txData);

// Print out hash
console.log(`Hash: ${tx.hash}`);

// Wait for tx to be mined
await tx.wait();
  1. Fetch all orders
import { getAllOpenOrders } from "@gelatonetwork/limit-orders-lib";

const openOrders = await getAllOpenOrders(userAddress, chainId);
  1. Cancel pending order
import { getAllOpenOrders } from "@gelatonetwork/limit-orders-lib";
import { getCancelLimitOrderPayload } from "@gelatonetwork/limit-orders-lib";

const openOrders = await getAllOpenOrders(userAddress, chainId);
if (openOrders.length === 0) throw new Error("No orders found");
const order = openOrders[0];

const { module, inputToken, outputToken, minReturn, owner, witness } = order;

const txData = await getCancelLimitOrderPayload(
  chainId,
  inputToken,
  outputToken,
  minReturn,
  owner,
  witness
);

const tx = await provider.getSigner().sendTransaction(txData);

await tx.wait();
  1. Get successfully executed orders
import { getExecutedOrders } from "@gelatonetwork/limit-orders-lib";

const executedOrders = await getExecutedOrders(userAddress, chainId);
  1. Get cancelled orders
import { getCancelledOrders } from "@gelatonetwork/limit-orders-lib";

const cancelledOrders = await getCancelledOrders(userAddress, chainId);

Types

See dist/src/index.d.ts

export interface Order {
  id: number;
  inputToken: string;
  outputToken: string;
  inputAmount: string;
  minReturn: string;
  bought: string;
  status: string;
  cancelledTxHash: BytesLike;
  executedTxHash: BytesLike;
  updatedAt: string;
}

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

export interface TransactionDataWithSecret {
  txData: TransactionData;
  secret: string;
  witness: string;
}

Need help?

Reach out to us on Telegram, Discord or Twitter

limit-orders-lib's People

Contributors

chouandy avatar gauddel avatar gitpusha avatar hilmarx avatar pedrocrvz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.