# Setup the Worker

The next thing we have to do is setup our worker handler. Our worker will listen to NewBlock events from tendermint (TX events can also be used if needed) . Every 3 blocks, it will query the ATOM/USDC price from Binance and submit the result. (Under the hood it will automatically submit a prevote first, and the actual claim in the following block).

# Setup Worker Handler

  • Create a new folder cmd/<yourapp>d/worker
  • Inside, crate a file called handlers.go
Copy package worker import ( "encoding/json" "fmt" "net/http" "github.com/relevant-community/oracle/x/atom/types" "github.com/relevant-community/oracle/x/oracle/client/cli" oracletypes "github.com/relevant-community/oracle/x/oracle/types" rpctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/spf13/cobra" ) type AtomData struct { Symbol string `json:"symbol"` Price string `json:"price"` } // HandleTx is our custom tx handler func HandleTx(cmd *cobra.Command, txEvent rpctypes.ResultEvent) error { return nil } // HandleBlock is our custom block handler func HandleBlock(cmd *cobra.Command, blockEvent rpctypes.ResultEvent) error { helper, err := cli.NewWorkerHelper(cmd, blockEvent) if err != nil { return err } // for each claim type in the array, run the approriate handler for _, param := range helper.OracleParams.ClaimParams { switch param.ClaimType { case types.AtomClaim: getAtomPrice(cmd, helper, param) } } return nil } func getAtomPrice(cmd *cobra.Command, helper *cli.WorkerHelper, param oracletypes.ClaimParams) error { // use VotePeriod to check if its time to submit a claim if helper.IsRoundStart(param.ClaimType) == false { return nil } // fetch ATOM/USDC pair price from Binance var atomData = AtomData{} r, err := http.Get("https://api.binance.com/api/v3/ticker/price?symbol=ATOMUSDC") if err != nil { fmt.Println("Error fetching ATOM price") return err } defer r.Body.Close() json.NewDecoder(r.Body).Decode(&atomData) // create a Claim about AtomUSD price atomUsd := types.NewAtomUsd(helper.BlockHeight, atomData.Price) // run process for the given claimType if atomUsd == nil { fmt.Println("Error creating claim") return nil } // submit our claim helper.SubmitWorkerTx(atomUsd) return nil }

# Attach Our Handlers

We need to tell the Oracle module about our worker code. To do this, navigatate to the cmd/<your_app>d/cmd/root.go file.

  1. Import the Oracle module CLI package

add this line to the initRootCmd method before rootCmd.AddCommand

Copy func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { ... oraclecli.InitializeWorker(worker.HandleBlock, worker.HandleTx) // add keybase, auxiliary RPC, query, and tx child commands rootCmd.AddCommand( rpc.StatusCommand(), queryCommand(), txCommand(), keys.Commands(app.DefaultNodeHome), ) }

(make sure to import oraclecli "github.com/relevant-community/oracle/x/oracle/client/cli") )

# Run the Worker!

We should now be able to run our worker!

From a new terminal window, run:

Copy $ <your_appd> tx oracle start-worker --from alice -o text -y

*replace alice with your app's validator address if needed

You should see some transaction logs in your terminal.

Try querying the oracle state:

Copy $ <your_appd> query oracle all-claims $ <your_appd> query oracle pending-rounds AtomClaim $ <your_appd> query oracle all-rounds