Skip to content

Commit

Permalink
[indexer.api.supply_view] add withdrawal suppply calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethen Pociask committed Nov 8, 2023
1 parent 6841e79 commit fd47aec
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 29 deletions.
1 change: 1 addition & 0 deletions indexer/api-ts/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ export interface WithdrawalResponse {
}
export interface BridgeSupplyView {
l1DepositSum: number /* float64 */;
l2WithdrawalSum: number /* float64 */;
}
3 changes: 3 additions & 0 deletions indexer/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ func (mbv *MockBridgeTransfersView) L2BridgeWithdrawalsByAddress(address common.
func (mbv *MockBridgeTransfersView) L1BridgeDepositSum() (float64, error) {
return 69, nil
}
func (mbv *MockBridgeTransfersView) L2BridgeWithdrawalSum() (float64, error) {
return 420, nil
}

func TestHealthz(t *testing.T) {
logger := testlog.Logger(t, log.LvlInfo)
Expand Down
3 changes: 2 additions & 1 deletion indexer/api/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type WithdrawalResponse struct {
}

type BridgeSupplyView struct {
L1DepositSum float64 `json:"l1DepositSum"`
L1DepositSum float64 `json:"l1DepositSum"`
L2WithdrawalSum float64 `json:"l2WithdrawalSum"`
}

// FIXME make a pure function that returns a struct instead of newWithdrawalResponse
Expand Down
25 changes: 0 additions & 25 deletions indexer/api/routes/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,3 @@ func (h Routes) L1DepositsHandler(w http.ResponseWriter, r *http.Request) {
h.logger.Error("Error writing response", "err", err)
}
}

// SupplyView ... Handles /api/v0/supply GET requests
func (h Routes) SupplyView(w http.ResponseWriter, r *http.Request) {
sum, err := h.view.L1BridgeDepositSum()
if err != nil {
http.Error(w, "Internal server error reading deposits", http.StatusInternalServerError)
h.logger.Error("Unable to read deposits from DB", "err", err.Error())
return
}

view := models.BridgeSupplyView{
L1DepositSum: sum,
}

// TODO - Add support for:
// - L2DepositSum
// - L1WithdrawalProvenSum
// - L1WithdrawalFinalizedSum
// - L2WithdrawalInitiatedSum

err = jsonResponse(w, view, http.StatusOK)
if err != nil {
h.logger.Error("Error writing response", "err", err)
}
}
35 changes: 35 additions & 0 deletions indexer/api/routes/supply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package routes

import (
"net/http"

"github.com/ethereum-optimism/optimism/indexer/api/models"
)

// SupplyView ... Handles /api/v0/supply GET requests
func (h Routes) SupplyView(w http.ResponseWriter, r *http.Request) {

depositSum, err := h.view.L1BridgeDepositSum()
if err != nil {
http.Error(w, "internal server error reading deposits", http.StatusInternalServerError)
h.logger.Error("unable to read deposits from DB", "err", err.Error())
return
}

withdrawalSum, err := h.view.L2BridgeWithdrawalSum()
if err != nil {
http.Error(w, "internal server error reading withdrawals", http.StatusInternalServerError)
h.logger.Error("unable to read withdrawals from DB", "err", err.Error())
return
}

view := models.BridgeSupplyView{
L1DepositSum: depositSum,
L2WithdrawalSum: withdrawalSum,
}

err = jsonResponse(w, view, http.StatusOK)
if err != nil {
h.logger.Error("error writing response", "err", err)
}
}
12 changes: 11 additions & 1 deletion indexer/database/bridge_transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type BridgeTransfersView interface {
L1BridgeDepositsByAddress(common.Address, string, int) (*L1BridgeDepositsResponse, error)

L2BridgeWithdrawal(common.Hash) (*L2BridgeWithdrawal, error)
L2BridgeWithdrawalSum() (float64, error)
L2BridgeWithdrawalWithFilter(BridgeTransfer) (*L2BridgeWithdrawal, error)
L2BridgeWithdrawalsByAddress(common.Address, string, int) (*L2BridgeWithdrawalsResponse, error)
}
Expand Down Expand Up @@ -139,7 +140,6 @@ type L1BridgeDepositsResponse struct {

// L1BridgeDepositSum ... returns the sum of all l1 bridge deposit mints in gwei
func (db *bridgeTransfersDB) L1BridgeDepositSum() (float64, error) {
// (1) Fetch the sum of all deposits in gwei
var sum float64
result := db.gorm.Model(&L1TransactionDeposit{}).Select("sum(amount)").Scan(&sum)
if result.Error != nil {
Expand Down Expand Up @@ -246,6 +246,16 @@ func (db *bridgeTransfersDB) L2BridgeWithdrawal(txWithdrawalHash common.Hash) (*
return &withdrawal, nil
}

func (db *bridgeTransfersDB) L2BridgeWithdrawalSum() (float64, error) {
var sum float64
result := db.gorm.Model(&L2TransactionWithdrawal{}).Select("sum(amount)").Scan(&sum)
if result.Error != nil {
return 0, result.Error
}

return sum, nil
}

// L2BridgeWithdrawalWithFilter queries for a bridge withdrawal with set fields in the `BridgeTransfer` filter
func (db *bridgeTransfersDB) L2BridgeWithdrawalWithFilter(filter BridgeTransfer) (*L2BridgeWithdrawal, error) {
var withdrawal L2BridgeWithdrawal
Expand Down
11 changes: 9 additions & 2 deletions indexer/e2e_tests/bridge_transfers_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ func TestClientBridgeFunctions(t *testing.T) {
}

mintSum := bigint.Zero
withdrawSum := bigint.Zero

actors := []actor{
{
addr: aliceAddr,
Expand Down Expand Up @@ -512,6 +514,8 @@ func TestClientBridgeFunctions(t *testing.T) {
return l2Header != nil && l2Header.Number.Uint64() >= l2ToL1WithdrawReceipt.BlockNumber.Uint64(), nil
}))

withdrawSum = new(big.Int).Add(withdrawSum, l2ToL1MessagePasserWithdrawTx.Value())

// (3.d) Ensure that withdrawal and deposit txs are retrievable via API
deposits, err := testSuite.Client.GetAllDepositsByAddress(actor.addr)
require.NoError(t, err)
Expand All @@ -529,7 +533,10 @@ func TestClientBridgeFunctions(t *testing.T) {
assessment, err := testSuite.Client.GetSupplyAssessment()
require.NoError(t, err)

asFloat, _ := mintSum.Float64()
require.Equal(t, asFloat, assessment.L1DepositSum)
mintFloat, _ := mintSum.Float64()
require.Equal(t, mintFloat, assessment.L1DepositSum)

withdrawFloat, _ := withdrawSum.Float64()
require.Equal(t, withdrawFloat, assessment.L2WithdrawalSum)

}

0 comments on commit fd47aec

Please sign in to comment.