HomePhabricator

[ecash-agora] Do not include offers that have not been bought or canceled in…

Description

[ecash-agora] Do not include offers that have not been bought or canceled in historicOffers

Summary: We only want to parse historicOffers that have been bought or canceled. Ignore offers that have not been bought or canceled.

Test Plan:
existing CI tests to confirm we do not break anything

Note: this function is not currently fully tested.

Run this script with this version of ecash-agora and get no errors. (If you run this script not on this version, will get errors).

import { ChronikClient } from 'chronik-client';
import { Agora, AgoraOffer, AgoraHistoryResult } from 'ecash-agora';

// Note this must be a server that indexes agora partial offers
const chronik = new ChronikClient(['https://chronik-native3.fabien.cash']);
// Initialize new Agora chronik wrapper at app startup
const agora = new Agora(chronik);

const CHRONIK_MAX_PAGE_SIZE = 200;

export const getAllAgoraHistory = async (agora: Agora, tokenId: string) => {
    const agoraHistoryFirstPage: AgoraHistoryResult =
        await agora.historicOffers({
            type: 'TOKEN_ID',
            tokenId,
            table: 'HISTORY',
            pageSize: CHRONIK_MAX_PAGE_SIZE,
        });
    const { offers, numPages } = agoraHistoryFirstPage;

    // Get historic offers from all pages
    // We start with i = 1 because we already have the data from page 0
    const historicOffersPromises = [];
    for (let i = 1; i < numPages; i += 1) {
        historicOffersPromises.push(
            new Promise<AgoraOffer[]>((resolve, reject) => {
                agora
                    .historicOffers({
                        type: 'TOKEN_ID',
                        tokenId,
                        table: 'HISTORY',
                        page: i,
                        pageSize: CHRONIK_MAX_PAGE_SIZE,
                    })
                    .then(
                        result => {
                            resolve(result.offers);
                        },
                        err => {
                            reject(err);
                        },
                    );
            }),
        );
    }

    // Get rest of historicOffers using Promise.all() to execute requests in parallel
    const restOfHistoricOffers = await Promise.all(historicOffersPromises);
    // Flatten so we have an array of tx objects, and not an array of arrays of tx objects
    const flatHistoricOffers = restOfHistoricOffers.flat();
    // Combine with the first page
    for (const offer of flatHistoricOffers) {
        offers.push(offer);
    }
    console.log(`returning ${offers.length} offers`);

    return offers;
};

const getHistoricOffers = async (agora: Agora, tokenId: string) => {
    let historicOffers;
    try {
        historicOffers = await getAllAgoraHistory(agora, tokenId);
        console.log(`Fetched ${historicOffers.length} historic offers`);
    } catch (err) {
        console.log(`Error`, err);
    }
};

// XECX
const tokenId =
    'c67bf5c2b6d91cfb46a5c1772582eff80d88686887be10aa63b0945479cf4ed4';
getHistoricOffers(agora, tokenId);

Reviewers: #bitcoin_abc, emack

Reviewed By: #bitcoin_abc, emack

Subscribers: emack

Differential Revision: https://reviews.bitcoinabc.org/D17630

Details

Provenance
bytesofmanAuthored on Tue, Feb 4, 00:16
bytesofmanPushed on Wed, Feb 5, 12:12
Reviewer
Restricted Project
Differential Revision
D17630: [ecash-agora] Do not include offers that have not been bought or canceled in historicOffers
Parents
rABC9b78e7fbb03e: bugfix: correct is_snapshot_cs in VerifyDB
Branches
Unknown
Tags
Unknown