From 9eef55e2abcbd69c839a9cd6c37fcc158d8ac83c Mon Sep 17 00:00:00 2001 From: ikprk Date: Tue, 14 May 2024 13:52:59 +0200 Subject: [PATCH 1/3] Add earnings volume endpoint --- .../resolvers/StateResolver/index.ts | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/src/server-extension/resolvers/StateResolver/index.ts b/src/server-extension/resolvers/StateResolver/index.ts index 6aaa3b9cd..8dc452d7d 100644 --- a/src/server-extension/resolvers/StateResolver/index.ts +++ b/src/server-extension/resolvers/StateResolver/index.ts @@ -1,4 +1,4 @@ -import { Resolver, Root, Subscription } from 'type-graphql' +import { Resolver, Root, Subscription, Query, ObjectType, Field } from 'type-graphql' import type { EntityManager } from 'typeorm' import { ProcessorState } from './types' import _, { isObject } from 'lodash' @@ -73,4 +73,63 @@ export class StateResolver { processorState(@Root() state: ProcessorState): ProcessorState { return state } + + @Query(() => EarningStatsOutput) + async totalJoystreamEarnings(): Promise { + const em = await this.tx() + const result = ( + await em.query< + { + total_rewards_volume: string + total_crt_volume: string + total_nft_volume: string + }[] + >( + ` + SELECT + SUM( + COALESCE(event.data->>'amount', '0')::bigint + ) AS "total_rewards_volume", + SUM( + COALESCE(event.data->>'pricePaid', '0')::bigint + ) AS "total_crt_volume", + SUM( + COALESCE(event.data->>'price', '0')::bigint + COALESCE(winning_bid.amount, 0) + ) AS "total_nft_volume" + FROM + "event" + LEFT JOIN amm_transaction AS amm_buy ON "data"->>'ammMintTransaction' = amm_buy.id + LEFT JOIN bid AS winning_bid ON "data"->>'winningBid' = winning_bid.id + WHERE + "event"."data"->>'isTypeOf' IN ( + 'ChannelPaymentMadeEventData', + 'CreatorTokenMarketMintEventData', + 'NftBoughtEventData', + 'EnglishAuctionSettledEventData', + 'BidMadeCompletingAuctionEventData', + 'OpenAuctionBidAcceptedEventData' + ) + + ` + ) + )[0] + + return { + crtSaleVolume: result.total_crt_volume ?? 0, + nftSaleVolume: result.total_nft_volume ?? 0, + totalRewardsPaid: result.total_rewards_volume ?? 0, + } + } +} + +@ObjectType() +export class EarningStatsOutput { + @Field({ nullable: false }) + crtSaleVolume: string + + @Field({ nullable: false }) + totalRewardsPaid: string + + @Field({ nullable: false }) + nftSaleVolume: string } From 356d9227ceda4f1bf5bcb5309075d9530b600ca3 Mon Sep 17 00:00:00 2001 From: ikprk Date: Fri, 17 May 2024 12:21:12 +0200 Subject: [PATCH 2/3] CR fix v1 --- src/server-extension/resolvers/StateResolver/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server-extension/resolvers/StateResolver/index.ts b/src/server-extension/resolvers/StateResolver/index.ts index 8dc452d7d..e7d54b328 100644 --- a/src/server-extension/resolvers/StateResolver/index.ts +++ b/src/server-extension/resolvers/StateResolver/index.ts @@ -91,7 +91,7 @@ export class StateResolver { COALESCE(event.data->>'amount', '0')::bigint ) AS "total_rewards_volume", SUM( - COALESCE(event.data->>'pricePaid', '0')::bigint + COALESCE(amm_buy.pricePaid, '0')::bigint ) AS "total_crt_volume", SUM( COALESCE(event.data->>'price', '0')::bigint + COALESCE(winning_bid.amount, 0) From 28d6c85f8d2bd27791920228e011c8f5ae6d0846 Mon Sep 17 00:00:00 2001 From: ikprk Date: Wed, 22 May 2024 17:02:58 +0200 Subject: [PATCH 3/3] Correct field name --- src/server-extension/resolvers/StateResolver/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/server-extension/resolvers/StateResolver/index.ts b/src/server-extension/resolvers/StateResolver/index.ts index e7d54b328..4d3c1f303 100644 --- a/src/server-extension/resolvers/StateResolver/index.ts +++ b/src/server-extension/resolvers/StateResolver/index.ts @@ -91,7 +91,7 @@ export class StateResolver { COALESCE(event.data->>'amount', '0')::bigint ) AS "total_rewards_volume", SUM( - COALESCE(amm_buy.pricePaid, '0')::bigint + COALESCE(amm_buy.price_paid, '0')::bigint ) AS "total_crt_volume", SUM( COALESCE(event.data->>'price', '0')::bigint + COALESCE(winning_bid.amount, 0)