Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve traders count #494

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/server-extension/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const issuanceHistory = (assetId: string) => `
date(timestamp)
ORDER BY
date ASC;
`
`;

export const marketParticipants = (ids: number[]) => `
SELECT
Expand All @@ -70,7 +70,7 @@ export const marketParticipants = (ids: number[]) => `
LEFT JOIN
historical_asset ha ON ha.asset_id = ANY (m.outcome_assets)
WHERE
m.market_id in (${ids})
m.market_id IN (${ids})
GROUP BY
m.market_id;
`;
Expand Down Expand Up @@ -111,6 +111,22 @@ export const marketLiquidity = (ids: number[]) => `
SELECT * FROM t4;
`;

export const marketTraders = (ids: number[]) => `
SELECT
m.market_id,
COALESCE(COUNT(DISTINCT hs.account_id), 0) AS traders
FROM
market m
LEFT JOIN
historical_swap hs ON
hs.asset_in LIKE '%'||'['||(m.market_id)::text||','||'%' OR
hs.asset_out LIKE '%'||'['||(m.market_id)::text||','||'%'
WHERE
m.market_id IN (${ids})
GROUP BY
m.market_id;
`;

export const marketVolume = (ids: number[], prices: Map<BaseAsset, number>) => `
SELECT
market_id,
Expand Down Expand Up @@ -255,4 +271,4 @@ export const volumeHistory = (prices: Map<BaseAsset, number>) => `
date
ORDER BY
date ASC;
`
`;
11 changes: 8 additions & 3 deletions src/server-extension/resolvers/marketStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Arg, Field, Int, ObjectType, Query, Resolver } from 'type-graphql';
import type { EntityManager } from 'typeorm';
import { Market } from '../../model/generated';
import { getAssetUsdPrices, mergeByField } from '../helper';
import { marketLiquidity, marketParticipants, marketVolume } from '../query';
import { marketLiquidity, marketParticipants, marketTraders, marketVolume } from '../query';

@ObjectType()
export class MarketStats {
Expand All @@ -15,6 +15,9 @@ export class MarketStats {
@Field(() => BigInt)
liquidity!: bigint;

@Field(() => Int)
traders!: number;

@Field(() => BigInt)
volume!: bigint;

Expand All @@ -32,10 +35,12 @@ export class MarketStatsResolver {
const manager = await this.tx();
const participants = await manager.getRepository(Market).query(marketParticipants(ids));
const liquidity = await manager.getRepository(Market).query(marketLiquidity(ids));
const traders = await manager.getRepository(Market).query(marketTraders(ids));
const volume = await manager.getRepository(Market).query(marketVolume(ids, await getAssetUsdPrices()));

let result = mergeByField(participants, liquidity, 'market_id');
result = mergeByField(result, volume, 'market_id');
const merged1 = mergeByField(liquidity, volume, 'market_id');
const merged2 = mergeByField(traders, participants, 'market_id');
const result = mergeByField(merged1, merged2, 'market_id');
return result;
}
}