Skip to content

Commit

Permalink
Create contract utility (#14)
Browse files Browse the repository at this point in the history
* 🌱 Add chain utils

* 🚧 Add chain constants file

* 🚧 Add initial implementation to interact with contracts on chain

* ♻️ Update interfaces and rename file

* ♻️ Fix test and mocking

* Update chainIds type

* 🎨 Code cleanup

* ✅ Add tests

* 👌 Applied suggestions from code review

* 👌 Applied suggestions

* 👌 Applied suggestions

* 👌 Applied suggestions from code review

* 👌 Applied suggestions

---------

Co-authored-by: Ishan <[email protected]>
  • Loading branch information
nagdahimanshu and ishantiw authored Jan 23, 2024
1 parent 87774a9 commit 4dc7463
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 14 deletions.
15 changes: 11 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,42 @@ module github.com/LiskHQ/op-fault-detector
go 1.21.5

require (
github.com/ethereum-optimism/optimism/op-bindings v0.10.14
github.com/ethereum/go-ethereum v1.13.10
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844 v0.4.0 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/tools v0.15.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
30 changes: 20 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ github.com/DataDog/zstd v1.4.5 h1:EndNeuB0l9syBZhut0wns3gV1hL8zX8LIu6ZiVHWLIQ=
github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
github.com/VictoriaMetrics/fastcache v1.12.1 h1:i0mICQuojGDL3KblA7wUNlY5lOK6a4bwt3uRKnkZU40=
github.com/VictoriaMetrics/fastcache v1.12.1/go.mod h1:tX04vaqcNoQeGLD+ra5pU5sWkuxnzWhEzLwhP9w653o=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand All @@ -14,6 +12,8 @@ github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPx
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/cespare/cp v0.1.0 h1:SE+dxFebS7Iik5LK0tsi1k9ZCxEaFX4AjQmoyA+1dJk=
github.com/cespare/cp v0.1.0/go.mod h1:SOGHArjBr4JWaSDEVpWpo/hNg6RoKrls6Oh40hiwW+s=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cockroachdb/errors v1.8.1 h1:A5+txlVZfOqFBDa4mGz2bUWSp0aHElvHX2bKkdbQu+Y=
Expand Down Expand Up @@ -46,6 +46,8 @@ github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/ethereum-optimism/optimism/op-bindings v0.10.14 h1:SMMnMdNb1QIhJDyvk7QMUv+crAP4UHHoSYBOASBDIjM=
github.com/ethereum-optimism/optimism/op-bindings v0.10.14/go.mod h1:9ZSUq/rjlzp3uYyBN4sZmhTc3oZgDVqJ4wrUja7vj6c=
github.com/ethereum/c-kzg-4844 v0.4.0 h1:3MS1s4JtA868KpJxroZoepdV0ZKBp3u/O5HcZ7R3nlY=
github.com/ethereum/c-kzg-4844 v0.4.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
github.com/ethereum/go-ethereum v1.13.10 h1:Ppdil79nN+Vc+mXfge0AuUgmKWuVv4eMqzoIVSdqZek=
Expand All @@ -58,8 +60,8 @@ github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff h1:tY80oXqG
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff/go.mod h1:x7DCsMOv1taUwEWCzT4cmDeAkigA5/QCwUodaVOe8Ww=
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 h1:BAIP2GihuqhwdILrV+7GJel5lyPV3u1+PgzrWLc0TkE=
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46/go.mod h1:QNpY22eby74jVhqH4WhDLDwxc/vqsern6pW+u2kbkpc=
github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
Expand All @@ -70,11 +72,13 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb h1:PBC98N2aIaM3XXiurYmW7fx4GZkL8feAMVq7nEjURHk=
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/go-bexpr v0.1.10 h1:9kuI5PFotCboP3dkDYFr/wi0gg0QVbSNz5oFRpxn4uE=
github.com/hashicorp/go-bexpr v0.1.10/go.mod h1:oxlubA2vC/gFVfX1A6JGp7ls7uCDlfJn732ehYYg+g0=
github.com/holiman/billy v0.0.0-20230718173358-1c7e68d277a7 h1:3JQNjnMRil1yD0IfZKHF9GxxWKDJGj8I0IqOUol//sw=
Expand Down Expand Up @@ -134,16 +138,16 @@ github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobtDnDzA=
github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9+mHxBEeo3Hbg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/supranational/blst v0.3.11 h1:LyU6FolezeWAhvQk0k6O/d49jqgO52MSDDfYgbeoEm4=
github.com/supranational/blst v0.3.11/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a h1:1ur3QoCqvE5fl+nylMaIr9PVV1w343YRDtsy+Rwu7XI=
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a/go.mod h1:RRCYJbIwD5jmqPI9XoAFR0OcDxqUctll6zUj/+B4S48=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
Expand All @@ -154,6 +158,8 @@ github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg=
github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
Expand All @@ -169,6 +175,7 @@ golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
Expand All @@ -181,6 +188,9 @@ golang.org/x/tools v0.15.0 h1:zdAyfUGbYmuVokhzVmghFl2ZJh5QhcfebBgmVPFYA+8=
golang.org/x/tools v0.15.0/go.mod h1:hpksKq4dtpQWS1uQ61JkdqWM3LscIS6Slf+VVkm+wQk=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
136 changes: 136 additions & 0 deletions pkg/chain/chain_constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package chain

// L2ChainIDs manages L2 network chainIDs.
type L2ChainIDs struct {
optimism uint64
optimismGoerli uint64
optimismSepolia uint64
optimismHardhatLocal uint64
optimismHardhatDevnet uint64
optimismBedrockAlphaTestnet uint64
baseMainnet uint64
baseGoerli uint64
baseSepolia uint64
zoraGoerli uint64
zoraMainnet uint64
liskSepolia uint64
}

var l2NetworkChainIDs = L2ChainIDs{
optimism: 10,
optimismGoerli: 420,
optimismSepolia: 11155420,
optimismHardhatLocal: 31337,
optimismHardhatDevnet: 17,
optimismBedrockAlphaTestnet: 28528,
baseGoerli: 84531,
baseSepolia: 84532,
baseMainnet: 8453,
zoraGoerli: 999,
zoraMainnet: 7777777,
liskSepolia: 4202,
}

type NetworkType = string

const (
L1 = NetworkType("L1")
L2 = NetworkType("L2")
)

// Contracts has information of the L1 & L2 contracts addresses.
type Contracts struct {
stateCommitmentChain string
optimismPortal string
l2OutputOracle string
networkType NetworkType
}

var contractAddresses map[uint64]Contracts

func init() {
contractAddresses = map[uint64]Contracts{
l2NetworkChainIDs.optimism: {
stateCommitmentChain: "0xBe5dAb4A2e9cd0F27300dB4aB94BeE3A233AEB19",
optimismPortal: "0xbEb5Fc579115071764c7423A4f12eDde41f106Ed",
l2OutputOracle: "0xdfe97868233d1aa22e815a266982f2cf17685a27",
networkType: L1,
},
l2NetworkChainIDs.optimismGoerli: {
stateCommitmentChain: "0x9c945aC97Baf48cB784AbBB61399beB71aF7A378",
optimismPortal: "0x5b47E1A08Ea6d985D6649300584e6722Ec4B1383",
l2OutputOracle: "0xE6Dfba0953616Bacab0c9A8ecb3a9BBa77FC15c0",
networkType: L1,
},
l2NetworkChainIDs.optimismSepolia: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x16Fc5058F25648194471939df75CF27A2fdC48BC",
l2OutputOracle: "0x90E9c4f8a994a250F6aEfd61CAFb4F2e895D458F",
networkType: L1,
},
l2NetworkChainIDs.optimismHardhatLocal: {
stateCommitmentChain: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
optimismPortal: "0x0000000000000000000000000000000000000000",
l2OutputOracle: "0x0000000000000000000000000000000000000000",
networkType: L1,
},
l2NetworkChainIDs.optimismHardhatDevnet: {
stateCommitmentChain: "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
optimismPortal: "0x0000000000000000000000000000000000000000",
l2OutputOracle: "0x0000000000000000000000000000000000000000",
networkType: L1,
},
l2NetworkChainIDs.optimismBedrockAlphaTestnet: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0xA581Ca3353DB73115C4625FFC7aDF5dB379434A8",
l2OutputOracle: "0x3A234299a14De50027eA65dCdf1c0DaC729e04A6",
networkType: L1,
},
l2NetworkChainIDs.baseGoerli: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0xe93c8cD0D409341205A592f8c4Ac1A5fe5585cfA",
l2OutputOracle: "0x2A35891ff30313CcFa6CE88dcf3858bb075A2298",
networkType: L1,
},
l2NetworkChainIDs.baseSepolia: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x49f53e41452C74589E85cA1677426Ba426459e85",
l2OutputOracle: "0x84457ca9D0163FbC4bbfe4Dfbb20ba46e48DF254",
networkType: L1,
},
l2NetworkChainIDs.baseMainnet: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x49048044D57e1C92A77f79988d21Fa8fAF74E97e",
l2OutputOracle: "0x56315b90c40730925ec5485cf004d835058518A0",
networkType: L1,
},
l2NetworkChainIDs.zoraGoerli: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0xDb9F51790365e7dc196e7D072728df39Be958ACe",
l2OutputOracle: "0xdD292C9eEd00f6A32Ff5245d0BCd7f2a15f24e00",
networkType: L1,
},
l2NetworkChainIDs.zoraMainnet: {
stateCommitmentChain: "0x0000000000000000000000000000000000000000",
optimismPortal: "0x1a0ad011913A150f69f6A19DF447A0CfD9551054",
l2OutputOracle: "0x9E6204F750cD866b299594e2aC9eA824E2e5f95c",
networkType: L1,
},
l2NetworkChainIDs.liskSepolia: {
optimismPortal: "0xe3d90F21490686Ec7eF37BE788E02dfC12787264",
l2OutputOracle: "0xA0E35F56C318DE1bD5D9ca6A94Fe7e37C5663348",
networkType: L1,
},
}
}

// GetContractAddressesByChainID returns contract addresses by network chainID.
func GetContractAddressesByChainID(chainID uint64) (Contracts, bool) {
filteredContracts := contractAddresses[chainID]

if len(filteredContracts.l2OutputOracle) == 0 {
return filteredContracts, false
}

return filteredContracts, true
}
27 changes: 27 additions & 0 deletions pkg/chain/chain_constants_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package chain

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetContractAddressesByChainID(t *testing.T) {
assert := assert.New(t)

const availableChainID uint64 = 10
contractAddresses, areAddressesAvailable := GetContractAddressesByChainID(availableChainID)
contractAddressesExpected := Contracts{
stateCommitmentChain: "0xBe5dAb4A2e9cd0F27300dB4aB94BeE3A233AEB19",
optimismPortal: "0xbEb5Fc579115071764c7423A4f12eDde41f106Ed",
l2OutputOracle: "0xdfe97868233d1aa22e815a266982f2cf17685a27",
networkType: "L1",
}
assert.Equal(true, areAddressesAvailable)
assert.Equal(contractAddressesExpected, contractAddresses)

const unavailableChainID uint64 = 5
contractAddresses, areAddressesAvailable = GetContractAddressesByChainID(unavailableChainID)
assert.Equal(false, areAddressesAvailable)
assert.Equal(0, len(contractAddresses.l2OutputOracle))
}
73 changes: 73 additions & 0 deletions pkg/chain/contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package chain

import (
"context"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"

"github.com/ethereum-optimism/optimism/op-bindings/bindings"
)

// OracleAccessor binds oracle contract to an instance for querying data.
type OracleAccessor struct {
contractInstance *bindings.L2OutputOracle
}

// ConfigOptions are the options required to interact with the oracle contract.
type ConfigOptions struct {
L1RPCEndpoint string
ChainID uint64
L2OutputOracleContractAddress string
}

func getL1OracleContractAddressByChainID(chainID uint64) (string, bool) {
cAddr, areAddressesAvailable := GetContractAddressesByChainID(chainID)
if !areAddressesAvailable {
return "", false
}

return cAddr.l2OutputOracle, true
}

// NewOracleAccessor returns [OracleAccessor] with contract instance.
func NewOracleAccessor(ctx context.Context, opts *ConfigOptions) (*OracleAccessor, error) {
client, err := ethclient.DialContext(ctx, opts.L1RPCEndpoint)
if err != nil {
return nil, err
}

oracleContractAddress, isAddressExists := getL1OracleContractAddressByChainID(opts.ChainID)

// Verify if oracle contract address is available in the chain constants
// If not available, use l2OutputContractAddress from the config options
if !isAddressExists {
if len(opts.L2OutputOracleContractAddress) == 0 {
return nil, fmt.Errorf("L2OutputOracleContractAddress is not available")
}
oracleContractAddress = opts.L2OutputOracleContractAddress
}

oracleContractInstance, err := bindings.NewL2OutputOracle(common.HexToAddress(oracleContractAddress), client)

if err != nil {
return nil, err
}

return &OracleAccessor{
contractInstance: oracleContractInstance,
}, nil
}

// GetNextOutputIndex returns index of next output to be proposed.
func (oc *OracleAccessor) GetNextOutputIndex() (*big.Int, error) {
return oc.contractInstance.NextOutputIndex(&bind.CallOpts{})
}

// GetL2Output returns L2 output at given index.
func (oc *OracleAccessor) GetL2Output(index *big.Int) (bindings.TypesOutputProposal, error) {
return oc.contractInstance.GetL2Output(&bind.CallOpts{}, index)
}
Loading

0 comments on commit 4dc7463

Please sign in to comment.