Skip to content

Commit

Permalink
added permission and target file
Browse files Browse the repository at this point in the history
  • Loading branch information
paulelisha authored and paulelisha committed Oct 14, 2024
1 parent 993da30 commit da2cdb4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 55 deletions.
109 changes: 54 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 44 additions & 0 deletions script/CreateJsonStructScript.s.sol
Original file line number Diff line number Diff line change
@@ -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, "{}");
}
}
15 changes: 15 additions & 0 deletions script/target/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"kaia": {
"network": "testnet",
"chainid": "1001",
"developers": [
"xx",
"oo",
"ppp"
]
},
"base": {
"network": "testnet",
"chainid": "8354"
}
}
1 change: 1 addition & 0 deletions test/CreateJson.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit da2cdb4

Please sign in to comment.