Skip to content

Commit

Permalink
feat: implement solidity ast parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RayXpub committed Aug 25, 2024
1 parent 814770f commit 82c2e0b
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 193 deletions.
124 changes: 82 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,106 @@
## Foundry

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**

Foundry consists of:
# Spack

Spack parses Solidity structs and packs the fields efficiently to reduce the
number of storage slots they use. It also adds struct packing comments to clearly indicate
how the fields are packed.

It can deal with comments and whitespace in the struct definition, and will
preserve them in the output. It handles unknown types by assuming they cannot be
packed, treating they as `bytes32`.

## Disclaimer

This code is a work in progress and can contain bugs. Use it at your own risk.
Feature request and bug reports are welcome.

### Example

input

```solidity
struct RequestMeta {
uint64 completedRequests;
Custom.Datatype data;
address requestingContract;
uint72 adminFee; // in wei
address subscriptionOwner;
bytes32 flags; // 32 bytes of flags
uint96 availableBalance; // in wei. 0 if not specified.
uint64 subscriptionId;
uint64 initiatedRequests;// number of requests initiated by this contract
uint32 callbackGasLimit;
uint16 dataVersion;
}
```

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.
output

```solidity
struct RequestMeta {
Custom.Datatype data; //
bytes32 flags; //
address requestingContract; // ──╮
uint96 availableBalance; // ─────╯
address subscriptionOwner; // ───╮
uint64 completedRequests; // │
uint32 callbackGasLimit; // ─────╯
uint72 adminFee; // ─────────────╮
uint64 subscriptionId; // │
uint64 initiatedRequests; // │
uint16 dataVersion; // ──────────╯
}
```

## Documentation
## Quickstart

https://book.getfoundry.sh/
build

## Usage
```bash
go build
```

### Build
Selecting all structs from a contract

```shell
$ forge build
```bash
./spack -c <CONTRACT> <COMMAND>
```

### Test
Selecting a specific struct from a contract

```shell
$ forge test
```bash
./spack -c <CONTRACT> -s <STRUCT> <COMMAND>
```

### Format
Counting storage slots

```shell
$ forge fmt
```bash
./spack -c Spack count
```

### Gas Snapshots
Printing structs without optimizations but with struct packing comments

```shell
$ forge snapshot
```bash
./spack -c Spack -u <COMMAND>
```

### Anvil
Printing the struct with optimizations and struct packing comments:

```shell
$ anvil
```
## Commands and flags

### Deploy
### Commands

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```
- `pack` - packs the struct
- `count` - counts the number of storage slots the struct uses

### Cast
### Flags

```shell
$ cast <subcommand>
```
- `-c` or `--contract` - load all structs from a contract
- `-s` or `--struct` - load a specific struct from a contract
- `-u` or `--unoptimized` - print structs without optimizations but with struct packing comments
- `--cfg` or `--config` - load the config from a file

### Help
## TODO

```shell
$ forge --help
$ anvil --help
$ cast --help
```
- [ ] Add more flexible command line options
- [ ] Add tests
- [ ] Improve error handling
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

type GlobalConfig struct {
PrintingConfig PrintingConfig
OutDir string
}

type PrintingConfig struct {
Expand Down
1 change: 1 addition & 0 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
func GetDefaultConfig() GlobalConfig {
return GlobalConfig{
PrintingConfig: GetDefaultPrintingConfig(),
OutDir: "./out/",
}
}

Expand Down
13 changes: 0 additions & 13 deletions examples/RequestMeta.txt

This file was deleted.

Loading

0 comments on commit 82c2e0b

Please sign in to comment.