import { gql } from 'graphql-request' import { type Address } from 'viem' import type { ClientWithPns } from '../../contracts/consts.js' import { createSubgraphClient } from './client.js' export type GetReferralStatsParameters = { /** The referral address for */ address: Address } export type GetReferralStatsReturnType = { id: string, count: number, commission: string } const query = gql` query getReferralStats($address: ID!) { referrer(id: $address) { id count commission } } ` type SubgraphResult = { referrer?: { id: string, count: number, commission: string } } /** * Gets referral statistics of an address from the subgraph. * @param client - {@link ClientWithPns} * @param parameters - {@link GetReferralStatsParameters} * @returns Referral statistics detail. {@link GetReferralStatsReturnType} * * @example * import { createPublicClient, http } from 'viem' * import { mainnet } from 'viem/chains' * import { addPnsContracts } from '@pnsdomains/pnsjs' * import { getReferralStats } from '@pnsdomains/pnsjs/subgraph' * * const client = createPublicClient({ * chain: addPnsContracts(mainnet), * transport: http(), * }) * const result = await getReferralStats(client, { address: '0xb6E040C9ECAaE172a89bD561c5F73e1C48d28cd9' }) * // { id: '0xb6E040C9ECAaE172a89bD561c5F73e1C48d28cd9', commission: '0', count: 0} */ const getReferralStats = async ( client: ClientWithPns, { address }: GetReferralStatsParameters, ): Promise => { const subgraphClient = createSubgraphClient({ client }) const result = await subgraphClient.request(query, { address, }) return result?.referrer || { id: address, commission: '0', count: 0 } } export default getReferralStats