2023-04-21 11:59:29 +00:00
|
|
|
package activity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
eth "github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/services/wallet/common"
|
2023-04-21 11:59:29 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/transfer"
|
2023-05-11 07:50:07 +00:00
|
|
|
|
|
|
|
"golang.org/x/exp/constraints"
|
2023-04-21 11:59:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PayloadType = int
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
// Beware if adding/removing please check if affected and update the functions below
|
|
|
|
// - NewActivityEntryWithTransaction
|
|
|
|
// - multiTransactionTypeToActivityType
|
2023-04-21 11:59:29 +00:00
|
|
|
const (
|
|
|
|
MultiTransactionPT PayloadType = iota + 1
|
|
|
|
SimpleTransactionPT
|
|
|
|
PendingTransactionPT
|
|
|
|
)
|
|
|
|
|
|
|
|
type Entry struct {
|
2023-05-11 07:50:07 +00:00
|
|
|
payloadType PayloadType
|
|
|
|
transaction *transfer.TransactionIdentity
|
|
|
|
id transfer.MultiTransactionIDType
|
|
|
|
timestamp int64
|
|
|
|
activityType Type
|
|
|
|
activityStatus Status
|
|
|
|
tokenType TokenType
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type jsonSerializationTemplate struct {
|
2023-05-11 07:50:07 +00:00
|
|
|
PayloadType PayloadType `json:"payloadType"`
|
|
|
|
Transaction *transfer.TransactionIdentity `json:"transaction"`
|
|
|
|
ID transfer.MultiTransactionIDType `json:"id"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
ActivityType Type `json:"activityType"`
|
|
|
|
ActivityStatus Status `json:"activityStatus"`
|
|
|
|
TokenType TokenType `json:"tokenType"`
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Entry) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(jsonSerializationTemplate{
|
2023-05-11 07:50:07 +00:00
|
|
|
PayloadType: e.payloadType,
|
|
|
|
Transaction: e.transaction,
|
|
|
|
ID: e.id,
|
|
|
|
Timestamp: e.timestamp,
|
|
|
|
ActivityType: e.activityType,
|
|
|
|
ActivityStatus: e.activityStatus,
|
|
|
|
TokenType: e.tokenType,
|
2023-04-21 11:59:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Entry) UnmarshalJSON(data []byte) error {
|
|
|
|
aux := jsonSerializationTemplate{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
e.payloadType = aux.PayloadType
|
2023-04-21 11:59:29 +00:00
|
|
|
e.transaction = aux.Transaction
|
|
|
|
e.id = aux.ID
|
|
|
|
e.timestamp = aux.Timestamp
|
|
|
|
e.activityType = aux.ActivityType
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
func NewActivityEntryWithTransaction(payloadType PayloadType, transaction *transfer.TransactionIdentity, timestamp int64, activityType Type, activityStatus Status) Entry {
|
|
|
|
if payloadType != SimpleTransactionPT && payloadType != PendingTransactionPT {
|
2023-04-21 11:59:29 +00:00
|
|
|
panic("invalid transaction type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return Entry{
|
2023-05-11 07:50:07 +00:00
|
|
|
payloadType: payloadType,
|
|
|
|
transaction: transaction,
|
|
|
|
id: 0,
|
|
|
|
timestamp: timestamp,
|
|
|
|
activityType: activityType,
|
|
|
|
activityStatus: activityStatus,
|
|
|
|
tokenType: AssetTT,
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
func NewActivityEntryWithMultiTransaction(id transfer.MultiTransactionIDType, timestamp int64, activityType Type, activityStatus Status) Entry {
|
2023-04-21 11:59:29 +00:00
|
|
|
return Entry{
|
2023-05-11 07:50:07 +00:00
|
|
|
payloadType: MultiTransactionPT,
|
|
|
|
id: id,
|
|
|
|
timestamp: timestamp,
|
|
|
|
activityType: activityType,
|
|
|
|
activityStatus: activityStatus,
|
|
|
|
tokenType: AssetTT,
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
func (e *Entry) PayloadType() PayloadType {
|
|
|
|
return e.payloadType
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func multiTransactionTypeToActivityType(mtType transfer.MultiTransactionType) Type {
|
|
|
|
if mtType == transfer.MultiTransactionSend {
|
|
|
|
return SendAT
|
|
|
|
} else if mtType == transfer.MultiTransactionSwap {
|
|
|
|
return SwapAT
|
|
|
|
} else if mtType == transfer.MultiTransactionBridge {
|
|
|
|
return BridgeAT
|
|
|
|
}
|
|
|
|
panic("unknown multi transaction type")
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
func sliceContains[T constraints.Ordered](slice []T, item T) bool {
|
2023-04-21 11:59:29 +00:00
|
|
|
for _, a := range slice {
|
|
|
|
if a == item {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
func joinItems[T interface{}](items []T, itemConversion func(T) string) string {
|
|
|
|
if len(items) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2023-04-21 11:59:29 +00:00
|
|
|
var sb strings.Builder
|
2023-05-11 07:50:07 +00:00
|
|
|
if itemConversion == nil {
|
|
|
|
itemConversion = func(item T) string {
|
|
|
|
return fmt.Sprintf("%v", item)
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-11 07:50:07 +00:00
|
|
|
for i, item := range items {
|
2023-04-21 11:59:29 +00:00
|
|
|
if i == 0 {
|
2023-05-11 07:50:07 +00:00
|
|
|
sb.WriteString("(")
|
2023-04-21 11:59:29 +00:00
|
|
|
} else {
|
2023-05-11 07:50:07 +00:00
|
|
|
sb.WriteString("),(")
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
2023-05-11 07:50:07 +00:00
|
|
|
sb.WriteString(itemConversion(item))
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
2023-05-11 07:50:07 +00:00
|
|
|
sb.WriteString(")")
|
2023-04-21 11:59:29 +00:00
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
func joinAddresses(addresses []eth.Address) string {
|
|
|
|
return joinItems(addresses, func(a eth.Address) string {
|
|
|
|
return fmt.Sprintf("'%s'", strings.ToUpper(hex.EncodeToString(a[:])))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-21 11:59:29 +00:00
|
|
|
func activityTypesToMultiTransactionTypes(trTypes []Type) []transfer.MultiTransactionType {
|
|
|
|
mtTypes := make([]transfer.MultiTransactionType, 0, len(trTypes))
|
|
|
|
for _, t := range trTypes {
|
|
|
|
var mtType transfer.MultiTransactionType
|
|
|
|
if t == SendAT {
|
|
|
|
mtType = transfer.MultiTransactionSend
|
|
|
|
} else if t == SwapAT {
|
|
|
|
mtType = transfer.MultiTransactionSwap
|
|
|
|
} else if t == BridgeAT {
|
|
|
|
mtType = transfer.MultiTransactionBridge
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mtTypes = append(mtTypes, mtType)
|
|
|
|
}
|
|
|
|
return mtTypes
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
const (
|
|
|
|
fromTrType = byte(1)
|
|
|
|
//toTrType = byte(2)
|
|
|
|
|
|
|
|
// TODO: Multi-transaction network information is missing in filtering
|
|
|
|
// TODO: extract token code for non transfer type eth
|
|
|
|
// TODO optimization: consider implementing nullable []byte instead of using strings for addresses
|
|
|
|
//
|
|
|
|
// Query includes duplicates, will return multiple rows for the same transaction if both to and from addresses
|
|
|
|
// are in the address list.
|
|
|
|
//
|
|
|
|
// The addresses list will have priority in deciding the source of the duplicate transaction. However, if the
|
|
|
|
// if the addresses list is empty, and all addresses should be included, the accounts table will be used
|
|
|
|
// see filter_addresses temp table is used
|
|
|
|
// The switch for tr_type is used to de-conflict the source for the two entries for the same transaction
|
|
|
|
//
|
|
|
|
// UNION ALL is used to avoid the overhead of DISTINCT given that we don't expect to have duplicate entries outside
|
|
|
|
// the sender and receiver addresses being in the list which is handled separately
|
|
|
|
queryFormatString = `
|
2023-04-21 11:59:29 +00:00
|
|
|
WITH filter_conditions AS (
|
|
|
|
SELECT
|
|
|
|
? AS startFilterDisabled,
|
|
|
|
? AS startTimestamp,
|
|
|
|
? AS endFilterDisabled,
|
|
|
|
? AS endTimestamp,
|
|
|
|
|
|
|
|
? AS filterActivityTypeAll,
|
|
|
|
? AS filterActivityTypeSend,
|
|
|
|
? AS filterActivityTypeReceive,
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
? AS filterAllAddresses,
|
|
|
|
? AS filterAllToAddresses,
|
|
|
|
? AS filterAllActivityStatus,
|
|
|
|
? AS includeAllTokenTypeAssets,
|
|
|
|
? AS statusIsPending,
|
|
|
|
|
|
|
|
? AS includeAllNetworks
|
2023-04-21 11:59:29 +00:00
|
|
|
),
|
|
|
|
filter_addresses(address) AS (
|
2023-05-24 07:32:11 +00:00
|
|
|
SELECT HEX(address) FROM keypairs_accounts WHERE (SELECT filterAllAddresses FROM filter_conditions) != 0
|
2023-05-11 07:50:07 +00:00
|
|
|
UNION ALL
|
|
|
|
SELECT * FROM (VALUES %s) WHERE (SELECT filterAllAddresses FROM filter_conditions) = 0
|
|
|
|
),
|
|
|
|
filter_to_addresses(address) AS (
|
|
|
|
VALUES %s
|
|
|
|
),
|
|
|
|
filter_assets(token_code) AS (
|
|
|
|
VALUES %s
|
|
|
|
),
|
|
|
|
filter_networks(network_id) AS (
|
2023-04-21 11:59:29 +00:00
|
|
|
VALUES %s
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
transfers.hash AS transfer_hash,
|
|
|
|
NULL AS pending_hash,
|
|
|
|
transfers.network_id AS network_id,
|
|
|
|
0 AS multi_tx_id,
|
|
|
|
transfers.timestamp AS timestamp,
|
|
|
|
NULL AS mt_type,
|
2023-05-11 07:50:07 +00:00
|
|
|
|
|
|
|
CASE
|
|
|
|
WHEN from_join.address IS NOT NULL AND to_join.address IS NULL THEN 1
|
|
|
|
WHEN to_join.address IS NOT NULL AND from_join.address IS NULL THEN 2
|
|
|
|
WHEN from_join.address IS NOT NULL AND to_join.address IS NOT NULL THEN
|
|
|
|
CASE
|
|
|
|
WHEN from_join.address < to_join.address THEN 1
|
|
|
|
ELSE 2
|
|
|
|
END
|
|
|
|
ELSE NULL
|
|
|
|
END as tr_type,
|
|
|
|
|
|
|
|
transfers.sender AS from_address,
|
|
|
|
transfers.address AS to_address
|
2023-04-21 11:59:29 +00:00
|
|
|
FROM transfers, filter_conditions
|
2023-05-11 07:50:07 +00:00
|
|
|
LEFT JOIN
|
|
|
|
filter_addresses from_join ON HEX(transfers.sender) = from_join.address
|
|
|
|
LEFT JOIN
|
|
|
|
filter_addresses to_join ON HEX(transfers.address) = to_join.address
|
2023-04-21 11:59:29 +00:00
|
|
|
WHERE transfers.multi_transaction_id = 0
|
2023-05-11 07:50:07 +00:00
|
|
|
AND ((startFilterDisabled OR timestamp >= startTimestamp)
|
|
|
|
AND (endFilterDisabled OR timestamp <= endTimestamp)
|
|
|
|
)
|
|
|
|
AND (filterActivityTypeAll
|
|
|
|
OR (filterActivityTypeSend
|
|
|
|
AND (filterAllAddresses
|
|
|
|
OR (HEX(transfers.sender) IN filter_addresses)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
OR (filterActivityTypeReceive
|
|
|
|
AND (filterAllAddresses OR (HEX(transfers.address) IN filter_addresses))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
AND (filterAllAddresses
|
|
|
|
OR (HEX(transfers.sender) IN filter_addresses)
|
|
|
|
OR (HEX(transfers.address) IN filter_addresses)
|
|
|
|
)
|
|
|
|
AND (filterAllToAddresses
|
|
|
|
OR (HEX(transfers.address) IN filter_to_addresses)
|
|
|
|
)
|
|
|
|
AND (includeAllTokenTypeAssets OR (transfers.type = "eth" AND ("ETH" IN filter_assets)))
|
|
|
|
AND (includeAllNetworks OR (transfers.network_id IN filter_networks))
|
2023-04-21 11:59:29 +00:00
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
NULL AS transfer_hash,
|
|
|
|
pending_transactions.hash AS pending_hash,
|
|
|
|
pending_transactions.network_id AS network_id,
|
|
|
|
0 AS multi_tx_id,
|
|
|
|
pending_transactions.timestamp AS timestamp,
|
|
|
|
NULL AS mt_type,
|
2023-05-11 07:50:07 +00:00
|
|
|
|
|
|
|
CASE
|
|
|
|
WHEN from_join.address IS NOT NULL AND to_join.address IS NULL THEN 1
|
|
|
|
WHEN to_join.address IS NOT NULL AND from_join.address IS NULL THEN 2
|
|
|
|
WHEN from_join.address IS NOT NULL AND to_join.address IS NOT NULL THEN
|
|
|
|
CASE
|
|
|
|
WHEN from_join.address < to_join.address THEN 1
|
|
|
|
ELSE 2
|
|
|
|
END
|
|
|
|
ELSE NULL
|
|
|
|
END as tr_type,
|
|
|
|
|
|
|
|
pending_transactions.from_address AS from_address,
|
|
|
|
pending_transactions.to_address AS to_address
|
2023-04-21 11:59:29 +00:00
|
|
|
FROM pending_transactions, filter_conditions
|
2023-05-11 07:50:07 +00:00
|
|
|
LEFT JOIN
|
|
|
|
filter_addresses from_join ON HEX(pending_transactions.from_address) = from_join.address
|
|
|
|
LEFT JOIN
|
|
|
|
filter_addresses to_join ON HEX(pending_transactions.to_address) = to_join.address
|
2023-04-21 11:59:29 +00:00
|
|
|
WHERE pending_transactions.multi_transaction_id = 0
|
2023-05-11 07:50:07 +00:00
|
|
|
AND (filterAllActivityStatus OR statusIsPending)
|
|
|
|
AND ((startFilterDisabled OR timestamp >= startTimestamp)
|
|
|
|
AND (endFilterDisabled OR timestamp <= endTimestamp)
|
|
|
|
)
|
2023-04-21 11:59:29 +00:00
|
|
|
AND (filterActivityTypeAll OR filterActivityTypeSend)
|
2023-05-11 07:50:07 +00:00
|
|
|
AND (filterAllAddresses
|
|
|
|
OR (HEX(pending_transactions.from_address) IN filter_addresses)
|
|
|
|
OR (HEX(pending_transactions.to_address) IN filter_addresses)
|
|
|
|
)
|
|
|
|
AND (filterAllToAddresses
|
|
|
|
OR (HEX(pending_transactions.to_address) IN filter_to_addresses)
|
|
|
|
)
|
|
|
|
AND (includeAllTokenTypeAssets OR (UPPER(pending_transactions.symbol) IN filter_assets))
|
|
|
|
AND (includeAllNetworks OR (pending_transactions.network_id IN filter_networks))
|
2023-04-21 11:59:29 +00:00
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
NULL AS transfer_hash,
|
|
|
|
NULL AS pending_hash,
|
|
|
|
NULL AS network_id,
|
|
|
|
multi_transactions.ROWID AS multi_tx_id,
|
|
|
|
multi_transactions.timestamp AS timestamp,
|
|
|
|
multi_transactions.type AS mt_type,
|
2023-05-11 07:50:07 +00:00
|
|
|
NULL as tr_type,
|
|
|
|
multi_transactions.from_address AS from_address,
|
|
|
|
multi_transactions.to_address AS to_address
|
2023-04-21 11:59:29 +00:00
|
|
|
FROM multi_transactions, filter_conditions
|
2023-05-11 07:50:07 +00:00
|
|
|
WHERE ((startFilterDisabled OR timestamp >= startTimestamp)
|
|
|
|
AND (endFilterDisabled OR timestamp <= endTimestamp)
|
|
|
|
)
|
2023-04-21 11:59:29 +00:00
|
|
|
AND (filterActivityTypeAll OR (multi_transactions.type IN (%s)))
|
2023-05-11 07:50:07 +00:00
|
|
|
AND (filterAllAddresses
|
|
|
|
OR (HEX(multi_transactions.from_address) IN filter_addresses)
|
|
|
|
OR (HEX(multi_transactions.to_address) IN filter_addresses)
|
|
|
|
)
|
|
|
|
AND (filterAllToAddresses
|
|
|
|
OR (HEX(multi_transactions.to_address) IN filter_to_addresses)
|
|
|
|
)
|
|
|
|
AND (includeAllTokenTypeAssets OR (UPPER(multi_transactions.from_asset) IN filter_assets) OR (UPPER(multi_transactions.to_asset) IN filter_assets))
|
2023-04-21 11:59:29 +00:00
|
|
|
|
|
|
|
ORDER BY timestamp DESC
|
|
|
|
LIMIT ? OFFSET ?`
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
noEntriesInTmpTableSQLValues = "(NULL)"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetActivityEntries returns query the transfers, pending_transactions, and multi_transactions tables
|
|
|
|
// based on filter parameters and arguments
|
|
|
|
// it returns metadata for all entries ordered by timestamp column
|
|
|
|
//
|
|
|
|
// Adding a no-limit option was never considered or required.
|
|
|
|
func GetActivityEntries(db *sql.DB, addresses []eth.Address, chainIDs []common.ChainID, filter Filter, offset int, limit int) ([]Entry, error) {
|
|
|
|
// TODO: filter collectibles after they are added to multi_transactions table
|
|
|
|
if len(filter.Tokens.EnabledTypes) > 0 && !sliceContains(filter.Tokens.EnabledTypes, AssetTT) {
|
|
|
|
// For now we deal only with assets so return empty result
|
|
|
|
return []Entry{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
includeAllTokenTypeAssets := (len(filter.Tokens.EnabledTypes) == 0 ||
|
|
|
|
sliceContains(filter.Tokens.EnabledTypes, AssetTT)) && len(filter.Tokens.Assets) == 0
|
|
|
|
|
|
|
|
assets := noEntriesInTmpTableSQLValues
|
|
|
|
if !includeAllTokenTypeAssets {
|
|
|
|
assets = joinItems(filter.Tokens.Assets, func(item TokenCode) string { return fmt.Sprintf("'%v'", item) })
|
|
|
|
}
|
|
|
|
|
|
|
|
includeAllNetworks := len(chainIDs) == 0
|
|
|
|
networks := noEntriesInTmpTableSQLValues
|
|
|
|
if !includeAllNetworks {
|
|
|
|
networks = joinItems(chainIDs, nil)
|
|
|
|
}
|
2023-04-21 11:59:29 +00:00
|
|
|
|
|
|
|
startFilterDisabled := !(filter.Period.StartTimestamp > 0)
|
|
|
|
endFilterDisabled := !(filter.Period.EndTimestamp > 0)
|
2023-05-11 07:50:07 +00:00
|
|
|
filterActivityTypeAll := len(filter.Types) == 0
|
2023-04-21 11:59:29 +00:00
|
|
|
filterAllAddresses := len(addresses) == 0
|
2023-05-11 07:50:07 +00:00
|
|
|
filterAllToAddresses := len(filter.CounterpartyAddresses) == 0
|
|
|
|
includeAllStatuses := len(filter.Statuses) == 0
|
2023-04-21 11:59:29 +00:00
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
statusIsPending := false
|
|
|
|
if !includeAllStatuses {
|
|
|
|
statusIsPending = sliceContains(filter.Statuses, PendingAS)
|
|
|
|
}
|
2023-04-21 11:59:29 +00:00
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
involvedAddresses := noEntriesInTmpTableSQLValues
|
2023-04-21 11:59:29 +00:00
|
|
|
if !filterAllAddresses {
|
2023-05-11 07:50:07 +00:00
|
|
|
involvedAddresses = joinAddresses(addresses)
|
|
|
|
}
|
|
|
|
toAddresses := noEntriesInTmpTableSQLValues
|
|
|
|
if !filterAllToAddresses {
|
|
|
|
toAddresses = joinAddresses(filter.CounterpartyAddresses)
|
2023-04-21 11:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mtTypes := activityTypesToMultiTransactionTypes(filter.Types)
|
2023-05-11 07:50:07 +00:00
|
|
|
joinedMTTypes := joinItems(mtTypes, func(t transfer.MultiTransactionType) string {
|
|
|
|
return strconv.Itoa(int(t))
|
|
|
|
})
|
2023-04-21 11:59:29 +00:00
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
queryString := fmt.Sprintf(queryFormatString, involvedAddresses, toAddresses, assets, networks,
|
|
|
|
joinedMTTypes)
|
2023-04-21 11:59:29 +00:00
|
|
|
|
|
|
|
rows, err := db.Query(queryString,
|
|
|
|
startFilterDisabled, filter.Period.StartTimestamp, endFilterDisabled, filter.Period.EndTimestamp,
|
2023-05-11 07:50:07 +00:00
|
|
|
filterActivityTypeAll, sliceContains(filter.Types, SendAT), sliceContains(filter.Types, ReceiveAT),
|
|
|
|
filterAllAddresses, filterAllToAddresses, includeAllStatuses, includeAllTokenTypeAssets, statusIsPending,
|
|
|
|
includeAllNetworks,
|
2023-04-21 11:59:29 +00:00
|
|
|
limit, offset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var entries []Entry
|
|
|
|
for rows.Next() {
|
|
|
|
var transferHash, pendingHash []byte
|
|
|
|
var chainID, multiTxID sql.NullInt64
|
|
|
|
var timestamp int64
|
2023-05-11 07:50:07 +00:00
|
|
|
var dbMtType, dbTrType sql.NullByte
|
|
|
|
var toAddress, fromAddress eth.Address
|
|
|
|
err := rows.Scan(&transferHash, &pendingHash, &chainID, &multiTxID, ×tamp, &dbMtType, &dbTrType, &fromAddress, &toAddress)
|
2023-04-21 11:59:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-11 07:50:07 +00:00
|
|
|
getActivityType := func(trType sql.NullByte) (activityType Type, filteredAddress eth.Address) {
|
|
|
|
if trType.Valid && trType.Byte == fromTrType {
|
|
|
|
return SendAT, fromAddress
|
|
|
|
}
|
|
|
|
// Don't expect this to happen due to trType = NULL outside of tests
|
|
|
|
return ReceiveAT, toAddress
|
|
|
|
}
|
|
|
|
|
2023-04-21 11:59:29 +00:00
|
|
|
var entry Entry
|
|
|
|
if transferHash != nil && chainID.Valid {
|
2023-05-11 07:50:07 +00:00
|
|
|
// TODO: extend DB with status in order to filter by status. The status has to be extracted from the receipt upon downloading
|
|
|
|
activityStatus := FinalizedAS
|
|
|
|
activityType, filteredAddress := getActivityType(dbTrType)
|
|
|
|
entry = NewActivityEntryWithTransaction(SimpleTransactionPT,
|
|
|
|
&transfer.TransactionIdentity{ChainID: common.ChainID(chainID.Int64), Hash: eth.BytesToHash(transferHash), Address: filteredAddress},
|
|
|
|
timestamp, activityType, activityStatus)
|
2023-04-21 11:59:29 +00:00
|
|
|
} else if pendingHash != nil && chainID.Valid {
|
2023-05-11 07:50:07 +00:00
|
|
|
activityStatus := PendingAS
|
|
|
|
activityType, _ := getActivityType(dbTrType)
|
|
|
|
entry = NewActivityEntryWithTransaction(PendingTransactionPT,
|
|
|
|
&transfer.TransactionIdentity{ChainID: common.ChainID(chainID.Int64), Hash: eth.BytesToHash(pendingHash)},
|
|
|
|
timestamp, activityType, activityStatus)
|
2023-04-21 11:59:29 +00:00
|
|
|
} else if multiTxID.Valid {
|
2023-05-11 07:50:07 +00:00
|
|
|
activityType := multiTransactionTypeToActivityType(transfer.MultiTransactionType(dbMtType.Byte))
|
|
|
|
// TODO: aggregate status from all sub-transactions
|
|
|
|
activityStatus := FinalizedAS
|
2023-04-21 11:59:29 +00:00
|
|
|
entry = NewActivityEntryWithMultiTransaction(transfer.MultiTransactionIDType(multiTxID.Int64),
|
2023-05-11 07:50:07 +00:00
|
|
|
timestamp, activityType, activityStatus)
|
2023-04-21 11:59:29 +00:00
|
|
|
} else {
|
|
|
|
return nil, errors.New("invalid row data")
|
|
|
|
}
|
|
|
|
entries = append(entries, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|