Skip to content

Commit

Permalink
feat(opsim): handle deposit transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
jakim929 committed Jul 15, 2024
1 parent d341956 commit 2cb7605
Show file tree
Hide file tree
Showing 9 changed files with 440 additions and 15 deletions.
45 changes: 43 additions & 2 deletions anvil/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"sync/atomic"
"time"

"github.com/ethereum-optimism/supersim/chainapi"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"

"github.com/ethereum/go-ethereum/log"
Expand All @@ -31,6 +36,8 @@ type Config struct {
type Anvil struct {
rpcClient *rpc.Client

ethClient *ethclient.Client

log log.Logger
logFilePath string

Expand All @@ -49,6 +56,8 @@ const (
anvilListeningLogStr = "Listening on"
)

var _ chainapi.Chain = &Anvil{}

func New(log log.Logger, cfg *Config) *Anvil {
resCtx, resCancel := context.WithCancel(context.Background())
return &Anvil{
Expand Down Expand Up @@ -163,12 +172,15 @@ func (a *Anvil) Start(ctx context.Context) error {
}
}

rpcClient, err := rpc.Dial(a.Endpoint())
rpcClient, err := rpc.Dial(a.wsEndpoint())
if err != nil {
return fmt.Errorf("failed to create RPC client: %w", err)
}
a.rpcClient = rpcClient

ethClient := ethclient.NewClient(rpcClient)
a.ethClient = ethClient

return nil
}

Expand All @@ -194,6 +206,10 @@ func (a *Anvil) Endpoint() string {
return fmt.Sprintf("http://%s:%d", host, a.cfg.Port)
}

func (a *Anvil) wsEndpoint() string {
return fmt.Sprintf("ws://%s:%d", host, a.cfg.Port)
}

func (a *Anvil) ChainID() uint64 {
return a.cfg.ChainID
}
Expand Down Expand Up @@ -221,7 +237,9 @@ func (a *Anvil) WaitUntilReady(ctx context.Context) error {
return fmt.Errorf("timed out waiting for response from client")
case <-ticker.C:
var result string
if err := a.rpcClient.Call(&result, "web3_clientVersion"); err != nil {
result, err := a.Web3ClientVersion(ctx)

if err != nil {
continue
}
if strings.HasPrefix(result, "anvil") {
Expand All @@ -238,3 +256,26 @@ func (a *Anvil) String() string {
fmt.Fprintf(&b, "Chain ID: %d RPC: %s LogPath: %s", a.ChainID(), a.Endpoint(), a.LogPath())
return b.String()
}

// web3_ API
func (a *Anvil) Web3ClientVersion(ctx context.Context) (string, error) {
var result string
if err := a.rpcClient.CallContext(ctx, &result, "web3_clientVersion"); err != nil {
return "", err
}
return result, nil
}

// eth_ API
func (a *Anvil) EthGetLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
return a.ethClient.FilterLogs(ctx, q)
}

func (a *Anvil) EthSendTransaction(ctx context.Context, tx *types.Transaction) error {
return a.ethClient.SendTransaction(ctx, tx)
}

// subscription API
func (a *Anvil) SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error) {
return a.ethClient.SubscribeFilterLogs(ctx, q, ch)
}
22 changes: 22 additions & 0 deletions chainapi/chainapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package chainapi

import (
"context"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
)

type Chain interface {
// metadata methods
Endpoint() string
ChainID() uint64
LogPath() string

// API methods
EthSendTransaction(ctx context.Context, tx *types.Transaction) error

EthGetLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error)

SubscribeFilterLogs(ctx context.Context, q ethereum.FilterQuery, ch chan<- types.Log) (ethereum.Subscription, error)
}
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.1 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect
Expand All @@ -36,19 +37,32 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/ethereum-optimism/superchain-registry/superchain v0.0.0-20240614103325-d8902381f5d8 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/fjl/memsize v0.0.2 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gballet/go-verkle v0.1.1-0.20231031103413-a67434b50f46 // indirect
github.com/getsentry/sentry-go v0.18.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-bexpr v0.1.11 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/pointerstructure v1.2.1 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand All @@ -59,8 +73,10 @@ require (
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/rs/cors v1.9.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
Expand All @@ -75,8 +91,10 @@ require (
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Expand Down
Loading

0 comments on commit 2cb7605

Please sign in to comment.