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 reversion with custom errors & maximum view call size #196

Merged
merged 6 commits into from
Oct 30, 2023
Merged
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
34 changes: 34 additions & 0 deletions contracts/contracts/tests/SemanticTests.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.0;

contract SemanticTests {
function testViewLength(uint256 len) external pure returns (bytes memory) {
return new bytes(len);
}

error CustomError(uint256 value);
uint256 private x;

uint256 constant ERROR_NUM =
0x1023456789abcdef1023456789abcdef1023456789abcdef1023456789abcdef;

function testCustomRevert() external {
x += 1;
revert CustomError(ERROR_NUM);
}

function testCustomViewRevert() external pure returns (uint256) {
revert CustomError(ERROR_NUM);
}

function testViewRevert() external pure returns (uint256) {
require(false, "ThisIsAnError");
return ERROR_NUM;
}

function testRevert() external {
x += 1;
require(false, "ThisIsAnError");
}
}
2 changes: 1 addition & 1 deletion contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const config: HardhatUserConfig = {
chainId: 0x5aff,
accounts: process.env.SAPPHIRE_TESTNET_PRIVATE_KEY
? [process.env.SAPPHIRE_TESTNET_PRIVATE_KEY]
: TEST_HDWALLET,
: [],
CedarMist marked this conversation as resolved.
Show resolved Hide resolved
},
'sapphire-mainnet': {
url: 'https://sapphire.oasis.io',
Expand Down
51 changes: 51 additions & 0 deletions contracts/test/semantics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: Apache-2.0

import { ethers } from 'hardhat';
import { expect } from 'chai';
import { SemanticTests } from '../typechain-types/contracts/tests/SemanticTests';
import { SemanticTests__factory } from '../typechain-types/factories/contracts/tests';

const ERROR_NUM =
'0x1023456789abcdef1023456789abcdef1023456789abcdef1023456789abcdef';

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

before(async () => {
const f = (await ethers.getContractFactory(
'SemanticTests',
)) as SemanticTests__factory;
c = await f.deploy();
await c.deployed();
chainId = (await c.provider.getNetwork()).chainId;
});

it('eth_call maximum return length vs gas limit', async () => {
const i = 1787872;
const respHex = await c.testViewLength(i);
const respBytes = ethers.utils.arrayify(respHex);
expect(respBytes.length).eq(i);
expect(c.testViewLength(i + 1)).reverted;
});

it('Error string in view call', async () => {
try {
await c.testViewRevert();
} catch (x: any) {
expect(x.errorArgs[0]).to.eq('ThisIsAnError');
expect(x.errorName).to.eq('Error');
}
});

it('Custom revert in view call', async () => {
// Perform view call, which is expected to revert
try {
await c.testCustomViewRevert();
expect(false).to.be.true;
} catch (x: any) {
expect(x.errorArgs[0]).to.eq(ERROR_NUM);
expect(x.errorName).to.eq('CustomError');
}
});
});
Loading