From 98e92c986fcae881422190c5677768d54b331dae Mon Sep 17 00:00:00 2001 From: Corey Rice Date: Thu, 5 Oct 2023 18:34:33 -0300 Subject: [PATCH] refactor: break out tests into separate files --- .../tests/integration/accessControlManager.ts | 64 ++++++ .../tests/integration/alpha.ts | 154 +++++++++++++ .../tests/integration/index.ts | 216 +----------------- .../tests/integration/setup.ts | 19 ++ 4 files changed, 240 insertions(+), 213 deletions(-) create mode 100644 subgraphs/venus-governance/tests/integration/accessControlManager.ts create mode 100644 subgraphs/venus-governance/tests/integration/alpha.ts create mode 100644 subgraphs/venus-governance/tests/integration/setup.ts diff --git a/subgraphs/venus-governance/tests/integration/accessControlManager.ts b/subgraphs/venus-governance/tests/integration/accessControlManager.ts new file mode 100644 index 00000000..d4a32766 --- /dev/null +++ b/subgraphs/venus-governance/tests/integration/accessControlManager.ts @@ -0,0 +1,64 @@ +import '@nomiclabs/hardhat-ethers'; +import { expect } from 'chai'; +import { ethers } from 'hardhat'; +import { waitForSubgraphToBeSynced } from 'venus-subgraph-utils'; + +import subgraphClient from '../../subgraph-client/index'; +import { SYNC_DELAY } from './utils/constants'; + +describe('AccessControlManager', function () { + before(async function () { + this.timeout(50000000); // sometimes it takes a long time + + await waitForSubgraphToBeSynced(SYNC_DELAY); + }); + + describe('Permission events', function () { + it('indexes permission granted events', async function () { + const { data } = await subgraphClient.getPermissions(); + + const { permissions } = data!; + expect(permissions.length).to.be.equal(12); + + permissions.forEach(pe => { + expect(pe.type).to.be.equal('GRANTED'); + }); + }); + + it('indexes permission revoked events', async function () { + const accessControlManager = await ethers.getContract('AccessControlManager'); + const tx = await accessControlManager.revokeCallPermission( + ethers.constants.AddressZero, + 'setMinLiquidatableCollateral(uint256)', + ethers.constants.AddressZero, + ); + await tx.wait(); + await waitForSubgraphToBeSynced(SYNC_DELAY); + + const { data } = await subgraphClient.getPermissions(); + + const { permissions } = data!; + expect(permissions.length).to.be.equal(13); + + expect(permissions[0].type).to.be.equal('REVOKED'); + }); + + it('updates a previously created record with a new permission type', async function () { + const accessControlManager = await ethers.getContract('AccessControlManager'); + const tx = await accessControlManager.giveCallPermission( + ethers.constants.AddressZero, + 'setMinLiquidatableCollateral(uint256)', + ethers.constants.AddressZero, + ); + await tx.wait(); + await waitForSubgraphToBeSynced(SYNC_DELAY); + + const { data } = await subgraphClient.getPermissions(); + + const { permissions } = data!; + expect(permissions.length).to.be.equal(13); + + expect(permissions[0].type).to.be.equal('GRANTED'); + }); + }); +}); diff --git a/subgraphs/venus-governance/tests/integration/alpha.ts b/subgraphs/venus-governance/tests/integration/alpha.ts new file mode 100644 index 00000000..11e18614 --- /dev/null +++ b/subgraphs/venus-governance/tests/integration/alpha.ts @@ -0,0 +1,154 @@ +import { mine } from '@nomicfoundation/hardhat-network-helpers'; +import '@nomiclabs/hardhat-ethers'; +import type { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; +import { expect } from 'chai'; +import { Contract } from 'ethers'; +import { ethers } from 'hardhat'; +import { scaleValue, waitForSubgraphToBeSynced } from 'venus-subgraph-utils'; + +import subgraphClient from '../../subgraph-client/index'; +import { SYNC_DELAY } from './utils/constants'; +import { enfranchiseAccount } from './utils/voter'; + +describe('GovernorAlpha', function () { + let signers: SignerWithAddress[]; + let governorAlpha: Contract; + let governorAlpha2: Contract; + + before(async function () { + this.timeout(50000000); // sometimes it takes a long time + governorAlpha = await ethers.getContract('GovernorAlpha'); + governorAlpha2 = await ethers.getContract('GovernorAlpha2'); + signers = await ethers.getSigners(); + + const [_, user1, user2, user3, user4] = signers; + + await enfranchiseAccount(user1, scaleValue(100000, 18)); + await enfranchiseAccount(user2, scaleValue(200000, 18)); + await enfranchiseAccount(user3, scaleValue(300000, 18)); + await enfranchiseAccount(user4, scaleValue(400000, 18)); + await waitForSubgraphToBeSynced(SYNC_DELAY); + }); + + describe('Alpha', function () { + it('indexes created proposal success', async function () { + const [_, _1, _2, _3, user4] = signers; + const callData = ethers.utils.defaultAbiCoder.encode(['address'], [governorAlpha.address]); + + const vip = [ + ['0x939bD8d64c0A9583A7Dcea9933f7b21697ab6396'], // targets + ['0'], // values + ['setPendingAdmin(address)'], // signatures + [callData], // params + 'Test proposal 1', // description + ]; + + const tx = await governorAlpha.connect(user4).propose(...vip); + await tx.wait(1); + + await waitForSubgraphToBeSynced(SYNC_DELAY); + + const { + data: { proposal }, + } = await subgraphClient.getProposalById('1'); + expect(proposal.id).to.be.equal('1'); + expect(proposal.description).to.be.equal('Test proposal 1'); + expect(proposal.status).to.be.equal('PENDING'); + expect(proposal.executionETA).to.be.null; + expect(proposal.targets).to.deep.equal([ + '0x939bD8d64c0A9583A7Dcea9933f7b21697ab6396'.toLowerCase(), + ]); + expect(proposal.values).to.deep.equal(['0']); + expect(proposal.signatures).to.deep.equal(['setPendingAdmin(address)']); + expect(proposal.calldatas).to.deep.equal([callData]); + }); + + it('index for vote cast', async function () { + const [_, user1, user2, user3, user4] = signers; + + const time = Date.now() + 106400; + await ethers.provider.send('evm_setNextBlockTimestamp', [time]); + await mine(1); + + let tx = await governorAlpha.connect(user1).castVote('1', false); + await tx.wait(1); + tx = await governorAlpha.connect(user2).castVote('1', true); + await tx.wait(1); + tx = await governorAlpha.connect(user3).castVote('1', true); + await tx.wait(1); + tx = await governorAlpha.connect(user4).castVote('1', true); + await tx.wait(1); + + await waitForSubgraphToBeSynced(SYNC_DELAY); + + const { + data: { proposal }, + } = await subgraphClient.getProposalById('1'); + expect(proposal.votes.length).to.be.equal(4); + + const { + data: { delegate: delegate1 }, + } = await subgraphClient.getDelegateById(user1.address.toLowerCase()); + + expect(delegate1.votes[0].id).to.equal(`${user1.address.toLowerCase()}-0x1`); + expect(delegate1.votes[0].support).to.equal('AGAINST'); + expect(delegate1.votes[0].votes).to.equal('100000000000000000000000'); + expect(delegate1.proposals).to.deep.equal([]); + + const { + data: { delegate: delegate2 }, + } = await subgraphClient.getDelegateById(user2.address.toLowerCase()); + expect(delegate2.votes[0].id).to.equal(`${user2.address.toLowerCase()}-0x1`); + expect(delegate2.votes[0].support).to.equal('FOR'); + expect(delegate2.votes[0].votes).to.equal('200000000000000000000000'); + expect(delegate2.proposals).to.deep.equal([]); + expect(delegate2.proposals).to.deep.equal([]); + + const { + data: { delegate: delegate4 }, + } = await subgraphClient.getDelegateById(user4.address.toLowerCase()); + expect(delegate4.proposals).to.deep.equal([{ id: '1', __typename: 'Proposal' }]); + }); + }); + + // @TODO Update proposal Status + // @TODO Changing delegates + // @TODO Governance + + describe('Alpha2', function () { + it('indexes created proposals - alpha2', async function () { + const [_, user1] = signers; + await enfranchiseAccount(user1, scaleValue(600000, 18)); + + const callData = ethers.utils.defaultAbiCoder.encode(['address'], [governorAlpha2.address]); + + const vip = [ + ['0x939bD8d64c0A9583A7Dcea9933f7b21697ab6396'], // targets + ['0'], // values + ['setPendingAdmin(address)'], // signatures + [callData], // params + 'Test proposal 21', // description + ]; + + const tx = await governorAlpha2.connect(user1).propose(...vip); + await tx.wait(1); + + await waitForSubgraphToBeSynced(SYNC_DELAY); + + const { + data: { proposal }, + } = await subgraphClient.getProposalById('21'); + + expect(proposal.id).to.be.equal('21'); + expect(proposal.description).to.be.equal('Test proposal 21'); + expect(proposal.status).to.be.equal('PENDING'); + expect(proposal.executionETA).to.be.null; + expect(proposal.targets).to.deep.equal([ + '0x939bD8d64c0A9583A7Dcea9933f7b21697ab6396'.toLowerCase(), + ]); + expect(proposal.values).to.deep.equal(['0']); + expect(proposal.signatures).to.deep.equal(['setPendingAdmin(address)']); + expect(proposal.calldatas).to.deep.equal([callData]); + }); + }); +}); diff --git a/subgraphs/venus-governance/tests/integration/index.ts b/subgraphs/venus-governance/tests/integration/index.ts index 67829416..7a1d32fb 100644 --- a/subgraphs/venus-governance/tests/integration/index.ts +++ b/subgraphs/venus-governance/tests/integration/index.ts @@ -1,213 +1,3 @@ -import { mine } from '@nomicfoundation/hardhat-network-helpers'; -import '@nomiclabs/hardhat-ethers'; -import type { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers'; -import { expect } from 'chai'; -import { Contract } from 'ethers'; -import { ethers } from 'hardhat'; -import { deploy, scaleValue, waitForSubgraphToBeSynced } from 'venus-subgraph-utils'; - -import subgraphClient from '../../subgraph-client/index'; -import { SUBGRAPH_ACCOUNT, SUBGRAPH_NAME, SYNC_DELAY } from './utils/constants'; -import { enfranchiseAccount } from './utils/voter'; - -describe('Governance', function () { - let signers: SignerWithAddress[]; - let governorAlpha: Contract; - let governorAlpha2: Contract; - - before(async function () { - this.timeout(50000000); // sometimes it takes a long time - governorAlpha = await ethers.getContract('GovernorAlpha'); - governorAlpha2 = await ethers.getContract('GovernorAlpha2'); - signers = await ethers.getSigners(); - - const root = `${__dirname}/../..`; - - await deploy({ - root, - packageName: 'venus-governance-subgraph', - subgraphAccount: SUBGRAPH_ACCOUNT, - subgraphName: SUBGRAPH_NAME, - syncDelay: SYNC_DELAY, - }); - - const [_, user1, user2, user3, user4] = signers; - - await enfranchiseAccount(user1, scaleValue(100000, 18)); - await enfranchiseAccount(user2, scaleValue(200000, 18)); - await enfranchiseAccount(user3, scaleValue(300000, 18)); - await enfranchiseAccount(user4, scaleValue(400000, 18)); - await waitForSubgraphToBeSynced(SYNC_DELAY); - }); - - describe('Alpha', function () { - it('indexes created proposal success', async function () { - const [_, _1, _2, _3, user4] = signers; - const callData = ethers.utils.defaultAbiCoder.encode(['address'], [governorAlpha.address]); - - const vip = [ - ['0x939bD8d64c0A9583A7Dcea9933f7b21697ab6396'], // targets - ['0'], // values - ['setPendingAdmin(address)'], // signatures - [callData], // params - 'Test proposal 1', // description - ]; - - const tx = await governorAlpha.connect(user4).propose(...vip); - await tx.wait(1); - - await waitForSubgraphToBeSynced(SYNC_DELAY); - - const { - data: { proposal }, - } = await subgraphClient.getProposalById('1'); - expect(proposal.id).to.be.equal('1'); - expect(proposal.description).to.be.equal('Test proposal 1'); - expect(proposal.status).to.be.equal('PENDING'); - expect(proposal.executionETA).to.be.null; - expect(proposal.targets).to.deep.equal([ - '0x939bD8d64c0A9583A7Dcea9933f7b21697ab6396'.toLowerCase(), - ]); - expect(proposal.values).to.deep.equal(['0']); - expect(proposal.signatures).to.deep.equal(['setPendingAdmin(address)']); - expect(proposal.calldatas).to.deep.equal([callData]); - }); - - it('index for vote cast', async function () { - const [_, user1, user2, user3, user4] = signers; - - const time = Date.now() + 106400; - await ethers.provider.send('evm_setNextBlockTimestamp', [time]); - await mine(1); - - let tx = await governorAlpha.connect(user1).castVote('1', false); - await tx.wait(1); - tx = await governorAlpha.connect(user2).castVote('1', true); - await tx.wait(1); - tx = await governorAlpha.connect(user3).castVote('1', true); - await tx.wait(1); - tx = await governorAlpha.connect(user4).castVote('1', true); - await tx.wait(1); - - await waitForSubgraphToBeSynced(SYNC_DELAY); - - const { - data: { proposal }, - } = await subgraphClient.getProposalById('1'); - expect(proposal.votes.length).to.be.equal(4); - - const { - data: { delegate: delegate1 }, - } = await subgraphClient.getDelegateById(user1.address.toLowerCase()); - - expect(delegate1.votes[0].id).to.equal(`${user1.address.toLowerCase()}-0x1`); - expect(delegate1.votes[0].support).to.equal('AGAINST'); - expect(delegate1.votes[0].votes).to.equal('100000000000000000000000'); - expect(delegate1.proposals).to.deep.equal([]); - - const { - data: { delegate: delegate2 }, - } = await subgraphClient.getDelegateById(user2.address.toLowerCase()); - expect(delegate2.votes[0].id).to.equal(`${user2.address.toLowerCase()}-0x1`); - expect(delegate2.votes[0].support).to.equal('FOR'); - expect(delegate2.votes[0].votes).to.equal('200000000000000000000000'); - expect(delegate2.proposals).to.deep.equal([]); - expect(delegate2.proposals).to.deep.equal([]); - - const { - data: { delegate: delegate4 }, - } = await subgraphClient.getDelegateById(user4.address.toLowerCase()); - expect(delegate4.proposals).to.deep.equal([{ id: '1', __typename: 'Proposal' }]); - }); - }); - - // @TODO Update proposal Status - // @TODO Changing delegates - // @TODO Governance - - describe('Alpha2', function () { - it('indexes created proposals - alpha2', async function () { - const [_, user1] = signers; - await enfranchiseAccount(user1, scaleValue(600000, 18)); - - const callData = ethers.utils.defaultAbiCoder.encode(['address'], [governorAlpha2.address]); - - const vip = [ - ['0x939bD8d64c0A9583A7Dcea9933f7b21697ab6396'], // targets - ['0'], // values - ['setPendingAdmin(address)'], // signatures - [callData], // params - 'Test proposal 21', // description - ]; - - const tx = await governorAlpha2.connect(user1).propose(...vip); - await tx.wait(1); - - await waitForSubgraphToBeSynced(SYNC_DELAY); - - const { - data: { proposal }, - } = await subgraphClient.getProposalById('21'); - - expect(proposal.id).to.be.equal('21'); - expect(proposal.description).to.be.equal('Test proposal 21'); - expect(proposal.status).to.be.equal('PENDING'); - expect(proposal.executionETA).to.be.null; - expect(proposal.targets).to.deep.equal([ - '0x939bD8d64c0A9583A7Dcea9933f7b21697ab6396'.toLowerCase(), - ]); - expect(proposal.values).to.deep.equal(['0']); - expect(proposal.signatures).to.deep.equal(['setPendingAdmin(address)']); - expect(proposal.calldatas).to.deep.equal([callData]); - }); - }); - - describe('Permission events', function () { - it('indexes permission granted events', async function () { - const { data } = await subgraphClient.getPermissions(); - - const { permissions } = data!; - expect(permissions.length).to.be.equal(12); - - permissions.forEach(pe => { - expect(pe.type).to.be.equal('GRANTED'); - }); - }); - - it('indexes permission revoked events', async function () { - const accessControlManager = await ethers.getContract('AccessControlManager'); - const tx = await accessControlManager.revokeCallPermission( - ethers.constants.AddressZero, - 'setMinLiquidatableCollateral(uint256)', - ethers.constants.AddressZero, - ); - await tx.wait(); - await waitForSubgraphToBeSynced(SYNC_DELAY); - - const { data } = await subgraphClient.getPermissions(); - - const { permissions } = data!; - expect(permissions.length).to.be.equal(13); - - expect(permissions[0].type).to.be.equal('REVOKED'); - }); - - it('updates a previously created record with a new permission type', async function () { - const accessControlManager = await ethers.getContract('AccessControlManager'); - const tx = await accessControlManager.giveCallPermission( - ethers.constants.AddressZero, - 'setMinLiquidatableCollateral(uint256)', - ethers.constants.AddressZero, - ); - await tx.wait(); - await waitForSubgraphToBeSynced(SYNC_DELAY); - - const { data } = await subgraphClient.getPermissions(); - - const { permissions } = data!; - expect(permissions.length).to.be.equal(13); - - expect(permissions[0].type).to.be.equal('GRANTED'); - }); - }); -}); +import './accessControlManager'; +import './alpha'; +import './setup'; diff --git a/subgraphs/venus-governance/tests/integration/setup.ts b/subgraphs/venus-governance/tests/integration/setup.ts new file mode 100644 index 00000000..0ac8a1c1 --- /dev/null +++ b/subgraphs/venus-governance/tests/integration/setup.ts @@ -0,0 +1,19 @@ +import { deploy } from 'venus-subgraph-utils'; + +import { SUBGRAPH_ACCOUNT, SUBGRAPH_NAME, SYNC_DELAY } from './utils/constants'; + +describe('Deploy Subgraph', function () { + before(async function () { + this.timeout(50000000); // sometimes it takes a long time + + const root = `${__dirname}/../..`; + + await deploy({ + root, + packageName: 'venus-governance-subgraph', + subgraphAccount: SUBGRAPH_ACCOUNT, + subgraphName: SUBGRAPH_NAME, + syncDelay: SYNC_DELAY, + }); + }); +});