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,
) )
...@@ -132,8 +147,10 @@ type Config struct { ...@@ -132,8 +147,10 @@ type Config struct {
Hostname string Hostname string
Port uint16 Port uint16
L1EthRpc string L1EthRpc string
L2EthRpc string
DepositAddress string DepositAddress string
NumConfirmations uint64 NumConfirmations uint64
DisburserWalletAddress string
PostgresHost string PostgresHost string
PostgresPort uint16 PostgresPort uint16
PostgresUser string PostgresUser string
...@@ -151,8 +168,10 @@ func NewConfig(ctx *cli.Context) (Config, error) { ...@@ -151,8 +168,10 @@ func NewConfig(ctx *cli.Context) (Config, error) {
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),
L2EthRpc: ctx.GlobalString(flags.L2EthRpcFlag.Name),
DepositAddress: ctx.GlobalString(flags.DepositAddressFlag.Name), DepositAddress: ctx.GlobalString(flags.DepositAddressFlag.Name),
NumConfirmations: ctx.GlobalUint64(flags.NumDepositConfirmationsFlag.Name), NumConfirmations: ctx.GlobalUint64(flags.NumDepositConfirmationsFlag.Name),
DisburserWalletAddress: ctx.GlobalString(flags.DisburserWalletAddressFlag.Name),
PostgresHost: ctx.GlobalString(flags.PostgresHostFlag.Name), PostgresHost: ctx.GlobalString(flags.PostgresHostFlag.Name),
PostgresPort: uint16(ctx.GlobalUint64(flags.PostgresPortFlag.Name)), PostgresPort: uint16(ctx.GlobalUint64(flags.PostgresPortFlag.Name)),
PostgresUser: ctx.GlobalString(flags.PostgresUserFlag.Name), PostgresUser: ctx.GlobalString(flags.PostgresUserFlag.Name),
...@@ -176,8 +195,10 @@ const ( ...@@ -176,8 +195,10 @@ const (
type Server struct { type Server struct {
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
...@@ -187,12 +208,13 @@ type Server struct { ...@@ -187,12 +208,13 @@ 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")
} }
...@@ -200,8 +222,10 @@ func NewServer( ...@@ -200,8 +222,10 @@ func NewServer(
return &Server{ return &Server{
ctx: ctx, ctx: ctx,
l1Client: l1Client, l1Client: l1Client,
l2Client: l2Client,
database: database, database: database,
depositAddr: depositAddr, depositAddr: depositAddr,
disburserWalletAddr: disburserWalletAddr,
depositContract: depositContract, depositContract: depositContract,
numConfirmations: numConfirmations, numConfirmations: numConfirmations,
} }
...@@ -246,7 +270,8 @@ func HandleHealthz(w http.ResponseWriter, r *http.Request) { ...@@ -246,7 +270,8 @@ 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"`
DepositContractBalanceWei string `json:"deposit_contract_balance_wei"`
MaximumBalanceWei string `json:"maximum_balance_wei"` MaximumBalanceWei string `json:"maximum_balance_wei"`
MinDepositAmountWei string `json:"min_deposit_amount_wei"` MinDepositAmountWei string `json:"min_deposit_amount_wei"`
MaxDepositAmountWei string `json:"max_deposit_amount_wei"` MaxDepositAmountWei string `json:"max_deposit_amount_wei"`
...@@ -285,17 +310,24 @@ func (s *Server) HandleStatus( ...@@ -285,17 +310,24 @@ 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(),
DepositContractBalanceWei: curBalance.String(),
MaximumBalanceWei: maxBalance.String(), MaximumBalanceWei: maxBalance.String(),
MinDepositAmountWei: minDepositAmount.String(), MinDepositAmountWei: minDepositAmount.String(),
MaxDepositAmountWei: maxDepositAmount.String(), MaxDepositAmountWei: maxDepositAmount.String(),
......
...@@ -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