ramda#sort JavaScript Examples

The following examples show how to use ramda#sort. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: processOrderBookSnapshot.js    From binance-websocket-examples with MIT License 6 votes vote down vote up
processOrderBookSnapshot = (orderBookData, snapshotOrderbook) => {
  const { lastUpdateId, bids, asks } = snapshotOrderbook;

  // clean the order that is out of date
  const cleanOutOfDateOrder = (order) => order[2] > lastUpdateId;
  orderBookData.bid = filter(cleanOutOfDateOrder, orderBookData.bid);
  orderBookData.ask = filter(cleanOutOfDateOrder, orderBookData.ask);

  // append the updateId into snapshotOrderbook
  const snapshotOrders = appendUpdatedId(lastUpdateId, asks, bids);
  const compareValueFn = cond([
    [equals('ask'), () => (a, b) => (new Big(a[0])).minus(b[0])],
    [equals('bid'), () => (a, b) => (new Big(b[0])).minus(a[0])],
  ]);

  const validateValue = (v) => Big(v[0]);
  orderBookData.bid = uniqBy(validateValue, [...snapshotOrders[1], ...orderBookData.bid])
    .sort(compareValueFn('bid'), orderBookData.bid);

  orderBookData.ask = uniqBy(validateValue, [...snapshotOrders[0], ...orderBookData.ask])
    .sort(compareValueFn('ask'), orderBookData.ask);


  return orderBookData;
}
Example #2
Source File: processOrderBookUpdate.js    From binance-websocket-examples with MIT License 6 votes vote down vote up
processOrderBookUpdate = (data, bid, ask) => {
  const validateValue = (v) => Big(v[0]);
  const compareValueFn = cond([
    [equals('ask'), () => (a, b) => (new Big(a[0])).minus(b[0])],
    [equals('bid'), () => (a, b) => (new Big(b[0])).minus(a[0])],
  ]);
  const purgeEmptyVolume = (v) => Big(v[1]).gt(0);

  data.bid = uniqBy(validateValue, [...bid, ...data.bid])
    .sort(compareValueFn('bid'), data.bid)
    .filter(purgeEmptyVolume, data.bid);

  data.ask = uniqBy(validateValue, [...ask, ...data.ask])
    .sort(compareValueFn('ask'), data.ask)
    .filter(purgeEmptyVolume, data.ask);
  return data;
}
Example #3
Source File: createApiInstance.js    From cross-chain-realitio-proxy with MIT License 4 votes vote down vote up
export default async function createApiInstance() {
  const [batchSend, homeProxy, realitio] = await Promise.all([
    createBatchSend(web3, HOME_TX_BATCHER_CONTRACT_ADDRESS),
    getContract(web3, HomeProxy.abi, process.env.HOME_PROXY_CONTRACT_ADDRESS),
    getContract(web3, RealitioInterface.abi, process.env.HOME_REALITIO_CONTRACT_ADDRESS),
  ]);

  async function getBlockNumber() {
    return Number(await web3.eth.getBlockNumber());
  }

  async function getChainId() {
    return Number(await web3.eth.getChainId());
  }

  async function getRequest({ questionId, requester }) {
    const [request, chainId] = await Promise.all([
      homeProxy.methods.requests(questionId, requester).call(),
      getChainId(),
    ]);

    return {
      ...request,
      chainId,
      questionId,
      requester,
      status: Number(request.status),
    };
  }

  async function getNotifiedRequests({ fromBlock = 0, toBlock = "latest" } = {}) {
    const events = await getPastEvents(homeProxy, "RequestNotified", { fromBlock, toBlock });

    const allNotifiedRequests = await P.allSettled(
      map(
        ({ returnValues }) =>
          getRequest({
            questionId: returnValues._questionID,
            requester: returnValues._requester,
          }),
        events
      )
    );

    const onlyFulfilled = compose(filter(propEq("status", "fulfilled")), map(prop("value")));

    return into([], onlyFulfilled, allNotifiedRequests);
  }

  async function getRejectedRequests({ fromBlock = 0, toBlock = "latest" } = {}) {
    const events = await getPastEvents(homeProxy, "RequestRejected", { fromBlock, toBlock });

    const allRejectedRequests = await P.allSettled(
      map(
        ({ returnValues }) =>
          getRequest({
            questionId: returnValues._questionID,
            requester: returnValues._requester,
          }),
        events
      )
    );
    const onlyFulfilled = compose(filter(propEq("status", "fulfilled")), map(prop("value")));

    return into([], onlyFulfilled, allRejectedRequests);
  }

  async function handleNotifiedRequest(request) {
    await batchSend({
      args: [request.questionId, request.requester],
      method: homeProxy.methods.handleNotifiedRequest,
      to: homeProxy.options.address,
    });
    return request;
  }

  async function handleChangedAnswer(request) {
    await batchSend({
      args: [request.questionId, request.requester],
      method: homeProxy.methods.handleChangedAnswer,
      to: homeProxy.options.address,
    });
    return request;
  }

  async function handleFinalizedQuestion(request) {
    await batchSend({
      args: [request.questionId, request.requester],
      method: homeProxy.methods.handleFinalizedQuestion,
      to: homeProxy.options.address,
    });
    return request;
  }

  async function handleRejectedRequest(request) {
    await batchSend({
      args: [request.questionId, request.requester],
      method: homeProxy.methods.handleRejectedRequest,
      to: homeProxy.options.address,
    });
    return request;
  }

  async function reportArbitrationAnswer(request) {
    const { questionId } = request;
    const { historyHash, answerOrCommitmentID, answerer } = await _getLatestAnswerParams(questionId);

    await batchSend({
      args: [questionId, historyHash, answerOrCommitmentID, answerer],
      method: homeProxy.methods.reportArbitrationAnswer,
      to: homeProxy.options.address,
    });
    return request;
  }

  async function _getLatestAnswerParams(questionId) {
    const answers = await getPastEvents(realitio, "LogNewAnswer", {
      filter: {
        question_id: questionId,
      },
    });

    if (answers.length == 0) {
      throw new Error(`Question ${questionId} was never answered`);
    }

    const byMostRecentBlock = descend(prop("blockNumber"));
    const sortedAnswers = sort(byMostRecentBlock, answers);

    const latestAnswer = sortedAnswers[0].returnValues;
    const previousAnswer = sortedAnswers[1]?.returnValues;

    return {
      historyHash: previousAnswer?.history_hash ?? ZERO_HASH,
      answerOrCommitmentID: latestAnswer.answer,
      answerer: latestAnswer.user,
    };
  }

  return {
    getBlockNumber,
    getChainId,
    getNotifiedRequests,
    getRejectedRequests,
    getRequest,
    handleChangedAnswer,
    handleFinalizedQuestion,
    handleNotifiedRequest,
    handleRejectedRequest,
    reportArbitrationAnswer,
  };
}