logoAcademy

Creating Counter Precompile

Learn how to create a Stateful Counter Precompiles in Avalanche L1 EVM.

What We Are Building

It's time to build our first stateful precompile. In particular, we'll build a counter that keeps track of an integer. Our Counter precompile will have the following logic:

  • Users are able to get the current value of the counter
  • Users are able to set a new value to the counter
  • Users are able to increment the value of the counter

To help you understand, we've provided a reference stateful precompile: StringStore. As the name suggests, StringStore stores a string that users can change. This string is stored in the EVM state.

Overview of Steps

Compared to the process before, we must also add tests for our precompile. Here's a quick overview of the steps we'll follow:

  1. Create a Solidity interface for the precompile and generate the ABI
  2. Generate the precompile Go boilerplate files
  3. Write the precompile code in Go with access to the EVM state
  4. Set the initial state in the configurator and register the precompile
  5. Add and run tests
  6. Build and run your customized EVM
  7. Connect Remix to your customized EVM and interact with the counter

This tutorial will help you create more complex precompiles. Let's begin!

On this page