import { fetchGraphData } from './fetch-graph'; import { COIN_MOVING_HISTORY_QUERY_BY_USER, COIN_MOVING_HISTORY_QUERY_BY_TOKEN, COIN_MOVING_HISTORY_QUERY_BY_SENDER_AND_RECIPIENT, COIN_MOVING_HISTORY_QUERY_BY_TOKEN_AND_TYPE, CoinMovingHistoriesEntity, transform, } from './query/coin-moving'; import { PrexApiService } from '../api'; import { TransferHistoryQuery } from '../types'; export async function queryTransferHistory( apiService: PrexApiService, query: TransferHistoryQuery, offset: number, limit: number ) { if ('user' in query) { const body = await fetchGraphData( apiService, 'coinMovingHistories', COIN_MOVING_HISTORY_QUERY_BY_USER, { offset, limit, address: query.user.toLowerCase(), } ); const history = body as CoinMovingHistoriesEntity; return transform(history); } else if ('token' in query && 'movingType' in query) { const body = await fetchGraphData( apiService, 'coinMovingHistories', COIN_MOVING_HISTORY_QUERY_BY_TOKEN_AND_TYPE, { offset, limit, token: query.token.toLowerCase(), type: query.movingType, } ); const history = body as CoinMovingHistoriesEntity; return transform(history); } else if ('token' in query) { const body = await fetchGraphData( apiService, 'coinMovingHistories', COIN_MOVING_HISTORY_QUERY_BY_TOKEN, { offset, limit, token: query.token.toLowerCase(), } ); const history = body as CoinMovingHistoriesEntity; return transform(history); } else if ('me' in query && 'other' in query) { const body = await fetchGraphData( apiService, 'coinMovingHistories', COIN_MOVING_HISTORY_QUERY_BY_SENDER_AND_RECIPIENT, { offset, limit, me: query.me.toLowerCase(), other: query.other.toLowerCase(), } ); const history = body as CoinMovingHistoriesEntity; return transform(history); } else { throw new Error('Invalid query'); } }