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

Create rewards and exec data points in subgraph. #337

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions packages/connect-voting/src/models/Call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CallData } from '../types'

export default class Call {
readonly id: string
readonly vote: string
readonly contract: string
readonly calldata: string

constructor(data: CallData) {
this.id = data.id
this.vote = data.vote.id
this.contract = data.contract
this.calldata = data.calldata
}
}
17 changes: 17 additions & 0 deletions packages/connect-voting/src/models/Reward.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { RewardData } from '../types'

export default class Reward {
readonly id: string
readonly vote: string
readonly token: string
readonly to: string
readonly amount: string

constructor(data: RewardData) {
this.id = data.id
this.vote = data.vote.id
this.token = data.token
this.to = data.to
this.amount = data.amount
}
}
31 changes: 30 additions & 1 deletion packages/connect-voting/src/models/Vote.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { SubscriptionCallback, SubscriptionResult } from '@aragon/connect-types'
import { subscription } from '@aragon/connect-core'
import { IVotingConnector, VoteData } from '../types'
import { IVotingConnector, VoteData, RewardData } from '../types'
import Cast from './Cast'
import Reward from './Reward'
import Call from './Call'

export default class Vote {
#connector: IVotingConnector
Expand All @@ -20,6 +22,7 @@ export default class Vote {
readonly nay: string
readonly votingPower: string
readonly script: string
readonly spec: string

constructor(data: VoteData, connector: IVotingConnector) {
this.#connector = connector
Expand All @@ -38,6 +41,7 @@ export default class Vote {
this.nay = data.nay
this.votingPower = data.votingPower
this.script = data.script
this.spec = data.spec
}

async casts({ first = 1000, skip = 0 } = {}): Promise<Cast[]> {
Expand All @@ -52,4 +56,29 @@ export default class Vote {
this.#connector.onCastsForVote(this.id, first, skip, callback)
)
}

async rewards({ first = 1000, skip = 0 } = {}): Promise<Reward[]> {
return this.#connector.rewardsForVote(this.id, first, skip)
}

onRewards(
{ first = 1000, skip = 0 } = {},
callback?: SubscriptionCallback<Reward[]>
): SubscriptionResult<Reward[]> {
return subscription<Reward[]>(callback, (callback) =>
this.#connector.onRewardsForVote(this.id, first, skip, callback)
)
}
async calls({ first = 1000, skip = 0 } = {}): Promise<Call[]> {
return this.#connector.callsForVote(this.id, first, skip)
}

onCalls(
{ first = 1000, skip = 0 } = {},
callback?: SubscriptionCallback<Call[]>
): SubscriptionResult<Call[]> {
return subscription<Call[]>(callback, (callback) =>
this.#connector.onCallsForVote(this.id, first, skip, callback)
)
}
}
56 changes: 55 additions & 1 deletion packages/connect-voting/src/thegraph/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { GraphQLWrapper, QueryResult } from '@aragon/connect-thegraph'
import { IVotingConnector } from '../types'
import Vote from '../models/Vote'
import Cast from '../models/Cast'
import Reward from '../models/Reward'
import * as queries from './queries'
import { parseVotes, parseCasts } from './parsers'
import { parseVotes, parseCasts, parseRewards, parseCalls } from './parsers'
import Call from '../models/Call'

export function subgraphUrlFromChainId(chainId: number) {
if (chainId === 1) {
Expand Down Expand Up @@ -98,4 +100,56 @@ export default class VotingConnectorTheGraph implements IVotingConnector {
(result: QueryResult) => parseCasts(result)
)
}

async rewardsForVote(
vote: string,
first: number,
skip: number
): Promise<Reward[]> {
return this.#gql.performQueryWithParser(
queries.REWARDS_FOR_VOTE('query'),
{ vote, first, skip },
(result: QueryResult) => parseRewards(result)
)
}

onRewardsForVote(
vote: string,
first: number,
skip: number,
callback: SubscriptionCallback<Reward[]>
): SubscriptionHandler {
return this.#gql.subscribeToQueryWithParser<Reward[]>(
queries.REWARDS_FOR_VOTE('subscription'),
{ vote, first, skip },
callback,
(result: QueryResult) => parseRewards(result)
)
}

async callsForVote(
vote: string,
first: number,
skip: number
): Promise<Call[]> {
return this.#gql.performQueryWithParser(
queries.CALLS_FOR_VOTE('query'),
{ vote, first, skip },
(result: QueryResult) => parseCalls(result)
)
}

onCallsForVote(
vote: string,
first: number,
skip: number,
callback: SubscriptionCallback<Call[]>
): SubscriptionHandler {
return this.#gql.subscribeToQueryWithParser<Call[]>(
queries.CALLS_FOR_VOTE('subscription'),
{ vote, first, skip },
callback,
(result: QueryResult) => parseCalls(result)
)
}
}
27 changes: 27 additions & 0 deletions packages/connect-voting/src/thegraph/parsers/calls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ErrorUnexpectedResult } from 'packages/connect-core/dist/cjs'
import { QueryResult } from 'packages/connect-thegraph/dist/cjs'
import Call from '../../models/Call'
import { CallData } from '../../types'

export function parseCalls(result: QueryResult): Call[] {
const calls = result.data.calls

if (!calls) {
throw new ErrorUnexpectedResult('Unable to parse calls.')
}

const datas = calls.map(
(call: any): CallData => {
return {
id: call.id,
vote: call.vote.id,
contract: call.contract,
calldata: call.calldata,
}
}
)

return datas.map((data: CallData) => {
return new Call(data)
})
}
2 changes: 2 additions & 0 deletions packages/connect-voting/src/thegraph/parsers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { parseVotes } from './votes'
export { parseCasts } from './casts'
export { parseRewards } from './rewards'
export { parseCalls } from './calls'
28 changes: 28 additions & 0 deletions packages/connect-voting/src/thegraph/parsers/rewards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ErrorUnexpectedResult } from 'packages/connect-core/dist/cjs'
import { QueryResult } from 'packages/connect-thegraph/dist/cjs'
import Reward from '../../models/Reward'
import { RewardData } from '../../types'

export function parseRewards(result: QueryResult): Reward[] {
const rewards = result.data.rewards

if (!rewards) {
throw new ErrorUnexpectedResult('Unable to parse rewards.')
}

const datas = rewards.map(
(reward: any): RewardData => {
return {
id: reward.id,
vote: reward.vote.id,
token: reward.token,
to: reward.to,
amount: reward.amount,
}
}
)

return datas.map((data: RewardData) => {
return new Reward(data)
})
}
31 changes: 31 additions & 0 deletions packages/connect-voting/src/thegraph/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const ALL_VOTES = (type: string) => gql`
nay
votingPower
script
spec
contract
calldata
}
}
`
Expand All @@ -46,6 +49,7 @@ export const CASTS_FOR_VOTE = (type: string) => gql`
nay
votingPower
script
spec
}
voter {
id
Expand All @@ -57,3 +61,30 @@ export const CASTS_FOR_VOTE = (type: string) => gql`
}
}
`

export const REWARDS_FOR_VOTE = (type: string) => gql`
${type} Rewards($vote: ID!, $first: Int!, $skip: Int!) {
rewards(where: { vote: $vote }, first: $first, skip: $skip) {
id
vote {
id
}
token
amount
to
}
}
`

export const CALLS_FOR_VOTE = (type: string) => gql`
${type} Calls($vote: ID!, $first: Int!, $skip: Int!) {
calls(where: { vote: $vote }, first: $first, skip: $skip) {
id
vote {
id
}
contract
calldata
}
}
`
32 changes: 32 additions & 0 deletions packages/connect-voting/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
} from '@aragon/connect-types'
import Vote from './models/Vote'
import Cast from './models/Cast'
import Reward from './models/Reward'
import Call from './models/Call'

export interface VoteData {
id: string
Expand All @@ -20,6 +22,22 @@ export interface VoteData {
nay: string
votingPower: string
script: string
spec: string
}

export interface RewardData {
id: string
vote: VoteData
token: string
to: string
amount: string
}

export interface CallData {
id: string
vote: VoteData
contract: string
calldata: string
}

export interface CastData {
Expand Down Expand Up @@ -52,4 +70,18 @@ export interface IVotingConnector {
skip: number,
callback: SubscriptionCallback<Cast[]>
): SubscriptionHandler
rewardsForVote(vote: string, first: number, skip: number): Promise<Reward[]>
onRewardsForVote(
vote: string,
first: number,
skip: number,
callback: SubscriptionCallback<Reward[]>
): SubscriptionHandler
callsForVote(vote: string, first: number, skip: number): Promise<Call[]>
onCallsForVote(
vote: string,
first: number,
skip: number,
callback: SubscriptionCallback<Call[]>
): SubscriptionHandler
}
18 changes: 18 additions & 0 deletions packages/connect-voting/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@ type Vote @entity {
votingPower: BigInt!
script: String!
voteNum: BigInt!
spec: BigInt!
castVotes: [Cast!] @derivedFrom(field: "vote")
rewards: [Reward!] @derivedFrom(field: "vote")
calls: [Call!] @derivedFrom(field: "vote")
}

type Reward @entity {
id: ID!
vote: Vote!
token: Bytes!
amount: BigInt!
to: Bytes!
}

type Call @entity {
id: ID!
vote: Vote!
contract: Bytes!
calldata: Bytes!
}

type Cast @entity {
Expand Down
Loading