Skip to content

Commit

Permalink
feat: init code
Browse files Browse the repository at this point in the history
  • Loading branch information
Atlasoin committed Oct 20, 2024
0 parents commit 0fcc721
Show file tree
Hide file tree
Showing 16 changed files with 537 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:

env:
FOUNDRY_PROFILE: ci

jobs:
check:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Show Forge version
run: |
forge --version
- name: Run Forge fmt
run: |
forge fmt --check
id: fmt

- name: Run Forge build
run: |
forge build --sizes
id: build

- name: Run Forge tests
run: |
forge test -vvv
id: test
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Compiler files
cache/
out/

# Ignores development broadcast logs
!/broadcast
/broadcast/*/31337/
/broadcast/**/dry-run/

# Docs
docs/

# Dotenv file
.env

node_modules/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
## Cal-Pi-On-Chain

This is a demo repo using OVM contracts lib to calculate Pi onchain.

`./prgrams` contains the python code for computing task.
`./src` is the main entry for the contract code.

## Documentation

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```

### Test

```shell
$ forge test
```

### Format

```shell
$ forge fmt
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
6 changes: 6 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = "src"
out = "out"
libs = ["node_modules","lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 8f24d6
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"@webisopen/ovm-contracts": "^1.0.0",
"@openzeppelin/contracts": "^5.0.2"
}
}
27 changes: 27 additions & 0 deletions programs/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import math
import sys
from typing import Union
import eth_abi
import ovm

bridge = ovm.Bridge()


def cal_pi(n: int) -> Union[bool, str]:
if n > 15:
raise ValueError("n must be less than 15")

return True, format(math.pi, f".{n}f")


def main():
if len(sys.argv) != 2:
raise ValueError("missing messages argument")

(input,) = eth_abi.decode(["int"], bytes.fromhex(sys.argv[1]))

bridge.submit(["bool", "string"], [True, cal_pi(input)])


if __name__ == "__main__":
main()
11 changes: 11 additions & 0 deletions programs/ovm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os
import sys


sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from . import bridge_pb2
from . import bridge_pb2_grpc
from .bridge import Bridge

__all__ = ["Bridge", "bridge_pb2", "bridge_pb2_grpc"]
16 changes: 16 additions & 0 deletions programs/ovm/bridge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import Any, Iterable
import eth_abi
from eth_typing import TypeStr
import grpc
from . import bridge_pb2
from . import bridge_pb2_grpc

class Bridge:
def __init__(self):
self.bridge_client = bridge_pb2_grpc.BridgeStub(grpc.insecure_channel("unix:///var/run/ovm.sock"))

def load(self):
return self.bridge_client.Load(bridge_pb2.LoadRequest())

def submit(self, abi: Iterable[TypeStr], values: Iterable[Any]):
return self.bridge_client.Submit(bridge_pb2.SubmitRequest(output=eth_abi.encode(abi, values).hex()))
45 changes: 45 additions & 0 deletions programs/ovm/bridge_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0fcc721

Please sign in to comment.