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 the Hybrid Compute SDK (viewable on Github here) and any other necessary libraries, depending on whether you develop in Python
or TypeScript
:
Initialize 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 sdk.gen_response()
function from our imported library. Putting the whole thing together, our whole offchain_add_sub()
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