Skip to content

Commit

Permalink
contracts: format
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarMist committed Oct 30, 2023
1 parent 0d3d957 commit 66c8ab8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 74 deletions.
20 changes: 10 additions & 10 deletions contracts/contracts/tests/SemanticTests.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
pragma solidity ^0.8.0;

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

error CustomError(uint value);
uint private x;
error CustomError(uint256 value);
uint256 private x;

uint constant ERROR_NUM = 0x1023456789abcdef1023456789abcdef1023456789abcdef1023456789abcdef;
uint256 constant ERROR_NUM =
0x1023456789abcdef1023456789abcdef1023456789abcdef1023456789abcdef;

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

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

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

function testRevert () external {
function testRevert() external {
x += 1;
require(false, "ThisIsAnError");
}
}
}
129 changes: 65 additions & 64 deletions contracts/test/semantics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,78 @@ 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';
const ERROR_NUM =
'0x1023456789abcdef1023456789abcdef1023456789abcdef1023456789abcdef';

describe('EVM Semantics', () => {
let c: SemanticTests;
let chainId: number;
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
});
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 () => {
if( chainId != 31337 ) {
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('eth_call maximum return length vs gas limit', async () => {
if (chainId != 31337) {
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;
}
});

// Apparently `expect().to.be.revertedWithCustomError` doesn't work sometimes
// Otherwise we could do this:
//expect(c.testCustomRevert()).to.be.revertedWithCustomError(c, 'CustomError').withArgs(errorNum);
//expect(c.testCustomViewRevert()).to.be.revertedWithCustomError(c, 'CustomError').withArgs(errorNum);
// Apparently `expect().to.be.revertedWithCustomError` doesn't work sometimes
// Otherwise we could do this:
//expect(c.testCustomRevert()).to.be.revertedWithCustomError(c, 'CustomError').withArgs(errorNum);
//expect(c.testCustomViewRevert()).to.be.revertedWithCustomError(c, 'CustomError').withArgs(errorNum);

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('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('Error string in tx', async () => {
// Perform transaction which is expected to revert
try {
const tx = await c.testRevert();
await tx.wait();
expect(false).to.be.true;
}
catch( x: any ) {
expect(x.data).eq('0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d546869734973416e4572726f7200000000000000000000000000000000000000');
}
});
it('Error string in tx', async () => {
// Perform transaction which is expected to revert
try {
const tx = await c.testRevert();
await tx.wait();
expect(false).to.be.true;
} catch (x: any) {
expect(x.data).eq(
'0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d546869734973416e4572726f7200000000000000000000000000000000000000',
);
}
});

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');
}
});
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');
}
});

it('Custom error in tx', async () => {
// Perform transaction which is expected to revert
try {
const tx = await c.testCustomRevert();
await tx.wait();
expect(false).to.be.true;
}
catch( x: any ) {
expect(x.data).eq('0x110b3655' + ERROR_NUM.slice(2));
}
});
it('Custom error in tx', async () => {
// Perform transaction which is expected to revert
try {
const tx = await c.testCustomRevert();
await tx.wait();
expect(false).to.be.true;
} catch (x: any) {
expect(x.data).eq('0x110b3655' + ERROR_NUM.slice(2));
}
});
});

0 comments on commit 66c8ab8

Please sign in to comment.