Skip to content

Commit

Permalink
chore: add util function to check emitted event with specific params
Browse files Browse the repository at this point in the history
  • Loading branch information
0xlucian committed Nov 17, 2023
1 parent 4d354a1 commit a8f2e16
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion simulations/vip-205/simulations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from "chai";
import { parseUnits } from "ethers/lib/utils";
import { ethers } from "hardhat";

import { expectEvents } from "../../src/utils";
import { expectEventWithParams, expectEvents } from "../../src/utils";
import { forking, testVip } from "../../src/vip-framework";
import { checkInterestRate } from "../../src/vip-framework/checks/interestRateModel";
import { vip205 } from "../../vips/vip-205";
Expand Down Expand Up @@ -89,6 +89,7 @@ forking(33570600, () => {
["NewSupplyCap", "NewBorrowCap", "NewMarketInterestRateModel", "NewInterestParams", "Failure"],
[1, 1, 5, 1, 1], // Failure account for fail(Error.TOKEN_INSUFFICIENT_CASH, FailureInfo.REDUCE_RESERVES_CASH_NOT_AVAILABLE).
);
await expectEventWithParams(txResponse, VBEP20_DELEGATOR_ABI, "Failure", ["14", "50", "0"]);
},
});
describe("Post-VIP behaviour", async () => {
Expand Down
29 changes: 29 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,35 @@ export const expectEvents = async (
}
};

export const expectEventWithParams = async (
txResponse: TransactionResponse,
abi: ContractInterface,
expectedEvent: string,
expectedParams: any[], // Array of expected parameters
) => {
const receipt = await txResponse.wait();
const iface = new ethers.utils.Interface(abi);

// Extract the events that match the expected event name
const matchingEvents = receipt.events
.map(event => {
try {
return iface.parseLog(event);
} catch (error) {
return null; // Ignore events that do not match the ABI
}
})
.filter(parsedEvent => parsedEvent && parsedEvent.name === expectedEvent);

// Check each event's parameters
matchingEvents.forEach((event, index) => {
expect(
event.args[index],
`Parameters of event ${expectedEvent} did not match at instance ${index + 1}`,
).to.deep.equal(expectedParams[index]);
});
};

export const proposalSchema = {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
Expand Down

0 comments on commit a8f2e16

Please sign in to comment.