2023-04-21 11:59:29 +00:00
package activity
import (
2023-06-08 23:52:45 +00:00
"context"
2023-04-21 11:59:29 +00:00
"database/sql"
"testing"
"github.com/status-im/status-go/appdatabase"
2023-05-11 07:50:07 +00:00
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts/accounts"
"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/testutils"
"github.com/status-im/status-go/services/wallet/transfer"
2023-05-11 07:50:07 +00:00
eth "github.com/ethereum/go-ethereum/common"
eth_common "github.com/ethereum/go-ethereum/common"
2023-04-21 11:59:29 +00:00
"github.com/stretchr/testify/require"
)
func setupTestActivityDB ( t * testing . T ) ( db * sql . DB , close func ( ) ) {
db , err := appdatabase . SetupTestMemorySQLDB ( "wallet-activity-tests" )
require . NoError ( t , err )
return db , func ( ) {
require . NoError ( t , db . Close ( ) )
}
}
type testData struct {
2023-06-14 16:10:20 +00:00
tr1 transfer . TestTransfer // index 1
pendingTr transfer . TestTransfer // index 2
multiTx1Tr1 transfer . TestTransfer // index 3
multiTx2Tr1 transfer . TestTransfer // index 4
multiTx1Tr2 transfer . TestTransfer // index 5
multiTx2Tr2 transfer . TestTransfer // index 6
multiTx2PendingTr transfer . TestTransfer // index 7
multiTx1 transfer . TestMultiTransaction
multiTx1ID transfer . MultiTransactionIDType
multiTx2 transfer . TestMultiTransaction
multiTx2ID transfer . MultiTransactionIDType
nextIndex int
2023-04-21 11:59:29 +00:00
}
2023-05-28 10:40:50 +00:00
func mockTestAccountsWithAddresses ( t * testing . T , db * sql . DB , addresses [ ] eth_common . Address ) {
mockedAccounts := [ ] * accounts . Account { }
for _ , address := range addresses {
mockedAccounts = append ( mockedAccounts , & accounts . Account {
Address : types . Address ( address ) ,
Type : accounts . AccountTypeWatch ,
} )
}
accounts . MockTestAccounts ( t , db , mockedAccounts )
}
2023-06-14 16:10:20 +00:00
// Generates and adds to the DB 7 transfers and 2 multitransactions.
// There are only 4 extractable activity entries (transactions + multi-transactions) with timestamps 1-4. The others are associated with a multi-transaction
2023-05-28 10:40:50 +00:00
func fillTestData ( t * testing . T , db * sql . DB ) ( td testData , fromAddresses , toAddresses [ ] eth_common . Address ) {
2023-06-14 16:10:20 +00:00
trs , fromAddresses , toAddresses := transfer . GenerateTestTransfers ( t , db , 1 , 7 )
// Plain transfer
2023-04-21 11:59:29 +00:00
td . tr1 = trs [ 0 ]
transfer . InsertTestTransfer ( t , db , & td . tr1 )
2023-06-14 16:10:20 +00:00
// Pending transfer
2023-04-21 11:59:29 +00:00
td . pendingTr = trs [ 1 ]
2023-05-11 07:50:07 +00:00
transfer . InsertTestPendingTransaction ( t , db , & td . pendingTr )
2023-04-21 11:59:29 +00:00
2023-06-14 16:10:20 +00:00
// Send Multitransaction containing 2 x Plain transfers
td . multiTx1Tr1 = trs [ 2 ]
td . multiTx1Tr2 = trs [ 4 ]
td . multiTx1Tr1 . Token = testutils . SntSymbol
td . multiTx1 = transfer . GenerateTestSendMultiTransaction ( td . multiTx1Tr1 )
td . multiTx1 . ToToken = testutils . DaiSymbol
td . multiTx1ID = transfer . InsertTestMultiTransaction ( t , db , & td . multiTx1 )
td . multiTx1Tr1 . MultiTransactionID = td . multiTx1ID
transfer . InsertTestTransfer ( t , db , & td . multiTx1Tr1 )
td . multiTx1Tr2 . MultiTransactionID = td . multiTx1ID
transfer . InsertTestTransfer ( t , db , & td . multiTx1Tr2 )
2023-04-21 11:59:29 +00:00
2023-06-14 16:10:20 +00:00
// Send Multitransaction containing 2 x Plain transfers + 1 x Pending transfer
td . multiTx2Tr1 = trs [ 3 ]
td . multiTx2Tr2 = trs [ 5 ]
td . multiTx2PendingTr = trs [ 6 ]
2023-04-21 11:59:29 +00:00
2023-06-14 16:10:20 +00:00
td . multiTx2 = transfer . GenerateTestSendMultiTransaction ( td . multiTx2Tr1 )
td . multiTx1 . ToToken = testutils . SntSymbol
td . multiTx2ID = transfer . InsertTestMultiTransaction ( t , db , & td . multiTx2 )
2023-05-28 10:40:50 +00:00
2023-06-14 16:10:20 +00:00
td . multiTx2Tr1 . MultiTransactionID = td . multiTx2ID
transfer . InsertTestTransfer ( t , db , & td . multiTx2Tr1 )
td . multiTx2Tr2 . MultiTransactionID = td . multiTx2ID
transfer . InsertTestTransfer ( t , db , & td . multiTx2Tr2 )
td . multiTx2PendingTr . MultiTransactionID = td . multiTx2ID
transfer . InsertTestPendingTransaction ( t , db , & td . multiTx2PendingTr )
2023-04-21 11:59:29 +00:00
2023-05-28 10:40:50 +00:00
td . nextIndex = 8
return td , fromAddresses , toAddresses
2023-04-21 11:59:29 +00:00
}
func TestGetActivityEntriesAll ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
2023-05-28 10:40:50 +00:00
td , fromAddresses , toAddresses := fillTestData ( t , db )
2023-04-21 11:59:29 +00:00
var filter Filter
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , append ( toAddresses , fromAddresses ... ) , [ ] common . ChainID { } , filter , 0 , 10 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 4 , len ( entries ) )
// Ensure we have the correct order
var curTimestamp int64 = 4
for _ , entry := range entries {
require . Equal ( t , curTimestamp , entry . timestamp , "entries are sorted by timestamp; expected %d, got %d" , curTimestamp , entry . timestamp )
curTimestamp --
}
require . True ( t , testutils . StructExistsInSlice ( Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : td . tr1 . ChainID , Hash : td . tr1 . Hash , Address : td . tr1 . From } ,
2023-05-11 07:50:07 +00:00
id : td . tr1 . MultiTransactionID ,
timestamp : td . tr1 . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries ) )
require . True ( t , testutils . StructExistsInSlice ( Entry {
2023-05-11 07:50:07 +00:00
payloadType : PendingTransactionPT ,
transaction : & transfer . TransactionIdentity { ChainID : td . pendingTr . ChainID , Hash : td . pendingTr . Hash } ,
id : td . pendingTr . MultiTransactionID ,
timestamp : td . pendingTr . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
2023-05-11 07:50:07 +00:00
activityStatus : PendingAS ,
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries ) )
require . True ( t , testutils . StructExistsInSlice ( Entry {
2023-05-11 07:50:07 +00:00
payloadType : MultiTransactionPT ,
transaction : nil ,
2023-06-14 16:10:20 +00:00
id : td . multiTx1ID ,
timestamp : td . multiTx1 . Timestamp ,
2023-05-11 07:50:07 +00:00
activityType : SendAT ,
2023-05-28 10:40:50 +00:00
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries ) )
require . True ( t , testutils . StructExistsInSlice ( Entry {
2023-05-11 07:50:07 +00:00
payloadType : MultiTransactionPT ,
transaction : nil ,
2023-06-14 16:10:20 +00:00
id : td . multiTx2ID ,
timestamp : td . multiTx2 . Timestamp ,
2023-05-11 07:50:07 +00:00
activityType : SendAT ,
2023-05-28 10:40:50 +00:00
activityStatus : PendingAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries ) )
// Ensure the sub-transactions of the multi-transactions are not returned
require . False ( t , testutils . StructExistsInSlice ( Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-06-14 16:10:20 +00:00
transaction : & transfer . TransactionIdentity { ChainID : td . multiTx1Tr1 . ChainID , Hash : td . multiTx1Tr1 . Hash , Address : td . multiTx1Tr1 . To } ,
id : td . multiTx1Tr1 . MultiTransactionID ,
timestamp : td . multiTx1Tr1 . Timestamp ,
activityType : SendAT ,
activityStatus : CompleteAS ,
tokenType : AssetTT ,
} , entries ) )
require . False ( t , testutils . StructExistsInSlice ( Entry {
payloadType : SimpleTransactionPT ,
transaction : & transfer . TransactionIdentity { ChainID : td . multiTx1Tr2 . ChainID , Hash : td . multiTx1Tr2 . Hash , Address : td . multiTx1Tr2 . To } ,
id : td . multiTx1Tr2 . MultiTransactionID ,
timestamp : td . multiTx1Tr2 . Timestamp ,
activityType : SendAT ,
activityStatus : CompleteAS ,
tokenType : AssetTT ,
} , entries ) )
require . False ( t , testutils . StructExistsInSlice ( Entry {
payloadType : SimpleTransactionPT ,
transaction : & transfer . TransactionIdentity { ChainID : td . multiTx2Tr1 . ChainID , Hash : td . multiTx2Tr1 . Hash , Address : td . multiTx2Tr1 . To } ,
id : td . multiTx2Tr1 . MultiTransactionID ,
timestamp : td . multiTx2Tr1 . Timestamp ,
activityType : SendAT ,
activityStatus : CompleteAS ,
tokenType : AssetTT ,
} , entries ) )
require . False ( t , testutils . StructExistsInSlice ( Entry {
payloadType : SimpleTransactionPT ,
transaction : & transfer . TransactionIdentity { ChainID : td . multiTx2Tr2 . ChainID , Hash : td . multiTx2Tr2 . Hash , Address : td . multiTx2Tr2 . To } ,
id : td . multiTx2Tr2 . MultiTransactionID ,
timestamp : td . multiTx2Tr2 . Timestamp ,
2023-05-11 07:50:07 +00:00
activityType : SendAT ,
2023-05-28 10:40:50 +00:00
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries ) )
require . False ( t , testutils . StructExistsInSlice ( Entry {
2023-05-11 07:50:07 +00:00
payloadType : PendingTransactionPT ,
2023-06-14 16:10:20 +00:00
transaction : & transfer . TransactionIdentity { ChainID : td . multiTx2PendingTr . ChainID , Hash : td . multiTx2PendingTr . Hash } ,
id : td . multiTx2PendingTr . MultiTransactionID ,
timestamp : td . multiTx2PendingTr . Timestamp ,
2023-05-11 07:50:07 +00:00
activityType : SendAT ,
activityStatus : PendingAS ,
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries ) )
}
// TestGetActivityEntriesWithSenderFilter covers the issue with returning the same transaction
// twice when the sender and receiver have entries in the transfers table
func TestGetActivityEntriesWithSameTransactionForSenderAndReceiverInDB ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
// Add 4 extractable transactions with timestamps 1-4
2023-05-28 10:40:50 +00:00
td , fromAddresses , toAddresses := fillTestData ( t , db )
mockTestAccountsWithAddresses ( t , db , append ( fromAddresses , toAddresses ... ) )
2023-04-21 11:59:29 +00:00
// Add another transaction with sender and receiver reversed
receiverTr := td . tr1
prevTo := receiverTr . To
receiverTr . To = td . tr1 . From
receiverTr . From = prevTo
2023-05-11 07:50:07 +00:00
// TODO: test also when there is a transaction in the other direction
// Ensure they are the oldest transactions (last in the list) and we have a consistent order
receiverTr . Timestamp --
2023-04-21 11:59:29 +00:00
transfer . InsertTestTransfer ( t , db , & receiverTr )
var filter Filter
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , [ ] eth . Address { td . tr1 . From , receiverTr . From } , [ ] common . ChainID { } , filter , 0 , 10 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 2 , len ( entries ) )
// Check that the transaction are labeled alternatively as send and receive
require . Equal ( t , ReceiveAT , entries [ 1 ] . activityType )
require . NotEqual ( t , eth . Address { } , entries [ 1 ] . transaction . Address )
require . Equal ( t , receiverTr . To , entries [ 1 ] . transaction . Address )
require . Equal ( t , SendAT , entries [ 0 ] . activityType )
require . NotEqual ( t , eth . Address { } , entries [ 0 ] . transaction . Address )
require . Equal ( t , td . tr1 . From , entries [ 0 ] . transaction . Address )
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth . Address { } , [ ] common . ChainID { } , filter , 0 , 10 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 5 , len ( entries ) )
2023-05-11 07:50:07 +00:00
// Check that the transaction are labeled alternatively as send and receive
require . Equal ( t , ReceiveAT , entries [ 4 ] . activityType )
require . Equal ( t , SendAT , entries [ 3 ] . activityType )
2023-04-21 11:59:29 +00:00
}
func TestGetActivityEntriesFilterByTime ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
2023-05-28 10:40:50 +00:00
td , fromTds , toTds := fillTestData ( t , db )
2023-04-21 11:59:29 +00:00
// Add 6 extractable transactions with timestamps 6-12
2023-06-14 16:10:20 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , db , td . nextIndex , 6 )
2023-04-21 11:59:29 +00:00
for i := range trs {
transfer . InsertTestTransfer ( t , db , & trs [ i ] )
}
2023-05-28 10:40:50 +00:00
mockTestAccountsWithAddresses ( t , db , append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... ) )
2023-04-21 11:59:29 +00:00
// Test start only
var filter Filter
2023-06-14 16:10:20 +00:00
filter . Period . StartTimestamp = td . multiTx1 . Timestamp
2023-05-11 07:50:07 +00:00
filter . Period . EndTimestamp = NoLimitTimestampForPeriod
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 8 , len ( entries ) )
// Check start and end content
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 5 ] . ChainID , Hash : trs [ 5 ] . Hash , Address : trs [ 5 ] . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 5 ] . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 0 ] )
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : MultiTransactionPT ,
transaction : nil ,
2023-06-14 16:10:20 +00:00
id : td . multiTx1ID ,
timestamp : td . multiTx1 . Timestamp ,
2023-05-11 07:50:07 +00:00
activityType : SendAT ,
2023-05-28 10:40:50 +00:00
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 7 ] )
// Test complete interval
filter . Period . EndTimestamp = trs [ 2 ] . Timestamp
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 5 , len ( entries ) )
// Check start and end content
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 2 ] . ChainID , Hash : trs [ 2 ] . Hash , Address : trs [ 2 ] . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 2 ] . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 0 ] )
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : MultiTransactionPT ,
transaction : nil ,
2023-06-14 16:10:20 +00:00
id : td . multiTx1ID ,
timestamp : td . multiTx1 . Timestamp ,
2023-05-11 07:50:07 +00:00
activityType : SendAT ,
2023-05-28 10:40:50 +00:00
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 4 ] )
// Test end only
2023-05-11 07:50:07 +00:00
filter . Period . StartTimestamp = NoLimitTimestampForPeriod
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 7 , len ( entries ) )
// Check start and end content
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 2 ] . ChainID , Hash : trs [ 2 ] . Hash , Address : trs [ 2 ] . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 2 ] . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 0 ] )
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : td . tr1 . ChainID , Hash : td . tr1 . Hash , Address : td . tr1 . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : td . tr1 . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 6 ] )
}
func TestGetActivityEntriesCheckOffsetAndLimit ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
// Add 10 extractable transactions with timestamps 1-10
2023-06-14 16:10:20 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , db , 1 , 10 )
2023-04-21 11:59:29 +00:00
for i := range trs {
transfer . InsertTestTransfer ( t , db , & trs [ i ] )
}
2023-05-28 10:40:50 +00:00
mockTestAccountsWithAddresses ( t , db , append ( fromTrs , toTrs ... ) )
2023-04-21 11:59:29 +00:00
var filter Filter
// Get all
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 5 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 5 , len ( entries ) )
// Get time based interval
filter . Period . StartTimestamp = trs [ 2 ] . Timestamp
filter . Period . EndTimestamp = trs [ 8 ] . Timestamp
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 3 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 3 , len ( entries ) )
// Check start and end content
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 8 ] . ChainID , Hash : trs [ 8 ] . Hash , Address : trs [ 8 ] . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 8 ] . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 0 ] )
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 6 ] . ChainID , Hash : trs [ 6 ] . Hash , Address : trs [ 6 ] . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 6 ] . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 2 ] )
// Move window 2 entries forward
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 2 , 3 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 3 , len ( entries ) )
// Check start and end content
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 6 ] . ChainID , Hash : trs [ 6 ] . Hash , Address : trs [ 6 ] . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 6 ] . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 0 ] )
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 4 ] . ChainID , Hash : trs [ 4 ] . Hash , Address : trs [ 4 ] . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 4 ] . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 2 ] )
// Move window 4 more entries to test filter cap
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 6 , 3 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
// Check start and end content
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-05-28 10:40:50 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 2 ] . ChainID , Hash : trs [ 2 ] . Hash , Address : trs [ 2 ] . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 2 ] . Timestamp ,
2023-05-28 10:40:50 +00:00
activityType : SendAT ,
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 0 ] )
}
2023-05-28 10:40:50 +00:00
func countTypes ( entries [ ] Entry ) ( sendCount , receiveCount , swapCount , buyCount , bridgeCount int ) {
for _ , entry := range entries {
switch entry . activityType {
case SendAT :
sendCount ++
case ReceiveAT :
receiveCount ++
case SwapAT :
swapCount ++
case BuyAT :
buyCount ++
case BridgeAT :
bridgeCount ++
}
}
return
}
2023-04-21 11:59:29 +00:00
func TestGetActivityEntriesFilterByType ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
// Adds 4 extractable transactions
2023-05-28 10:40:50 +00:00
td , _ , _ := fillTestData ( t , db )
// Add 5 extractable transactions: one MultiTransactionSwap, two MultiTransactionBridge and two MultiTransactionSend
2023-06-14 16:10:20 +00:00
multiTxs := make ( [ ] transfer . TestMultiTransaction , 5 )
trs , _ , _ := transfer . GenerateTestTransfers ( t , db , td . nextIndex , len ( multiTxs ) * 2 )
multiTxs [ 0 ] = transfer . GenerateTestBridgeMultiTransaction ( trs [ 0 ] , trs [ 1 ] )
multiTxs [ 1 ] = transfer . GenerateTestSwapMultiTransaction ( trs [ 2 ] , testutils . SntSymbol , 100 ) // trs[3]
multiTxs [ 2 ] = transfer . GenerateTestSendMultiTransaction ( trs [ 4 ] ) // trs[5]
multiTxs [ 3 ] = transfer . GenerateTestBridgeMultiTransaction ( trs [ 6 ] , trs [ 7 ] )
multiTxs [ 4 ] = transfer . GenerateTestSendMultiTransaction ( trs [ 8 ] ) // trs[9]
2023-05-28 10:40:50 +00:00
var lastMT transfer . MultiTransactionIDType
2023-04-21 11:59:29 +00:00
for i := range trs {
2023-05-28 10:40:50 +00:00
if i % 2 == 0 {
2023-06-14 16:10:20 +00:00
lastMT = transfer . InsertTestMultiTransaction ( t , db , & multiTxs [ i / 2 ] )
2023-04-21 11:59:29 +00:00
}
2023-06-14 16:10:20 +00:00
trs [ i ] . MultiTransactionID = lastMT
transfer . InsertTestTransfer ( t , db , & trs [ i ] )
2023-04-21 11:59:29 +00:00
}
// Test filtering out without address involved
var filter Filter
2023-05-11 07:50:07 +00:00
filter . Types = allActivityTypesFilter ( )
2023-05-28 10:40:50 +00:00
// Set tr1 to Receive and pendingTr to Send; rest of two MT remain default Send
2023-06-14 16:10:20 +00:00
addresses := [ ] eth_common . Address { td . tr1 . To , td . pendingTr . From , td . multiTx1 . FromAddress , td . multiTx2 . FromAddress , trs [ 0 ] . From , trs [ 2 ] . From , trs [ 4 ] . From , trs [ 6 ] . From , trs [ 8 ] . From }
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , addresses , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-05-28 10:40:50 +00:00
require . Equal ( t , 9 , len ( entries ) )
2023-05-11 07:50:07 +00:00
2023-04-21 11:59:29 +00:00
filter . Types = [ ] Type { SendAT , SwapAT }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , addresses , [ ] common . ChainID { } , filter , 0 , 15 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
2023-05-28 10:40:50 +00:00
// 3 from td Send + 2 trs MT Send + 1 (swap)
require . Equal ( t , 6 , len ( entries ) )
sendCount , receiveCount , swapCount , _ , bridgeCount := countTypes ( entries )
require . Equal ( t , 5 , sendCount )
require . Equal ( t , 0 , receiveCount )
2023-04-21 11:59:29 +00:00
require . Equal ( t , 1 , swapCount )
2023-05-28 10:40:50 +00:00
require . Equal ( t , 0 , bridgeCount )
2023-04-21 11:59:29 +00:00
filter . Types = [ ] Type { BridgeAT , ReceiveAT }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , addresses , [ ] common . ChainID { } , filter , 0 , 15 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 3 , len ( entries ) )
2023-05-28 10:40:50 +00:00
sendCount , receiveCount , swapCount , _ , bridgeCount = countTypes ( entries )
require . Equal ( t , 0 , sendCount )
2023-04-21 11:59:29 +00:00
require . Equal ( t , 1 , receiveCount )
2023-05-28 10:40:50 +00:00
require . Equal ( t , 0 , swapCount )
require . Equal ( t , 2 , bridgeCount )
2023-04-21 11:59:29 +00:00
}
2023-05-11 07:50:07 +00:00
func TestGetActivityEntriesFilterByAddresses ( t * testing . T ) {
2023-04-21 11:59:29 +00:00
db , close := setupTestActivityDB ( t )
defer close ( )
// Adds 4 extractable transactions
2023-05-28 10:40:50 +00:00
td , fromTds , toTds := fillTestData ( t , db )
2023-06-14 16:10:20 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , db , td . nextIndex , 6 )
2023-04-21 11:59:29 +00:00
for i := range trs {
transfer . InsertTestTransfer ( t , db , & trs [ i ] )
}
2023-05-28 10:40:50 +00:00
mockTestAccountsWithAddresses ( t , db , append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... ) )
2023-04-21 11:59:29 +00:00
var filter Filter
2023-05-11 07:50:07 +00:00
addressesFilter := allAddressesFilter ( )
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , addressesFilter , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 10 , len ( entries ) )
2023-06-14 16:10:20 +00:00
addressesFilter = [ ] eth_common . Address { td . multiTx2 . ToAddress , trs [ 1 ] . From , trs [ 4 ] . To }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , addressesFilter , [ ] common . ChainID { } , filter , 0 , 15 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 3 , len ( entries ) )
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
transaction : & transfer . TransactionIdentity { ChainID : trs [ 4 ] . ChainID , Hash : trs [ 4 ] . Hash , Address : trs [ 4 ] . To } ,
id : 0 ,
timestamp : trs [ 4 ] . Timestamp ,
activityType : ReceiveAT ,
2023-05-28 10:40:50 +00:00
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 0 ] )
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
transaction : & transfer . TransactionIdentity { ChainID : trs [ 1 ] . ChainID , Hash : trs [ 1 ] . Hash , Address : trs [ 1 ] . From } ,
id : 0 ,
timestamp : trs [ 1 ] . Timestamp ,
activityType : SendAT ,
2023-05-28 10:40:50 +00:00
activityStatus : CompleteAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 1 ] )
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : MultiTransactionPT ,
transaction : nil ,
2023-06-14 16:10:20 +00:00
id : td . multiTx2ID ,
timestamp : td . multiTx2 . Timestamp ,
2023-05-11 07:50:07 +00:00
activityType : SendAT ,
2023-05-28 10:40:50 +00:00
activityStatus : PendingAS ,
2023-05-11 07:50:07 +00:00
tokenType : AssetTT ,
2023-04-21 11:59:29 +00:00
} , entries [ 2 ] )
}
2023-05-11 07:50:07 +00:00
func TestGetActivityEntriesFilterByStatus ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
2023-05-28 10:40:50 +00:00
// Adds 4 extractable transactions: 1 T, 1 T pending, 1 MT pending, 1 MT with 2xT success
td , fromTds , toTds := fillTestData ( t , db )
// Add 7 extractable transactions: 1 pending, 1 Tr failed, 1 MT failed, 4 success
2023-06-14 16:10:20 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , db , td . nextIndex , 7 )
multiTx := transfer . GenerateTestSendMultiTransaction ( trs [ 6 ] )
failedMTID := transfer . InsertTestMultiTransaction ( t , db , & multiTx )
2023-05-28 10:40:50 +00:00
trs [ 6 ] . MultiTransactionID = failedMTID
2023-05-11 07:50:07 +00:00
for i := range trs {
2023-05-28 10:40:50 +00:00
if i == 1 {
transfer . InsertTestPendingTransaction ( t , db , & trs [ i ] )
} else {
trs [ i ] . Success = i != 3 && i != 6
transfer . InsertTestTransfer ( t , db , & trs [ i ] )
}
2023-05-11 07:50:07 +00:00
}
2023-05-28 10:40:50 +00:00
mockTestAccountsWithAddresses ( t , db , append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... ) )
2023-05-11 07:50:07 +00:00
var filter Filter
2023-05-28 10:40:50 +00:00
filter . Statuses = allActivityStatusesFilter ( )
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-05-28 10:40:50 +00:00
require . Equal ( t , 11 , len ( entries ) )
2023-05-11 07:50:07 +00:00
2023-05-28 10:40:50 +00:00
filter . Statuses = [ ] Status { PendingAS }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-05-28 10:40:50 +00:00
require . Equal ( t , 3 , len ( entries ) )
require . Equal ( t , td . pendingTr . Hash , entries [ 2 ] . transaction . Hash )
2023-06-14 16:10:20 +00:00
require . Equal ( t , td . multiTx2ID , entries [ 1 ] . id )
2023-05-28 10:40:50 +00:00
require . Equal ( t , trs [ 1 ] . Hash , entries [ 0 ] . transaction . Hash )
filter . Statuses = [ ] Status { FailedAS }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-28 10:40:50 +00:00
require . NoError ( t , err )
require . Equal ( t , 2 , len ( entries ) )
filter . Statuses = [ ] Status { CompleteAS }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-28 10:40:50 +00:00
require . NoError ( t , err )
require . Equal ( t , 6 , len ( entries ) )
// Finalized is treated as Complete, would need dynamic blockchain status to track the Finalized level
filter . Statuses = [ ] Status { FinalizedAS }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-28 10:40:50 +00:00
require . NoError ( t , err )
require . Equal ( t , 6 , len ( entries ) )
2023-05-11 07:50:07 +00:00
2023-05-28 10:40:50 +00:00
// Combined filter
filter . Statuses = [ ] Status { FailedAS , PendingAS }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-28 10:40:50 +00:00
require . NoError ( t , err )
require . Equal ( t , 5 , len ( entries ) )
2023-05-11 07:50:07 +00:00
}
func TestGetActivityEntriesFilterByTokenType ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
// Adds 4 extractable transactions 2 transactions ETH, one MT SNT to DAI and another MT ETH to SNT
2023-05-28 10:40:50 +00:00
td , fromTds , toTds := fillTestData ( t , db )
2023-05-11 07:50:07 +00:00
// Add 6 extractable transactions with USDC (only erc20 as type in DB)
2023-06-14 16:10:20 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , db , td . nextIndex , 6 )
2023-05-11 07:50:07 +00:00
for i := range trs {
2023-06-14 16:10:20 +00:00
trs [ i ] . Token = "USDC"
2023-05-11 07:50:07 +00:00
transfer . InsertTestTransfer ( t , db , & trs [ i ] )
}
2023-05-28 10:40:50 +00:00
mockTestAccountsWithAddresses ( t , db , append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... ) )
2023-05-11 07:50:07 +00:00
var filter Filter
filter . Tokens = noAssetsFilter ( )
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 0 , len ( entries ) )
filter . Tokens = allTokensFilter ( )
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 10 , len ( entries ) )
// Regression when collectibles is nil
filter . Tokens = Tokens { [ ] TokenCode { } , nil , [ ] TokenType { } }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 10 , len ( entries ) )
filter . Tokens = Tokens { Assets : [ ] TokenCode { "ETH" } , EnabledTypes : [ ] TokenType { AssetTT } }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 3 , len ( entries ) )
// TODO: update tests after adding token type to transfers
filter . Tokens = Tokens { Assets : [ ] TokenCode { "USDC" , "DAI" } , EnabledTypes : [ ] TokenType { AssetTT } }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
// Regression when EnabledTypes ar empty
filter . Tokens = Tokens { Assets : [ ] TokenCode { "USDC" , "DAI" } , EnabledTypes : [ ] TokenType { } }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
}
func TestGetActivityEntriesFilterByToAddresses ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
// Adds 4 extractable transactions
2023-05-28 10:40:50 +00:00
td , fromTds , toTds := fillTestData ( t , db )
2023-05-11 07:50:07 +00:00
// Add 6 extractable transactions
2023-06-14 16:10:20 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , db , td . nextIndex , 6 )
2023-05-11 07:50:07 +00:00
for i := range trs {
transfer . InsertTestTransfer ( t , db , & trs [ i ] )
}
2023-05-28 10:40:50 +00:00
mockTestAccountsWithAddresses ( t , db , append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... ) )
2023-05-11 07:50:07 +00:00
var filter Filter
filter . CounterpartyAddresses = allAddressesFilter ( )
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 10 , len ( entries ) )
filter . CounterpartyAddresses = [ ] eth_common . Address { eth_common . HexToAddress ( "0x567890" ) }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 0 , len ( entries ) )
2023-06-14 16:10:20 +00:00
filter . CounterpartyAddresses = [ ] eth_common . Address { td . pendingTr . To , td . multiTx2 . ToAddress , trs [ 3 ] . To }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 3 , len ( entries ) )
filter . CounterpartyAddresses = [ ] eth_common . Address { td . tr1 . To , td . pendingTr . From , trs [ 3 ] . From , trs [ 5 ] . To }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 2 , len ( entries ) )
}
func TestGetActivityEntriesFilterByNetworks ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
// Adds 4 extractable transactions
2023-05-28 10:40:50 +00:00
td , fromTds , toTds := fillTestData ( t , db )
2023-05-11 07:50:07 +00:00
// Add 6 extractable transactions
2023-06-14 16:10:20 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , db , td . nextIndex , 6 )
2023-05-11 07:50:07 +00:00
for i := range trs {
transfer . InsertTestTransfer ( t , db , & trs [ i ] )
}
2023-05-28 10:40:50 +00:00
mockTestAccountsWithAddresses ( t , db , append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... ) )
2023-05-11 07:50:07 +00:00
var filter Filter
chainIDs := allNetworksFilter ( )
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , chainIDs , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 10 , len ( entries ) )
chainIDs = [ ] common . ChainID { 5674839210 }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , chainIDs , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
// TODO: update after multi-transactions are filterable by ChainID
require . Equal ( t , 2 /*0*/ , len ( entries ) )
2023-06-14 16:10:20 +00:00
chainIDs = [ ] common . ChainID { td . pendingTr . ChainID , td . multiTx2Tr1 . ChainID , trs [ 3 ] . ChainID }
2023-06-08 23:52:45 +00:00
entries , err = getActivityEntries ( context . Background ( ) , db , [ ] eth_common . Address { } , chainIDs , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
// TODO: update after multi-transactions are filterable by ChainID
require . Equal ( t , 4 /*3*/ , len ( entries ) )
}
func TestGetActivityEntriesCheckToAndFrom ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
// Adds 6 transactions from which 4 are filered out
2023-05-28 10:40:50 +00:00
td , _ , _ := fillTestData ( t , db )
2023-05-11 07:50:07 +00:00
// Add extra transactions to test To address
2023-06-14 16:10:20 +00:00
trs , _ , _ := transfer . GenerateTestTransfers ( t , db , td . nextIndex , 2 )
2023-05-11 07:50:07 +00:00
transfer . InsertTestTransfer ( t , db , & trs [ 0 ] )
transfer . InsertTestPendingTransaction ( t , db , & trs [ 1 ] )
addresses := [ ] eth_common . Address { td . tr1 . From , td . pendingTr . From ,
2023-06-14 16:10:20 +00:00
td . multiTx1 . FromAddress , td . multiTx2 . ToAddress , trs [ 0 ] . To , trs [ 1 ] . To }
2023-05-11 07:50:07 +00:00
var filter Filter
2023-06-08 23:52:45 +00:00
entries , err := getActivityEntries ( context . Background ( ) , db , addresses , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 6 , len ( entries ) )
require . Equal ( t , SendAT , entries [ 5 ] . activityType ) // td.tr1
require . NotEqual ( t , eth . Address { } , entries [ 5 ] . transaction . Address ) // td.tr1
require . Equal ( t , td . tr1 . From , entries [ 5 ] . transaction . Address ) // td.tr1
require . Equal ( t , SendAT , entries [ 4 ] . activityType ) // td.pendingTr
// Multi-transactions are always considered as SendAT
2023-06-14 16:10:20 +00:00
require . Equal ( t , SendAT , entries [ 3 ] . activityType ) // td.multiTx1
require . Equal ( t , SendAT , entries [ 2 ] . activityType ) // td.multiTx2
2023-05-11 07:50:07 +00:00
require . Equal ( t , ReceiveAT , entries [ 1 ] . activityType ) // trs[0]
require . NotEqual ( t , eth . Address { } , entries [ 1 ] . transaction . Address ) // trs[0]
require . Equal ( t , trs [ 0 ] . To , entries [ 1 ] . transaction . Address ) // trs[0]
require . Equal ( t , ReceiveAT , entries [ 0 ] . activityType ) // trs[1] (pending)
// TODO: add accounts to DB for proper detection of sender/receiver
// TODO: Test with all addresses
}
2023-05-28 10:40:50 +00:00
// TODO test sub-transaction count for multi-transactions
2023-06-08 23:52:45 +00:00
func TestGetActivityEntriesCheckContextCancellation ( t * testing . T ) {
db , close := setupTestActivityDB ( t )
defer close ( )
_ , _ , _ = fillTestData ( t , db )
cancellableCtx , cancelFn := context . WithCancel ( context . Background ( ) )
cancelFn ( )
activities , err := getActivityEntries ( cancellableCtx , db , [ ] eth . Address { } , [ ] common . ChainID { } , Filter { } , 0 , 10 )
require . ErrorIs ( t , err , context . Canceled )
require . Equal ( t , 0 , len ( activities ) )
}