Building an app using the WORM
This page goes over how you can develop an application that leverages outputs from the worms brain that have been stored and verified on-chain.
Querying the On-Chain Worm
// Connect to Hyperliquid or any Ethereum-compatible blockchain
client, err := ethclient.Dial(hypeAPI)
if err != nil {
return nil, fmt.Errorf("error connecting to hype client: %w", err)
}
// ABI parsing
contractAbi, err := abi.JSON(strings.NewReader(abiStr))
if err != nil {
return nil, fmt.Errorf("failed to parse contract ABI: %w", err)
}
query := ethereum.FilterQuery{
FromBlock: big.NewInt(from),
ToBlock: big.NewInt(to),
Addresses: []common.Address{contractAddress},
}
// Fetch logs
logs, err := bf.client.FilterLogs(ctx, query)
if err != nil {
log.Fatalf("Failed to fetch logs: %v", err)
}
cds := make([]contractData, 0, len(logs))
// Decode logs
for _, vLog := range logs {
event := struct {
DeltaX *big.Int
DeltaY *big.Int
LeftMuscle *big.Int
RightMuscle *big.Int
PositionTimestamp *big.Int
PositionPrice *big.Int
}{}
if err := bf.abi.UnpackIntoInterface(&event, "WormStateUpdated", vLog.Data); err != nil {
return nil, fmt.Errorf("failed to unpack log data: %w", err)
}
cd := contractData{
transactionHash: vLog.TxHash.String(),
block: int(vLog.BlockNumber),
leftMuscle: event.LeftMuscle.Int64(),
rightMuscle: event.RightMuscle.Int64(),
price: float64(event.PositionPrice.Int64()) / 10000000,
ts: time.Unix(event.PositionTimestamp.Int64(), 0), // Set ts by converting the UNIX timestamp
}Storing State
Last updated