Skip to content

Commit

Permalink
Merge pull request #334 from input-output-hk/feature/rewards
Browse files Browse the repository at this point in the history
Feature/rewards
  • Loading branch information
rhyslbw committed Oct 16, 2020
2 parents f7b2426 + 46cc878 commit 4e84425
Show file tree
Hide file tree
Showing 12 changed files with 215 additions and 24 deletions.
6 changes: 1 addition & 5 deletions config/network/mainnet/cardano-db-sync/config.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
"ByronGenesisFile": "../genesis/byron.json",
"ByronGenesisHash": "5f20df933584822601f9e3f8c024eb5eb252fe8cefb24d1317dc3d432e940ebb",
"EnableLogMetrics": false,
"EnableLogging": true,
"NetworkName": "mainnet",
"Protocol": "Cardano",
"NodeConfigFile": "../cardano-node/config.json",
"RequiresNetworkMagic": "RequiresNoMagic",
"ShelleyGenesisFile": "../genesis/shelley.json",
"ShelleyGenesisHash": "1a3be38bcbb7911969283716ad7aa550250226b76a61fc51cc9a9a35d9276d81",
"defaultBackends": [
"KatipBK"
],
Expand Down
6 changes: 1 addition & 5 deletions config/network/testnet/cardano-db-sync/config.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
{
"ByronGenesisFile": "../genesis/byron.json",
"ByronGenesisHash": "96fceff972c2c06bd3bb5243c39215333be6d56aaf4823073dca31afe5038471",
"EnableLogMetrics": false,
"EnableLogging": true,
"NetworkName": "testnet",
"Protocol": "Cardano",
"NodeConfigFile": "../cardano-node/config.json",
"RequiresNetworkMagic": "RequiresMagic",
"ShelleyGenesisFile": "../genesis/shelley.json",
"ShelleyGenesisHash": "849a1764f152e1b09c89c0dfdbcbdd38d711d1fec2db5dfa0f87cf2737a0eaf4",
"defaultBackends": [
"KatipBK"
],
Expand Down
16 changes: 9 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ services:
max-size: "400k"
max-file: "20"
cardano-db-sync-extended:
image: inputoutput/cardano-db-sync:${CARDANO_DB_SYNC_VERSION:-5.0.1}
image: rhyslbw/cardano-db-sync:${CARDANO_DB_SYNC_VERSION:-3e02b9370a122cef068e55ac2887b41acc7587ed}
command: [
"--config", "/config/config.json",
"--socket-path", "/node-ipc/node.socket"
"--config", "/config/cardano-db-sync/config.json",
"--socket-path", "/node-ipc/node.socket",
"--state-dir", "/data"
]
environment:
- EXTENDED=true
Expand All @@ -61,8 +62,8 @@ services:
- postgres_user
- postgres_db
volumes:
- ./config/network/${NETWORK:-mainnet}/cardano-db-sync:/config
- ./config/network/${NETWORK:-mainnet}/genesis:/genesis
- ./config/network/${NETWORK:-mainnet}:/config
- db-sync-data:/data
- node-ipc:/node-ipc
restart: on-failure
logging:
Expand All @@ -73,7 +74,7 @@ services:
hasura:
build:
context: ./packages/api-cardano-db-hasura/hasura
image: inputoutput/cardano-graphql-hasura:${CARDANO_GRAPHQL_VERSION:-2.1.0}
image: inputoutput/cardano-graphql-hasura:${CARDANO_GRAPHQL_VERSION:-2.2.0}
ports:
- ${HASURA_PORT:-8090}:8080
depends_on:
Expand Down Expand Up @@ -103,7 +104,7 @@ services:
target: server
args:
- NETWORK=${NETWORK:-mainnet}
image: inputoutput/cardano-graphql:${CARDANO_GRAPHQL_VERSION:-2.1.0}
image: inputoutput/cardano-graphql:${CARDANO_GRAPHQL_VERSION:-2.2.0}
environment:
- ALLOW_INTROSPECTION=true
- CACHE_ENABLED=true
Expand Down Expand Up @@ -137,6 +138,7 @@ secrets:
postgres_user:
file: ./config/secrets/postgres_user
volumes:
db-sync-data:
node-db:
node-ipc:
postgres-data:
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
71 changes: 69 additions & 2 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,9 +289,15 @@ 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
withdrawals: [Withdrawal]
}


Expand All @@ -256,8 +323,8 @@ input StakePool_bool_exp {
relays: Relay_bool_exp
retirements: StakePoolRetirement_bool_exp
rewardAddress: text_comparison_exp
rewards: Relay_bool_exp
url: text_comparison_exp
withdrawals: Withdrawal_bool_exp
}

type StakePool_aggregate {
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 4e84425

Please sign in to comment.