Implementing Precompile
Learn how to implement the precompile in `contract.go`
In this section, we will go define the logic for our CalculatorPlus precompile; in particular, we want to add the logic for the following three functions: powOfThree
, moduloPlus
, and simplFrac
.
For those worried about this section - don't be! Our solution only added 12 lines of code to contract.go
.
Looking at Calculator
Before we define the logic of CalculatorPlus, we first will examine the implementation of the Calculator precompile:
Although the code snippet above may be long, you might notice that we added only four lines of code to the autogenerated code provided to us by Precompile-EVM! In particular, we only added lines 19, 48, 79, and 80. In general, note the following:
- Structs vs Singular Values: make sure to keep track which inputs/outputs are structs and which one are values like big.Int. As an example, in
nextTwo
, we are dealing with a big.Int type input. However, in repeat, we are passed in a input of type struct RepeatInput. - Documentation: for both Calculator and CalculatorPlus, the big package documentation is of great reference: https://pkg.go.dev/math/big
Now that we have looked at the implementation for the Calculator precompile, its time you define the CalculatorPlus precompile!
Implementing moduloPlus
We start by looking at the starter code for moduloPlus
:
We want to note the following:
inputStruct
is the input that we want to work with (i.e.inputStruct
contains the two numbers that we want to use for the modulo calculation)- All of our code will go after line 15
- We want the struct output to contain the result of our modulo operation (the struct will contain the multiple and remainder)
With this in mind, try to implement moduloPlus
.
Implementing powOfThree
Likewise, for powOfThree
, we want to define the logic of the function in the custom code section. However, note that while we are working with an output struct, our input is a singular value. With this in mind, take a crack at implementing powOfThree
:
Implementing simplFrac
For implementing simplFrac
, note the following:
- The documentation for the big package will be of use here
- Remember to take care of the case when the denominator is 0
Final Solution
Below is the final solution for the CalculatorPlus precompile: