Generating Your Precompile
In this section, we will go over the process for automatically generating the template code which you can configure accordingly for your stateful precompile.
First, we must create the Solidity interface that we want our precompile to implement. This will be the HelloWorld Interface. It will have two simple functions, sayHello()
, setGreeting()
and an event GreetingChanged
. These two functions will demonstrate the getting and setting respectively of a value stored in the precompile's state space.
The sayHello()
function is a view
function, meaning it does not modify the state of the precompile and returns a string result. The setGreeting()
function is a state changer function, meaning it modifies the state of the precompile. The HelloWorld
interface inherits IAllowList
interface to use the allow list functionality.
For this tutorial, we will be working in a new branch in Subnet-EVM/Precompile-EVM repo.
Then checkout to a new branch:
We will start off in this directory ./contracts/
:
Create a new file called IHelloWorld.sol
and copy and paste the below code:
Now we have an interface that our precompile can implement! Let's create an ABI of our Solidity interface.
In the same directory, let's run:
This generates the ABI code under ./abis/IHelloWorld.abi
.
As you can see the ABI also contains the IAllowList
interface functions. This is because the IHelloWorld
interface inherits from the IAllowList
interface.
Note: The ABI must have named outputs in order to generate the precompile template.
Now that we have an ABI for the precompile gen tool to interact with, we can run the following command to generate our HelloWorld precompile files!
Let's go back to the root of the repository and run the PrecompileGen script helper:
Both of these Subnet-EVM and Precompile-EVM have the same generate_precompile.sh
script. The one in Precompile-EVM installs the script from Subnet-EVM and runs it.
Now let's generate the precompile template files!
In Subnet-EVM precompile implementations reside under the ./precompile/contracts
directory. Let's generate our precompile template in the ./precompile/contracts/helloworld
directory, where helloworld
is the name of the Go package we want to generate the precompile into.
This generates a precompile template files contract.go
, contract.abi
, config.go
, module.go
, event.go
and README.md
files. README.md
explains general guidelines for precompile development. You should carefully read this file before modifying the precompile template.
Let's follow these steps and create our HelloWorld precompile.
Last updated on