Skip to content

Commit

Permalink
Merge branch 'hello-world-example' of https://github.com/ava-labs/pre…
Browse files Browse the repository at this point in the history
…compile-evm into hello-world-example
  • Loading branch information
ceyonur committed Oct 14, 2024
2 parents 2fa0105 + 9823d9d commit 80ec6f7
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 106 deletions.
5 changes: 0 additions & 5 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,3 @@ FROM mcr.microsoft.com/devcontainers/base

COPY --from=avalanchego /avalanchego/build /go/src/github.com/ava-labs/avalanchego/build
COPY --from=avalanche-cli /avalanche /usr/local/bin/avalanche

COPY --from=foundry /usr/local/bin/forge /usr/local/bin/forge
COPY --from=foundry /usr/local/bin/cast /usr/local/bin/cast
COPY --from=foundry /usr/local/bin/anvil /usr/local/bin/anvil
COPY --from=foundry /usr/local/bin/chisel /usr/local/bin/chisel
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"version": "latest"
},
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers/features/go:1": {}
"ghcr.io/devcontainers/features/go:1": {"version": 1.22}
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
Expand Down
129 changes: 75 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,66 +28,46 @@ In order to run the Dev Container locally:

## Learn about Precompile-EVM

To get a comprehensive introduction to Precompile-EVM, take the Avalanche Academy course on [Customizing the EVM](https://academy.avax.com/course/customize-evm).
To get a comprehensive introduction to Precompile-EVM, take the Avalanche Academy course on [Customizing the EVM](https://academy.avax.com/course/customizing-evm).

## How to use
## Hello World Example

There is an example branch [hello-world-example](https://github.com/ava-labs/precompile-evm/tree/hello-world-example) in this repository. You can check the example branch to see how to register precompiles and test them.
### 1. Clone the Repo

### Clone the Repo

```zsh
```bash
git clone https://github.com/ava-labs/precompile-evm.git
cd precompile-evm/ # change directory to the precompile-evm/ directory
cd precompile-evm/
```

### Checkout the `hello-world-example` Branch
### 2. Checkout the `hello-world-example` Branch

```zsh
```bash
git checkout hello-world-example

branch 'hello-world-example' set up to track 'origin/hello-world-example'.
Switched to a new branch 'hello-world-example'
```

### Install NodeJS Dependencies
### 3. Install NodeJS Dependencies

First you have to `cd contracts/` and run `npm install` to get the dependencies.

```zsh
cd contracts/ # change directory to the contracts/ directory
```bash
cd contracts/
npm install
```

### Create a New Contract
### 4. Create a New Contract

`hello-world-example` branch has already a precompile contract called `HelloWorld.sol`. All necessary files were already created for you. You can check existing files and see how a fully implemented precompile should look like. If you'd like to redo steps to create a new precompile contract, you can follow the steps below.

Copy the existing `IHelloWorld.sol` interface to a new file called `IHolaMundo.sol`.

```zsh
```bash
cd .. # change directory back to the root of the repo
cp contracts/contracts/interfaces/IHelloWorld.sol contracts/contracts/interfaces/IHolaMundo.sol
```

### Install `solc` and Confirm Dependency Version

Install the `solc` dependency.

```zsh
brew install solidity
```

Confirm `solc` is >=0.8.8.

```zsh
solc --version

solc, the solidity compiler commandline interface
Version: 0.8.17+commit.8df45f5f.Darwin.appleclang
```bash
cp contracts/contracts/interfaces/IHelloWorld.sol contracts/contracts/interfaces/IHolaMundo.sol
```

### Generate an `.abi`
### 5. Generate an ABI

Now generate a `.abi` from a `.sol` using `solc`.

Expand All @@ -104,21 +84,32 @@ Passing in the following flags
- `--overwrite`
- Overwrite existing files (used together with `--output-dir`).

```zsh
```bash
cd contracts/ # change directory to the contracts/ directory
solc --abi contracts/interfaces/IHolaMundo.sol --output-dir abis --base-path . --include-path ./node_modules --overwrite
```
```bash
npx solc --abi contracts/interfaces/IHolaMundo.sol --output-dir abis --base-path . --include-path ./node_modules
```

Compiler run successful. Artifact(s) can be found in directory "abis".
Rename the files for easier readability
```bash
mv abis/@avalabs_subnet-evm-contracts_contracts_interfaces_IAllowList_sol_IAllowList.abi abis/IAllowList.abi
```
```bash
mv abis/contracts_interfaces_IHolaMundo_sol_IHelloWorld.abi abis/IHolaMundo.abi
```

### Generate Precompile Files
### 6. Generate Precompile Files

First, you need to create your precompile contract interface in the `contracts` directory and build the ABI. Then you can generate your precompile files with `./scripts/generate_precompile.sh --abi {abiPath} --out {outPath}`. This script installs the `precompilegen` tool from Subnet-EVM and runs it to generate your precompile.

```zsh
```bash
cd .. # change directory back to the root directory of the repo
```
```bash
./scripts/generate_precompile.sh --abi contracts/abis/IHolaMundo.abi --out holamundo/

```
```bash
Using branch: hello-world-example
installing precompilegen from Subnet-EVM v0.5.2
generating precompile with Subnet-EVM v0.5.2
Expand All @@ -127,9 +118,10 @@ Precompile files generated successfully at: holamundo/

Confirm that the new `holamundo/` directory has the appropriate files.

```zsh
ls -lh helloworld

```bash
ls -lh holamundo
```
```bash
-rw-r--r-- 1 user group 2.3K Jul 5 13:26 README.md
-rw-r--r-- 1 user group 2.3K Jul 5 13:26 config.go
-rw-r--r-- 1 user group 2.8K Jul 5 13:26 config_test.go
Expand All @@ -139,28 +131,50 @@ ls -lh helloworld
-rw-r--r-- 1 user group 2.7K Jul 5 13:26 module.go
```

### Register Precompile
### 7. Register Precompile

In `plugin/main.go` Subnet-EVM is already imported and ready to be Run from the main package.

In `plugin/main.go` Subnet-EVM is already imported and ready to be Run from the main package. All you need to do is explicitly register your precompiles to Subnet-EVM in `plugin/main.go` and build it together with Subnet-EVM. Precompiles generated by `precompilegen` tool have a self-registering mechanism in their `module.go/init()` function. All you need to do is to force-import your precompile packprecompile package in `plugin/main.go`.
All you need to do is explicitly register your precompiles to Subnet-EVM in `plugin/main.go` and build it together with Subnet-EVM.

### Build
Precompiles generated by `precompilegen` tool have a self-registering mechanism in their `module.go/init()` function.

You can build your precompile and Subnet-EVM with `./scripts/build.sh`. This script builds Subnet-EVM, and your precompile together and generates a binary file. The binary file is compatible with AvalancheGo plugins.
```go
package main
import (
"fmt"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/subnet-evm/plugin/evm"
"github.com/ava-labs/subnet-evm/plugin/runner"
// Each precompile generated by the precompilegen tool has a self-registering init function
// that registers the precompile with the subnet-evm. Importing the precompile package here
// will cause the precompile to be registered with the subnet-evm.
_ "github.com/ava-labs/precompile-evm/helloworld"
// ADD YOUR PRECOMPILE HERE
//_ "github.com/ava-labs/precompile-evm/holamundo"
)
```

### Run
### 8. Build

You can run you Precompile-EVM by using the Avalanche CLI.
You can build your precompile and Subnet-EVM with
```bash
./scripts/build.sh
```
This script builds Subnet-EVM, and your precompile together and generates a binary file. The binary file is compatible with AvalancheGo plugins.

First, create the configuration for your subnet.
### 9. Run

You can now run Precompile-EVM by using the Avalanche-CLI

```bash
avalanche subnet create mysubnet --custom --vm $AVALANCHEGO_PLUGIN_PATH/srEXiWaHuhNyGwPUi444Tu47ZEDwxTWrbQiuD7FmgSAQ6X7Dy --genesis ./.devcontainer/genesis-example.json
avalanche blockchain create myblockchain --custom --vm $AVALANCHEGO_PLUGIN_PATH/srEXiWaHuhNyGwPUi444Tu47ZEDwxTWrbQiuD7FmgSAQ6X7Dy --genesis ./.devcontainer/genesis-example.json
```

Next, launch the Subnet with your custom VM:
Then launch the blockchain with your custom VM:

```bash
avalanche subnet deploy mysubnet
avalanche blockchain deploy myblockchain
```

### Test
Expand All @@ -175,8 +189,15 @@ In order to upgrade the Subnet-EVM version, you need to change the version in `g

```text
[v0.2.0] [email protected] (Protocol Version: 33)
<<<<<<< HEAD
[v0.2.1] [email protected] (Protocol Version: 35)
[v0.2.2] [email protected] (Protocol Version: 35)
[v0.2.3] [email protected] (Protocol Version: 35)
[v0.2.4] [email protected] (Protocol Version: 37)
```
=======
[v0.2.1] [email protected] (Protocol Version: 35)
[v0.2.2] [email protected] (Protocol Version: 35)
[v0.2.3] [email protected] (Protocol Version: 35)
```
>>>>>>> 9823d9df75a3db1a5466075297b4b03f088ff390
Loading

0 comments on commit 80ec6f7

Please sign in to comment.