From da2cdb4b559397846229fec3413e5c2ac7170674 Mon Sep 17 00:00:00 2001 From: paulelisha Date: Mon, 14 Oct 2024 13:37:10 +0100 Subject: [PATCH] added permission and target file --- README.md | 109 ++++++++++++++-------------- foundry.toml | 1 + script/CreateJsonStructScript.s.sol | 44 +++++++++++ script/target/input.json | 15 ++++ test/CreateJson.t.sol | 1 + 5 files changed, 115 insertions(+), 55 deletions(-) create mode 100644 script/CreateJsonStructScript.s.sol create mode 100644 script/target/input.json diff --git a/README.md b/README.md index 781e14d..5f01f63 100644 --- a/README.md +++ b/README.md @@ -25,79 +25,78 @@ To every 'start' method, ensure you close them with the required functions. ## Usage -```solidity - // SPDX-License-Identifier: MIT - pragma solidity ^0.8.0; +- Create a target file in your script folder. In this case, I used this path: "script/target/input.json" +- Assign the target file to a variable like this: `string private constant INPUT_PATH = "script/target/input.json"`; +- Copy the fs_permissions configuration in the foundry.toml file and paste in your foundry config file + +- Example: - import "../src/CreateJsonStruct.sol"; +```solidity +// SPDX-License-Identifier: SEE LICENSE IN LICENSE +pragma solidity ^0.8.0; - contract CreateJsonTest { - CreateJsonStruct createJsonStruct = new CreateJsonStruct(); +import "forge-std/Script.sol"; +import "forge-std/StdJson.sol"; +import "../src/CreateJsonStruct.sol"; - function testMainJsonObjectIsEmpty() public { - createJsonStruct.startMainObject(); - createJsonStruct.closeMainObject(); +contract CreateJsonStructScript is Script { + CreateJsonStruct createJsonStruct = new CreateJsonStruct(); + string private constant INPUT_PATH = "script/target/input.json"; - string memory jsonObject = createJsonStruct.getJson(); - console.log(jsonObject); - assertEq(jsonObject, "{}"); - } + function run() public { + string memory jsonObject = getJson(); + vm.writeFile(INPUT_PATH, jsonObject); + } - function testCreateAStartObject() public { - createJsonStruct.startMainObject(); - createJsonStruct.startObject("mainnet"); - createJsonStruct.addKeyValuePairWithUint("chainid", 1); - createJsonStruct.closeObject(); - createJsonStruct.closeMainObject(); + function getJson() public returns (string memory jsonObject) { + createJsonStruct.startMainObject(); - string memory jsonObject = createJsonStruct.getJson(); - console.log(jsonObject); - assertEq(jsonObject, '{"mainnet": {"chainid": 1}}'); - } + createJsonStruct.startObject("kaia"); - function testCreateArray() public { - createJsonStruct.startMainObject(); + createJsonStruct.addKeyValuePairWithString("network", "testnet"); + createJsonStruct.addKeyValuePairWithString("chainid", "1001"); - createJsonStruct.startObject("kaia"); + createJsonStruct.startArray("developers"); + createJsonStruct.addArrayElementWithString("xx"); + createJsonStruct.addArrayElementWithString("oo"); + createJsonStruct.addArrayElementWithString("ppp"); + createJsonStruct.closeArray(); - createJsonStruct.addKeyValuePairWithString("network", "testnet"); - createJsonStruct.addKeyValuePairWithString("chainid", "1001"); + createJsonStruct.closeObject(); - createJsonStruct.startArray("developers"); - createJsonStruct.addArrayElementWithString("xx"); - createJsonStruct.addArrayElementWithString("oo"); - createJsonStruct.addArrayElementWithString("pp"); - createJsonStruct.closeArray(); + createJsonStruct.startObject("base"); + createJsonStruct.addKeyValuePairWithString("network", "testnet"); + createJsonStruct.addKeyValuePairWithString("chainid", "8354"); + createJsonStruct.closeObject(); + createJsonStruct.closeMainObject(); - createJsonStruct.closeObject(); + jsonObject = createJsonStruct.getJson(); - createJsonStruct.startObject("base"); - createJsonStruct.addKeyValuePairWithString("network", "testnet"); - createJsonStruct.addKeyValuePairWithString("chainid", "8354"); - createJsonStruct.closeObject(); - createJsonStruct.closeMainObject(); + console.log("file has been written to script/target/input.json"); - string memory jsonObject = createJsonStruct.getJson(); - console.log(jsonObject); - } } +} + ``` -## Test Output +## Output ```shell - Ran 3 tests for test/CreateJson.t.sol:CreateJsonTest -[PASS] testCreateAStartObject() (gas: 53109) -Logs: - {"mainnet": {"chainid": 1}} - -[PASS] testCreateArray() (gas: 226954) -Logs: - {"kaia": {"network": "testnet","chainid": "1001","developers": ["xx","oo","pp"]}, "base": {"network": "testnet","chainid": "8354"}} - -[PASS] testMainJsonObjectIsEmpty() (gas: 60915) -Logs: - {} +{ + "kaia": { + "network": "testnet", + "chainid": "1001", + "developers": [ + "xx", + "oo", + "ppp" + ] + }, + "base": { + "network": "testnet", + "chainid": "8354" + } +} ``` ## Documentation diff --git a/foundry.toml b/foundry.toml index 25b918f..ba8683e 100644 --- a/foundry.toml +++ b/foundry.toml @@ -2,5 +2,6 @@ src = "src" out = "out" libs = ["lib"] +fs_permissions = [{access = "read-write", path = "./"}] # See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options diff --git a/script/CreateJsonStructScript.s.sol b/script/CreateJsonStructScript.s.sol new file mode 100644 index 0000000..b99bc58 --- /dev/null +++ b/script/CreateJsonStructScript.s.sol @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: SEE LICENSE IN LICENSE +pragma solidity ^0.8.0; + +import "forge-std/Script.sol"; +import "forge-std/StdJson.sol"; +import "../src/CreateJsonStruct.sol"; + +contract CreateJsonStructScript is Script { + CreateJsonStruct createJsonStruct = new CreateJsonStruct(); + string private constant INPUT_PATH = "script/target/input.json"; + + function run() public { + string memory jsonObject = getJson(); + vm.writeFile(INPUT_PATH, jsonObject); + } + + function getJson() public returns (string memory jsonObject) { + createJsonStruct.startMainObject(); + + createJsonStruct.startObject("kaia"); + + createJsonStruct.addKeyValuePairWithString("network", "testnet"); + createJsonStruct.addKeyValuePairWithString("chainid", "1001"); + + createJsonStruct.startArray("developers"); + createJsonStruct.addArrayElementWithString("xx"); + createJsonStruct.addArrayElementWithString("oo"); + createJsonStruct.addArrayElementWithString("ppp"); + createJsonStruct.closeArray(); + + createJsonStruct.closeObject(); + + createJsonStruct.startObject("base"); + createJsonStruct.addKeyValuePairWithString("network", "testnet"); + createJsonStruct.addKeyValuePairWithString("chainid", "8354"); + createJsonStruct.closeObject(); + createJsonStruct.closeMainObject(); + + jsonObject = createJsonStruct.getJson(); + + console.log(jsonObject, "has been written to script/target/input.json"); + // assertEq(jsonObject, "{}"); + } +} diff --git a/script/target/input.json b/script/target/input.json new file mode 100644 index 0000000..3409ac1 --- /dev/null +++ b/script/target/input.json @@ -0,0 +1,15 @@ +{ + "kaia": { + "network": "testnet", + "chainid": "1001", + "developers": [ + "xx", + "oo", + "ppp" + ] + }, + "base": { + "network": "testnet", + "chainid": "8354" + } +} \ No newline at end of file diff --git a/test/CreateJson.t.sol b/test/CreateJson.t.sol index 304938d..bf22fd7 100644 --- a/test/CreateJson.t.sol +++ b/test/CreateJson.t.sol @@ -2,6 +2,7 @@ pragma solidity ^0.8.0; import {Test, console} from "forge-std/Test.sol"; +import "forge-std/StdJson.sol"; import "../src/CreateJsonStruct.sol"; contract CreateJsonTest is Test {