Commit df61d215 authored by Matthew Slipper's avatar Matthew Slipper Committed by GitHub

Add disburser wallet balance to teleportr (#2551)

parent 2177c8ef
---
'@eth-optimism/teleportr': patch
---
Add disburser balance to status
...@@ -56,6 +56,11 @@ func Main(gitVersion string) func(*cli.Context) error { ...@@ -56,6 +56,11 @@ func Main(gitVersion string) func(*cli.Context) error {
return err return err
} }
disburserWalletAddr, err := bsscore.ParseAddress(cfg.DisburserWalletAddress)
if err != nil {
return err
}
l1Client, err := dial.L1EthClientWithTimeout( l1Client, err := dial.L1EthClientWithTimeout(
ctx, cfg.L1EthRpc, cfg.DisableHTTP2, ctx, cfg.L1EthRpc, cfg.DisableHTTP2,
) )
...@@ -64,6 +69,14 @@ func Main(gitVersion string) func(*cli.Context) error { ...@@ -64,6 +69,14 @@ func Main(gitVersion string) func(*cli.Context) error {
} }
defer l1Client.Close() defer l1Client.Close()
l2Client, err := dial.L1EthClientWithTimeout(
ctx, cfg.L2EthRpc, cfg.DisableHTTP2,
)
if err != nil {
return err
}
defer l2Client.Close()
depositContract, err := deposit.NewTeleportrDeposit( depositContract, err := deposit.NewTeleportrDeposit(
depositAddr, l1Client, depositAddr, l1Client,
) )
...@@ -92,8 +105,10 @@ func Main(gitVersion string) func(*cli.Context) error { ...@@ -92,8 +105,10 @@ func Main(gitVersion string) func(*cli.Context) error {
server := NewServer( server := NewServer(
ctx, ctx,
l1Client, l1Client,
l2Client,
database, database,
depositAddr, depositAddr,
disburserWalletAddr,
depositContract, depositContract,
cfg.NumConfirmations, cfg.NumConfirmations,
) )
...@@ -129,40 +144,44 @@ func Main(gitVersion string) func(*cli.Context) error { ...@@ -129,40 +144,44 @@ func Main(gitVersion string) func(*cli.Context) error {
} }
type Config struct { type Config struct {
Hostname string Hostname string
Port uint16 Port uint16
L1EthRpc string L1EthRpc string
DepositAddress string L2EthRpc string
NumConfirmations uint64 DepositAddress string
PostgresHost string NumConfirmations uint64
PostgresPort uint16 DisburserWalletAddress string
PostgresUser string PostgresHost string
PostgresPassword string PostgresPort uint16
PostgresDBName string PostgresUser string
PostgresEnableSSL bool PostgresPassword string
MetricsServerEnable bool PostgresDBName string
MetricsHostname string PostgresEnableSSL bool
MetricsPort uint64 MetricsServerEnable bool
DisableHTTP2 bool MetricsHostname string
MetricsPort uint64
DisableHTTP2 bool
} }
func NewConfig(ctx *cli.Context) (Config, error) { func NewConfig(ctx *cli.Context) (Config, error) {
return Config{ return Config{
Hostname: ctx.GlobalString(flags.APIHostnameFlag.Name), Hostname: ctx.GlobalString(flags.APIHostnameFlag.Name),
Port: uint16(ctx.GlobalUint64(flags.APIPortFlag.Name)), Port: uint16(ctx.GlobalUint64(flags.APIPortFlag.Name)),
L1EthRpc: ctx.GlobalString(flags.L1EthRpcFlag.Name), L1EthRpc: ctx.GlobalString(flags.L1EthRpcFlag.Name),
DepositAddress: ctx.GlobalString(flags.DepositAddressFlag.Name), L2EthRpc: ctx.GlobalString(flags.L2EthRpcFlag.Name),
NumConfirmations: ctx.GlobalUint64(flags.NumDepositConfirmationsFlag.Name), DepositAddress: ctx.GlobalString(flags.DepositAddressFlag.Name),
PostgresHost: ctx.GlobalString(flags.PostgresHostFlag.Name), NumConfirmations: ctx.GlobalUint64(flags.NumDepositConfirmationsFlag.Name),
PostgresPort: uint16(ctx.GlobalUint64(flags.PostgresPortFlag.Name)), DisburserWalletAddress: ctx.GlobalString(flags.DisburserWalletAddressFlag.Name),
PostgresUser: ctx.GlobalString(flags.PostgresUserFlag.Name), PostgresHost: ctx.GlobalString(flags.PostgresHostFlag.Name),
PostgresPassword: ctx.GlobalString(flags.PostgresPasswordFlag.Name), PostgresPort: uint16(ctx.GlobalUint64(flags.PostgresPortFlag.Name)),
PostgresDBName: ctx.GlobalString(flags.PostgresDBNameFlag.Name), PostgresUser: ctx.GlobalString(flags.PostgresUserFlag.Name),
PostgresEnableSSL: ctx.GlobalBool(flags.PostgresEnableSSLFlag.Name), PostgresPassword: ctx.GlobalString(flags.PostgresPasswordFlag.Name),
MetricsServerEnable: ctx.GlobalBool(flags.MetricsServerEnableFlag.Name), PostgresDBName: ctx.GlobalString(flags.PostgresDBNameFlag.Name),
MetricsHostname: ctx.GlobalString(flags.MetricsHostnameFlag.Name), PostgresEnableSSL: ctx.GlobalBool(flags.PostgresEnableSSLFlag.Name),
MetricsPort: ctx.GlobalUint64(flags.MetricsPortFlag.Name), MetricsServerEnable: ctx.GlobalBool(flags.MetricsServerEnableFlag.Name),
DisableHTTP2: ctx.GlobalBool(flags.HTTP2DisableFlag.Name), MetricsHostname: ctx.GlobalString(flags.MetricsHostnameFlag.Name),
MetricsPort: ctx.GlobalUint64(flags.MetricsPortFlag.Name),
DisableHTTP2: ctx.GlobalBool(flags.HTTP2DisableFlag.Name),
}, nil }, nil
} }
...@@ -174,12 +193,14 @@ const ( ...@@ -174,12 +193,14 @@ const (
) )
type Server struct { type Server struct {
ctx context.Context ctx context.Context
l1Client *ethclient.Client l1Client *ethclient.Client
database *db.Database l2Client *ethclient.Client
depositAddr common.Address database *db.Database
depositContract *deposit.TeleportrDeposit depositAddr common.Address
numConfirmations uint64 disburserWalletAddr common.Address
depositContract *deposit.TeleportrDeposit
numConfirmations uint64
httpServer *http.Server httpServer *http.Server
} }
...@@ -187,23 +208,26 @@ type Server struct { ...@@ -187,23 +208,26 @@ type Server struct {
func NewServer( func NewServer(
ctx context.Context, ctx context.Context,
l1Client *ethclient.Client, l1Client *ethclient.Client,
l2Client *ethclient.Client,
database *db.Database, database *db.Database,
depositAddr common.Address, depositAddr common.Address,
disburserWalletAddr common.Address,
depositContract *deposit.TeleportrDeposit, depositContract *deposit.TeleportrDeposit,
numConfirmations uint64, numConfirmations uint64,
) *Server { ) *Server {
if numConfirmations == 0 { if numConfirmations == 0 {
panic("NumConfirmations cannot be zero") panic("NumConfirmations cannot be zero")
} }
return &Server{ return &Server{
ctx: ctx, ctx: ctx,
l1Client: l1Client, l1Client: l1Client,
database: database, l2Client: l2Client,
depositAddr: depositAddr, database: database,
depositContract: depositContract, depositAddr: depositAddr,
numConfirmations: numConfirmations, disburserWalletAddr: disburserWalletAddr,
depositContract: depositContract,
numConfirmations: numConfirmations,
} }
} }
...@@ -246,11 +270,12 @@ func HandleHealthz(w http.ResponseWriter, r *http.Request) { ...@@ -246,11 +270,12 @@ func HandleHealthz(w http.ResponseWriter, r *http.Request) {
} }
type StatusResponse struct { type StatusResponse struct {
CurrentBalanceWei string `json:"current_balance_wei"` DisburserWalletBalanceWei string `json:"disburser_wallet_balance_wei"`
MaximumBalanceWei string `json:"maximum_balance_wei"` DepositContractBalanceWei string `json:"deposit_contract_balance_wei"`
MinDepositAmountWei string `json:"min_deposit_amount_wei"` MaximumBalanceWei string `json:"maximum_balance_wei"`
MaxDepositAmountWei string `json:"max_deposit_amount_wei"` MinDepositAmountWei string `json:"min_deposit_amount_wei"`
IsAvailable bool `json:"is_available"` MaxDepositAmountWei string `json:"max_deposit_amount_wei"`
IsAvailable bool `json:"is_available"`
} }
func (s *Server) HandleStatus( func (s *Server) HandleStatus(
...@@ -285,21 +310,28 @@ func (s *Server) HandleStatus( ...@@ -285,21 +310,28 @@ func (s *Server) HandleStatus(
curBalance, err := s.l1Client.BalanceAt(ctx, s.depositAddr, nil) curBalance, err := s.l1Client.BalanceAt(ctx, s.depositAddr, nil)
if err != nil { if err != nil {
rpcErrorsTotal.WithLabelValues("balance_at").Inc() rpcErrorsTotal.WithLabelValues("deposit_balance_at").Inc()
return err
}
disburserWalletBal, err := s.l2Client.BalanceAt(ctx, s.disburserWalletAddr, nil)
if err != nil {
rpcErrorsTotal.WithLabelValues("disburser_wallet_balance_at").Inc()
return err return err
} }
balanceAfterMaxDeposit := new(big.Int).Add( balanceAfterMaxDeposit := new(big.Int).Add(
curBalance, maxDepositAmount, curBalance, maxDepositAmount,
) )
isAvailable := maxBalance.Cmp(balanceAfterMaxDeposit) >= 0 isAvailable := maxBalance.Cmp(balanceAfterMaxDeposit) >= 0 && disburserWalletBal.Cmp(maxDepositAmount) > 0
resp := StatusResponse{ resp := StatusResponse{
CurrentBalanceWei: curBalance.String(), DisburserWalletBalanceWei: disburserWalletBal.String(),
MaximumBalanceWei: maxBalance.String(), DepositContractBalanceWei: curBalance.String(),
MinDepositAmountWei: minDepositAmount.String(), MaximumBalanceWei: maxBalance.String(),
MaxDepositAmountWei: maxDepositAmount.String(), MinDepositAmountWei: minDepositAmount.String(),
IsAvailable: isAvailable, MaxDepositAmountWei: maxDepositAmount.String(),
IsAvailable: isAvailable,
} }
jsonResp, err := json.Marshal(resp) jsonResp, err := json.Marshal(resp)
......
...@@ -24,12 +24,20 @@ var ( ...@@ -24,12 +24,20 @@ var (
Required: true, Required: true,
EnvVar: prefixAPIEnvVar("PORT"), EnvVar: prefixAPIEnvVar("PORT"),
} }
DisburserWalletAddressFlag = cli.StringFlag{
Name: "disburser-wallet-address",
Usage: "The address of the disburser wallet",
Required: true,
EnvVar: prefixAPIEnvVar("DISBURSER_WALLET_ADDRESS"),
}
) )
var APIFlags = []cli.Flag{ var APIFlags = []cli.Flag{
APIHostnameFlag, APIHostnameFlag,
APIPortFlag, APIPortFlag,
DisburserWalletAddressFlag,
L1EthRpcFlag, L1EthRpcFlag,
L2EthRpcFlag,
DepositAddressFlag, DepositAddressFlag,
NumDepositConfirmationsFlag, NumDepositConfirmationsFlag,
PostgresHostFlag, PostgresHostFlag,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment