Skip to content

Commit

Permalink
feature: Support rewards
Browse files Browse the repository at this point in the history
- top-level queries rewards and rewards_aggregate
- StakePool.rewards and StakePool.rewards_aggregate
  • Loading branch information
rhyslbw committed Oct 9, 2020
1 parent 46dd937 commit 46cc878
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 5 deletions.
23 changes: 20 additions & 3 deletions packages/api-cardano-db-hasura/hasura/project/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,28 @@
select: rewards
custom_column_names: {}
object_relationships:
- name: transaction
- name: earnedIn
using:
manual_configuration:
remote_table:
schema: public
name: Transaction
name: Epoch
column_mapping:
tx_id: id
epochNo: number
- name: stakePool
using:
manual_configuration:
remote_table:
schema: public
name: StakePool
column_mapping:
pool_hash: hash
select_permissions:
- role: cardano-graphql
permission:
columns:
- address
- amount
filter: {}
limit: 100
allow_aggregations: true
Expand Down Expand Up @@ -279,6 +288,14 @@
name: StakePoolRetirement
column_mapping:
hash: pool_hash
- name: rewards
using:
manual_configuration:
remote_table:
schema: public
name: Reward
column_mapping:
hash: pool_hash
select_permissions:
- role: cardano-graphql
permission:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ FROM epoch;
CREATE VIEW "Reward" AS
SELECT
reward.amount AS "amount",
reward.id AS "id",
(
SELECT stake_address.view
FROM stake_address
WHERE stake_address.id = reward.addr_id
) AS "address",
reward.tx_id AS "tx_id"
reward.epoch_no AS "epochNo",
( SELECT pool_hash.hash FROM pool_hash WHERE pool_hash.id = reward.pool_id ) AS "pool_hash"
FROM reward;

CREATE VIEW "SlotLeader" AS
Expand Down
69 changes: 69 additions & 0 deletions packages/api-cardano-db-hasura/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ type Query {
offset: Int
where: Epoch_bool_exp
): Epoch_aggregate!
rewards (
limit: Int
order_by: [Reward_order_by!]
offset: Int
where: Reward_bool_exp
): [Reward]!
rewards_aggregate (
limit: Int
order_by: [Reward_order_by!]
offset: Int
where: Reward_bool_exp
): Reward_aggregate!
stakeDeregistrations (
limit: Int
order_by: [StakeDeregistration_order_by!]
Expand Down Expand Up @@ -180,6 +192,55 @@ input Relay_bool_exp {
port: Int_comparison_exp
}

type Reward {
address: String!
amount: String!
earnedIn: Epoch!
stakePool: StakePool!
}

type Reward_aggregate {
aggregate: Reward_aggregate_fields
}

type Reward_aggregate_fields {
avg: Reward_avg_fields!
count: String!
max: Reward_max_fields!
min: Reward_min_fields!
sum: Reward_sum_fields!
}

type Reward_avg_fields {
amount: Float
}

type Reward_max_fields {
amount: String
}

type Reward_min_fields {
amount: String
}

type Reward_sum_fields {
amount: String
}

input Reward_bool_exp {
address: text_comparison_exp
amount: text_comparison_exp
earnedIn: Epoch_bool_exp
stakePool: StakePool_bool_exp
}

input Reward_order_by {
address: text_comparison_exp
amount: text_comparison_exp
earnedIn: Epoch_order_by
stakePool: StakePool_order_by
}

type StakeDeregistration {
address: String!
transaction: Transaction!
Expand Down Expand Up @@ -228,6 +289,13 @@ type StakePool {
relays: [Relay]
retirements: [StakePoolRetirement]
rewardAddress: String!
rewards: [Reward]!
rewards_aggregate (
limit: Int
order_by: [Reward_order_by!]
offset: Int
where: Reward_bool_exp
): Reward_aggregate!
updatedIn: Transaction!
url: URL
}
Expand Down Expand Up @@ -255,6 +323,7 @@ input StakePool_bool_exp {
relays: Relay_bool_exp
retirements: StakePoolRetirement_bool_exp
rewardAddress: text_comparison_exp
rewards: Relay_bool_exp
url: text_comparison_exp
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
query allRewardsAggregateFields {
rewards_aggregate {
aggregate {
avg {
amount
}
count
max {
amount
}
min {
amount
}
sum {
amount
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
query rewardsForAddress (
$limit: Int!
$where: Reward_bool_exp
) {
rewards (limit: $limit, where: $where) {
address
amount
stakePool {
hash
}
earnedIn {
number
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ query allStakePoolFields (
inEffectFrom
}
rewardAddress
rewards {
address
amount
earnedIn {
number
}
}
rewards_aggregate {
aggregate {
count
sum {
amount
}
}
}
updatedIn {
hash
}
Expand Down
20 changes: 20 additions & 0 deletions packages/api-cardano-db-hasura/src/executableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ export async function buildSchema (hasuraClient: HasuraClient) {
schema: hasuraSchema
})
},
rewards: (_root, args, context, info) => {
return delegateToSchema({
args,
context,
fieldName: 'rewards',
info,
operation: 'query',
schema: hasuraSchema
})
},
rewards_aggregate: (_root, args, context, info) => {
return delegateToSchema({
args,
context,
fieldName: 'rewards_aggregate',
info,
operation: 'query',
schema: hasuraSchema
})
},
stakeDeregistrations: (_root, args, context, info) => {
return delegateToSchema({
args,
Expand Down
41 changes: 41 additions & 0 deletions packages/api-cardano-db-hasura/test/rewards.query.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable camelcase */
import path from 'path'

import { DocumentNode } from 'graphql'
import util from '@cardano-graphql/util'
import { TestClient } from '@cardano-graphql/util-dev'
import { buildClient } from './util'

function loadQueryNode (name: string): Promise<DocumentNode> {
return util.loadQueryNode(path.resolve(__dirname, '..', 'src', 'example_queries', 'rewards'), name)
}

describe('rewards', () => {
let client: TestClient
beforeAll(async () => {
client = await buildClient('http://localhost:3100', 'http://localhost:8090', 5442)
})

it('can return details for rewards scoped to an address', async () => {
const result = await client.query({
query: await loadQueryNode('rewardsForAddress'),
variables: { limit: 5, where: { address: { _eq: 'stake1uyp6rqthh9n7y4rng75tz85t7djy7hny35fw27say5mfxygq3er9k' } } }
})
const { rewards } = result.data
expect(rewards.length).toBe(5)
expect(rewards[0].stakePool.hash).toBeDefined()
expect(rewards[0].earnedIn.number).toBeDefined()
})

it('can return aggregated data on all delegations', async () => {
const result = await client.query({
query: await loadQueryNode('aggregateRewards')
})
const { rewards_aggregate } = result.data
expect(parseInt(rewards_aggregate.aggregate.avg.amount)).toBeDefined()
expect(parseInt(rewards_aggregate.aggregate.max.amount)).toBeDefined()
expect(parseInt(rewards_aggregate.aggregate.min.amount)).toBeDefined()
expect(parseInt(rewards_aggregate.aggregate.sum.amount)).toBeDefined()
expect(parseInt(rewards_aggregate.aggregate.count)).toBeGreaterThan(30000)
})
})
3 changes: 3 additions & 0 deletions packages/api-cardano-db-hasura/test/stakePool.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ describe('stakePools', () => {
expect(stakePools[0].relays).toBeDefined()
expect(stakePools[0].retirements).toBeDefined()
expect(stakePools[0].rewardAddress.slice(0, 5)).toBe('stake')
expect(stakePools[0].rewards).toBeDefined()
expect(stakePools[0].rewards_aggregate.aggregate.count).toBeDefined()
expect(stakePools[0].rewards_aggregate.aggregate.sum.amount).toBeDefined()
expect(stakePools[0].updatedIn.hash).toBeDefined()
expect(stakePools[0].url).toBeDefined()
})
Expand Down

0 comments on commit 46cc878

Please sign in to comment.