Skip to content

Commit

Permalink
imp: cleanup V1 unused endpoints (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
facs95 authored Jun 23, 2023
1 parent b191dc1 commit 3071afe
Show file tree
Hide file tree
Showing 16 changed files with 103 additions and 881 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ run:
@go build && ./dashboard-backend

lint:
golangci-lint run --fix --out-format=tab --issues-exit-code=0 --config .golangci.yml --color always ./...
golangci-lint run --out-format=tab --issues-exit-code=0 --config .golangci.yml --color always ./...
3 changes: 2 additions & 1 deletion internal/blockchain/transactionbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ func CreateTransactionWithMessage(
bytes := []byte{}

gasAmount := sdk.NewCoins(sdk.NewCoin(denom, fee))
feeSdk := legacytx.NewStdFee(gasLimit, gasAmount) //nolint:staticcheck

//nolint:staticcheck
feeSdk := legacytx.NewStdFee(gasLimit, gasAmount)
// TODO: use AuxTxBuilder
dataAmino := legacytx.StdSignBytes(chainID, accountNumber, sequence, 0, feeSdk, sdkMessages, memo, nil)

Expand Down
67 changes: 0 additions & 67 deletions internal/handler/v1/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
package v1

import (
"encoding/json"

"github.com/fasthttp/router"
"github.com/tharsis/dashboard-backend/internal/blockchain"
"github.com/tharsis/dashboard-backend/internal/resources"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -49,63 +45,6 @@ func BalanceByNetworkAndDenom(ctx *fasthttp.RequestCtx) {
sendResponse(val, err, ctx)
}

func EVMOSIBCBalances(ctx *fasthttp.RequestCtx) {
pubKey := paramToString("pubkey", ctx)

networkConfigs, err := resources.GetNetworkConfigs()
if err != nil {
sendResponse("Unable to get registry configurations", err, ctx)
return
}

balances := []EVMOSBalance{}

// TODO: We should send all the requests at the same time to speed up this call
for _, v := range networkConfigs {
derivedAddress, err := blockchain.DeriveCosmosAddress(pubKey, v.Prefix)
if err != nil {
// Unable to derive address
continue
}

configuration := resources.GetMainnetConfig(v)
evmosIbcDenom, err := GetDenom("EVMOS", configuration.Identifier)
if err != nil {
// Unable to get EVMOS denom for source chain
continue
}

endpoint := BuildFourParamEndpoint("/cosmos/bank/v1beta1/balances/", derivedAddress, "/by_denom?denom=", evmosIbcDenom)
val, err := getRequestRest(configuration.Identifier, endpoint)
if err != nil {
// Unable to get EVMOS balance in chain provided
continue
}

var balance BalanceResponse

err = json.Unmarshal([]byte(val), &balance)

if err != nil {
// Unable to get EVMOS balance in chain provided
continue
}

balances = append(balances, EVMOSBalance{
Chain: configuration.Identifier,
EvmosBalance: balance.Balance.Amount,
})
}

res, err := json.Marshal(balances)
if err != nil {
sendResponse("Unable to get EVMOS balances", err, ctx)
return
}

sendResponse("{\"values\":"+string(res)+"}", err, ctx)
}

func EVMOSIBCBalance(ctx *fasthttp.RequestCtx) {
sourceChain := getChain(ctx)

Expand All @@ -122,9 +61,3 @@ func EVMOSIBCBalance(ctx *fasthttp.RequestCtx) {
}
sendResponse(val, err, ctx)
}

func AddBalancesRoutes(r *router.Router) {
r.GET("/BalanceByNetworkAndDenom/{chain}/{token}/{address}", BalanceByNetworkAndDenom)
r.GET("/EVMOSIBCBalances/{pubkey:*}", EVMOSIBCBalances)
r.GET("/EVMOSIBCBalance/{chain}/{address}", EVMOSIBCBalance)
}
153 changes: 0 additions & 153 deletions internal/handler/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

sdkmath "cosmossdk.io/math"

"github.com/fasthttp/router"
"github.com/tharsis/dashboard-backend/internal/db"
"github.com/valyala/fasthttp"
)
Expand Down Expand Up @@ -156,151 +155,6 @@ func GetValidatorsWithNoFilter(chain string) (map[string]Validator, error) {
return valMap, nil
}

func UnbondingByAddressWithValidatorInfo(ctx *fasthttp.RequestCtx) {
chain := getChain(ctx)
address := paramToString("address", ctx)

if val, err := db.RedisGetUnbondingByAddressWithValidatorInfo(chain, address); err == nil {
sendResponse(val, nil, ctx)
return
}

endpoint := buildThreeParamEndpoint("/cosmos/staking/v1beta1/delegators/", address, "/unbonding_delegations")
val, err := getRequestRest(chain, endpoint)
if err != nil {
sendResponse("", err, ctx)
return
}

var unbodings UnbondingResponseAPI
err = json.Unmarshal([]byte(val), &unbodings)
if err != nil {
sendResponse("", err, ctx)
return
}

valMap, err := GetValidatorsWithNoFilter(chain)
if err != nil {
sendResponse("", err, ctx)
return
}

res := []interface{}{}
for _, u := range unbodings.UnbondingResponses {
_, exists := valMap[u.ValidatorAddress]
if exists {
u.Validator = valMap[u.ValidatorAddress]
}
res = append(res, u)
}

valuesToSend, err := json.Marshal(res)
if err != nil {
sendResponse("", err, ctx)
return
}

value := "{\"values\":" + string(valuesToSend) + "}"

db.RedisSetUnbondingByAddressWithValidatorInfo(chain, address, value)

sendResponse(value, nil, ctx)
}

func DelegationsByAddressWithValidatorRanks(ctx *fasthttp.RequestCtx) {
chain := getChain(ctx)
address := paramToString("address", ctx)

if val, err := db.RedisGetDelegationsByAddressWithValidatorRanks(chain, address); err == nil {
sendResponse(val, nil, ctx)
return
}

endpoint := buildThreeParamEndpoint("/cosmos/staking/v1beta1/delegations/", address, "?pagination.limit=200")
val, _ := getRequestRest(getChain(ctx), endpoint)

// ValidatorsWithRank
valWithRanks, err := GetValidatorsWithRanks(chain)
if err != nil {
sendResponse("", err, ctx)
return
}

// Parse the response to append the ranks
var delegation DelegationResponsesResponse
err = json.Unmarshal([]byte(val), &delegation)
if err != nil {
sendResponse("", err, ctx)
return
}

for i, v := range delegation.DelegationResponse {
item := v.Delegation
if val, ok := valWithRanks[item.ValidatorAddress]; ok {
item.ValidatorRank = val.Rank
} else {
item.ValidatorRank = -1
}
delegation.DelegationResponse[i].Delegation = item
}

valuesToSend, err := json.Marshal(delegation)
if err != nil {
sendResponse("", err, ctx)
return
}

db.RedisSetDelegationsByAddressWithValidatorRanks(chain, address, string(valuesToSend))
sendResponse(string(valuesToSend), err, ctx)
}

func ValidatorsByAddressWithValidatorRanks(ctx *fasthttp.RequestCtx) {
chain := getChain(ctx)
address := paramToString("address", ctx)

if val, err := db.RedisGetValidatorsByAddressWithValidatorRanks(chain, address); err == nil {
sendResponse(val, nil, ctx)
return
}

endpoint := buildThreeParamEndpoint("/cosmos/staking/v1beta1/delegators/", address, "/validators")
val, _ := getRequestRest(chain, endpoint)

// ValidatorsWithRank
valWithRanks, err := GetValidatorsWithRanks(chain)
if err != nil {
sendResponse("", err, ctx)
return
}

// Parse the response to append the ranks
var validators ValidatorAPIResponse
err = json.Unmarshal([]byte(val), &validators)
if err != nil {
sendResponse("", err, ctx)
return
}

for i, v := range validators.Validators {
item := v
if val, ok := valWithRanks[item.OperatorAddress]; ok {
item.Rank = val.Rank
} else {
item.Rank = -1
}
validators.Validators[i] = item
}

valuesToSend, err := json.Marshal(validators)
if err != nil {
sendResponse("", err, ctx)
return
}

db.RedisSetValidatorsByAddressWithValidatorRanks(chain, address, string(valuesToSend))
sendResponse(string(valuesToSend), err, ctx)
}

func RemainingEpochs(ctx *fasthttp.RequestCtx) {
// query skipped epochs
skippedEndpoint := "/evmos/inflation/v1/skipped_epochs"
Expand Down Expand Up @@ -355,10 +209,3 @@ func RemainingEpochs(ctx *fasthttp.RequestCtx) {

sendResponse(string(res), err, ctx)
}

func AddCustomRoutes(r *router.Router) {
r.GET("/UnbondingByAddressWithValidatorInfo/{chain}/{address}", UnbondingByAddressWithValidatorInfo)
r.GET("/ValidatorsByAddressWithValidatorRanks/{chain}/{address}", ValidatorsByAddressWithValidatorRanks)
r.GET("/DelegationsByAddressWithValidatorRanks/{chain}/{address}", DelegationsByAddressWithValidatorRanks)
r.GET("/RemainingEpochs", RemainingEpochs)
}
31 changes: 0 additions & 31 deletions internal/handler/v1/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"

decimal "github.com/cosmos/cosmos-sdk/types"
"github.com/fasthttp/router"
"github.com/tharsis/dashboard-backend/internal/blockchain"
"github.com/tharsis/dashboard-backend/internal/constants"
"github.com/tharsis/dashboard-backend/internal/db"
Expand All @@ -22,11 +21,6 @@ import (
"golang.org/x/exp/slices"
)

func ERC20Balance(ctx *fasthttp.RequestCtx) {
val, err := blockchain.GetERC20Balance(paramToString("contract", ctx), paramToString("address", ctx))
sendResponse(val, err, ctx)
}

type Pagination struct {
NextKey interface{} `json:"next_key"`
Total string `json:"total"`
Expand Down Expand Up @@ -298,28 +292,3 @@ func ERC20TokensByNameInternal(name string) (string, error) {
}
return "", fmt.Errorf("invalid token, please try again")
}

func ERC20Tokens(ctx *fasthttp.RequestCtx) {
erc20Tokens, err := resources.GetERC20Tokens()
if err != nil {
sendResponse("", err, ctx)
return
}

stringRes, err := json.Marshal(erc20Tokens)
if err != nil {
sendResponse("", err, ctx)
return
}

res := "{\"values\":" + string(stringRes) + "}"

sendResponse(res, nil, ctx)
}

func AddERC20Routes(r *router.Router) {
r.GET("/ERC20Balance/{contract}/{address}", ERC20Balance)
r.GET("/ERC20ModuleBalance", ERC20ModuleEmptyBalance)
r.GET("/ERC20ModuleBalance/{evmos_address}/{eth_address}", ERC20ModuleBalance)
r.GET("/ERC20Tokens", ERC20Tokens)
}
37 changes: 0 additions & 37 deletions internal/handler/v1/governance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package v1
import (
"encoding/json"

"github.com/fasthttp/router"
"github.com/tharsis/dashboard-backend/internal/blockchain"
"github.com/tharsis/dashboard-backend/internal/db"

Expand Down Expand Up @@ -51,37 +50,6 @@ func ProcessProposals(proposalsRes string, v1 bool) ([]byte, error) {
return proposalRes, nil
}

// This endpoint returns a list of the latest 50 governance proposals. In order
// to support both v1 and v1beta1 versions this endpoint converts the v1 payload
// to be the same as the v1beta1 payload.
func GovernanceProposals(ctx *fasthttp.RequestCtx) {
var proposalRes []byte
if redisVal, err := db.RedisGetGovernanceProposals(); err == nil && redisVal != "null" {
proposalRes = []byte(redisVal)
if err != nil {
sendResponse("Unable to fetch governance proposals", err, ctx)
return
}
} else {
endpoint := buildThreeParamEndpoint("/cosmos/gov/v1/proposals?pagination.limit=", "50", "&pagination.reverse=true")
val, err := getRequestRest("EVMOS", endpoint)
if err != nil {
sendResponse("Unable to fetch governance proposals", err, ctx)
return
}

// Process and convert v1 payload into v1beta1 payload version
proposalRes, err = ProcessProposals(val, false)
if err != nil {
sendResponse("Unable to fetch governance proposals", err, ctx)
return
}

db.RedisSetGovernanceProposals(string(proposalRes))
}
sendResponse(string(proposalRes), nil, ctx)
}

func V1GovernanceProposals(ctx *fasthttp.RequestCtx) { //nolint: revive
var proposalRes []byte
if redisVal, err := db.RedisGetGovernanceV1Proposals(); err == nil && redisVal != "null" {
Expand Down Expand Up @@ -109,8 +77,3 @@ func V1GovernanceProposals(ctx *fasthttp.RequestCtx) { //nolint: revive
}
sendResponse(string(proposalRes), nil, ctx)
}

func AddGovernanceRoutes(r *router.Router) {
r.GET("/Proposals", GovernanceProposals)
r.GET("/V1Proposals", V1GovernanceProposals)
}
6 changes: 0 additions & 6 deletions internal/handler/v1/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"strings"

"github.com/fasthttp/router"
"github.com/tharsis/dashboard-backend/internal/db"
"github.com/tharsis/dashboard-backend/internal/requester"
"github.com/tharsis/dashboard-backend/internal/resources"
Expand Down Expand Up @@ -87,8 +86,3 @@ func NetworkConfigByName(ctx *fasthttp.RequestCtx) {
}
sendResponse(val, nil, ctx)
}

func AddNetworkRoutes(r *router.Router) {
r.GET("/NetworkConfig", NetworkConfig)
r.GET("/NetworkConfig/{name}", NetworkConfigByName)
}
Loading

0 comments on commit 3071afe

Please sign in to comment.