Write an Off-Chain Handler
The first step is to write our off-chain handler. This handler is responsible for receiving requests from the bundler along with their payloads and returning the appropriate responses.
Next Example
Let's begin by allowing our handler to receive two numbers. It will perform both addition and subtraction on these numbers. If the result of the subtraction results in an underflow (i.e. if b > a
in the expression a-b
), the handler will respond with an underflow error. Create a Python
file in which to copy all the following code blocks.
First, we'll need to import some Python
libraries to help with our example:
The first two, Web3
and eth_abi
, are established PyPI
libraries. The third library, offchain_utils
, was developed by the Boba Foundation. You can view the library on our Github.
Now, we can write our function. Let's start by initializing an err_code
and a resp
object with values in case of an exception:
Next, we'll add a try
block. Inside of it, let's make use of some of our imported library functions to parse and decode our binary data to an array of two unit32
s:
With this decoded information, we can add and subtract the two numbers, re-encode the information (this time to an array of two uint256
s), and check for an overflow. The entire try
block should look like this:
Before we can return these objects, we need to transform them into a specific object. We can do so with the gen_response()
function from our imported library from the Boba Foundation. Putting the whole thing together, our whole offchain_addsub2()
looks like this:
We've now successfully implemented a function which can receive a request from the bundler, perform some calculation with its payload, and return a response. Proceed to the next section to learn more about setting up a server to run this function.
Last updated