import { Address } from 'viem'; import { TransferHistoryItem } from '../../types'; import { decodeMetadata } from '../../utils/metadata'; import { splitAddress } from './utils'; const COIN_MOVING_HISTORY_PARAMS = `movingType amount createdAt id metadata recipient { id name pictureHash } sender { id name pictureHash } token { id } recipientName senderName recipientTokenHolder { balance } txHash`; export const COIN_MOVING_HISTORY_QUERY_BY_USER = `query coinMovingHistories($limit: Int, $offset: Int, $address: Bytes!) { coinMovingHistories( first: $limit skip: $offset orderBy: createdAt orderDirection: desc where: {or: [ {sender: $address}, {recipient: $address} ]} ) { ${COIN_MOVING_HISTORY_PARAMS} } }`; export const COIN_MOVING_HISTORY_QUERY_BY_SENDER_AND_RECIPIENT = `query coinMovingHistories($limit: Int, $offset: Int, $me: Bytes!, $other: Bytes!) { coinMovingHistories( first: $limit skip: $offset orderBy: createdAt orderDirection: desc where: { or: [ { sender: $me, recipient: $other }, { sender: $other, recipient: $me } ] } ) { ${COIN_MOVING_HISTORY_PARAMS} } }`; export const COIN_MOVING_HISTORY_QUERY_BY_TOKEN = `query coinMovingHistories($limit: Int, $offset: Int, $token: Bytes!) { coinMovingHistories( first: $limit skip: $offset orderBy: createdAt orderDirection: desc where: {token: $token} ) { ${COIN_MOVING_HISTORY_PARAMS} } }`; export const COIN_MOVING_HISTORY_QUERY_BY_TOKEN_AND_TYPE = `query coinMovingHistories($limit: Int, $offset: Int, $token: Bytes!, $type: String!) { coinMovingHistories( first: $limit skip: $offset orderBy: createdAt orderDirection: desc where: {token: $token, movingType: $type} ) { ${COIN_MOVING_HISTORY_PARAMS} } }`; export interface CoinMovingHistoriesEntity { coinMovingHistories: { id: string; movingType: string; token: { id: string; }; sender: { id: string; name?: string; pictureHash?: string; }; recipient: { id: string; name?: string; pictureHash?: string; }; recipientName?: string; senderName?: string; recipientTokenHolder?: { balance: string; }; amount: string; metadata: string; txHash: string; createdAt: string; }[]; } export function transform( entities: CoinMovingHistoriesEntity ): TransferHistoryItem[] { return entities.coinMovingHistories.map((entity) => ({ id: entity.id, movingType: entity.movingType, token: entity.token.id as Address, sender: entity.sender.id as Address, recipient: entity.recipient.id as Address, recipientPictureHash: entity.recipient.pictureHash, senderPictureHash: entity.sender.pictureHash, recipientName: entity.recipientName || entity.recipient.name || splitAddress(entity.recipient.id), senderName: entity.senderName || entity.sender.name || splitAddress(entity.sender.id), recipientTokenHolder: entity.recipientTokenHolder ? { balance: BigInt(entity.recipientTokenHolder.balance), } : undefined, amount: BigInt(entity.amount), metadata: decodeMetadata(entity.metadata as `0x${string}`), txHash: entity.txHash, createdAt: Number(entity.createdAt), })); }