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 {
return err
}
disburserWalletAddr, err := bsscore.ParseAddress(cfg.DisburserWalletAddress)
if err != nil {
return err
}
l1Client, err := dial.L1EthClientWithTimeout(
ctx, cfg.L1EthRpc, cfg.DisableHTTP2,
)
......@@ -64,6 +69,14 @@ func Main(gitVersion string) func(*cli.Context) error {
}
defer l1Client.Close()
l2Client, err := dial.L1EthClientWithTimeout(
ctx, cfg.L2EthRpc, cfg.DisableHTTP2,
)
if err != nil {
return err
}
defer l2Client.Close()
depositContract, err := deposit.NewTeleportrDeposit(
depositAddr, l1Client,
)
......@@ -92,8 +105,10 @@ func Main(gitVersion string) func(*cli.Context) error {
server := NewServer(
ctx,
l1Client,
l2Client,
database,
depositAddr,
disburserWalletAddr,
depositContract,
cfg.NumConfirmations,
)
......@@ -132,8 +147,10 @@ type Config struct {
Hostname string
Port uint16
L1EthRpc string
L2EthRpc string
DepositAddress string
NumConfirmations uint64
DisburserWalletAddress string
PostgresHost string
PostgresPort uint16
PostgresUser string
......@@ -151,8 +168,10 @@ func NewConfig(ctx *cli.Context) (Config, error) {
Hostname: ctx.GlobalString(flags.APIHostnameFlag.Name),
Port: uint16(ctx.GlobalUint64(flags.APIPortFlag.Name)),
L1EthRpc: ctx.GlobalString(flags.L1EthRpcFlag.Name),
L2EthRpc: ctx.GlobalString(flags.L2EthRpcFlag.Name),
DepositAddress: ctx.GlobalString(flags.DepositAddressFlag.Name),
NumConfirmations: ctx.GlobalUint64(flags.NumDepositConfirmationsFlag.Name),
DisburserWalletAddress: ctx.GlobalString(flags.DisburserWalletAddressFlag.Name),
PostgresHost: ctx.GlobalString(flags.PostgresHostFlag.Name),
PostgresPort: uint16(ctx.GlobalUint64(flags.PostgresPortFlag.Name)),
PostgresUser: ctx.GlobalString(flags.PostgresUserFlag.Name),
......@@ -176,8 +195,10 @@ const (
type Server struct {
ctx context.Context
l1Client *ethclient.Client
l2Client *ethclient.Client
database *db.Database
depositAddr common.Address
disburserWalletAddr common.Address
depositContract *deposit.TeleportrDeposit
numConfirmations uint64
......@@ -187,12 +208,13 @@ type Server struct {
func NewServer(
ctx context.Context,
l1Client *ethclient.Client,
l2Client *ethclient.Client,
database *db.Database,
depositAddr common.Address,
disburserWalletAddr common.Address,
depositContract *deposit.TeleportrDeposit,
numConfirmations uint64,
) *Server {
if numConfirmations == 0 {
panic("NumConfirmations cannot be zero")
}
......@@ -200,8 +222,10 @@ func NewServer(
return &Server{
ctx: ctx,
l1Client: l1Client,
l2Client: l2Client,
database: database,
depositAddr: depositAddr,
disburserWalletAddr: disburserWalletAddr,
depositContract: depositContract,
numConfirmations: numConfirmations,
}
......@@ -246,7 +270,8 @@ func HandleHealthz(w http.ResponseWriter, r *http.Request) {
}
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"`
MinDepositAmountWei string `json:"min_deposit_amount_wei"`
MaxDepositAmountWei string `json:"max_deposit_amount_wei"`
......@@ -285,17 +310,24 @@ func (s *Server) HandleStatus(
curBalance, err := s.l1Client.BalanceAt(ctx, s.depositAddr, 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
}
balanceAfterMaxDeposit := new(big.Int).Add(
curBalance, maxDepositAmount,
)
isAvailable := maxBalance.Cmp(balanceAfterMaxDeposit) >= 0
isAvailable := maxBalance.Cmp(balanceAfterMaxDeposit) >= 0 && disburserWalletBal.Cmp(maxDepositAmount) > 0
resp := StatusResponse{
CurrentBalanceWei: curBalance.String(),
DisburserWalletBalanceWei: disburserWalletBal.String(),
DepositContractBalanceWei: curBalance.String(),
MaximumBalanceWei: maxBalance.String(),
MinDepositAmountWei: minDepositAmount.String(),
MaxDepositAmountWei: maxDepositAmount.String(),
......
......@@ -24,12 +24,20 @@ var (
Required: true,
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{
APIHostnameFlag,
APIPortFlag,
DisburserWalletAddressFlag,
L1EthRpcFlag,
L2EthRpcFlag,
DepositAddressFlag,
NumDepositConfirmationsFlag,
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