// The caller structure
// Fun fact: this structure is the exact same as the Monitor!
type Caller struct {
bvsContract string // The address of the BVS contract
chainIO io.ChainIO // ChainIO to enable communication with the BVS
}
// RunCaller runs the caller by creating a new caller and executing its Run method.
func RunCaller() {
c := NewCaller()
c.Run()
}
// NewCaller creates a new Caller instance.
// The function is nearly the same one as the monitor program
// which is unsuperising since the structure is the same
func NewMonitor() *Monitor {
// Create a new ChainIO instance
// ChainIO is our main interfaace from a off-chain program to any on-chain contract
chainIO, err := io.NewChainIO(core.C.Chain.Id, core.C.Chain.Rpc, core.C.Owner.KeyDir, core.C.Owner.Bech32Prefix, elkLogger, metricsIndicators, types.TxManagerParams{
MaxRetries: 3,
RetryInterval: 1 * time.Second,
ConfirmationTimeout: 60 * time.Second,
GasPriceAdjustmentRate: "1.1",
})
if err != nil {
panic(err)
}
// Setup the keyring so we have access to private keys
client, err := chainIO.SetupKeyring(core.C.Owner.KeyName, core.C.Owner.KeyringBackend)
if err != nil {
panic(err)
}
// Query the BVS directory and find the BVS we are monitoring
txResp, err := api.NewBVSDirectoryImpl(client, core.C.Chain.BvsDirectory).GetBVSInfo(core.C.Chain.BvsHash)
if err != nil {
panic(err)
}
// Construct and returnthe Caller structure
return &Caller{
bvsContract: txResp.BVSContract,
chainIO: client,
}
}
// Run runs the caller in an infinite loop, creating a new task with a random number every second.
func (c *Caller) Run() {
bvsSquaring := BvsSquaringApi.NewBVSSquaring(c.chainIO)
for {
// Create a client of the BVS
bvsSquaring.BindClient(c.bvsContract)
// Create a new number, submits it to the BVS as a task
randomNumber := rand.Int63n(100)
resp, err := bvsSquaring.CreateNewTask(context.Background(), randomNumber)
if err != nil {
panic(err)
}
// print the transaction result
fmt.Printf("resp: %+v\n", resp)
}
}
You might notice a new library BvsSquaringApi and wonder what it does.
BvsSquaringApi is a Golang module specific to the Hello World BVS. See the next section for details!