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

Add earnings volume endpoint #331

Merged
merged 3 commits into from
May 23, 2024
Merged
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
61 changes: 60 additions & 1 deletion src/server-extension/resolvers/StateResolver/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -73,4 +73,63 @@ export class StateResolver {
processorState(@Root() state: ProcessorState): ProcessorState {
return state
}

@Query(() => EarningStatsOutput)
async totalJoystreamEarnings(): Promise<EarningStatsOutput> {
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(amm_buy.price_paid, '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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'ChannelPaymentMadeEventData',
'ChannelPaymentMadeEventData',
'ChannelRewardClaimedEventData',
'ChannelRewardClaimedAndWithdrawnEventData',

I think you should have to more event types for total rewards volume

'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
}
Loading