Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TomiOhl committed Sep 5, 2022
1 parent a9fbcc2 commit 1f19870
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { expect } from "chai";
import { utils } from "ethers";

/**
* Tests a contract call if it reverts with a specific custom error.
* @param contractInstance The contract artifact compiled with Truffle.
* @param contractCall The method call with arguments.
* @param errorName The name of the custom error.
* @param values The expected arguments of the custom error.
*/
export async function expectRevertCustomError(
contractInstance: any,
contractCall: Promise<any>,
Expand All @@ -12,19 +19,19 @@ export async function expectRevertCustomError(
expect.fail("Expected promise to throw but it didn't");
} catch (revert: any) {
const errorAbi = contractInstance.abi.find((elem: any) => elem.type === "error" && elem.name === errorName);
expect(errorAbi, `Expected custom error ${errorName}`).to.exist;
expect(errorAbi, `Expected custom error ${errorName}`).to.exist;

const types = errorAbi.inputs.map((elem: any) => elem.type);
const types: string[] = errorAbi.inputs.map((elem: any) => elem.type);

const errorId = utils
.solidityKeccak256(["string"], [`${errorName}(${types ? types.toString() : ""})`])
.substring(0, 10);
expect(JSON.stringify(revert), `Expected custom error ${errorName} (${errorId})`).to.include(errorId);
const errorId = utils
.solidityKeccak256(["string"], [`${errorName}(${types ? types.toString() : ""})`])
.substring(0, 10);
expect(JSON.stringify(revert), `Expected custom error ${errorName} (${errorId})`).to.include(errorId);

if (values) {
expect(values.length, "Expected the number of values to match the number of types").to.eq(types.length);
const decodedValues = utils.defaultAbiCoder.decode(types, utils.hexDataSlice(revert.data.result, 4));
decodedValues.forEach((elem, index) => expect(elem.toString()).to.eq(values[index].toString()));
if (values) {
expect(values.length, "Expected the number of values to match the number of types").to.eq(types.length);
const decodedValues = utils.defaultAbiCoder.decode(types, utils.hexDataSlice(revert.data.result, 4));
decodedValues.forEach((elem, index) => expect(elem.toString()).to.eq(values[index].toString()));
}
}
}

0 comments on commit 1f19870

Please sign in to comment.