UNPKG

@smartinvoicexyz/graphql

Version:

Unified source for helpers and schema used across the GraphQL services within the Smart Invoice protocol.

123 lines (122 loc) 4.29 kB
/* eslint-disable camelcase */ import { logDebug } from '@smartinvoicexyz/shared'; import { isAddress } from 'viem'; import { fetchTypedQuery } from './client'; import { _SubgraphErrorPolicy_, Deposit_orderBy, Dispute_orderBy, OrderDirection, Release_orderBy, Resolution_orderBy, Verified_orderBy, } from './zeus'; export const fetchInvoice = async (chainId, queryAddress) => { const address = isAddress(queryAddress) && queryAddress; if (!address || !chainId) return null; const data = await fetchTypedQuery(chainId)({ invoice: [ { id: address, subgraphError: _SubgraphErrorPolicy_.allow, }, { id: true, address: true, token: true, amounts: true, client: true, clientReceiver: true, createdAt: true, currentMilestone: true, deposits: [ { // first: 10, orderBy: Deposit_orderBy.timestamp, orderDirection: OrderDirection.desc, }, { id: true, txHash: true, sender: true, amount: true, timestamp: true, }, ], ipfsHash: true, disputes: [ { orderBy: Dispute_orderBy.timestamp, orderDirection: OrderDirection.desc, }, { id: true, details: true, disputeFee: true, disputeId: true, disputeToken: true, ipfsHash: true, sender: true, timestamp: true, txHash: true, }, ], invoiceType: true, isLocked: true, network: true, provider: true, providerReceiver: true, releases: [ { orderBy: Release_orderBy.timestamp, orderDirection: OrderDirection.desc, }, { id: true, amount: true, milestone: true, timestamp: true, txHash: true, }, ], released: true, resolutionRate: true, resolutions: [ { orderBy: Resolution_orderBy.timestamp, orderDirection: OrderDirection.desc, }, { id: true, clientAward: true, ipfsHash: true, providerAward: true, resolutionDetails: true, resolutionFee: true, resolver: true, resolverType: true, timestamp: true, txHash: true, }, ], resolver: true, resolverType: true, terminationTime: true, total: true, verified: [ { orderBy: Verified_orderBy.client, orderDirection: OrderDirection.asc, }, { id: true, client: true, }, ], version: true, }, ], }, { fetchPolicy: 'network-only' }); logDebug({ invoice: data?.invoice, address }); if (!data?.invoice) return null; const invoice = { ...data.invoice, // @ts-expect-error amounts is an array of BigInt amounts: data.invoice.amounts, }; return invoice; };