Skip to content

Commit

Permalink
feature: improved typing
Browse files Browse the repository at this point in the history
  • Loading branch information
heueristik committed Jul 20, 2023
1 parent 2ab0e5c commit 0b9889e
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 53 deletions.
5 changes: 3 additions & 2 deletions packages/contracts/deploy/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {promises as fs} from 'fs';
import {ethers} from 'hardhat';
import {Contract} from 'ethers';
import {SignerWithAddress} from '@nomiclabs/hardhat-ethers/signers';
import {HardhatRuntimeEnvironment} from 'hardhat/types';
import IPFS from 'ipfs-http-client';

Expand All @@ -14,7 +15,7 @@ import {
PluginRepo__factory,
} from '../typechain';
import {VersionCreatedEvent} from '../typechain/PluginRepo';
import {SignerWithAddress} from '@nomiclabs/hardhat-ethers/signers';
import {PluginRepoRegisteredEvent} from '../typechain/PluginRepoRegistry';

// TODO: Add support for L2 such as Arbitrum. (https://discuss.ens.domains/t/register-using-layer-2/688)
// Make sure you own the ENS set in the {{NETWORK}}_ENS_DOMAIN variable in .env
Expand Down Expand Up @@ -223,7 +224,7 @@ export async function createPluginRepo(
);
await tx.wait();

const event = await findEventTopicLog(
const event = await findEventTopicLog<PluginRepoRegisteredEvent>(
tx,
PluginRepoRegistry__factory.createInterface(),
'PluginRepoRegistered'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,14 @@ const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
address: installationPreparedEvent.plugin,
args: [
await multisigSetup.implementation(),
await Multisig__factory.createInterface().encodeFunctionData(
'initialize',
[
managingDAOAddress,
approvers,
{
onlyListed: listedOnly,
minApprovals: minApprovals,
},
]
),
Multisig__factory.createInterface().encodeFunctionData('initialize', [
managingDAOAddress,
approvers,
{
onlyListed: listedOnly,
minApprovals: minApprovals,
},
]),
],
});

Expand Down
40 changes: 23 additions & 17 deletions packages/contracts/test/framework/dao/dao-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import {
IERC165__factory,
PluginRepoRegistry__factory,
} from '../../../typechain';
import {DAORegisteredEvent} from '../../../typechain/DAORegistry';
import {InstallationPreparedEvent} from '../../../typechain/PluginSetupProcessor';
import {PluginRepoRegisteredEvent} from '../../../typechain/PluginRepoRegistry';

import {deployENSSubdomainRegistrar} from '../../test-utils/ens';
import {deployPluginSetupProcessor} from '../../test-utils/plugin-setup-processor';
Expand Down Expand Up @@ -110,17 +113,18 @@ async function extractInfoFromCreateDaoTx(tx: any): Promise<{
helpers: any;
permissions: any;
}> {
const daoRegisteredEvent = await findEventTopicLog(
const daoRegisteredEvent = await findEventTopicLog<DAORegisteredEvent>(
tx,
DAORegistry__factory.createInterface(),
EVENTS.DAORegistered
);

const installationPreparedEvent = await findEventTopicLog(
tx,
PluginSetupProcessor__factory.createInterface(),
EVENTS.InstallationPrepared
);
const installationPreparedEvent =
await findEventTopicLog<InstallationPreparedEvent>(
tx,
PluginSetupProcessor__factory.createInterface(),
EVENTS.InstallationPrepared
);

return {
dao: daoRegisteredEvent.args.dao,
Expand Down Expand Up @@ -247,7 +251,7 @@ describe('DAOFactory: ', function () {
'0x00',
'0x00'
);
const event = await findEventTopicLog(
const event = await findEventTopicLog<PluginRepoRegisteredEvent>(
tx,
PluginRepoRegistry__factory.createInterface(),
EVENTS.PluginRepoRegistered
Expand Down Expand Up @@ -584,11 +588,12 @@ describe('DAOFactory: ', function () {
'0x11',
'0x11'
);
const pluginRepoRegisteredEvent = await findEventTopicLog(
tx,
PluginRepoRegistry__factory.createInterface(),
EVENTS.PluginRepoRegistered
);
const pluginRepoRegisteredEvent =
await findEventTopicLog<PluginRepoRegisteredEvent>(
tx,
PluginRepoRegistry__factory.createInterface(),
EVENTS.PluginRepoRegistered
);
adminPluginRepoAddress = pluginRepoRegisteredEvent.args.pluginRepo;

// create dao with admin plugin.
Expand All @@ -611,11 +616,12 @@ describe('DAOFactory: ', function () {
);
tx = await daoFactory.createDao(daoSettings, [adminPluginInstallation]);
{
const installationPreparedEvent = await findEventTopicLog(
tx,
PluginSetupProcessor__factory.createInterface(),
EVENTS.InstallationPrepared
);
const installationPreparedEvent =
await findEventTopicLog<InstallationPreparedEvent>(
tx,
PluginSetupProcessor__factory.createInterface(),
EVENTS.InstallationPrepared
);

const adminFactory = new Admin__factory(signers[0]);
adminPlugin = adminFactory.attach(
Expand Down
23 changes: 13 additions & 10 deletions packages/contracts/test/framework/plugin/plugin-setup-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
PluginCloneableSetupV2Mock__factory,
PluginCloneableSetupV1MockBad__factory,
} from '../../../typechain';
import {PluginRepoRegisteredEvent} from '../../../typechain/PluginRepoRegistry';

import {deployENSSubdomainRegistrar} from '../../test-utils/ens';
import {deployNewDAO, ZERO_BYTES32} from '../../test-utils/dao';
Expand Down Expand Up @@ -245,11 +246,12 @@ describe('Plugin Setup Processor', function () {
buildMetadata
);

const PluginRepoRegisteredEvent1 = await findEventTopicLog(
tx,
PluginRepoRegistry__factory.createInterface(),
EVENTS.PluginRepoRegistered
);
const PluginRepoRegisteredEvent1 =
await findEventTopicLog<PluginRepoRegisteredEvent>(
tx,
PluginRepoRegistry__factory.createInterface(),
EVENTS.PluginRepoRegistered
);
const PluginRepo = new PluginRepo__factory(signers[0]);
repoU = PluginRepo.attach(PluginRepoRegisteredEvent1.args.pluginRepo);

Expand All @@ -268,11 +270,12 @@ describe('Plugin Setup Processor', function () {
buildMetadata
);

const PluginRepoRegisteredEvent2 = await findEventTopicLog(
tx,
PluginRepoRegistry__factory.createInterface(),
EVENTS.PluginRepoRegistered
);
const PluginRepoRegisteredEvent2 =
await findEventTopicLog<PluginRepoRegisteredEvent>(
tx,
PluginRepoRegistry__factory.createInterface(),
EVENTS.PluginRepoRegistered
);
repoC = PluginRepo.attach(PluginRepoRegisteredEvent2.args.pluginRepo);
await repoC.createVersion(1, setupCV1Bad.address, EMPTY_DATA, EMPTY_DATA);
await repoC.createVersion(1, setupCV2.address, EMPTY_DATA, EMPTY_DATA);
Expand Down
8 changes: 5 additions & 3 deletions packages/contracts/test/plugins/governance/admin/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
DAO__factory,
} from '../../../../typechain';
import {ProposalCreatedEvent} from '../../../../typechain/Admin';
import {ExecutedEvent} from '../../../../typechain/IDAO';

// Permissions
const EXECUTE_PROPOSAL_PERMISSION_ID = ethers.utils.id(
Expand Down Expand Up @@ -227,7 +228,7 @@ describe('Admin', function () {
expect(event.args.proposalId).to.equal(nextExpectedProposalId);
});

it("calls the DAO's execute function correctly with proposalId", async () => {
it.only("calls the DAO's execute function correctly with proposalId", async () => {
{
const proposalId = 0;
const allowFailureMap = 1;
Expand All @@ -237,7 +238,8 @@ describe('Admin', function () {
dummyActions,
allowFailureMap
);
const event = await findEventTopicLog(

const event = await findEventTopicLog<ExecutedEvent>(
tx,
DAO__factory.createInterface(),
DAO_EVENTS.EXECUTED
Expand All @@ -258,7 +260,7 @@ describe('Admin', function () {

const tx = await plugin.executeProposal(dummyMetadata, dummyActions, 0);

const event = await findEventTopicLog(
const event = await findEventTopicLog<ExecutedEvent>(
tx,
DAO__factory.createInterface(),
DAO_EVENTS.EXECUTED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
ProposalCreatedEvent,
ProposalExecutedEvent,
} from '../../../../../typechain/AddresslistVoting';
import {ExecutedEvent} from '../../../../../typechain/IDAO';

export const addresslistVotingInterface = new ethers.utils.Interface([
'function initialize(address,tuple(uint8,uint32,uint32,uint64,uint256),address[])',
Expand Down Expand Up @@ -1026,7 +1027,7 @@ describe('AddresslistVoting', function () {
.connect(signers[6])
.vote(id, VoteOption.Abstain, true);
{
const event = await findEventTopicLog(
const event = await findEventTopicLog<ExecutedEvent>(
tx,
DAO__factory.createInterface(),
DAO_EVENTS.EXECUTED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
ProposalCreatedEvent,
ProposalExecutedEvent,
} from '../../../../../typechain/TokenVoting';
import {ExecutedEvent} from '../../../../../typechain/IDAO';

export const tokenVotingInterface = new ethers.utils.Interface([
'function initialize(address,tuple(uint8,uint32,uint32,uint64,uint256),address)',
Expand Down Expand Up @@ -1382,7 +1383,7 @@ describe('TokenVoting', function () {
.connect(signers[6])
.vote(id, VoteOption.Yes, true);
{
const event = await findEventTopicLog(
const event = await findEventTopicLog<ExecutedEvent>(
tx,
DAO__factory.createInterface(),
DAO_EVENTS.EXECUTED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,7 @@ describe('Multisig', function () {
// `tryExecution` is turned on but the vote is not decided yet
let tx = await multisig.connect(signers[1]).approve(id, true);
await expect(
findEventTopicLog(
findEventTopicLog<ExecutedEvent>(
tx,
DAO__factory.createInterface(),
DAO_EVENTS.EXECUTED
Expand All @@ -1182,7 +1182,7 @@ describe('Multisig', function () {
// `tryExecution` is turned off and the vote is decided
tx = await multisig.connect(signers[2]).approve(id, false);
await expect(
findEventTopicLog(
findEventTopicLog<ExecutedEvent>(
tx,
DAO__factory.createInterface(),
DAO_EVENTS.EXECUTED
Expand All @@ -1192,7 +1192,7 @@ describe('Multisig', function () {
// `tryEarlyExecution` is turned on and the vote is decided
tx = await multisig.connect(signers[3]).approve(id, true);
{
const event = await findEventTopicLog(
const event = await findEventTopicLog<ExecutedEvent>(
tx,
DAO__factory.createInterface(),
DAO_EVENTS.EXECUTED
Expand Down
7 changes: 6 additions & 1 deletion packages/contracts/test/upgrade/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {UPGRADE_PERMISSIONS} from '../test-utils/permissions';
import {findEventTopicLog} from '../../utils/event';
import {readImplementationValueFromSlot} from '../../utils/storage';
import {getInterfaceID} from '../test-utils/interfaces';
import {UpgradedEvent} from '../../typechain/DAO';

let signers: SignerWithAddress[];
let DAO_old: DAO_V1_0_0__factory;
Expand Down Expand Up @@ -89,7 +90,11 @@ describe('DAO Upgrade', function () {

// Check the emitted implementation.
const emittedImplementation = (
await findEventTopicLog(upgradeTx, DAO_old.interface, 'Upgraded')
await findEventTopicLog<UpgradedEvent>(
upgradeTx,
DAO_old.interface,
'Upgraded'
)
).args.implementation;
expect(emittedImplementation).to.equal(daoCurrentImplementaion.address);

Expand Down
11 changes: 7 additions & 4 deletions packages/contracts/utils/event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {ContractTransaction} from 'ethers';
import {Interface, LogDescription} from 'ethers/lib/utils';

export async function findEvent<T>(tx: ContractTransaction, eventName: string) {
export async function findEvent<T>(
tx: ContractTransaction,
eventName: string
): Promise<T> {
const receipt = await tx.wait();
const event = (receipt.events || []).find(event => event.event === eventName);

Expand All @@ -12,18 +15,18 @@ export async function findEvent<T>(tx: ContractTransaction, eventName: string) {
return event as unknown as T;
}

export async function findEventTopicLog(
export async function findEventTopicLog<T>(
tx: ContractTransaction,
iface: Interface,
eventName: string
): Promise<LogDescription> {
): Promise<LogDescription & (T | LogDescription)> {
const receipt = await tx.wait();
const topic = iface.getEventTopic(eventName);
const log = receipt.logs.find(x => x.topics[0] === topic);
if (!log) {
throw new Error(`No logs found for the topic of event "${eventName}".`);
}
return iface.parseLog(log);
return iface.parseLog(log) as LogDescription & (T | LogDescription);
}

export async function filterEvents(tx: any, eventName: string) {
Expand Down

0 comments on commit 0b9889e

Please sign in to comment.