Skip to content

Commit

Permalink
Voting Connector: Add actions and rewards functionality unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PJColombo committed Nov 12, 2021
1 parent 095d1ac commit 0f56ea6
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
85 changes: 81 additions & 4 deletions packages/connect-voting/src/__test__/votes.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { BigNumber } from 'ethers'
import { App, connect } from '@aragon/connect'
import { VotingConnectorTheGraph, Vote, Cast } from '../../src'
import { VoteStatus } from '../types'
import { Action, VoteStatus } from '../types'

const VOTING_SUBGRAPH_URL =
'https://api.thegraph.com/subgraphs/name/aragon/aragon-voting-rinkeby-staging'
'https://api.thegraph.com/subgraphs/name/aragon/aragon-voting-rinkeby'

const VOTING_APP_ADDRESS = '0x37187b0f2089b028482809308e776f92eeb7334e'
// For testing action-fetching functionality
const ACTIONS_ORG_ADDRESS = "0x63210F64Ef6F4EBB9727F6c5665CB8bbeDf20480"
const ACTIONS_VOTING_APP_ADDRESS = '0x9943c2f55d91308b8ddbc58b6e70d1774ace125e'

describe('when connecting to a voting app', () => {
let connector: VotingConnectorTheGraph
let votes: Vote[]

beforeAll(() => {
connector = new VotingConnectorTheGraph({
Expand All @@ -19,8 +26,6 @@ describe('when connecting to a voting app', () => {
})

describe('when querying for all the votes of a voting app', () => {
let votes: Vote[]

beforeAll(async () => {
votes = await connector.votesForApp(VOTING_APP_ADDRESS, 1000, 0)
})
Expand Down Expand Up @@ -113,4 +118,76 @@ describe('when connecting to a voting app', () => {
})
})
})

describe("when looking at the vote's actions of a voting app", () => {
let installedApps: App[]
let signallingVoteActions: Action[]
let onlyCodeExecutionActions: Action[]
let voteActions: Action[]

beforeAll(async () => {
const org = await connect(ACTIONS_ORG_ADDRESS, "thegraph", { network: 4 })
installedApps = await org.apps()
connector = new VotingConnectorTheGraph({
subgraphUrl: VOTING_SUBGRAPH_URL,
})
votes = await connector.votesForApp(ACTIONS_VOTING_APP_ADDRESS, 1000, 0)

onlyCodeExecutionActions = votes[0].getActions(installedApps)
signallingVoteActions = votes[1].getActions(installedApps)
voteActions = votes[4].getActions(installedApps)
})

test("should return a list of actions", () => {
expect(voteActions.length).toBeGreaterThan(0)
})

test("shouldn't return anything when getting actions from a signaling vote", () => {
expect(signallingVoteActions).toEqual([])
})

test("shouldn't return rewards when getting actions from a vote that only executes code", () => {
const action = onlyCodeExecutionActions[0]
expect(action.rewards).toEqual([])
})

describe("when looking at a specific vote's action and reward", () => {
let rewardedAction: Action

beforeAll(() => {
rewardedAction = voteActions[0]
})

test('should have a valid to (target contract address)', () => {
expect(rewardedAction.to).toEqual("0xcaa6526abb106ff5c5f937e3ea9499243df86b7a")
})

test("should have a valid fnData", () => {
const { abi, notice, params, roles, sig } = rewardedAction.fnData!

expect(Object.keys(abi!).length).toBeGreaterThan(0)
expect(notice).toEqual("Create a new payment of `@tokenAmount(_token, _amount)` to `_receiver` for '`_reference`'")
expect(params!).toEqual(['0x0000000000000000000000000000000000000000',
'0x9943c2f55D91308B8DDbc58B6e70d1774AcE125e', BigNumber.from('3000000000000000000'), "\"reference\""])
expect(roles).toEqual([ 'CREATE_PAYMENTS_ROLE' ])
expect(sig).toEqual("newImmediatePayment(address,address,uint256,string)")
})

test("should have a list of rewards", () => {
expect(rewardedAction.rewards.length).toBeGreaterThan(0)
})

test("should have a valid reward", () => {
const reward = rewardedAction.rewards[0]
const { amount, token, receiver } = reward
const ETH = '0x0000000000000000000000000000000000000000'

expect(amount).toEqual('3000000000000000000')
expect(token).toEqual(ETH)
expect(receiver).toEqual('0x9943c2f55D91308B8DDbc58B6e70d1774AcE125e')
})
})

})

})
6 changes: 4 additions & 2 deletions packages/connect-voting/src/helpers/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { utils } from 'ethers'
import { AppMethod } from "@aragon/connect"
import { Reward } from '../types'

export const getRewards = (appId: string, fnData: AppMethod): Reward[] | undefined => {
export const getRewards = (appId: string, fnData: AppMethod): Reward[] => {
const {params, sig } = fnData

if (!params || !params.length) {
return
return []
}

const sigHash = utils.id(sig).substring(0, 10)
Expand Down Expand Up @@ -41,4 +41,6 @@ export const getRewards = (appId: string, fnData: AppMethod): Reward[] | undefin
}
break
}

return []
}
3 changes: 2 additions & 1 deletion packages/connect-voting/src/models/Vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export default class Vote {
// Check targetApp again to avoid typescript undefined warnings below
if (!targetApp || !fnData) {
return {
to
to,
rewards: []
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/connect-voting/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface Reward {
export interface Action {
to: Address
fnData?: AppMethod
rewards?: Reward[]
rewards: Reward[]
}

export interface IVotingConnector {
Expand Down

0 comments on commit 0f56ea6

Please sign in to comment.