mirror of
https://github.com/hanzoai/viem.git
synced 2026-10-30 19:36:39 +00:00
Adds the contract.write public action (encodes calldata and delegates to transaction.send, wrapping failures as contract errors), wires it into the publicActions decorator, and adds runtime + type tests covering args/overloads/payable value and revert/custom-error decoding. Adds a WriteExample test fixture and ...Write revert variants to ErrorsExample. Amp-Thread-ID: https://ampcode.com/threads/T-019f00f9-43e6-70d0-8d29-57ceb5a3b4cd
104 lines
2.6 KiB
Solidity
104 lines
2.6 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
pragma solidity ^0.8.23;
|
|
|
|
/// @dev Deterministic, self-hosted contract exercising every revert shape a
|
|
/// read or write action can surface: string reverts (`Error(string)`), panics
|
|
/// (`assert`/overflow/divide-by-zero), bare `require`, and custom errors with
|
|
/// and without arguments. `...Read` variants are `pure` (surfaced via
|
|
/// `eth_call`); `...Write` variants are state-mutating (surfaced via
|
|
/// `eth_estimateGas`). Used by the contract-error tests.
|
|
contract ErrorsExample {
|
|
struct Foo {
|
|
address sender;
|
|
uint256 bar;
|
|
}
|
|
|
|
error SimpleError(string message);
|
|
error SimpleErrorNoArgs();
|
|
error ComplexError(Foo foo, string message, uint256 number);
|
|
|
|
function revertRead() public pure {
|
|
revert("This is a revert message");
|
|
}
|
|
|
|
function assertRead() public pure {
|
|
assert(false);
|
|
}
|
|
|
|
function overflowRead() public pure returns (uint256) {
|
|
uint256 a = 2 ** 256 - 1;
|
|
uint256 b = 1;
|
|
uint256 c = a + b;
|
|
return c;
|
|
}
|
|
|
|
function divideByZeroRead() public pure returns (uint256) {
|
|
uint256 a = 69;
|
|
uint256 b = 0;
|
|
uint256 c = a / b;
|
|
return c;
|
|
}
|
|
|
|
function requireRead() public pure {
|
|
require(false);
|
|
}
|
|
|
|
function simpleCustomRead() public pure {
|
|
revert SimpleError("bugger");
|
|
}
|
|
|
|
function simpleCustomReadNoArgs() public pure {
|
|
revert SimpleErrorNoArgs();
|
|
}
|
|
|
|
function complexCustomRead() public pure {
|
|
revert ComplexError(
|
|
Foo({sender: 0x0000000000000000000000000000000000000000, bar: 69}),
|
|
"bugger",
|
|
69
|
|
);
|
|
}
|
|
|
|
function revertWrite() public {
|
|
revert("This is a revert message");
|
|
}
|
|
|
|
function assertWrite() public {
|
|
assert(false);
|
|
}
|
|
|
|
function overflowWrite() public returns (uint256) {
|
|
uint256 a = 2 ** 256 - 1;
|
|
uint256 b = 1;
|
|
uint256 c = a + b;
|
|
return c;
|
|
}
|
|
|
|
function divideByZeroWrite() public returns (uint256) {
|
|
uint256 a = 69;
|
|
uint256 b = 0;
|
|
uint256 c = a / b;
|
|
return c;
|
|
}
|
|
|
|
function requireWrite() public {
|
|
require(false);
|
|
}
|
|
|
|
function simpleCustomWrite() public {
|
|
revert SimpleError("bugger");
|
|
}
|
|
|
|
function simpleCustomWriteNoArgs() public {
|
|
revert SimpleErrorNoArgs();
|
|
}
|
|
|
|
function complexCustomWrite() public {
|
|
revert ComplexError(
|
|
Foo({sender: 0x0000000000000000000000000000000000000000, bar: 69}),
|
|
"bugger",
|
|
69
|
|
);
|
|
}
|
|
}
|