2/ Read a whole file into a string with `vm.readFile`, or read the next line of a file with `vm.readLine`.
Right now the best options are reading (1) a JSON file, or (2) a file with one piece of data per line. CSVs aren't well supported yet.
3/ Now you have data as a string, but string manipulation is terrible in Solidity so there's cheatcodes for that too. The below cheats take a string and convert to the specified type:
parseBytes
parseAddress
parseUint256
parseInt256
parseBytes32
parseBool
4/ If you read JSON, `parseJson(stringifiedJson, key)` returns the data as ABI-encoded bytes which you can decode with abi.decode
The `key` is a string and supports jq-syntax. If you don't know jq syntax, see earthly.dev/blog/jq-select/
Note the caveats if the key holds an object
5/ forge-std has a library to simplify this:
contract MyScript is Script {
using stdJson for string;
function parseInputs() internal {
string memory json = vm.readFile("data.json");
uint userBalance = json.readUint(".balance");
}
}
6/ It also has various structs defined that match the transactions and receipts from `forge script` broadcast logs, and helper methods for easily reading the txs/receipts.
twitter.com/gakonst/status/1564461115567878144
7/ Ok now you have a bunch of data, you ran some scripts or tests with it, and want to save outputs.
vm.writeFile(string)
vm.writeLine(string)
vm.toString(anyType)
Use `str.concat` to help format and you're done.
8/ If you want to write JSON you have to format it manually for now, but @odysseas_eth is working on some helpers to make that easier.
Big shoutout to @odysseas_eth and @ashekhirin for implementing most of the stuff in this thread 🙌