Skip to content

Commit

Permalink
Centralize gas cap management
Browse files Browse the repository at this point in the history
  • Loading branch information
azdagron committed Aug 13, 2024
1 parent dccaf07 commit 9aa559b
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 239 deletions.
10 changes: 3 additions & 7 deletions cmd/crybapy/cmd_payer_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/spf13/cobra"
)

Expand All @@ -27,24 +28,19 @@ func newPayerBalanceCommand(parentConfig *payerCommandConfig) *cobra.Command {
}

func doPayerBalance(config *payerBalanceConfig, spenderKeyPath string) error {

log, err := openConsoleLog()
if err != nil {
return err
}
payer, err := CreatePayer(config.Ctx, log, config.PayerConfig, config.NodeAddress, config.ChainID, spenderKeyPath)
payer, _, err := CreatePayer(config.Ctx, log, config.PayerConfig, config.NodeAddress, config.ChainID, spenderKeyPath)
if err != nil {
return err
}
balance, err := payer.GetTokenBalance(config.Ctx)
if err != nil {
return err
}
dec, err := payer.GetTokenDecimals(config.Ctx)
if err != nil {
return err
}

fmt.Println(printToken(balance, dec, ""))
fmt.Println(printToken(balance, payer.Decimals(), ""))
return nil
}
5 changes: 3 additions & 2 deletions cmd/crybapy/cmd_payer_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"storj.io/crypto-batch-payment/pkg/payouts"
"storj.io/crypto-batch-payment/pkg/pipeline"
"storj.io/crypto-batch-payment/pkg/pipelinedb"
"storj.io/crypto-batch-payment/pkg/txparams"
)

type payerTransferConfig struct {
Expand Down Expand Up @@ -63,7 +64,7 @@ func doPayerTransfer(config *payerTransferConfig, spenderKeyPath string) error {
}

fmt.Println("Running ad-hoc payout (single transfer)...")
payer, err := CreatePayer(config.Ctx, log, config.PayerConfig, config.NodeAddress, config.ChainID, spenderKeyPath)
payer, gasCaps, err := CreatePayer(config.Ctx, log, config.PayerConfig, config.NodeAddress, config.ChainID, spenderKeyPath)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,7 +113,7 @@ func doPayerTransfer(config *payerTransferConfig, spenderKeyPath string) error {
LastUpdated: time.Now(),
}, err
}),

GasCaps: txparams.FixedGasCaps(gasCaps),
PipelineLimit: pipeline.DefaultLimit,
TxDelay: pipeline.DefaultTxDelay,
Drain: false,
Expand Down
4 changes: 3 additions & 1 deletion cmd/crybapy/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"storj.io/crypto-batch-payment/pkg/pipelinedb"
"storj.io/crypto-batch-payment/pkg/txparams"

"github.com/spf13/cobra"
"github.com/zeebo/errs"
Expand Down Expand Up @@ -113,7 +114,7 @@ func doRun(config *runConfig) error {
}

fmt.Printf("Running %q payout...\n", config.Name)
payer, err := CreatePayer(config.Ctx, log, config.PayerConfig, config.NodeAddress, config.ChainID, config.SpenderKeyPath)
payer, gasCaps, err := CreatePayer(config.Ctx, log, config.PayerConfig, config.NodeAddress, config.ChainID, config.SpenderKeyPath)
if err != nil {
return err
}
Expand All @@ -127,6 +128,7 @@ func doRun(config *runConfig) error {

payoutsConfig := payouts.Config{
Quoter: quoter,
GasCaps: txparams.FixedGasCaps(gasCaps),
PipelineLimit: config.PipelineLimit,
TxDelay: config.TxDelay,
Drain: config.Drain,
Expand Down
61 changes: 35 additions & 26 deletions cmd/crybapy/factory_payer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"storj.io/crypto-batch-payment/pkg/eth"
"storj.io/crypto-batch-payment/pkg/payer"
"storj.io/crypto-batch-payment/pkg/storjtoken"
"storj.io/crypto-batch-payment/pkg/txparams"
"storj.io/crypto-batch-payment/pkg/zksyncera"
)

Expand Down Expand Up @@ -82,69 +83,69 @@ func registerNodeAddress(cmd *cobra.Command, addr *string) {
"/home/storj/.ethereum/geth.ipc",
"Address of the ETH node to use")
}
func CreatePayer(ctx context.Context, log *zap.Logger, config PayerConfig, nodeAddress string, chain string, spenderKeyPath string) (paymentPayer payer.Payer, err error) {

func CreatePayer(ctx context.Context, log *zap.Logger, config PayerConfig, nodeAddress string, chain string, spenderKeyPath string) (paymentPayer payer.Payer, gasCaps txparams.GasCaps, err error) {
spenderKey, spenderAddress, err := loadETHKey(spenderKeyPath, "spender")
if err != nil {
return nil, err
return nil, txparams.GasCaps{}, err
}
var maxGas big.Int
_, ok := maxGas.SetString(config.MaxGas, 10)
if !ok {
return nil, errs.New("invalid max gas setting")
maxGas := new(big.Int)
if _, ok := maxGas.SetString(config.MaxGas, 10); !ok {
return nil, txparams.GasCaps{}, errs.New("invalid max gas setting")
}

owner := spenderAddress
if config.Owner != "" {
owner, err = convertAddress(config.Owner, "owner")
if err != nil {
return nil, err
return nil, txparams.GasCaps{}, err
}
}

contractAddress, err := convertAddress(config.ContractAddress, "contract")
if err != nil {
return nil, err
return nil, txparams.GasCaps{}, err
}
chainID, err := convertInt(chain, 0, "chain-id")
if err != nil {
return nil, err
return nil, txparams.GasCaps{}, err
}

var maxFee *big.Int
if config.MaxFee != "" {
var tmp big.Int
if _, ok := tmp.SetString(config.MaxFee, 10); !ok {
return nil, errs.New("invalid max fee setting")
return nil, txparams.GasCaps{}, errs.New("invalid max fee setting")
}
maxFee = &tmp
}

var gasTipCap *big.Int
if config.GasTipCap != "" {
gasTipCap = new(big.Int)
_, ok = gasTipCap.SetString(config.GasTipCap, 10)
if !ok {
return nil, errs.New("invalid gas tip cap setting")
if _, ok := gasTipCap.SetString(config.GasTipCap, 10); !ok {
return nil, txparams.GasCaps{}, errs.New("invalid gas tip cap setting")
}
if gasTipCap.Cmp(big.NewInt(30*params.GWei)) > 0 {
return nil, errs.New("Gas tip cap is too high. Please use value less than 30 gwei")
return nil, txparams.GasCaps{}, errs.New("Gas tip cap is too high. Please use value less than 30 gwei")
}

if gasTipCap.Cmp(big.NewInt(int64(100))) < 0 {
return nil, errs.New("Gas tip cap is negligible. Please check if you really used wei unit (or set 0)")
return nil, txparams.GasCaps{}, errs.New("Gas tip cap is negligible. Please check if you really used wei unit (or set 0)")
}
}

pt, err := payer.TypeFromString(config.PayerType)
if err != nil {
return nil, errs.Wrap(err)
return nil, txparams.GasCaps{}, errs.Wrap(err)
}

switch pt {
case payer.Eth, payer.Polygon:
var client *ethclient.Client
client, err = ethclient.Dial(nodeAddress)
if err != nil {
return paymentPayer, errs.New("Failed to dial node %q: %v\n", nodeAddress, err)
return paymentPayer, txparams.GasCaps{}, errs.New("Failed to dial node %q: %v\n", nodeAddress, err)
}
defer client.Close()

Expand All @@ -154,12 +155,20 @@ func CreatePayer(ctx context.Context, log *zap.Logger, config PayerConfig, nodeA
owner,
spenderKey,
chainID,
gasTipCap,
&maxGas,
)
if err != nil {
return nil, errs.Wrap(err)
return nil, txparams.GasCaps{}, errs.Wrap(err)
}

if gasTipCap == nil {
gasTipCap, err = client.SuggestGasTipCap(ctx)
if err != nil {
return nil, txparams.GasCaps{}, errs.Wrap(err)
}
}

gasCaps.GasFeeCap = maxGas
gasCaps.GasTipCap = gasTipCap
case payer.ZkSyncEra:
var paymasterAddress *common.Address
var paymasterPayload []byte
Expand All @@ -174,18 +183,18 @@ func CreatePayer(ctx context.Context, log *zap.Logger, config PayerConfig, nodeA
spenderKey,
int(chainID.Int64()),
paymasterAddress,
paymasterPayload,
maxFee)
paymasterPayload)
if err != nil {
return nil, errs.Wrap(err)
return nil, txparams.GasCaps{}, errs.Wrap(err)
}
gasCaps.GasFeeCap = maxFee
case payer.Sim:
paymentPayer, err = payer.NewSimPayer()
if err != nil {
return nil, errs.Wrap(err)
return nil, txparams.GasCaps{}, errs.Wrap(err)
}
default:
return nil, errs.New("unsupported payer type: %v", config.PayerType)
return nil, txparams.GasCaps{}, errs.New("unsupported payer type: %v", config.PayerType)
}
return paymentPayer, nil
return paymentPayer, gasCaps, nil
}
7 changes: 0 additions & 7 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package config_test

import (
"math/big"
"os/user"
"path/filepath"
"testing"
Expand Down Expand Up @@ -41,15 +40,12 @@ func TestLoad_Defaults(t *testing.T) {
ERC20ContractAddress: common.HexToAddress("0x1111111111111111111111111111111111111111"),
ChainID: 0,
Owner: nil,
MaxGas: nil,
GasTipCap: nil,
},
ZkSyncEra: &config.ZkSyncEra{
NodeAddress: "https://mainnet.era.zksync.io",
SpenderKeyPath: homePath("some.key"),
ERC20ContractAddress: common.HexToAddress("0x2222222222222222222222222222222222222222"),
ChainID: 0,
MaxFee: nil,
PaymasterAddress: nil,
PaymasterPayload: nil,
},
Expand All @@ -76,15 +72,12 @@ func TestLoad_Overrides(t *testing.T) {
ERC20ContractAddress: common.HexToAddress("0xe66652d41EE7e81d3fcAe1dF7F9B9f9411ac835e"),
ChainID: 12345,
Owner: ptrOf(common.HexToAddress("0xe66652d41EE7e81d3fcAe1dF7F9B9f9411ac835e")),
MaxGas: big.NewInt(80_000_000_000),
GasTipCap: big.NewInt(2_000_000_000),
},
ZkSyncEra: &config.ZkSyncEra{
NodeAddress: "https://override.test",
SpenderKeyPath: "override",
ERC20ContractAddress: common.HexToAddress("0xe66652d41EE7e81d3fcAe1dF7F9B9f9411ac835e"),
ChainID: 12345,
MaxFee: big.NewInt(5678),
PaymasterAddress: ptrOf(common.HexToAddress("0xe66652d41EE7e81d3fcAe1dF7F9B9f9411ac835e")),
PaymasterPayload: []byte("\x01\x23"),
},
Expand Down
11 changes: 0 additions & 11 deletions pkg/config/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type Eth struct {
ERC20ContractAddress common.Address `toml:"erc20_contract_address"`
ChainID int `toml:"chain_id"`
Owner *common.Address `toml:"owner"`
MaxGas *big.Int `toml:"max_gas"`
GasTipCap *big.Int `toml:"gas_tip_cap"`
}

func (c Eth) NewPayer(ctx context.Context) (_ Payer, err error) {
Expand All @@ -41,13 +39,6 @@ func (c Eth) NewPayer(ctx context.Context) (_ Payer, err error) {
if c.ChainID == 0 {
c.ChainID = defaultEthChainID
}
if c.MaxGas == nil {
c.MaxGas, _ = new(big.Int).SetString(defaultMaxGas, 0)
}
if c.GasTipCap == nil {
c.GasTipCap, _ = new(big.Int).SetString(defaultGasTipCap, 0)
}

spenderKey, spenderAddress, err := loadSpenderKey(string(c.SpenderKeyPath))
if err != nil {
return nil, err
Expand All @@ -74,8 +65,6 @@ func (c Eth) NewPayer(ctx context.Context) (_ Payer, err error) {
owner,
spenderKey,
big.NewInt(int64(c.ChainID)),
c.GasTipCap,
c.MaxGas,
)
if err != nil {
return nil, errs.Wrap(err)
Expand Down
3 changes: 0 additions & 3 deletions pkg/config/testdata/defaults.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ node_address = "https://someaddress.test"
spender_key_path = "~/some.key"
erc20_contract_address = "0x1111111111111111111111111111111111111111"
# owner = ""
# max_gas = "70_000_000_000"
# gas_tip_cap = "1_000_000_000"

[zksync-era]
node_address = "https://mainnet.era.zksync.io"
spender_key_path = "~/some.key"
erc20_contract_address = "0x2222222222222222222222222222222222222222"
# chain_id = 324
# max_fee = ""
# paymaster_address = ""
# paymaster_payload = ""
3 changes: 0 additions & 3 deletions pkg/config/testdata/override.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ spender_key_path = "override"
erc20_contract_address = "0xe66652d41EE7e81d3fcAe1dF7F9B9f9411ac835e"
chain_id = 12345
owner = "0xe66652d41EE7e81d3fcAe1dF7F9B9f9411ac835e"
max_gas = "80_000_000_000"
gas_tip_cap = "2_000_000_000"

[zksync-era]
node_address = "https://override.test"
spender_key_path = "override"
erc20_contract_address = "0xe66652d41EE7e81d3fcAe1dF7F9B9f9411ac835e"
chain_id = 12345
max_fee = "5678"
paymaster_address = "0xe66652d41EE7e81d3fcAe1dF7F9B9f9411ac835e"
paymaster_payload = "0123"
5 changes: 1 addition & 4 deletions pkg/config/zksync_era.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"context"
"errors"
"math/big"

"github.com/ethereum/go-ethereum/common"

Expand All @@ -19,7 +18,6 @@ type ZkSyncEra struct {
SpenderKeyPath Path `toml:"spender_key_path"`
ERC20ContractAddress common.Address `toml:"erc20_contract_address"`
ChainID int `toml:"chain_id"`
MaxFee *big.Int `toml:"max_fee"`
PaymasterAddress *common.Address `toml:"paymaster_address"`
PaymasterPayload HexString `toml:"paymaster_payload"`
}
Expand Down Expand Up @@ -49,8 +47,7 @@ func (c ZkSyncEra) NewPayer(ctx context.Context) (_ Payer, err error) {
spenderKey,
c.ChainID,
c.PaymasterAddress,
c.PaymasterPayload,
c.MaxFee)
c.PaymasterPayload)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 9aa559b

Please sign in to comment.