Giter Site home page Giter Site logo

mwamedacen / paraswap-dex-lib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paraswap/paraswap-dex-lib

0.0 0.0 0.0 3.56 MB

DexLib is a library used by ParaSwap backend to integrate with decentralized exchanges. This library enables external DEX developers to integrate their DEX with ParaSwap by creating pull requests to this repository.

License: GNU General Public License v3.0

Shell 0.01% JavaScript 0.01% TypeScript 99.98%

paraswap-dex-lib's Introduction

ParaSwap DexLib

DexLib is a library used by ParaSwap backend to integrate with decentralized exchanges. This library enables external DEX developers to integrate their DEX with ParaSwap by creating pull requests to this repository.

Steps to add new exchange to DexLib

  1. Fork paraswap-dex-lib to your organization or personal account.
  2. Clone the repository locally and create an branch with appropriate name (eg: feature/super-dex)
  3. Install the repository dependencies using:
yarn install
  1. Initialize the DEX integration. The DEX name should be in param-case:
yarn init-integration <your-dex-name>

You can find template code for newly integrated Dex in src/dex/<your-dex-name>

  1. Complete the template code by filling the functions implementations. Template code is highly documented which should help you build the implementation. You should look into existing DEX implementation in src/dex/ to understand the interfaces. Please refer below for detailed explainations and good practices.

  2. Add <your-dex-name> to Dexes list in src/dex/index.ts

  3. Complete the test templates (All files with src/dex/<your-dex-name>/*.test.ts). Each DEX implementation should have thorough testing. We have multiple kinds of tests each dex must have. You can refer to Writing Tests for detailed explaination. You can run all the tests using

yarn test-integration <your-dex-name>
  1. Create a PR(pull-request) from your feature branch to DexLib master. The PR must contain brief explanation about the DEX background, pricing logic, links to existing documentation, important contract addresses, and anything you think could help us review your code faster.

Understanding the event based pricing approach

One of the most important features of ParaSwap is to serve prices efficiently and quickly. As the number of requests grow, and blockchain get faster, it's not feasible to perform fullnode rpc calls to hundreds of pools for every pricing request. To solve this issue ParaSwap uses a novel event based pricing approach which allows to create pricing without performing the fullnode calls everytime. Event are triggers released by the smart contract when certain changes happen. External services can easily subscribe to these events by websocket connection to a fullnode rpc.

To follow ParaSwap's event based approach DEX should fetch the required on chain state to do pricing once, subscribe to events, update state when events are released, and on price request only use in memory state to create prices.To make the whole event based approach easy to implement and optimized on backend, most of the implementation logic is abstracted and developers have to only obey a simple interface.

TODO: explain the blockmanager and stateful event subscriber

Good Practices for DEX integration

  • Fullnode calls are expensive. An integration should minimize the number of fullnode rpc calls by following Event based Pricing
  • Use Multicall. Instead of performing each RPC calls individually they should be batched together into multicalls.
  • Contract & Interface instances should be reused. There will be cases when you would be lured to create Contract/Interface objects for every dex pool to perform on-chain calls. This can lead to memory leaks in DEXes like UniswapV2 where there can be arbitrarily many pools. In such cases all the pools with the same abi should reuse the same Contract/Interface object.

Writing Testing

  • Integration Tests (<you-dex-name>-integration.test.ts): Tests the basic validity of the integration like prices are valid, obeys the limit pools, etc.
  • Events Unit Tests (<you-dex-name>-events.test.ts): Unit tests the event based system. This is done by fetching the state on-chain before the event, manually pushing the block logs to the event subsriber, comparing the local state with on-chain state.
  • E2E Tests (<you-dex-name>-e2e.test.ts): End to end test the intergation which involves pricing, transaction building and simulating the transaction on chain using tenderly fork simulations. E2E tests use the Tenderly fork api. Please add the following to your .env file:
TENDERLY_TOKEN=Find this under Account>Settings>Authorization.
TENDERLY_ACCOUNT_ID=Your Tenderly account name.
TENDERLY_PROJECT=Name of a Tenderly project you have created in your
dashboard.

paraswap-dex-lib's People

Contributors

verisana avatar louis-amas avatar shresthagrawal avatar mwamedacen avatar colonelj avatar nduchak avatar fabiencoutant avatar bogdoslavik avatar johngrantuk avatar borelien avatar mounibec avatar christiandahdah avatar 0xpercival avatar radioshackcreator avatar alexshchur avatar fb-alexcq avatar jomarip avatar ptpdefi 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.