Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test eth_call with deployments, to verify custom errors are caught in constructor #294

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions contracts/contracts/tests/CreateFailCustom.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.0;

contract CreateFailCustom {
uint256 constant ERROR_NUM =
0x1023456789abcdef1023456789abcdef1023456789abcdef1023456789abcdef;

error CustomError(uint256 value);

constructor() {
revert CustomError(ERROR_NUM);
}
}
9 changes: 9 additions & 0 deletions contracts/contracts/tests/CreateFailRequire.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.0;

contract CreateFailRequire {
constructor() {
require(false, "ThisIsAnError");
}
}
60 changes: 57 additions & 3 deletions contracts/test/semantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import { ethers } from 'hardhat';
import { expect } from 'chai';
import { SemanticTests } from '../typechain-types/contracts/tests/SemanticTests';
import { getBytes } from 'ethers';
import { ErrorFragment, Interface, getBytes } from 'ethers';

const ERROR_NUM =
'0x1023456789abcdef1023456789abcdef1023456789abcdef1023456789abcdef';

describe('EVM Semantics', () => {
let c: SemanticTests;
let chainId: number;
let chainId: bigint;

before(async () => {
const f = await ethers.getContractFactory('SemanticTests');
Expand All @@ -19,18 +19,72 @@ describe('EVM Semantics', () => {
chainId = (await ethers.provider.getNetwork()).chainId;
});

it('eth_call constructor with custom error', async () => {
const f = await ethers.getContractFactory('CreateFailCustom');
const tx = await f.getDeployTransaction();
const p = ethers.provider;
let caught = false;
try {
const r = await p.call({
data: tx.data,
gasPrice: tx.gasPrice,
gasLimit: tx.gasLimit,
});
} catch (x: any) {
// XXX: typechain doesn't fully support custom errors from constructor
const abi = {
inputs: [
{
internalType: 'uint256',
name: 'value',
type: 'uint256',
},
],
name: 'CustomError',
type: 'error',
};
const ef = ErrorFragment.from(abi);
const iface = Interface.from([ef]);
const expected = iface.encodeErrorResult(ef, [ERROR_NUM]);
expect(x.info.error.data).eq(expected);
caught = true;
}
expect(caught).eq(true);
});

it('eth_call constructor with require errror', async () => {
const f = await ethers.getContractFactory('CreateFailRequire');
const tx = await f.getDeployTransaction();
const p = ethers.provider;
let caught = false;
try {
await p.call({
data: tx.data,
gasPrice: tx.gasPrice,
gasLimit: tx.gasLimit,
});
} catch (x: any) {
expect(x.revert.args[0]).to.eq('ThisIsAnError');
expect(x.revert.name).to.eq('Error');
caught = true;
}
expect(caught).eq(true);
});

it('eth_call maximum return length vs gas limit', async () => {
const i = 1211104;
const respHex = await c.testViewLength(i);
const respBytes = getBytes(respHex);
expect(respBytes.length).eq(i);

let caught = false;
try {
await c.testViewLength(i + 1);
expect(false).eq(true);
} catch (e: any) {
caught = true;
expect(e.info.error.message).contains('out of gas');
}
expect(caught).eq(true);
});

it('Error string in view call', async () => {
Expand Down
3 changes: 3 additions & 0 deletions examples/hardhat-ignition-ethers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
typechain-types
artifacts
cache
40 changes: 40 additions & 0 deletions examples/hardhat-ignition-ethers/contracts/Example.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: CC-PDDC

pragma solidity ^0.8.0;

contract Example {
address public owner;

constructor () {
owner = msg.sender;
}

function getMsgSender ()
external view
returns (address)
{
return msg.sender;
}

function doNothing ()
external
{

}

function clearOwner ()
external
{
owner = address(0);
}

function changeOwner (address in_newOwner)
external
{
if( owner != address(0) ) {
require( msg.sender == owner, "not owner" );
}

owner = in_newOwner;
}
}
69 changes: 69 additions & 0 deletions examples/hardhat-ignition-ethers/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { HardhatUserConfig } from "hardhat/config";
import '@oasisprotocol/sapphire-hardhat';
import "@nomicfoundation/hardhat-ignition-ethers";
import "@nomicfoundation/hardhat-toolbox";

const env_private_key = process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [];

const TEST_HDWALLET = {
mnemonic: "test test test test test test test test test test test junk",
path: "m/44'/60'/0'/0",
initialIndex: 0,
count: 20,
passphrase: "",
};

const config: HardhatUserConfig = {
mocha: {
timeout: 400000
},
paths: {
tests: "./tests"
},
solidity: {
version: "0.8.18",
settings: {
viaIR: false,
/*
debug: {
revertStrings: "debug"
},
*/
optimizer: {
enabled: true,
runs: 200,
}
},
},
typechain: {
target: "ethers-v6"
},
networks: {
hardhat: {
chainId: 1337 // We set 1337 to make interacting with MetaMask simpler
},
'sapphire-localnet': {
url: "http://localhost:8545",
accounts: TEST_HDWALLET,
chainId: 0x5afd,
},
// https://docs.oasis.io/dapp/sapphire/
sapphire: {
url: "https://sapphire.oasis.io/",
accounts: env_private_key,
chainId: 0x5afe,
},
'sapphire-1rpc': {
url: 'https://1rpc.io/oasis/sapphire',
accounts: env_private_key,
chainId: 0x5afe
},
'sapphire-testnet': {
url: "https://testnet.sapphire.oasis.dev",
accounts: TEST_HDWALLET,
chainId: 0x5aff,
}
}
};

export default config;
1 change: 1 addition & 0 deletions examples/hardhat-ignition-ethers/ignition/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deployments
11 changes: 11 additions & 0 deletions examples/hardhat-ignition-ethers/ignition/modules/Example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

export default buildModule("Example", (m) => {
const example = m.contract("Example", []);

m.call(example, 'doNothing', []);

return {
example
};
});
35 changes: 35 additions & 0 deletions examples/hardhat-ignition-ethers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "examples-hardhat-ignition-ethers",
"version": "0.1.0",
"description": "Example using hardhat-ignition + Ethers with Oasis Sapphire",
"main": "lib/index.js",
"private": true,
"keywords": [
"oasis",
"sapphire",
"hardhat-ignition",
"ethers"
],
"directories": {
"test": "tests"
},
"author": "CedarMist",
"license": "CC-PDDC",
"devDependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.5",
"@nomicfoundation/hardhat-ignition": "^0.15.0",
"@nomicfoundation/hardhat-ignition-ethers": "^0.15.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.10",
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
"@oasisprotocol/sapphire-hardhat": "^2.19.4",
"@types/node": "^20.11.30",
"hardhat": "^2.22.2",
"ts-node": "^10.9.2",
"typescript": "^5.4.3"
},
"dependencies": {
"@oasisprotocol/sapphire-contracts": "^0.2.7",
"@oasisprotocol/sapphire-paratime": "^1.3.2",
"ethers": "^6.11.1"
}
}
11 changes: 11 additions & 0 deletions examples/hardhat-ignition-ethers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true
}
}
Loading