@melonproject/exchange-aggregator

Aggregates orderbooks across various exchanges.

Usage no npm install needed!

<script type="module">
  import melonprojectExchangeAggregator from 'https://cdn.skypack.dev/@melonproject/exchange-aggregator';
</script>

README

Exchange aggregator

Installation

# With yarn
yarn add @melonproject/exchange-aggregator

# With npm
npm install @melonproject/exchange-aggregator

Usage example

Please note, that not all asset pairs are available on all exchanges.

import { createToken } from '@melonproject/token-math';
import { Network, exchanges, aggregateOrders, createOrderbook } from '@melonproject/exchange-aggregator';

const options = {
  network: Network.MAINNET,
  pair: {
    // The token pair is used for constructing the proper
    // requests (e.g. endpoint urls) and calculations in
    // token-math.
    base: createToken('ZRX', undefined, 18),
    quote: createToken('WETH', undefined, 18),
  },
};

const environment = constructEnvironment({
  endpoint: 'ws://localhost:8545',
});

const promises = await Promise.all([
  exchanges.radarrelay.fetch(options),
  // Some exchanges require extra configuration.
  exchanges.oasisdex.fetch({
    ...options,
    environment,
  }),
]);

// Concatenate the api responses into a single array.
const orders = [].concat(...promises);
// Aggregate all orders into a sorted orderbook with
// cummulative volume information.
const orderbook = createOrderbook(options, orders);