Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ Aliases:
Flags:
-a, --addresses 0x891E368fE81cBa2aC6F6cc4b98e684c106e2EF4f ScribeOptimistic contract address. Example: 0x891E368fE81cBa2aC6F6cc4b98e684c106e2EF4f
--chain-id uint If no chain_id provided binary will try to get chain_id from given RPC
--from-block uint Block number to start from. If not provided, binary will try to get it from given RPC
--flashbot-rpc-url string Flashbot Node HTTP RPC_URL, normally starts with https://****
--from-block int Block number to start from. If not provided, binary will try to get it from given RPC
-h, --help help for run
--keystore string Keystore file (NOT FOLDER), path to key .json file. If provided, no need to use --secret-key
--password string Key raw password as text
--password-file string Path to key password file
--rpc-url string Node HTTP RPC_URL, normally starts with https://****
--secret-key 0x****** Private key in format 0x****** or `*******`. If provided, no need to use --keystore
--subscription-url string [Optional] Used if you want to subscribe to events rather than poll, typically starts with wss://****
--tx-type legacy Transaction type definition, possible values are: `legacy`, `eip1559` or `none` (default "none")
--tx-type legacy Transaction type definition, possible values are: legacy, `eip1559` or `none` (default "none")

```

Note that in *all* cases you must provide `--rpc-url`, but if you want to use event driven listening instead of polling you also need to provide `--subscription-url`.
Expand All @@ -30,13 +32,13 @@ Note that in *all* cases you must provide `--rpc-url`, but if you want to use ev
Starting with private key

```bash
challenger run --addresses 0x891E368fE81cBa2aC6F6cc4b98e684c106e2EF4f --rpc-url http://localhost:3334 --secret-key 0x******
challenger run --tx-type eip1559 --addresses 0x891E368fE81cBa2aC6F6cc4b98e684c106e2EF4f --rpc-url http://localhost:3334 --secret-key 0x******
```

Starting with key file and password

```bash
challenger run -a 0x891E368fE81cBa2aC6F6cc4b98e684c106e2EF4f --rpc-url http://localhost:3334 --keystore /path/to/key.json --password-file /path/to/file
challenger run --tx-type eip1559 -a 0x891E368fE81cBa2aC6F6cc4b98e684c106e2EF4f --rpc-url http://localhost:3334 --keystore /path/to/key.json --password-file /path/to/file
```

## Using Docker image
Expand Down Expand Up @@ -88,5 +90,5 @@ docker run --rm challenger-go
Full example:

```bash
docker run --it --rm --name challenger-go run -a ADDRESS --rpc-url http://localhost:3334 --secret-key asdfasdfas
docker run --it --rm --name challenger-go run -a ADDRESS --tx-type eip1559 --rpc-url http://localhost:3334 --secret-key asdfasdfas
```
76 changes: 57 additions & 19 deletions cmd/challenger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ import (
"sync"

challenger "github.com/chronicleprotocol/challenger/core"
"github.com/defiweb/go-eth/txmodifier"
"github.com/defiweb/go-eth/wallet"
logger "github.com/sirupsen/logrus"

"github.com/defiweb/go-eth/rpc"
"github.com/defiweb/go-eth/rpc/transport"
"github.com/defiweb/go-eth/txmodifier"
"github.com/defiweb/go-eth/types"
"github.com/defiweb/go-eth/wallet"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
Expand All @@ -48,9 +48,10 @@ type options struct {
Password string
PasswordFile string
RpcURL string
FlashbotRPCURL string
SubscriptionURL string
Address []string
FromBlock uint64
FromBlock int64
ChainID uint64
TransactionType string
}
Expand Down Expand Up @@ -139,11 +140,6 @@ func main() {
defer ctxCancel()
}

t, err := transport.NewHTTP(transport.HTTPOptions{URL: opts.RpcURL})
if err != nil {
logger.Fatalf("Failed to create transport: %v", err)
}

// Key generation
key, err := opts.getKey()
if err != nil {
Expand All @@ -152,10 +148,6 @@ func main() {

// Basic TX modifiers
txModifiers := []rpc.TXModifier{
txmodifier.NewGasLimitEstimator(txmodifier.GasLimitEstimatorOptions{
MaxGas: maxGasLimit,
Multiplier: defaultGasLimitMultiplier,
}),
txmodifier.NewNonceProvider(txmodifier.NonceProviderOptions{
UsePendingBlock: false,
Replace: false,
Expand Down Expand Up @@ -194,25 +186,68 @@ func main() {
logger.Fatalf("Unknown transaction type: %s. Have to be legacy, eip1559 or none", opts.TransactionType)
}

// Create a JSON-RPC client to mainnet.
t, err := transport.NewHTTP(transport.HTTPOptions{URL: opts.RpcURL})
if err != nil {
logger.Fatalf("Failed to create transport: %v", err)
}

// Set manual gas limit for flashbots, they might require more gas.
//nolint:gocritic
baseTxModifiers := append(txModifiers, txmodifier.NewGasLimitEstimator(txmodifier.GasLimitEstimatorOptions{
MaxGas: maxGasLimit,
Multiplier: defaultGasLimitMultiplier,
}))

clientOptions := []rpc.ClientOptions{
rpc.WithTransport(t),
rpc.WithKeys(key),
rpc.WithDefaultAddress(key.Address()),
rpc.WithTXModifiers(txModifiers...),
rpc.WithTXModifiers(baseTxModifiers...),
}

// Create a JSON-RPC client.
client, err := rpc.NewClient(clientOptions...)
if err != nil {
logger.Fatalf("Failed to create RPC client: %v", err)
}

// Create a JSON-RPC client to flashbot.
var flashbotClient *rpc.Client
if opts.FlashbotRPCURL != "" {
flashbotTransport, err := transport.NewHTTP(transport.HTTPOptions{URL: opts.FlashbotRPCURL})
if err != nil {
logger.Fatalf("Failed to create transport: %v", err)
}

// Set manual gas limit for flashbots, they might require more gas.
//nolint:gocritic
flashbotTxModifiers := append(txModifiers, txmodifier.NewGasLimitEstimator(txmodifier.GasLimitEstimatorOptions{
MaxGas: challenger.MaxFlashbotGasLimit,
Multiplier: defaultGasLimitMultiplier,
Replace: false,
}))

// TODO: tx modifiers have to be similar ?
flashbotClientOptions := []rpc.ClientOptions{
rpc.WithTransport(flashbotTransport),
rpc.WithKeys(key),
rpc.WithDefaultAddress(key.Address()),
rpc.WithTXModifiers(flashbotTxModifiers...),
}

flashbotClient, err = rpc.NewClient(flashbotClientOptions...)
if err != nil {
logger.Fatalf("Failed to create RPC client: %v", err)
}
}

// Spawning "challenger" for each address
var wg sync.WaitGroup
for _, address := range addresses {
wg.Add(1)

p := challenger.NewScribeOptimisticRpcProvider(client)
c := challenger.NewChallenger(ctx, address, p, 0, opts.SubscriptionURL, &wg)
p := challenger.NewScribeOptimisticRPCProvider(client, flashbotClient)
c := challenger.NewChallenger(ctx, address, p, opts.FromBlock, opts.SubscriptionURL, &wg)

go func(addr types.Address) {
err := c.Run()
Expand All @@ -237,8 +272,9 @@ func main() {
)
http.Handle("/metrics", promhttp.Handler())
// TODO: move `:9090` to config
logger.WithError(http.ListenAndServe(":9090", nil)). //nolint:gosec
Error("metrics server error")
logger.
WithError(http.ListenAndServe(":9090", nil)). //nolint:gosec
Error("metrics server error")
<-ctx.Done()
}()

Expand All @@ -251,9 +287,11 @@ func main() {
cmd.PersistentFlags().StringVar(&opts.Password, "password", "", "Key raw password as text")
cmd.PersistentFlags().StringVar(&opts.PasswordFile, "password-file", "", "Path to key password file")
cmd.PersistentFlags().StringVar(&opts.RpcURL, "rpc-url", "", "Node HTTP RPC_URL, normally starts with https://****")
cmd.PersistentFlags().StringVar(&opts.FlashbotRPCURL, "flashbot-rpc-url", "", "Flashbot Node HTTP RPC_URL, normally starts with https://****")
cmd.PersistentFlags().StringVar(&opts.SubscriptionURL, "subscription-url", "", "[Optional] Used if you want to subscribe to events rather than poll, typically starts with wss://****")
cmd.PersistentFlags().StringArrayVarP(&opts.Address, "addresses", "a", []string{}, "ScribeOptimistic contract address. Example: `0x891E368fE81cBa2aC6F6cc4b98e684c106e2EF4f`")
cmd.PersistentFlags().Uint64Var(&opts.FromBlock, "from-block", 0, "Block number to start from. If not provided, binary will try to get it from given RPC")
cmd.PersistentFlags().
Int64Var(&opts.FromBlock, "from-block", 0, "Block number to start from. If not provided, binary will try to get it from given RPC")
cmd.PersistentFlags().Uint64Var(&opts.ChainID, "chain-id", 0, "If no chain_id provided binary will try to get chain_id from given RPC")
cmd.PersistentFlags().StringVar(&opts.TransactionType, "tx-type", "none", "Transaction type definition, possible values are: `legacy`, `eip1559` or `none`")

Expand Down
Loading