Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2/3 Added route for /address/:addresses/txs and output as expected by V4 #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 62 additions & 36 deletions httpd/handlers/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,82 @@ import (
"strings"

"github.com/ledgerhq/satstack/httpd/svc"
"github.com/ledgerhq/satstack/types"
"github.com/ledgerhq/satstack/utils"

"github.com/gin-gonic/gin"
)

func GetAddresses(s svc.AddressesService) gin.HandlerFunc {
return func(ctx *gin.Context) {
param := ctx.Param("addresses")
blockHashQuery := ctx.Query("block_hash")
blockHeightQuery := ctx.Query("block_height")
addresses, shouldReturn := getAdresses(ctx, s)
if shouldReturn {
return
}

addressList := strings.Split(param, ",")
ctx.JSON(http.StatusOK, addresses)
}
}

var blockHash *string
if blockHashQuery != "" {
blockHash = &blockHashQuery
}
func getAdresses(ctx *gin.Context, s svc.AddressesService) (types.Addresses, bool) {
param := ctx.Param("addresses")
blockHashQuery := ctx.Query("block_hash")
blockHeightQuery := ctx.Query("block_height")

addressList := strings.Split(param, ",")

var blockHash *string
if blockHashQuery != "" {
blockHash = &blockHashQuery
}

var blockHeight *int32
if blockHeightQuery != "" {
n, _ := strconv.ParseInt(blockHeightQuery, 10, 32)
i32 := int32(n)
blockHeight = &i32
}

var blockHeight *int32
if blockHeightQuery != "" {
n, _ := strconv.ParseInt(blockHeightQuery, 10, 32)
i32 := int32(n)
blockHeight = &i32
addresses, err := s.GetAddresses(addressList, blockHash, blockHeight)
if err != nil {
ctx.String(http.StatusNotFound, "text/plain", []byte(err.Error()))
return types.Addresses{}, true
}

// FIXME: libcore relies on the order of the transactions, in order to
// correctly compute operation values (aka amounts). This order
// appears to be based on the ReceivedAt field, although it is
// not documented in the Ledger BE project.
//
// The bug seems to manifest itself only on accounts with a
// large number of operations.
sort.Slice(addresses.Transactions[:], func(i, j int) bool {
iReceivedAt, iErr := utils.ParseRFC3339Timestamp(addresses.Transactions[i].ReceivedAt)
jReceivedAt, jErr := utils.ParseRFC3339Timestamp(addresses.Transactions[j].ReceivedAt)

if iErr != nil || jErr != nil {
// Still a semi-reliable way of comparing RFC3339 timestamps.
return addresses.Transactions[i].ReceivedAt < addresses.Transactions[j].ReceivedAt
}

addresses, err := s.GetAddresses(addressList, blockHash, blockHeight)
if err != nil {
ctx.String(http.StatusNotFound, "text/plain", []byte(err.Error()))
return *iReceivedAt < *jReceivedAt
})
return addresses, false
}

func GetV4Addresses(s svc.AddressesService) gin.HandlerFunc {
return func(ctx *gin.Context) {

addresses, shouldReturn := getAdresses(ctx, s)
if shouldReturn {
return
}
type resp struct {
Data []types.Transaction `json:"data"`
Token *string `json:"token"`
}

// FIXME: libcore relies on the order of the transactions, in order to
// correctly compute operation values (aka amounts). This order
// appears to be based on the ReceivedAt field, although it is
// not documented in the Ledger BE project.
//
// The bug seems to manifest itself only on accounts with a
// large number of operations.
sort.Slice(addresses.Transactions[:], func(i, j int) bool {
iReceivedAt, iErr := utils.ParseRFC3339Timestamp(addresses.Transactions[i].ReceivedAt)
jReceivedAt, jErr := utils.ParseRFC3339Timestamp(addresses.Transactions[j].ReceivedAt)

if iErr != nil || jErr != nil {
// Still a semi-reliable way of comparing RFC3339 timestamps.
return addresses.Transactions[i].ReceivedAt < addresses.Transactions[j].ReceivedAt
}

return *iReceivedAt < *jReceivedAt
})

ctx.JSON(http.StatusOK, addresses)
response := resp{Data: addresses.Transactions}
ctx.JSON(http.StatusOK, response)
}
}
4 changes: 4 additions & 0 deletions httpd/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,9 @@ func GetRouter(s *svc.Service) *gin.Engine {
addressesRouter.GET(":addresses/transactions", handlers.GetAddresses(s))
}

addressRouter := currencyRouter.Group("/address")
{
addressRouter.GET(":addresses/txs", handlers.GetV4Addresses(s))
}
return engine
}