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).
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 handlerfuncHandleTx(cmd *cobra.Command, txEvent rpctypes.ResultEvent)error{returnnil}// HandleBlock is our custom block handlerfuncHandleBlock(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 handlerfor_, param :=range helper.OracleParams.ClaimParams {switch param.ClaimType {case types.AtomClaim:getAtomPrice(cmd, helper, param)}}returnnil}funcgetAtomPrice(cmd *cobra.Command, helper *cli.WorkerHelper, param oracletypes.ClaimParams)error{// use VotePeriod to check if its time to submit a claimif helper.IsRoundStart(param.ClaimType)==false{returnnil}// fetch ATOM/USDC pair price from Binancevar 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 claimTypeif atomUsd ==nil{
fmt.Println("Error creating claim")returnnil}// submit our claim
helper.SubmitWorkerTx(atomUsd)returnnil}