Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions contracts/v0.8.9/CustomError.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

/// Insufficient balance for transfer. Needed `required` but only
/// `available` available.
/// @param available balance available.
/// @param required requested amount to transfer.
error InsufficientBalance(uint256 available, uint256 required);

/// @notice example contract from https://blog.soliditylang.org/2021/04/21/custom-errors/
contract CustomError {
mapping(address => uint) balance;

function transfer(address to, uint256 amount) public {
if (amount > balance[msg.sender])
// Error call using named parameters. Equivalent to
// revert InsufficientBalance(balance[msg.sender], amount);
revert InsufficientBalance({
available: balance[msg.sender],
required: amount
});
balance[msg.sender] -= amount;
balance[to] += amount;
}
// ...
}
2 changes: 2 additions & 0 deletions packages/hardhat-test/typechain-types/Counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface CounterInterface extends utils.Interface {
};

getEvent(nameOrSignatureOrTopic: "CountedTo"): EventFragment;

errors: {};
}

export interface CountedToEventObject {
Expand Down
2 changes: 2 additions & 0 deletions packages/hardhat-test/typechain-types/Demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface DemoInterface extends utils.Interface {
functions: {};

events: {};

errors: {};
}

export interface Demo extends BaseContract {
Expand Down
2 changes: 2 additions & 0 deletions packages/hardhat-test/typechain-types/StructsInConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface StructsInConstructorInterface extends utils.Interface {
functions: {};

events: {};

errors: {};
}

export interface StructsInConstructor extends BaseContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface EdgeCasesInterface extends utils.Interface {
functions: {};

events: {};

errors: {};
}

export interface EdgeCases extends BaseContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface TestContractInterface extends utils.Interface {
functions: {};

events: {};

errors: {};
}

export interface TestContract extends BaseContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface TestContract1Interface extends utils.Interface {
functions: {};

events: {};

errors: {};
}

export interface TestContract1 extends BaseContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export interface SafeMathInterface extends utils.Interface {
decodeFunctionResult(functionFragment: "sub", data: BytesLike): Result;

events: {};

errors: {};
}

export interface SafeMath extends BaseContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export interface ERC20Interface extends utils.Interface {

getEvent(nameOrSignatureOrTopic: "Approval"): EventFragment;
getEvent(nameOrSignatureOrTopic: "Transfer"): EventFragment;

errors: {};
}

export interface ApprovalEventObject {
Expand Down
30 changes: 30 additions & 0 deletions packages/target-ethers-v5-test/test/CustomError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { CustomError } from '../types/v0.8.9/CustomError'
import { createNewBlockchain, deployContract } from './common'

describe('CustomError support', () => {
const chain = createNewBlockchain<CustomError>('CustomError')

let contract!: CustomError

beforeEach(async () => {
contract = chain.contract
const { signer } = chain

signer.provider.pollingInterval = 100
contract = await deployContract<CustomError>(signer, 'CustomError')
})

afterEach(async () => {})

it('typed error import', async () => {
try {
await contract.callStatic.transfer('0x0000000000000000000000000000000000000000', 1)
} catch (err) {
const expectedError = contract.interface.errors['InsufficientBalance(uint256,uint256)']
// eslint-disable-next-line no-console
console.log(expectedError, err)
// TODO: still wip on how to best consume the errors here
// if(err.reason !== expectedError.)
}
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */

import { Contract, Signer, utils } from "ethers";
import type { Provider } from "@ethersproject/providers";
import type {
CustomError,
CustomErrorInterface,
} from "../../v0.8.9/CustomError";

const _abi = [
{
inputs: [
{
internalType: "uint256",
name: "available",
type: "uint256",
},
{
internalType: "uint256",
name: "required",
type: "uint256",
},
],
name: "InsufficientBalance",
type: "error",
},
{
inputs: [
{
internalType: "address",
name: "to",
type: "address",
},
{
internalType: "uint256",
name: "amount",
type: "uint256",
},
],
name: "transfer",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
];

export class CustomError__factory {
static readonly abi = _abi;
static createInterface(): CustomErrorInterface {
return new utils.Interface(_abi) as CustomErrorInterface;
}
static connect(
address: string,
signerOrProvider: Signer | Provider
): CustomError {
return new Contract(address, _abi, signerOrProvider) as CustomError;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export * as kingOfTheHill from "./KingOfTheHill";
export * as rarity from "./Rarity";
export * as nested from "./nested";
export { CustomError__factory } from "./CustomError__factory";
export { ISimpleToken__factory } from "./ISimpleToken__factory";
export { Issue552_Reproduction__factory } from "./Issue552_Reproduction__factory";
export { SimpleToken__factory } from "./SimpleToken__factory";
2 changes: 2 additions & 0 deletions packages/target-ethers-v5-test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type { Payable } from "./v0.6.4/Payable/Payable";
export { Payable__factory } from "./factories/v0.6.4/Payable/Payable__factory";
export type { PayableFactory } from "./v0.6.4/Payable/PayableFactory";
export { PayableFactory__factory } from "./factories/v0.6.4/Payable/PayableFactory__factory";
export type { CustomError } from "./v0.8.9/CustomError";
export { CustomError__factory } from "./factories/v0.8.9/CustomError__factory";
export type { ISimpleToken } from "./v0.8.9/ISimpleToken";
export { ISimpleToken__factory } from "./factories/v0.8.9/ISimpleToken__factory";
export type { Issue552_Reproduction } from "./v0.8.9/Issue552_Reproduction";
Expand Down
2 changes: 2 additions & 0 deletions packages/target-ethers-v5-test/types/v0.6.4/DataTypesInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ export interface DataTypesInputInterface extends utils.Interface {
): Result;

events: {};

errors: {};
}

export interface DataTypesInput extends BaseContract {
Expand Down
2 changes: 2 additions & 0 deletions packages/target-ethers-v5-test/types/v0.6.4/DataTypesPure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export interface DataTypesPureInterface extends utils.Interface {
decodeFunctionResult(functionFragment: "pure_uint8", data: BytesLike): Result;

events: {};

errors: {};
}

export interface DataTypesPure extends BaseContract {
Expand Down
2 changes: 2 additions & 0 deletions packages/target-ethers-v5-test/types/v0.6.4/DataTypesView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export interface DataTypesViewInterface extends utils.Interface {
decodeFunctionResult(functionFragment: "view_uint8", data: BytesLike): Result;

events: {};

errors: {};
}

export interface DataTypesView extends BaseContract {
Expand Down
2 changes: 2 additions & 0 deletions packages/target-ethers-v5-test/types/v0.6.4/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export interface EventsInterface extends utils.Interface {
getEvent(nameOrSignatureOrTopic: "Event4"): EventFragment;
getEvent(nameOrSignatureOrTopic: "NoArgsEvent"): EventFragment;
getEvent(nameOrSignatureOrTopic: "UpdateFrequencySet"): EventFragment;

errors: {};
}

export interface AnonEvent1EventObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface AInterface extends utils.Interface {
};

getEvent(nameOrSignatureOrTopic: "Committed"): EventFragment;

errors: {};
}

export interface CommittedEventObject {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface BInterface extends utils.Interface {

getEvent(nameOrSignatureOrTopic: "Committed(uint256)"): EventFragment;
getEvent(nameOrSignatureOrTopic: "Committed(address[])"): EventFragment;

errors: {};
}

export interface Committed_uint256_EventObject {
Expand Down
2 changes: 2 additions & 0 deletions packages/target-ethers-v5-test/types/v0.6.4/Library/Lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface LibInterface extends utils.Interface {
decodeFunctionResult(functionFragment: "other", data: BytesLike): Result;

events: {};

errors: {};
}

export interface Lib extends BaseContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface LibraryConsumerInterface extends utils.Interface {
decodeFunctionResult(functionFragment: "someOther", data: BytesLike): Result;

events: {};

errors: {};
}

export interface LibraryConsumer extends BaseContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export interface NAME12manglingInterface extends utils.Interface {
decodeFunctionResult(functionFragment: "works", data: BytesLike): Result;

events: {};

errors: {};
}

export interface NAME12mangling extends BaseContract {
Expand Down
2 changes: 2 additions & 0 deletions packages/target-ethers-v5-test/types/v0.6.4/Overloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export interface OverloadsInterface extends utils.Interface {
): Result;

events: {};

errors: {};
}

export interface Overloads extends BaseContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface PayableInterface extends utils.Interface {
): Result;

events: {};

errors: {};
}

export interface Payable extends BaseContract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface PayableFactoryInterface extends utils.Interface {
decodeFunctionResult(functionFragment: "newPayable", data: BytesLike): Result;

events: {};

errors: {};
}

export interface PayableFactory extends BaseContract {
Expand Down
Loading