From 0f56ea69738b93b8732dc25e8221e571298dd66a Mon Sep 17 00:00:00 2001 From: PJColombo Date: Fri, 12 Nov 2021 19:10:36 +0100 Subject: [PATCH] Voting Connector: Add actions and rewards functionality unit tests --- .../connect-voting/src/__test__/votes.test.ts | 85 ++++++++++++++++++- .../connect-voting/src/helpers/actions.ts | 6 +- packages/connect-voting/src/models/Vote.ts | 3 +- packages/connect-voting/src/types.ts | 2 +- 4 files changed, 88 insertions(+), 8 deletions(-) diff --git a/packages/connect-voting/src/__test__/votes.test.ts b/packages/connect-voting/src/__test__/votes.test.ts index 379c154a..b414c616 100644 --- a/packages/connect-voting/src/__test__/votes.test.ts +++ b/packages/connect-voting/src/__test__/votes.test.ts @@ -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({ @@ -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) }) @@ -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') + }) + }) + + }) + }) diff --git a/packages/connect-voting/src/helpers/actions.ts b/packages/connect-voting/src/helpers/actions.ts index 2a9130fb..33490ceb 100644 --- a/packages/connect-voting/src/helpers/actions.ts +++ b/packages/connect-voting/src/helpers/actions.ts @@ -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) @@ -41,4 +41,6 @@ export const getRewards = (appId: string, fnData: AppMethod): Reward[] | undefin } break } + + return [] } diff --git a/packages/connect-voting/src/models/Vote.ts b/packages/connect-voting/src/models/Vote.ts index d77f9073..6be1806c 100644 --- a/packages/connect-voting/src/models/Vote.ts +++ b/packages/connect-voting/src/models/Vote.ts @@ -71,7 +71,8 @@ export default class Vote { // Check targetApp again to avoid typescript undefined warnings below if (!targetApp || !fnData) { return { - to + to, + rewards: [] } } diff --git a/packages/connect-voting/src/types.ts b/packages/connect-voting/src/types.ts index 197982ae..50d15ecd 100644 --- a/packages/connect-voting/src/types.ts +++ b/packages/connect-voting/src/types.ts @@ -56,7 +56,7 @@ export interface Reward { export interface Action { to: Address fnData?: AppMethod - rewards?: Reward[] + rewards: Reward[] } export interface IVotingConnector {