Repository: trustwallet/blockatlas
Pull request #1023 (2020-04-08 21:30:07)

Refactor tx direction and add caching for staking endpoints


Patch:
diff --git a/api/endpoint/transaction.go b/api/endpoint/transaction.go
index 6769021..52ba419 100644
--- a/api/endpoint/transaction.go
+++ b/api/endpoint/transaction.go
@@ -78,11 +78,6 @@ func GetTransactionsHistory(c *gin.Context, txAPI blockatlas.TxAPI, tokenTxAPI b
 
 	page := make(blockatlas.TxPage, 0)
 	for _, tx := range txs {
-		if tx.Direction != "" {
-			page = append(page, tx)
-			continue
-		}
-
 		tx.Direction = tx.GetTransactionDirection(address)
 		page = append(page, tx)
 	}
diff --git a/api/registry.go b/api/registry.go
index aabe416..8d1518e 100644
--- a/api/registry.go
+++ b/api/registry.go
@@ -3,9 +3,11 @@ package api
 import (
 	"github.com/gin-gonic/gin"
 	"github.com/trustwallet/blockatlas/api/endpoint"
+	"github.com/trustwallet/blockatlas/api/middleware"
 	"github.com/trustwallet/blockatlas/coin"
 	"github.com/trustwallet/blockatlas/pkg/blockatlas"
 	"github.com/trustwallet/blockatlas/platform"
+	"time"
 )
 
 func RegisterCollectionsAPI(root gin.IRouter, api blockatlas.Platform) {
@@ -65,9 +67,9 @@ func RegisterStakeAPI(root gin.IRouter, api blockatlas.Platform) {
 	}
 	handle := api.Coin().Handle
 
-	root.GET("/v2/"+handle+"/staking/validators", func(c *gin.Context) {
+	root.GET("/v2/"+handle+"/staking/validators", middleware.CacheMiddleware(time.Hour, func(c *gin.Context) {
 		endpoint.GetValidators(c, stakingAPI)
-	})
+	}))
 	root.GET("/v2/"+handle+"/staking/delegations/:address", func(c *gin.Context) {
 		endpoint.GetStakingDelegationsForSpecificCoin(c, stakingAPI)
 	})
@@ -77,9 +79,9 @@ func RegisterBatchAPI(root gin.IRouter) {
 	root.POST("v2/staking/delegations", func(c *gin.Context) {
 		endpoint.GetStakeDelegationsWithAllInfoForBatch(c, platform.StakeAPIs)
 	})
-	root.POST("v2/staking/list", func(c *gin.Context) {
+	root.POST("v2/staking/list", middleware.CacheMiddleware(time.Hour, func(c *gin.Context) {
 		endpoint.GetStakeInfoForBatch(c, platform.StakeAPIs)
-	})
+	}))
 	root.POST("/v3/collectibles/categories", func(c *gin.Context) {
 		endpoint.GetCollectionCategoriesFromListV3(c, platform.CollectionAPIs)
 	})
diff --git a/pkg/blockatlas/tx.go b/pkg/blockatlas/tx.go
index 09cbc57..1f5bc6e 100644
--- a/pkg/blockatlas/tx.go
+++ b/pkg/blockatlas/tx.go
@@ -265,6 +265,9 @@ func (t *Tx) GetAddresses() []string {
 }
 
 func (t *Tx) GetTransactionDirection(address string) Direction {
+	if t.Direction != "" {
+		return t.Direction
+	}
 	if len(t.Inputs) > 0 && len(t.Outputs) > 0 {
 		addressSet := mapset.NewSet(address)
 		return InferDirection(t, addressSet)


Test patch:
diff --git a/pkg/blockatlas/tx_test.go b/pkg/blockatlas/tx_test.go
index 787e770..5c1462c 100644
--- a/pkg/blockatlas/tx_test.go
+++ b/pkg/blockatlas/tx_test.go
@@ -525,4 +525,8 @@ func TestTx_GetTransactionDirection(t *testing.T) {
 
 	tx.Direction = tx.GetTransactionDirection("0x38d45371993eEc84f38FEDf93C646aA2D2267CEA")
 	assert.Equal(t, Direction("incoming"), tx.Direction)
+
+	tx.Direction = DirectionSelf
+	tx.Direction = tx.GetTransactionDirection("0x38d45371993eEc84f38FEDf93C646aA2D2267CEA")
+	assert.Equal(t, Direction("yourself"), tx.Direction)
 }