Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gas balance metric #7

Open
wants to merge 11 commits into
base: na/add-metrics
Choose a base branch
from
Open
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
19 changes: 9 additions & 10 deletions cmd/solver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,13 @@ func main() {

eg, ctx := errgroup.WithContext(ctx)

// Uncomment this section to run a prometheus server for metrics collection
//eg.Go(func() error {
// lmt.Logger(ctx).Info("Starting Prometheus")
// if err := metrics.StartPrometheus(ctx, fmt.Sprintf(cfg.Metrics.PrometheusAddress)); err != nil {
// return err
// }
// return nil
//})
eg.Go(func() error {
lmt.Logger(ctx).Info("Starting Prometheus")
if err := metrics.StartPrometheus(ctx, fmt.Sprintf(cfg.Metrics.PrometheusAddress)); err != nil {
return err
}
return nil
})

eg.Go(func() error {
transferMonitor := transfermonitor.NewTransferMonitor(db.New(dbConn), *quickStart)
Expand Down Expand Up @@ -135,7 +134,7 @@ func main() {
})

eg.Go(func() error {
r, err := fundrebalancer.NewFundRebalancer(ctx, *keysPath, skipgo, evmManager, db.New(dbConn))
r, err := fundrebalancer.NewFundRebalancer(ctx, *keysPath, skipgo, evmManager, clientManager, db.New(dbConn))
if err != nil {
return fmt.Errorf("creating fund rebalancer: %w", err)
}
Expand All @@ -149,7 +148,7 @@ func main() {
return fmt.Errorf("creating hyperlane multi client from config: %w", err)
}

relayer := hyperlane.NewRelayer(hype, make(map[string]string))
relayer := hyperlane.NewRelayer(hype, make(map[string]string), clientManager)
err = hyperlane.NewRelayerRunner(db.New(dbConn), hype, relayer).Run(ctx)
if err != nil {
return fmt.Errorf("relaying message: %v", err)
Expand Down
36 changes: 18 additions & 18 deletions config/local/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48
signer_gas_balance:
gas_balance:
warning_threshold_wei: 4290000000000000000
critical_threshold_wei: 1430000000000000000
ethereum-testnet:
Expand All @@ -38,7 +38,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238
signer_gas_balance:
gas_balance:
warning_threshold_wei: 250000000000000000
critical_threshold_wei: 0
avalanche:
Expand All @@ -55,7 +55,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E
signer_gas_balance:
gas_balance:
warning_threshold_wei: 1720000000000000000
critical_threshold_wei: 580000000000000000
avalanche-testnet:
Expand All @@ -70,7 +70,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0x5425890298aed601595a70ab815c96711a31bc65
signer_gas_balance:
gas_balance:
warning_threshold_wei: 1000000000000000000
critical_threshold_wei: 0
optimism:
Expand All @@ -86,7 +86,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0x0b2c639c533813f4aa9d7837caf62653d097ff85
signer_gas_balance:
gas_balance:
warning_threshold_wei: 180000000000000000
critical_threshold_wei: 60000000000000000
optimism-testnet:
Expand All @@ -101,7 +101,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0x5fd84259d66Cd46123540766Be93DFE6D43130D7
signer_gas_balance:
gas_balance:
warning_threshold_wei: 250000000000000000
critical_threshold_wei: 0
arbitrum:
Expand All @@ -122,7 +122,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0xaf88d065e77c8cC2239327C5EDb3A432268e5831
signer_gas_balance:
gas_balance:
warning_threshold_wei: 180000000000000000
critical_threshold_wei: 60000000000000000
relayer:
Expand All @@ -141,7 +141,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0x75faf114eafb1BDbe2F0316DF893fd58CE46AA4d
signer_gas_balance:
gas_balance:
warning_threshold_wei: 250000000000000000
critical_threshold_wei: 0
osmosis:
Expand All @@ -164,7 +164,7 @@ chains:
grpc: <osmosis_grpc_server_url>
grpc_tls_enabled: false
usdc_denom: "ibc/498A0751C798A0D9A389AA3691123DADA57DAA4FE165D5C75894505B876BA6E4"
signer_gas_balance:
gas_balance:
warning_threshold_wei: 42860000
critical_threshold_wei: 14290000
gas_price: 0.0025
Expand All @@ -185,7 +185,7 @@ chains:
address_prefix: "neutron"
rpc: <neutron_rpc_server_url>
rpc_basic_auth_var: <server_password>
signer_gas_balance:
gas_balance:
warning_threshold_wei: 42860000
critical_threshold_wei: 14290000
noble:
Expand All @@ -200,7 +200,7 @@ chains:
address_prefix: "noble"
rpc: <noble_rpc_server_url>
rpc_basic_auth_var: <server_password>
signer_gas_balance:
gas_balance:
warning_threshold_wei: 42860000
critical_threshold_wei: 14290000
noble-testnet:
Expand All @@ -213,7 +213,7 @@ chains:
cosmos:
address_prefix: "noble"
rpc: <noble_testnet_rpc_server_url>
signer_gas_balance:
gas_balance:
warning_threshold_wei: 0
critical_threshold_wei: 0
base-mainnet:
Expand All @@ -229,7 +229,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
signer_gas_balance:
gas_balance:
warning_threshold_wei: 180000000000000000
critical_threshold_wei: 60000000000000000
base-testnet:
Expand All @@ -244,7 +244,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0x036CbD53842c5426634e7929541eC2318f3dCF7e
signer_gas_balance:
gas_balance:
warning_threshold_wei: 250000000000000000
critical_threshold_wei: 0
polygon:
Expand All @@ -260,7 +260,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0x3c499c542cef5e3811e1192ce70d8cc03d5c3359
signer_gas_balance:
gas_balance:
warning_threshold_wei: 15000000000000000000
critical_threshold_wei: 5000000000000000000
min_gas_tip_cap: 30000000000
Expand All @@ -276,7 +276,7 @@ chains:
rpc_basic_auth_var: <server_password>
contracts:
usdc_erc20_address: 0x41e94eb019c0762f9bfcf9fb1e58725bfb0e7582
signer_gas_balance:
gas_balance:
warning_threshold_wei: 250000000000000000
critical_threshold_wei: 0
solana:
Expand All @@ -296,7 +296,7 @@ chains:
message_transmitter: CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd
token_messenger_minter: CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3
usdc: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
signer_gas_balance:
gas_balance:
warning_threshold_wei: 2150000000
critical_threshold_wei: 720000000
priority_fee: 600000
Expand All @@ -314,6 +314,6 @@ chains:
message_transmitter: CCTPmbSD7gX1bxKPAmg77w8oFzNFpaQiQUWD43TKaecd
token_messenger_minter: CCTPiPYPc6AsJuwueEnWgSgucamXDZwBd53dQ11YiKX3
usdc: 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU
signer_gas_balance:
gas_balance:
warning_threshold_wei: 1000000000
critical_threshold_wei: 0
4 changes: 4 additions & 0 deletions db/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ const (
TransferStatusPending string = "PENDING"
TransferStatusSuccess string = "SUCCESS"
TransferStatusAbandoned string = "ABANDONED"

GET string = "GET"
INSERT string = "INSERT"
UPDATE string = "UPDATE"
)
25 changes: 25 additions & 0 deletions fundrebalancer/fundrebalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ import (
"os"
"time"

dbtypes "github.com/skip-mev/go-fast-solver/db"
"github.com/skip-mev/go-fast-solver/shared/metrics"

"github.com/skip-mev/go-fast-solver/db/gen/db"
"github.com/skip-mev/go-fast-solver/shared/clientmanager"
"github.com/skip-mev/go-fast-solver/shared/clients/skipgo"
"github.com/skip-mev/go-fast-solver/shared/config"
"github.com/skip-mev/go-fast-solver/shared/evmrpc"
"github.com/skip-mev/go-fast-solver/shared/lmt"
"github.com/skip-mev/go-fast-solver/shared/signing"
"github.com/skip-mev/go-fast-solver/shared/signing/evm"
"github.com/skip-mev/go-fast-solver/shared/utils"
"go.uber.org/zap"
"golang.org/x/net/context"
)
Expand All @@ -35,6 +40,7 @@ type FundRebalancer struct {
chainIDToPrivateKey map[string]string
skipgo skipgo.SkipGoClient
evmClientManager evmrpc.EVMRPCClientManager
cctpClientManager *clientmanager.ClientManager
config map[string]config.FundRebalancerConfig
database Database
trasferTracker *TransferTracker
Expand All @@ -45,6 +51,7 @@ func NewFundRebalancer(
keysPath string,
skipgo skipgo.SkipGoClient,
evmClientManager evmrpc.EVMRPCClientManager,
cctpClientManager *clientmanager.ClientManager,
database Database,
) (*FundRebalancer, error) {
chainIDToPriavateKey, err := loadChainIDToPrivateKeyMap(keysPath)
Expand All @@ -56,6 +63,7 @@ func NewFundRebalancer(
chainIDToPrivateKey: chainIDToPriavateKey,
skipgo: skipgo,
evmClientManager: evmClientManager,
cctpClientManager: cctpClientManager,
config: config.GetConfigReader(ctx).Config().FundRebalancer,
database: database,
trasferTracker: NewTransferTracker(skipgo, database),
Expand Down Expand Up @@ -254,6 +262,21 @@ func (r *FundRebalancer) MoveFundsToChain(
return nil, nil, fmt.Errorf("submitting signed txns required for fund rebalancing: %w", err)
}

sourceChainClient, err := r.cctpClientManager.GetClient(ctx, rebalanceFromChainID)
if err != nil {
lmt.Logger(ctx).Error("failed to get chain client to monitor gas balance", zap.Error(err))
}

// dont fail if we cant get the chain client, just log an error
if sourceChainClient != nil {
err = utils.MonitorGasBalance(ctx, rebalanceFromChainID, sourceChainClient)
if err != nil {
lmt.Logger(ctx).Error("failed to monitor gas balance", zap.Error(err), zap.String("chainID", rebalanceFromChainID))
}
}

metrics.FromContext(ctx).IncFundsRebalanceTransfers(rebalanceFromChainID, rebalanceToChain, dbtypes.RebalanceTransactionStatusPending)

totalUSDCcMoved = new(big.Int).Add(totalUSDCcMoved, usdcToRebalance)
hashes = append(hashes, txnHashes...)

Expand Down Expand Up @@ -338,6 +361,7 @@ func (r *FundRebalancer) usdcBalance(ctx context.Context, chainID string) (*big.
func (r *FundRebalancer) pendingUSDCBalance(ctx context.Context, chainID string) (*big.Int, error) {
pendingTransfers, err := r.database.GetPendingRebalanceTransfersToChain(ctx, chainID)
if err != nil {
metrics.FromContext(ctx).IncDatabaseErrors(dbtypes.GET)
return nil, fmt.Errorf("getting pending rebalance transfers to chain from db: %w", err)
}

Expand Down Expand Up @@ -588,6 +612,7 @@ func (r *FundRebalancer) SubmitTxns(
Amount: signedTxn.Amount.String(),
}
if _, err := r.database.InsertRebalanceTransfer(ctx, args); err != nil {
metrics.FromContext(ctx).IncDatabaseErrors(dbtypes.INSERT)
return nil, fmt.Errorf("inserting rebalance transaction with hash %s into db: %w", hash, err)
}

Expand Down
8 changes: 4 additions & 4 deletions fundrebalancer/fundrebalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestFundRebalancer_Rebalance(t *testing.T) {
mockEVMClientManager.EXPECT().GetClient(mockContext, arbitrumChainID).Return(mockEVMClient, nil)
mockDatabse := mock_database.NewMockDatabase(t)

rebalancer, err := fundrebalancer.NewFundRebalancer(ctx, f.Name(), mockSkipGo, mockEVMClientManager, mockDatabse)
rebalancer, err := fundrebalancer.NewFundRebalancer(ctx, f.Name(), mockSkipGo, mockEVMClientManager, nil, mockDatabse)
assert.NoError(t, err)

// setup initial state of mocks
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestFundRebalancer_Rebalance(t *testing.T) {
mockEVMClientManager.EXPECT().GetClient(mockContext, arbitrumChainID).Return(mockEVMClient, nil)
mockDatabse := mock_database.NewMockDatabase(t)

rebalancer, err := fundrebalancer.NewFundRebalancer(ctx, f.Name(), mockSkipGo, mockEVMClientManager, mockDatabse)
rebalancer, err := fundrebalancer.NewFundRebalancer(ctx, f.Name(), mockSkipGo, mockEVMClientManager, nil, mockDatabse)
assert.NoError(t, err)

// setup initial state of mocks
Expand Down Expand Up @@ -316,7 +316,7 @@ func TestFundRebalancer_Rebalance(t *testing.T) {
// using an in memory database for this test
mockDatabse := mock_database.NewFakeDatabase()

rebalancer, err := fundrebalancer.NewFundRebalancer(ctx, f.Name(), mockSkipGo, mockEVMClientManager, mockDatabse)
rebalancer, err := fundrebalancer.NewFundRebalancer(ctx, f.Name(), mockSkipGo, mockEVMClientManager, nil, mockDatabse)
assert.NoError(t, err)

// setup initial state of mocks
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestFundRebalancer_Rebalance(t *testing.T) {
mockEVMClientManager.EXPECT().GetClient(mockContext, arbitrumChainID).Return(mockEVMClient, nil)
mockDatabse := mock_database.NewMockDatabase(t)

rebalancer, err := fundrebalancer.NewFundRebalancer(ctx, f.Name(), mockSkipGo, mockEVMClientManager, mockDatabse)
rebalancer, err := fundrebalancer.NewFundRebalancer(ctx, f.Name(), mockSkipGo, mockEVMClientManager, nil, mockDatabse)
assert.NoError(t, err)

// setup initial state of mocks
Expand Down
Loading