Skip to content

Commit b6a506d

Browse files
zerosnacksmds1
andauthored
feat: add TOML cheatcode support (#518)
* add StdToml, pending upstream TOML cheatcode support * fix name * pull in TOML cheatcode update * update interface id * add basic JSON and TOML test, update examples * chore: struct names * chore: fix function visibility warnings --------- Co-authored-by: Matt Solomon <[email protected]>
1 parent 1432518 commit b6a506d

File tree

10 files changed

+377
-13
lines changed

10 files changed

+377
-13
lines changed

src/StdJson.sol

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@ import {VmSafe} from "./Vm.sol";
99
// To parse:
1010
// ```
1111
// using stdJson for string;
12-
// string memory json = vm.readFile("some_path");
13-
// json.parseUint("<json_path>");
12+
// string memory json = vm.readFile("<some_path>");
13+
// json.readUint("<json_path>");
1414
// ```
1515
// To write:
1616
// ```
1717
// using stdJson for string;
18-
// string memory json = "deploymentArtifact";
19-
// Contract contract = new Contract();
20-
// json.serialize("contractAddress", address(contract));
21-
// json = json.serialize("deploymentTimes", uint(1));
22-
// // store the stringified JSON to the 'json' variable we have been using as a key
23-
// // as we won't need it any longer
24-
// string memory json2 = "finalArtifact";
25-
// string memory final = json2.serialize("depArtifact", json);
26-
// final.write("<some_path>");
18+
// string memory json = "json";
19+
// json.serialize("a", uint256(123));
20+
// string memory semiFinal = json.serialize("b", string("test"));
21+
// string memory finalJson = json.serialize("c", semiFinal);
22+
// finalJson.write("<some_path>");
2723
// ```
2824

2925
library stdJson {

src/StdToml.sol

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.6.0 <0.9.0;
3+
4+
pragma experimental ABIEncoderV2;
5+
6+
import {VmSafe} from "./Vm.sol";
7+
8+
// Helpers for parsing and writing TOML files
9+
// To parse:
10+
// ```
11+
// using stdToml for string;
12+
// string memory toml = vm.readFile("<some_path>");
13+
// toml.readUint("<json_path>");
14+
// ```
15+
// To write:
16+
// ```
17+
// using stdToml for string;
18+
// string memory json = "json";
19+
// json.serialize("a", uint256(123));
20+
// string memory semiFinal = json.serialize("b", string("test"));
21+
// string memory finalJson = json.serialize("c", semiFinal);
22+
// finalJson.write("<some_path>");
23+
// ```
24+
25+
library stdToml {
26+
VmSafe private constant vm = VmSafe(address(uint160(uint256(keccak256("hevm cheat code")))));
27+
28+
function parseRaw(string memory toml, string memory key) internal pure returns (bytes memory) {
29+
return vm.parseToml(toml, key);
30+
}
31+
32+
function readUint(string memory toml, string memory key) internal pure returns (uint256) {
33+
return vm.parseTomlUint(toml, key);
34+
}
35+
36+
function readUintArray(string memory toml, string memory key) internal pure returns (uint256[] memory) {
37+
return vm.parseTomlUintArray(toml, key);
38+
}
39+
40+
function readInt(string memory toml, string memory key) internal pure returns (int256) {
41+
return vm.parseTomlInt(toml, key);
42+
}
43+
44+
function readIntArray(string memory toml, string memory key) internal pure returns (int256[] memory) {
45+
return vm.parseTomlIntArray(toml, key);
46+
}
47+
48+
function readBytes32(string memory toml, string memory key) internal pure returns (bytes32) {
49+
return vm.parseTomlBytes32(toml, key);
50+
}
51+
52+
function readBytes32Array(string memory toml, string memory key) internal pure returns (bytes32[] memory) {
53+
return vm.parseTomlBytes32Array(toml, key);
54+
}
55+
56+
function readString(string memory toml, string memory key) internal pure returns (string memory) {
57+
return vm.parseTomlString(toml, key);
58+
}
59+
60+
function readStringArray(string memory toml, string memory key) internal pure returns (string[] memory) {
61+
return vm.parseTomlStringArray(toml, key);
62+
}
63+
64+
function readAddress(string memory toml, string memory key) internal pure returns (address) {
65+
return vm.parseTomlAddress(toml, key);
66+
}
67+
68+
function readAddressArray(string memory toml, string memory key) internal pure returns (address[] memory) {
69+
return vm.parseTomlAddressArray(toml, key);
70+
}
71+
72+
function readBool(string memory toml, string memory key) internal pure returns (bool) {
73+
return vm.parseTomlBool(toml, key);
74+
}
75+
76+
function readBoolArray(string memory toml, string memory key) internal pure returns (bool[] memory) {
77+
return vm.parseTomlBoolArray(toml, key);
78+
}
79+
80+
function readBytes(string memory toml, string memory key) internal pure returns (bytes memory) {
81+
return vm.parseTomlBytes(toml, key);
82+
}
83+
84+
function readBytesArray(string memory toml, string memory key) internal pure returns (bytes[] memory) {
85+
return vm.parseTomlBytesArray(toml, key);
86+
}
87+
88+
function serialize(string memory jsonKey, string memory rootObject) internal returns (string memory) {
89+
return vm.serializeJson(jsonKey, rootObject);
90+
}
91+
92+
function serialize(string memory jsonKey, string memory key, bool value) internal returns (string memory) {
93+
return vm.serializeBool(jsonKey, key, value);
94+
}
95+
96+
function serialize(string memory jsonKey, string memory key, bool[] memory value)
97+
internal
98+
returns (string memory)
99+
{
100+
return vm.serializeBool(jsonKey, key, value);
101+
}
102+
103+
function serialize(string memory jsonKey, string memory key, uint256 value) internal returns (string memory) {
104+
return vm.serializeUint(jsonKey, key, value);
105+
}
106+
107+
function serialize(string memory jsonKey, string memory key, uint256[] memory value)
108+
internal
109+
returns (string memory)
110+
{
111+
return vm.serializeUint(jsonKey, key, value);
112+
}
113+
114+
function serialize(string memory jsonKey, string memory key, int256 value) internal returns (string memory) {
115+
return vm.serializeInt(jsonKey, key, value);
116+
}
117+
118+
function serialize(string memory jsonKey, string memory key, int256[] memory value)
119+
internal
120+
returns (string memory)
121+
{
122+
return vm.serializeInt(jsonKey, key, value);
123+
}
124+
125+
function serialize(string memory jsonKey, string memory key, address value) internal returns (string memory) {
126+
return vm.serializeAddress(jsonKey, key, value);
127+
}
128+
129+
function serialize(string memory jsonKey, string memory key, address[] memory value)
130+
internal
131+
returns (string memory)
132+
{
133+
return vm.serializeAddress(jsonKey, key, value);
134+
}
135+
136+
function serialize(string memory jsonKey, string memory key, bytes32 value) internal returns (string memory) {
137+
return vm.serializeBytes32(jsonKey, key, value);
138+
}
139+
140+
function serialize(string memory jsonKey, string memory key, bytes32[] memory value)
141+
internal
142+
returns (string memory)
143+
{
144+
return vm.serializeBytes32(jsonKey, key, value);
145+
}
146+
147+
function serialize(string memory jsonKey, string memory key, bytes memory value) internal returns (string memory) {
148+
return vm.serializeBytes(jsonKey, key, value);
149+
}
150+
151+
function serialize(string memory jsonKey, string memory key, bytes[] memory value)
152+
internal
153+
returns (string memory)
154+
{
155+
return vm.serializeBytes(jsonKey, key, value);
156+
}
157+
158+
function serialize(string memory jsonKey, string memory key, string memory value)
159+
internal
160+
returns (string memory)
161+
{
162+
return vm.serializeString(jsonKey, key, value);
163+
}
164+
165+
function serialize(string memory jsonKey, string memory key, string[] memory value)
166+
internal
167+
returns (string memory)
168+
{
169+
return vm.serializeString(jsonKey, key, value);
170+
}
171+
172+
function write(string memory jsonKey, string memory path) internal {
173+
vm.writeToml(jsonKey, path);
174+
}
175+
176+
function write(string memory jsonKey, string memory path, string memory valueKey) internal {
177+
vm.writeToml(jsonKey, path, valueKey);
178+
}
179+
}

src/Test.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {stdJson} from "./StdJson.sol";
1919
import {stdMath} from "./StdMath.sol";
2020
import {StdStorage, stdStorage} from "./StdStorage.sol";
2121
import {StdStyle} from "./StdStyle.sol";
22+
import {stdToml} from "./StdToml.sol";
2223
import {StdUtils} from "./StdUtils.sol";
2324
import {Vm} from "./Vm.sol";
2425

src/Vm.sol

Lines changed: 76 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/StdCheats.t.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pragma solidity >=0.7.0 <0.9.0;
44
import "../src/StdCheats.sol";
55
import "../src/Test.sol";
66
import "../src/StdJson.sol";
7+
import "../src/StdToml.sol";
78
import "../src/interfaces/IERC20.sol";
89

910
contract StdCheatsTest is Test {

test/StdJson.t.sol

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.7.0 <0.9.0;
3+
4+
import "../src/Test.sol";
5+
6+
contract StdJsonTest is Test {
7+
using stdJson for string;
8+
9+
string root;
10+
string path;
11+
12+
function setUp() public {
13+
root = vm.projectRoot();
14+
path = string.concat(root, "/test/fixtures/test.json");
15+
}
16+
17+
struct SimpleJson {
18+
uint256 a;
19+
string b;
20+
}
21+
22+
struct NestedJson {
23+
uint256 a;
24+
string b;
25+
SimpleJson c;
26+
}
27+
28+
function test_readJson() public view {
29+
string memory json = vm.readFile(path);
30+
assertEq(json.readUint(".a"), 123);
31+
}
32+
33+
function test_writeJson() public {
34+
string memory json = "json";
35+
json.serialize("a", uint256(123));
36+
string memory semiFinal = json.serialize("b", string("test"));
37+
string memory finalJson = json.serialize("c", semiFinal);
38+
finalJson.write(path);
39+
40+
string memory json_ = vm.readFile(path);
41+
bytes memory data = json_.parseRaw("$");
42+
NestedJson memory decodedData = abi.decode(data, (NestedJson));
43+
44+
assertEq(decodedData.a, 123);
45+
assertEq(decodedData.b, "test");
46+
assertEq(decodedData.c.a, 123);
47+
assertEq(decodedData.c.b, "test");
48+
}
49+
}

0 commit comments

Comments
 (0)