Skip to content

Commit

Permalink
add hello world precompile
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur committed Aug 30, 2023
1 parent 197588c commit 283d9ca
Show file tree
Hide file tree
Showing 30 changed files with 1,171 additions and 10 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/sync.yml
@@ -0,0 +1,24 @@
name: Sync
on:
push:
branches:
- main

jobs:
sync-branches:
runs-on: ubuntu-latest
name: Syncing branches
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Node
uses: actions/setup-node@v1
with:
node-version: 12
- name: Opening pull request
id: pull
uses: tretuna/sync-branches@1.4.0
with:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
FROM_BRANCH: "main"
TO_BRANCH: "develop"
101 changes: 101 additions & 0 deletions README.md
Expand Up @@ -34,10 +34,111 @@ To get a comprehensive introduction to Precompile-EVM, take the Avalanche Academ

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.

### Clone the Repo

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

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

```zsh
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

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

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

### 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
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
```

### Generate an `.abi`

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

Passing in the following flags

- `--abi`
- ABI specification of the contracts.
- `--base-path path`
- Use the given path as the root of the source tree instead of the root of the filesystem.
- `--include-path path`
- Make an additional source directory available to the default import callback. Use this option if you want to import contracts whose location is not fixed in relation to your main source tree, e.g. third-party libraries installed using a package manager. Can be used multiple times. Can only be used if base path has a non-empty value.
- `--output-dir path`
- If given, creates one file per output component and contract/file at the specified directory.
- `--overwrite`
- Overwrite existing files (used together with `--output-dir`).

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

Compiler run successful. Artifact(s) can be found in directory "abis".
```

### 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
cd .. # change directory back to the root directory of the repo
./scripts/generate_precompile.sh --abi contracts/abis/IHolaMundo.abi --out holamundo/

Using branch: hello-world-example
installing precompilegen from Subnet-EVM v0.5.2
generating precompile with Subnet-EVM v0.5.2
Precompile files generated successfully at: holamundo/
```

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

```zsh
ls -lh helloworld

-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
-rw-r--r-- 1 user group 963B Jul 5 13:26 contract.abi
-rw-r--r-- 1 user group 8.1K Jul 5 13:26 contract.go
-rw-r--r-- 1 user group 8.3K Jul 5 13:26 contract_test.go
-rw-r--r-- 1 user group 2.7K Jul 5 13:26 module.go
```

### Register Precompile

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`.
Expand Down
1 change: 1 addition & 0 deletions contracts/abis/IAllowList.abi
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"readAllowList","outputs":[{"internalType":"uint256","name":"role","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"setAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"setEnabled","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"setNone","outputs":[],"stateMutability":"nonpayable","type":"function"}]
1 change: 1 addition & 0 deletions contracts/abis/IHelloWorld.abi
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"readAllowList","outputs":[{"internalType":"uint256","name":"role","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"sayHello","outputs":[{"internalType":"string","name":"result","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"setAdmin","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"setEnabled","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"response","type":"string"}],"name":"setGreeting","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"addr","type":"address"}],"name":"setNone","outputs":[],"stateMutability":"nonpayable","type":"function"}]
Empty file removed contracts/contracts/.gitkeep
Empty file.
19 changes: 19 additions & 0 deletions contracts/contracts/ExampleHelloWorld.sol
@@ -0,0 +1,19 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./interfaces/IHelloWorld.sol";

address constant HELLO_WORLD_ADDRESS = 0x0300000000000000000000000000000000000000;

// ExampleHelloWorld shows how the HelloWorld precompile can be used in a smart contract.
contract ExampleHelloWorld {
IHelloWorld helloWorld = IHelloWorld(HELLO_WORLD_ADDRESS);

function sayHello() public view returns (string memory) {
return helloWorld.sayHello();
}

function setGreeting(string calldata greeting) public {
helloWorld.setGreeting(greeting);
}
}
Empty file.
12 changes: 12 additions & 0 deletions contracts/contracts/interfaces/IHelloWorld.sol
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT

pragma solidity >=0.8.0;
import "@avalabs/subnet-evm-contracts/contracts/interfaces/IAllowList.sol";

interface IHelloWorld is IAllowList {
// sayHello returns the stored greeting string
function sayHello() external view returns (string calldata result);

// setGreeting stores the greeting string
function setGreeting(string calldata response) external;
}
Empty file removed contracts/contracts/test/.gitkeep
Empty file.
42 changes: 42 additions & 0 deletions contracts/contracts/test/ExampleHelloWorldTest.sol
@@ -0,0 +1,42 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../ExampleHelloWorld.sol";
import "../interfaces/IHelloWorld.sol";
import "@avalabs/subnet-evm-contracts/contracts/test/AllowListTest.sol";

contract ExampleHelloWorldTest is AllowListTest {
IHelloWorld helloWorld = IHelloWorld(HELLO_WORLD_ADDRESS);

function step_getDefaultHelloWorld() public {
ExampleHelloWorld example = new ExampleHelloWorld();
address exampleAddress = address(example);

assertRole(helloWorld.readAllowList(exampleAddress), AllowList.Role.None);
assertEq(example.sayHello(), "Hello World!");
}

function step_doesNotSetGreetingBeforeEnabled() public {
ExampleHelloWorld example = new ExampleHelloWorld();
address exampleAddress = address(example);

assertRole(helloWorld.readAllowList(exampleAddress), AllowList.Role.None);

try example.setGreeting("testing") {
assertTrue(false, "setGreeting should fail");
} catch {} // TODO should match on an error to make sure that this is failing in the way that's expected
}

function step_setAndGetGreeting() public {
ExampleHelloWorld example = new ExampleHelloWorld();
address exampleAddress = address(example);

assertRole(helloWorld.readAllowList(exampleAddress), AllowList.Role.None);
helloWorld.setEnabled(exampleAddress);
assertRole(helloWorld.readAllowList(exampleAddress), AllowList.Role.Enabled);

string memory greeting = "testgreeting";
example.setGreeting(greeting);
assertEq(example.sayHello(), greeting);
}
}
2 changes: 1 addition & 1 deletion contracts/hardhat.config.ts
@@ -1,6 +1,6 @@
import "@nomiclabs/hardhat-waffle"
import "@nomiclabs/hardhat-ethers"
import "./tasks.ts"
import "./tasks"

// HardHat users must populate these environment variables in order to connect to their subnet-evm instance
// Since the blockchainID is not known in advance, there's no good default to use and we use the C-Chain here.
Expand Down
1 change: 1 addition & 0 deletions contracts/package.json
Expand Up @@ -32,6 +32,7 @@
},
"license": "BSD-3-Clause",
"scripts": {
"build": "rm -rf dist/ && tsc -b",
"compile": "npx hardhat compile",
"console": "npx hardhat console",
"test": "npx hardhat test",
Expand Down
Empty file removed contracts/scripts/.gitkeep
Empty file.
20 changes: 20 additions & 0 deletions contracts/scripts/deployExampleHelloWorld.ts
@@ -0,0 +1,20 @@
import {
Contract,
ContractFactory
} from "ethers"
import { ethers } from "hardhat"

const main = async (): Promise<any> => {
const contractFactory: ContractFactory = await ethers.getContractFactory("ExampleHelloWorld")
const contract: Contract = await contractFactory.deploy()

await contract.deployed()
console.log(`Contract deployed to: ${contract.address}`)
}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})
61 changes: 60 additions & 1 deletion contracts/tasks.ts
Expand Up @@ -2,13 +2,28 @@ import { task } from "hardhat/config"
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"
import { BigNumber } from "ethers"

const HELLO_WORLD_ADDRESS = "0x0300000000000000000000000000000000000000"

const ROLES = {
0: "None",
1: "Enabled",
2: "Admin",
}

const getRole = async (allowList, address) => {
const role = await allowList.readAllowList(address)
console.log(`${address} has role: ${ROLES[role.toNumber()]}`)
}

// npx hardhat accounts --network local
task("accounts", "Prints the list of accounts", async (args, hre): Promise<void> => {
const accounts: SignerWithAddress[] = await hre.ethers.getSigners()
accounts.forEach((account: SignerWithAddress): void => {
console.log(account.address)
})
})

// npx hardhat balances --network local
task("balances", "Prints the list of account balances", async (args, hre): Promise<void> => {
const accounts: SignerWithAddress[] = await hre.ethers.getSigners()
for (const account of accounts) {
Expand All @@ -19,7 +34,7 @@ task("balances", "Prints the list of account balances", async (args, hre): Promi
}
})


// npx hardhat balance --network local --address [address]
task("balance", "get the balance")
.addParam("address", "the address you want to know balance of")
.setAction(async (args, hre) => {
Expand All @@ -28,5 +43,49 @@ task("balance", "get the balance")
console.log(`balance: ${balanceInCoin} Coin`)
})

// npx hardhat helloWorld:readRole --network local --address [address]
task("helloWorld:readRole", "Gets the network enabled allow list")
.addParam("address", "the address you want to know the allowlist role for")
.setAction(async (args, hre) => {
const allowList = await hre.ethers.getContractAt("IHelloWorld", HELLO_WORLD_ADDRESS)
await getRole(allowList, args.address)
})

// npx hardhat helloWorld:addEnabled --network local --address [address]
task("helloWorld:addEnabled", "Adds the enabled on the allow list")
.addParam("address", "the address you want to add as a enabled")
.setAction(async (args, hre) => {
const allowList = await hre.ethers.getContractAt("IHelloWorld", HELLO_WORLD_ADDRESS)
// ADD CODE BELOW
await allowList.setEnabled(args.address)
await getRole(allowList, args.address)
})

// npx hardhat helloWorld:addAdmin --network local --address [address]
task("helloWorld:addAdmin", "Adds an admin on the allowlist")
.addParam("address", "the address you want to add as a admin")
.setAction(async (args, hre) => {
const allowList = await hre.ethers.getContractAt("IHelloWorld", HELLO_WORLD_ADDRESS)
await allowList.setAdmin(args.address)
await getRole(allowList, args.address)
})

// npx hardhat helloWorld:sayHello --network local
task("helloWorld:sayHello", "Says hello")
.setAction(async (args, hre) => {
const helloWorld = await hre.ethers.getContractAt("IHelloWorld", HELLO_WORLD_ADDRESS)
const result = await helloWorld.sayHello()
console.log(result)
})

// npx hardhat helloWorld:setGreeting --network local --greeting [greeting]
task("helloWorld:setGreeting", "Says hello")
.addParam("greeting", "the greeting string you want to set")
.setAction(async (args, hre) => {
const helloWorld = await hre.ethers.getContractAt("IHelloWorld", HELLO_WORLD_ADDRESS)
const result = await helloWorld.setGreeting(args.greeting)
console.log(result)
})



Empty file removed contracts/test/.gitkeep
Empty file.
34 changes: 34 additions & 0 deletions contracts/test/hello_world.ts
@@ -0,0 +1,34 @@
// (c) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

import { ethers } from "hardhat"
import { test } from "@avalabs/subnet-evm-contracts"

// make sure this is always an admin for hello world precompile
const ADMIN_ADDRESS = "0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC"
const HELLO_WORLD_ADDRESS = "0x0300000000000000000000000000000000000000"

describe("ExampleHelloWorldTest", function () {
this.timeout("30s")

beforeEach('Setup DS-Test contract', async function () {
const signer = await ethers.getSigner(ADMIN_ADDRESS)
const helloWorldPromise = ethers.getContractAt("IHelloWorld", HELLO_WORLD_ADDRESS, signer)

return ethers.getContractFactory("ExampleHelloWorldTest", { signer })
.then(factory => factory.deploy())
.then(contract => {
this.testContract = contract
return contract.deployed().then(() => contract)
})
.then(() => Promise.all([helloWorldPromise]))
.then(([helloWorld]) => helloWorld.setAdmin(this.testContract.address))
.then(tx => tx.wait())
})

test("should gets default hello world", ["step_getDefaultHelloWorld"])

test("should not set greeting before enabled", "step_doesNotSetGreetingBeforeEnabled")

test("should set and get greeting with enabled account", "step_setAndGetGreeting")
});

0 comments on commit 283d9ca

Please sign in to comment.