Skip to content
Merged
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
104 changes: 82 additions & 22 deletions manager/gateway/cosmos_agregated3.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
"encoding/json"
"errors"
"fmt"
sekaitypes "github.com/KiraCore/sekai/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/KiraCore/sai-storage-mongo/external/adapter"
"go.uber.org/zap"
"math"
"net/http"
"regexp"
"strconv"
"strings"

"github.com/KiraCore/sai-interx-manager/logger"
"github.com/KiraCore/sai-interx-manager/types"
"github.com/KiraCore/sai-interx-manager/utils"
sekaitypes "github.com/KiraCore/sekai/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/saiset-co/sai-storage-mongo/external/adapter"
"go.uber.org/zap"

"github.com/saiset-co/sai-interx-manager/logger"
"github.com/saiset-co/sai-interx-manager/types"
"github.com/saiset-co/sai-interx-manager/utils"
)

func (g *CosmosGateway) txByHash(hash string) (interface{}, error) {
Expand Down Expand Up @@ -372,54 +373,85 @@ func (g *CosmosGateway) transactions(req types.InboundRequest) (interface{}, err
return nil, err
}

sortMap := make(map[string]interface{})
if request.Sort != nil {
sortMap = request.Sort
} else if req.Payload["sort"] != nil {
sortStr, ok := req.Payload["sort"].(string)
if ok && sortStr != "" {
parts := strings.Split(sortStr, ":")
var field string
var direction string

if len(parts) == 2 {
field = strings.TrimSpace(parts[0])
direction = strings.TrimSpace(parts[1])
} else if len(parts) == 1 {
field = "cr_time"
direction = strings.TrimSpace(parts[0])
}

if field != "" {
var dirValue int
if direction == "desc" || direction == "-1" {
dirValue = -1
} else {
dirValue = 1
}
sortMap[field] = dirValue
}
}
}

options := &adapter.Options{
Count: 1,
Limit: int64(request.Limit),
Skip: int64(request.Offset),
Sort: sortMap,
}

criteria["_id"] = map[string]interface{}{"$ne": nil}

if request.Hash != "" {
criteria["hash"] = request.Hash
}

if request.Height != "" {
criteria["height"] = request.Height
} else {
criteria["_id"] = map[string]interface{}{"$ne": nil}
}

if len(request.Types) > 0 {
criteria["messages"] = map[string]interface{}{
"typeUrl": map[string]interface{}{
"$in": request.Types,
},
criteria["messages.typeUrl"] = map[string]interface{}{
"$in": request.Types,
}
}

if request.StartDate > 0 {
criteria["timestamp"] = map[string]interface{}{
"$gt": request.StartDate,
}
//startTime := time.Unix(request.StartDate, 0).UTC()
//params.Add("tx.mintime", startTime.Format(time.RFC3339))
}

if request.EndDate > 0 {
if request.EndDate > 0 {
criteria["timestamp"] = map[string]interface{}{
"$gt": request.StartDate,
"$lt": request.EndDate,
}
}
} else if request.EndDate > 0 {
criteria["timestamp"] = map[string]interface{}{
"$lt": request.StartDate,
"$lt": request.EndDate,
}
//endTime := time.Unix(request.EndDate, 0).UTC()
//params.Add("tx.maxtime", endTime.Format(time.RFC3339))
}

if len(request.Statuses) > 0 {
includeConfirmed = false
includeFailed = false

for _, status := range request.Statuses {
if status == "success" {
switch status {
case "success":
includeConfirmed = true
} else if status == "failed" {
case "failed":
includeFailed = true
}
}
Expand All @@ -431,6 +463,34 @@ func (g *CosmosGateway) transactions(req types.InboundRequest) (interface{}, err
criteria["tx_result.code"] = map[string]interface{}{"$gt": 0}
}

if len(request.Directions) > 0 && request.Address == "" {
return nil, fmt.Errorf("directions filter requires address to be specified")
}

if request.Address != "" {
orConditions := []map[string]interface{}{}

if len(request.Directions) == 0 {
orConditions = []map[string]interface{}{
{"messages.from_address": request.Address},
{"messages.to_address": request.Address},
}
} else {
for _, direction := range request.Directions {
switch direction {
case "inbound":
orConditions = append(orConditions, map[string]interface{}{"messages.to_address": request.Address})
case "outbound":
orConditions = append(orConditions, map[string]interface{}{"messages.from_address": request.Address})
}
}
}

if len(orConditions) > 0 {
criteria["$or"] = orConditions
}
}

txsResponse, err := g.storage.Read("cosmos_txs", criteria, options, []string{})
if err != nil {
logger.Logger.Error("[query-transactions] Failed to get transactions", zap.Error(err))
Expand Down
24 changes: 13 additions & 11 deletions manager/types/cosmos.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package types

import (
"time"

types2 "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"time"
)

type GenesisChunkedResponse struct {
Expand Down Expand Up @@ -174,16 +175,17 @@ type TransactionResultResponse struct {
}

type QueryTxsParams struct {
Hash string `json:"hash,omitempty"`
Height string `json:"height,omitempty"`
Address string `json:"address,omitempty"`
StartDate int64 `json:"start_date,string,omitempty"`
EndDate int64 `json:"end_date,string,omitempty"`
Directions []string `json:"directions,omitempty"`
Statuses []string `json:"statuses,omitempty"`
Types []string `json:"types,omitempty"`
Offset int `json:"offset,string,omitempty"`
Limit int `json:"limit,string,omitempty"`
Hash string `json:"hash,omitempty"`
Height string `json:"height,omitempty"`
Address string `json:"address,omitempty"`
StartDate int64 `json:"start_date,string,omitempty"`
EndDate int64 `json:"end_date,string,omitempty"`
Directions []string `json:"directions,omitempty"`
Statuses []string `json:"statuses,omitempty"`
Types []string `json:"types,omitempty"`
Offset int `json:"offset,string,omitempty"`
Limit int `json:"limit,string,omitempty"`
Sort map[string]interface{} `json:"sort,omitempty"`
}

type TxResponse struct {
Expand Down
Loading