Skip to content

Commit

Permalink
feat: add unique depositors to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
sol-mocha committed Apr 22, 2023
1 parent d42a256 commit b0ed89e
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 106 deletions.
8 changes: 8 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ components:
- lifeTimeUsdDeposit
- lifeTimeUsdWithdrawal
- lifeTimeUsdVolume
- activeWallets
- uniqueDepositors
properties:
usdTvl:
type: string
Expand All @@ -772,6 +774,12 @@ components:
lifeTimeUsdVolume:
type: string
description: A float 64 string for life time volume (normalized to the current usd price of assets).
activeWallets:
type: number
description: Number of wallets with active postions.
uniqueDepositors:
type: number
description: Total number of unique depositors.


# DATA MODELS
Expand Down
141 changes: 74 additions & 67 deletions pkg/api/apispec/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/api/routes/get_v1_analytics_overview.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"net/http"

"github.com/AlekSi/pointer"
"github.com/dcaf-labs/drip/pkg/api/apispec"
"github.com/dcaf-labs/drip/pkg/service/repository"
"github.com/labstack/echo/v4"
)

Expand All @@ -26,10 +28,22 @@ func (h Handler) GetV1AnalyticsOverview(c echo.Context) error {
if err != nil {
return c.JSON(http.StatusInternalServerError, apispec.ErrorResponse{Error: "internal server error"})
}
uniqueDepositorCount, err := h.analyticsRepo.GetUniqueDepositorCount(ctx)
if err != nil {
return c.JSON(http.StatusInternalServerError, apispec.ErrorResponse{Error: "internal server error"})
}
activeWallets, err := h.repo.GetActiveWallets(ctx, repository.GetActiveWalletParams{
PositionIsClosed: pointer.ToBool(false),
})
if err != nil {
return c.JSON(http.StatusInternalServerError, apispec.ErrorResponse{Error: "internal server error"})
}
return c.JSON(http.StatusOK, apispec.AnalyticsOverviewResponse{
UsdTvl: fmt.Sprintf("%f", tvl.TotalUSDValue),
LifeTimeUsdDeposit: fmt.Sprintf("%f", lifeTimeDeposit.TotalUSDDeposit),
LifeTimeUsdVolume: fmt.Sprintf("%f", lifeTimeVolume.TotalUSDVolume),
LifeTimeUsdWithdrawal: fmt.Sprintf("%f", lifeTimeWithdrawal.TotalUSDWithdrawal),
ActiveWallets: float32(len(activeWallets)),
UniqueDepositors: float32(uniqueDepositorCount),
})
}
8 changes: 8 additions & 0 deletions pkg/service/processor/transaction_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ func (p impl) getDepositWithMetadataMetric(
) (*model.DepositMetric, error) {
var (
vault string
depositor string
referrer *string
tokenADepositAmount uint64
)
Expand All @@ -434,6 +435,7 @@ func (p impl) getDepositWithMetadataMetric(
vault = parsedAccounts.Common.Vault.String()
referrer = pointer.ToString(parsedAccounts.Common.Referrer.String())
tokenADepositAmount = parsedIx.Params.TokenADepositAmount
depositor = parsedAccounts.Common.Depositor.String()
} else if err != nil {
return nil, err
}
Expand All @@ -443,6 +445,7 @@ func (p impl) getDepositWithMetadataMetric(
vault = parsedAccounts.Vault.String()
referrer = nil
tokenADepositAmount = parsedIx.Params.TokenADepositAmount
depositor = parsedAccounts.Depositor.String()
} else if err != nil {
return nil, err
}
Expand All @@ -457,6 +460,7 @@ func (p impl) getDepositWithMetadataMetric(
Vault: vault,
Referrer: referrer,
TokenADepositAmount: tokenADepositAmount,
Depositor: pointer.ToString(depositor),
TokenAUsdPriceDay: nil,
}
return metric, nil
Expand All @@ -468,6 +472,7 @@ func (p impl) getDepositMetric(
) (*model.DepositMetric, error) {
var (
vault string
depositor string
referrer *string
tokenADepositAmount uint64
)
Expand All @@ -478,6 +483,7 @@ func (p impl) getDepositMetric(
vault = parsedAccounts.Common.Vault.String()
referrer = pointer.ToString(parsedAccounts.Common.Referrer.String())
tokenADepositAmount = parsedIx.Params.TokenADepositAmount
depositor = parsedAccounts.Common.Depositor.String()
} else if err != nil {
return nil, err
}
Expand All @@ -487,6 +493,7 @@ func (p impl) getDepositMetric(
vault = parsedAccounts.Vault.String()
referrer = nil
tokenADepositAmount = parsedIx.Params.TokenADepositAmount
depositor = parsedAccounts.Depositor.String()
} else if err != nil {
return nil, err
}
Expand All @@ -501,6 +508,7 @@ func (p impl) getDepositMetric(
Vault: vault,
Referrer: referrer,
TokenADepositAmount: tokenADepositAmount,
Depositor: pointer.ToString(depositor),
TokenAUsdPriceDay: nil,
}
return metric, nil
Expand Down
38 changes: 38 additions & 0 deletions pkg/service/repository/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,44 @@ func (d repositoryImpl) AdminGetVaultsByTokenPairID(ctx context.Context, tokenPa
Find()
}

func (d repositoryImpl) GetAdminPositions(
ctx context.Context, isVaultEnabled *bool,
positionFilterParams PositionFilterParams,
params PaginationParams,
) ([]*model.Position, error) {
stmt := d.repo.Position.WithContext(ctx)

// Apply Joins
if isVaultEnabled != nil {
stmt = stmt.Join(d.repo.Vault, d.repo.Vault.Pubkey.EqCol(d.repo.Position.Vault))
}
if positionFilterParams.Wallet != nil {
stmt = stmt.
Join(d.repo.TokenAccount, d.repo.TokenAccount.Mint.EqCol(d.repo.Position.Authority))
}

// Apply Filters
if isVaultEnabled != nil {
stmt = stmt.Where(d.repo.Vault.Enabled.Is(*isVaultEnabled))
}
if positionFilterParams.Wallet != nil {
stmt = stmt.
Where(
d.repo.TokenAccount.Owner.Eq(*positionFilterParams.Wallet),
d.repo.TokenAccount.Amount.Gt(0))
}
if positionFilterParams.IsClosed != nil {
stmt = stmt.Where(d.repo.Position.IsClosed.Is(*positionFilterParams.IsClosed))
}
if params.Limit != nil {
stmt = stmt.Limit(*params.Limit)
}
if params.Offset != nil {
stmt = stmt.Offset(*params.Offset)
}
return stmt.Find()
}

func (d repositoryImpl) AdminSetVaultEnabled(ctx context.Context, vaultPubkey string, enabled bool) (*model.Vault, error) {
var res model.Vault
_, err := d.repo.Vault.
Expand Down
15 changes: 15 additions & 0 deletions pkg/service/repository/analytics/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b0ed89e

Please sign in to comment.