Sushi Farmer - contract monitoring

This tutorial looks at how to monitor your farms so you can top off the farm accounts when needed.
We are looking at Sushi in this example, but it works just as well with any other farm you need to manage.
Its broken up into 3 main parts

  1. Creating a blueprint that will allow us to watch monitor the balance of any farm account
  2. Deploying a stream that will monitor specific farm accounts
  3. Consuming that stream to see historical and real-time changes of the rate

1. Creating a blueprint

We start by creating a blueprint that will monitor the balanceOf farm accounts in the SUSHI token.
Navigate to the Streams page and click the Stream Blueprints view.

26322632

Click the Create Stream Blueprint button
Since we are going to be watching when the value of a contract call is less than X, we will pick the Contract View Function type. That will monitor this view function and if the value changes so it goes out of line of the filter you set, it will fire an event with all the context.
In this particular example, we will be watching when these farm account have less than 1.000.000 SUSHI available, and then fire of an event with the current balance as well as other information.

26002600

In the network we choose Ethereum and then we put the SUSHI token contract address, as that is the one we will be monitoring.

26002600

When clicking continue, we can pick what View function we are going to monitor, in our case it's the balanceOf function.

26002600

The balance of function takes one argument, an address you want to know the balance for. Since we want to deploy this blueprint for multiple addresses, we put that as a variable by checking the Variable checkbox.
We also add a filter, since we only want to know when this balance is less than 1.000.000, we also make that value a variable, so we can change it later in a running stream.

26002600

We then give our blueprint a good name and save it.

26002600

In the blueprint overview we can now deploy this blueprint.
In this overview we can see that we are looking at the balanceOf and the variables this blueprint needs to be deployed are:

  • account: that refers to the account we want the balance of
  • unnamed-balanceOf-output-idx-0: that refers to the amount we set our threshold as, it will set an event on the stream when the balance falls beneath this value.
26002600

2. Deploying a stream for monitoring Sushi Farm

We are going to deploy two farm monitors, one we will configure through the UI, and the other we will deploy with code ( shell in this case ).

deploying with UI

To deploy from the UI, we go to the Blueprint page, find the blueprint card and press the Deploy button.
It brings up this screen, where we input the farm address into account and put the balance as 1311738 in this case. When we then click Deploy Stream, a process will be created that monitors the blockchain and puts data on this stream when the value goes below our threshold.

20662066

Deploying a stream with code

We can deploy the same blueprint through code with this simple command, to run it we will need an API key, and you can create one using this guide Getting an API Key

curl --request POST \
     --url https://api.hal.xyz/v1/blueprints/addb634b-7442-40c1-8176-b75cfcd50cac/deploy \
     --header 'Accept: application/json' \
     --header 'Content-Type: application/json' \
     --header 'X-Api-Key: <YOUR API KEY>' \
     --data '
{
     "variables": {
          "account": "0xc2EdaD668740f1aA35E4D8f227fB8E17dcA888Cd",
          "unnamed-balanceOf-output-idx-0": "1000000"
     },
     "name": "Monitor SUSHI farm account: 2"
}
'

We can view our deployed stream config by making this API call

curl --request GET \
     --url https://api.hal.xyz/v1/stream/1d7bcd57-dcb0-4184-94f8-057e7bbf208b/deploy \
     --header 'Accept: application/json' \
     --header 'Content-Type: application/json' \
     --header 'X-Api-Key: <YOUR API KEY>'

3. Consuming a stream

Now we want to consume this stream, and if we get an event, we could automate topping off that farm account with our internal systems.
Now the Stream won't have any event, as the balance is higher than the threshold that we put, we can update the stream to have a way lower threshold to get an event.

Update variables in a running stream

curl --request PUT \
     --url https://api.hal.xyz/v1/stream/62da0b3c-5f82-4884-b7b0-40cfcafea169 \
     --header 'Accept: application/json' \
     --header 'Content-Type: application/json' \
     --header 'X-Api-Key: <YOUR API KEY>'
      --data '
     {
          "variables": {
              "account": "0xc2EdaD668740f1aA35E4D8f227fB8E17dcA888Cd",
              "unnamed-balanceOf-output-idx-0": "500000"
         }
     }
'

With the variable updated, the monitor will match and when we query the stream we get and event like this

{
    "blockHash": "0x5a4e4f4cd9f746df9fb60b7cecd6ef9fb34c989bece335c6a4272c5803976abf",
    "blockNumber": 15532737,
    "blockTimestamp": 1663155587,
    "viewFunctionInput": {
      "args": {
        "account": "0xc2EdaD668740f1aA35E4D8f227fB8E17dcA888Cd"
      },
      "method": "balanceOf",
      "contractAddress": "0x6b3595068778dd592e39a122f4f5a5cf09c90fe2"
    },
    "viewFunctionOutput": {
      "0": "1296293788598927532005243"
    }
  }

That would tell us that we would need to top of the account as when we convert the viewFunctionOutput to SUSHI, we see that we have (1296293788598927532005243 divided by 10^18) or 1.296.293 Sushi tokens still.

Summary

In this tutorial we looked at a couple of things.

  • Creating blueprint with variables to watch multiple accounts
  • Deploying it to running streams that are monitoring the balanceOf our accounts
  • Consume that stream and have the ability to take action when we need to top it off.