Commit de3e1a66 authored by Ben Wilson's avatar Ben Wilson

Just got a basic contract read working

parent 73027578
SHELL := /bin/bash
GITCOMMIT := $(shell git rev-parse HEAD)
GITDATE := $(shell git show -s --format='%ct')
GITVERSION := $(shell cat package.json | jq .version)
LDFLAGSSTRING +=-X main.GitCommit=$(GITCOMMIT)
LDFLAGSSTRING +=-X main.GitDate=$(GITDATE)
LDFLAGSSTRING +=-X main.GitVersion=$(GITVERSION)
LDFLAGS :=-ldflags "$(LDFLAGSSTRING)"
l2geth-exporter:
env GO111MODULE=on go build $(LDFLAGS)
clean:
rm l2geth-exporter
test:
go test -v ./...
lint:
golangci-lint run ./...
binding:
$(eval temp := $(shell mktemp))
cat ../../packages/contracts/deployments/mainnet/OVM_CanonicalTransactionChain.json \
| jq -r .bytecode > $(temp)
cat ../../packages/contracts/deployments/mainnet/OVM_CanonicalTransactionChain.json \
| jq .abi \
| abigen --pkg bindings \
--abi - \
--out bindings/OVM_CanonicalTransactionChain.go \
--type OVMCanonicalTransactionChain \
--bin $(temp)
rm $(temp)
This diff is collapsed.
package main
import (
"github.com/prometheus/client_golang/prometheus"
)
//Define the metrics we wish to expose
var (
ovmctcTotalElements = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ovmctc_total_elements",
Help: "OVM CTC GetTotalElements value."},
[]string{"state"},
)
)
func init() {
//Register metrics with prometheus
prometheus.MustRegister(ovmctcTotalElements)
}
module github.com/optimisticben/optimism/go/l2geth-exporter
go 1.16
require (
github.com/ethereum/go-ethereum v1.10.8
github.com/prometheus/client_golang v1.11.0 // indirect
)
This diff is collapsed.
package l1contracts
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/optimisticben/optimism/go/l2geth-exporter/bindings"
)
// OVMCTC interacts with the OVM CTC contract
type OVMCTC struct {
Ctx context.Context
Address common.Address
Client *ethclient.Client
}
func (ovmctc *OVMCTC) GetTotalElements() (*big.Int, error) {
contract, err := bindings.NewOVMCanonicalTransactionChainCaller(ovmctc.Address, ovmctc.Client)
if err != nil {
return nil, err
}
totalElements, err := contract.GetTotalElements(&bind.CallOpts{
Context: ovmctc.Ctx,
})
if err != nil {
return nil, err
}
return totalElements, nil
}
package main
import (
"math/big"
"net/http"
"os"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
"github.com/optimisticben/optimism/go/l2geth-exporter/l1contracts"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
listenAddress := os.Getenv("LISTEN_ADDRESS")
if listenAddress == "" {
listenAddress = ":9100"
}
gethUrl := os.Getenv("GETH_URL")
if gethUrl == "" {
log.Error("GETH_URL environmental variable is required")
os.Exit(1)
}
ovmCtcAddress := os.Getenv("OVM_CTC_ADDRESS")
if ovmCtcAddress == "" {
log.Error("OVM_CTC_ADDRESS environmental variable is required")
os.Exit(1)
}
client, err := ethclient.Dial(gethUrl)
if err != nil {
log.Error("Problem connecting to GETH: %s", err)
}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>L2geth Exporter</title></head>
<body>
<h1>L2geth Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
go getCTCTotalElements(ovmCtcAddress, client)
log.Info("Listening on", listenAddress)
if err := http.ListenAndServe(listenAddress, nil); err != nil {
log.Error("Can't start http server: %s", err)
}
}
func getCTCTotalElements(address string, client *ethclient.Client) {
ovmCTC := l1contracts.OVMCTC{
Address: common.HexToAddress(address),
Client: client,
}
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
<-ticker.C
totalElements, err := ovmCTC.GetTotalElements()
if err != nil {
log.Error("Error calling GetTotalElements: %s", err)
continue
}
totalElementsFloat, _ := new(big.Float).SetInt(totalElements).Float64()
ovmctcTotalElements.WithLabelValues(
"latest").Set(totalElementsFloat)
}
}
{
"name": "@eth-optimism/l2geth-exporter",
"version": "0.0.1",
"private": true,
"devDependencies": {}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment