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"
2023-10-11 05:10:08 +00:00
"encoding/hex"
2023-06-14 19:43:28 +00:00
"math/big"
2023-04-21 11:59:29 +00:00
"testing"
2023-09-20 08:30:31 +00:00
"time"
2023-04-21 11:59:29 +00:00
2023-05-11 07:50:07 +00:00
"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-08-11 11:28:45 +00:00
"github.com/status-im/status-go/t/helpers"
"github.com/status-im/status-go/walletdatabase"
2023-04-21 11:59:29 +00:00
2023-05-11 07:50:07 +00:00
eth "github.com/ethereum/go-ethereum/common"
2023-06-14 19:43:28 +00:00
"github.com/ethereum/go-ethereum/common/hexutil"
2023-10-12 10:21:03 +00:00
"github.com/ethereum/go-ethereum/core/types"
2023-04-21 11:59:29 +00:00
"github.com/stretchr/testify/require"
)
2023-09-20 08:30:31 +00:00
var mockupTime = time . Unix ( 946724400 , 0 ) // 2000-01-01 12:00:00
2023-06-13 09:25:23 +00:00
func tokenFromSymbol ( chainID * common . ChainID , symbol string ) * Token {
for i , t := range transfer . TestTokens {
if ( chainID == nil || t . ChainID == uint64 ( * chainID ) ) && t . Symbol == symbol {
tokenType := Erc20
if testutils . SliceContains ( transfer . NativeTokenIndices , i ) {
tokenType = Native
}
return & Token {
TokenType : tokenType ,
ChainID : common . ChainID ( t . ChainID ) ,
Address : t . Address ,
}
}
}
return nil
}
2023-09-21 06:58:36 +00:00
func tokenFromCollectible ( c * transfer . TestCollectible ) Token {
return Token {
TokenType : Erc721 ,
ChainID : c . ChainID ,
Address : c . TokenAddress ,
TokenID : ( * hexutil . Big ) ( c . TokenID ) ,
}
}
2023-07-10 15:00:35 +00:00
func setupTestActivityDBStorageChoice ( tb testing . TB , inMemory bool ) ( deps FilterDependencies , close func ( ) ) {
2023-09-12 10:19:15 +00:00
var db * sql . DB
2023-07-10 15:00:35 +00:00
var err error
cleanupDB := func ( ) error { return nil }
2023-08-11 11:28:45 +00:00
cleanupWalletDB := func ( ) error { return nil }
2023-07-10 15:00:35 +00:00
if inMemory {
2023-08-11 11:28:45 +00:00
db , err = helpers . SetupTestMemorySQLDB ( walletdatabase . DbInitializer { } )
require . NoError ( tb , err )
2023-07-10 15:00:35 +00:00
} else {
2023-08-11 11:28:45 +00:00
db , cleanupWalletDB , err = helpers . SetupTestSQLDB ( walletdatabase . DbInitializer { } , "wallet-activity-tests" )
require . NoError ( tb , err )
2023-07-10 15:00:35 +00:00
}
2023-08-11 11:28:45 +00:00
2023-06-13 09:25:23 +00:00
deps = FilterDependencies {
2023-09-12 10:19:15 +00:00
db : db ,
2023-06-13 09:25:23 +00:00
tokenSymbol : func ( token Token ) string {
switch token . TokenType {
case Native :
for i , t := range transfer . TestTokens {
if t . ChainID == uint64 ( token . ChainID ) && testutils . SliceContains ( transfer . NativeTokenIndices , i ) {
return t . Symbol
}
}
case Erc20 :
for _ , t := range transfer . TestTokens {
if t . ChainID == uint64 ( token . ChainID ) && t . Address == token . Address {
return t . Symbol
}
}
}
// In case of ERC721 and ERC1155 we don't have a symbol and they are not yet handled
return ""
} ,
// tokenFromSymbol nil chainID accepts first symbol found
tokenFromSymbol : tokenFromSymbol ,
2023-09-20 08:30:31 +00:00
currentTimestamp : func ( ) int64 {
return mockupTime . Unix ( )
} ,
2023-06-13 09:25:23 +00:00
}
2023-04-21 11:59:29 +00:00
2023-06-13 09:25:23 +00:00
return deps , func ( ) {
2023-07-10 15:00:35 +00:00
require . NoError ( tb , cleanupDB ( ) )
2023-08-11 11:28:45 +00:00
require . NoError ( tb , cleanupWalletDB ( ) )
2023-04-21 11:59:29 +00:00
}
}
2023-07-10 15:00:35 +00:00
func setupTestActivityDB ( tb testing . TB ) ( deps FilterDependencies , close func ( ) ) {
return setupTestActivityDBStorageChoice ( tb , true )
}
2023-04-21 11:59:29 +00:00
type testData struct {
2023-06-13 09:25:23 +00:00
tr1 transfer . TestTransfer // index 1, ETH/Goerli
pendingTr transfer . TestTransfer // index 2, ETH/Optimism
multiTx1Tr1 transfer . TestTransfer // index 3, USDC/Mainnet
multiTx2Tr1 transfer . TestTransfer // index 4, USDC/Goerli
multiTx1Tr2 transfer . TestTransfer // index 5, USDC/Optimism
multiTx2Tr2 transfer . TestTransfer // index 6, SNT/Mainnet
multiTx2PendingTr transfer . TestTransfer // index 7, DAI/Mainnet
2023-06-14 16:10:20 +00:00
multiTx1 transfer . TestMultiTransaction
2024-03-12 09:15:30 +00:00
multiTx1ID common . MultiTransactionIDType
2023-06-14 16:10:20 +00:00
multiTx2 transfer . TestMultiTransaction
2024-03-12 09:15:30 +00:00
multiTx2ID common . MultiTransactionIDType
2023-06-14 16:10:20 +00:00
nextIndex int
2023-04-21 11:59:29 +00:00
}
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-07-10 14:56:08 +00:00
func fillTestData ( t * testing . T , db * sql . DB ) ( td testData , fromAddresses , toAddresses [ ] eth . Address ) {
2023-06-13 09:25:23 +00:00
// Generates ETH/Goerli, ETH/Optimism, USDC/Mainnet, USDC/Goerli, USDC/Optimism, SNT/Mainnet, DAI/Mainnet
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 ]
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , db , td . tr1 . To , & td . tr1 )
2023-04-21 11:59:29 +00:00
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 . multiTx1 = transfer . GenerateTestSendMultiTransaction ( td . multiTx1Tr1 )
td . multiTx1 . ToToken = testutils . DaiSymbol
td . multiTx1ID = transfer . InsertTestMultiTransaction ( t , db , & td . multiTx1 )
td . multiTx1Tr1 . MultiTransactionID = td . multiTx1ID
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , db , td . multiTx1Tr1 . To , & td . multiTx1Tr1 )
2023-06-14 16:10:20 +00:00
td . multiTx1Tr2 . MultiTransactionID = td . multiTx1ID
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , db , td . multiTx1Tr2 . To , & 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 )
2023-06-13 09:25:23 +00:00
td . multiTx2 . ToToken = testutils . SntSymbol
2023-06-14 16:10:20 +00:00
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
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , db , td . multiTx2Tr1 . To , & td . multiTx2Tr1 )
2023-06-14 16:10:20 +00:00
td . multiTx2Tr2 . MultiTransactionID = td . multiTx2ID
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , db , td . multiTx2Tr2 . To , & td . multiTx2Tr2 )
2023-06-14 16:10:20 +00:00
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
}
2023-06-13 09:25:23 +00:00
func TTrToToken ( t * testing . T , tt * transfer . TestTransaction ) * Token {
token , isNative := transfer . TestTrToToken ( t , tt )
tokenType := Erc20
if isNative {
tokenType = Native
}
return & Token {
TokenType : tokenType ,
ChainID : common . ChainID ( token . ChainID ) ,
Address : token . Address ,
}
}
2023-07-18 15:57:44 +00:00
func expectedTokenType ( tokenAddress eth . Address ) * TransferType {
transferType := new ( TransferType )
if ( tokenAddress != eth . Address { } ) {
* transferType = TransferTypeErc20
} else {
* transferType = TransferTypeEth
}
return transferType
}
2023-04-21 11:59:29 +00:00
func TestGetActivityEntriesAll ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-04-21 11:59:29 +00:00
defer close ( )
2023-06-13 09:25:23 +00:00
td , fromAddresses , toAddresses := fillTestData ( t , deps . db )
2023-04-21 11:59:29 +00:00
var filter Filter
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , append ( toAddresses , fromAddresses ... ) , true , [ ] 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 --
}
2023-06-13 09:25:23 +00:00
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : td . tr1 . ChainID , Hash : td . tr1 . Hash , Address : td . tr1 . To } ,
2023-05-11 07:50:07 +00:00
id : td . tr1 . MultiTransactionID ,
timestamp : td . tr1 . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( td . tr1 . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & td . tr1 . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "ETH" ) ,
2023-07-18 15:57:44 +00:00
sender : & td . tr1 . From ,
recipient : & td . tr1 . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & td . tr1 . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( td . tr1 . Token . Address ) ,
2023-06-13 09:25:23 +00:00
} , entries [ 3 ] )
require . Equal ( t , 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 ,
2023-06-14 19:43:28 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( td . pendingTr . Value ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
2023-06-13 09:25:23 +00:00
tokenOut : TTrToToken ( t , & td . pendingTr . TestTransaction ) ,
tokenIn : nil ,
2023-08-30 16:14:57 +00:00
symbolOut : common . NewAndSet ( "ETH" ) ,
2023-08-28 09:02:05 +00:00
symbolIn : nil ,
2023-07-18 15:57:44 +00:00
sender : & td . pendingTr . From ,
recipient : & td . pendingTr . To ,
chainIDOut : & td . pendingTr . ChainID ,
chainIDIn : nil ,
transferType : expectedTokenType ( eth . Address { } ) ,
2023-06-13 09:25:23 +00:00
} , entries [ 2 ] )
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-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-06-14 19:43:28 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( td . multiTx1 . FromAmount ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( td . multiTx1 . ToAmount ) ) ,
2023-06-13 09:25:23 +00:00
tokenOut : tokenFromSymbol ( nil , td . multiTx1 . FromToken ) ,
tokenIn : tokenFromSymbol ( nil , td . multiTx1 . ToToken ) ,
2023-08-30 16:14:57 +00:00
symbolOut : common . NewAndSet ( "USDC" ) ,
symbolIn : common . NewAndSet ( "DAI" ) ,
2023-07-18 15:57:44 +00:00
sender : & td . multiTx1 . FromAddress ,
recipient : & td . multiTx1 . ToAddress ,
2023-06-13 09:25:23 +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-06-14 19:43:28 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( td . multiTx2 . FromAmount ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( td . multiTx2 . ToAmount ) ) ,
2023-08-30 16:14:57 +00:00
symbolOut : common . NewAndSet ( "USDC" ) ,
symbolIn : common . NewAndSet ( "SNT" ) ,
2023-06-13 09:25:23 +00:00
tokenOut : tokenFromSymbol ( nil , td . multiTx2 . FromToken ) ,
tokenIn : tokenFromSymbol ( nil , td . multiTx2 . ToToken ) ,
2023-07-18 15:57:44 +00:00
sender : & td . multiTx2 . FromAddress ,
recipient : & td . multiTx2 . ToAddress ,
2023-06-13 09:25:23 +00:00
} , entries [ 0 ] )
2023-04-21 11:59:29 +00:00
}
2023-09-06 19:15:07 +00:00
// TestGetActivityEntriesWithSenderFilter covers the corner-case of having both sender and receiver in the filter.
// In this specific case we expect that there will be two transactions (one probably backed by a multi-transaction)
// In case of both sender and receiver are included we validate we receive both entries otherwise only the "owned"
// transactions should be retrieved by the filter
2023-04-21 11:59:29 +00:00
func TestGetActivityEntriesWithSameTransactionForSenderAndReceiverInDB ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-04-21 11:59:29 +00:00
defer close ( )
// Add 4 extractable transactions with timestamps 1-4
2023-09-12 10:19:15 +00:00
td , _ , _ := fillTestData ( t , deps . db )
2023-05-28 10:40:50 +00:00
2023-09-06 19:15:07 +00:00
// Add another transaction with owner reversed
senderTr := td . tr1
// Ensure we have a consistent order
senderTr . Timestamp ++
// add sender as owner, fillTestData adds receiver as owner
transfer . InsertTestTransfer ( t , deps . db , senderTr . From , & senderTr )
2023-04-21 11:59:29 +00:00
var filter Filter
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , [ ] eth . Address { td . tr1 . To , senderTr . From } , false , [ ] 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 , SendAT , entries [ 0 ] . activityType )
2023-09-06 19:15:07 +00:00
require . Equal ( t , senderTr . From , entries [ 0 ] . transaction . Address )
require . Equal ( t , senderTr . From , * entries [ 0 ] . sender )
require . Equal ( t , senderTr . To , * entries [ 0 ] . recipient )
2023-05-11 07:50:07 +00:00
2023-09-06 19:15:07 +00:00
require . Equal ( t , ReceiveAT , entries [ 1 ] . activityType )
require . Equal ( t , td . tr1 . To , * entries [ 1 ] . recipient )
require . Equal ( t , td . tr1 . From , * entries [ 1 ] . sender )
require . Equal ( t , td . tr1 . To , * entries [ 1 ] . recipient )
2023-04-21 11:59:29 +00:00
}
func TestGetActivityEntriesFilterByTime ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-04-21 11:59:29 +00:00
defer close ( )
2023-06-13 09:25:23 +00:00
td , fromTds , toTds := fillTestData ( t , deps . db )
2023-05-28 10:40:50 +00:00
2023-04-21 11:59:29 +00:00
// Add 6 extractable transactions with timestamps 6-12
2023-06-13 09:25:23 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , 6 )
2023-04-21 11:59:29 +00:00
for i := range trs {
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , deps . db , trs [ i ] . To , & trs [ i ] )
2023-04-21 11:59:29 +00:00
}
2023-09-12 10:19:15 +00:00
allAddresses := append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... )
2023-05-28 10:40:50 +00:00
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-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 8 , len ( entries ) )
2023-07-18 15:57:44 +00:00
2023-04-21 11:59:29 +00:00
// Check start and end content
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 5 ] . ChainID , Hash : trs [ 5 ] . Hash , Address : trs [ 5 ] . To } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 5 ] . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( trs [ 5 ] . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & trs [ 5 ] . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "USDC" ) ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 5 ] . From ,
recipient : & trs [ 5 ] . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & trs [ 5 ] . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( trs [ 5 ] . Token . Address ) ,
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-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-06-14 19:43:28 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( td . multiTx1 . FromAmount ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( td . multiTx1 . ToAmount ) ) ,
2023-06-13 09:25:23 +00:00
tokenOut : tokenFromSymbol ( nil , td . multiTx1 . FromToken ) ,
tokenIn : tokenFromSymbol ( nil , td . multiTx1 . ToToken ) ,
2023-08-30 16:14:57 +00:00
symbolOut : common . NewAndSet ( "USDC" ) ,
symbolIn : common . NewAndSet ( "DAI" ) ,
2023-07-18 15:57:44 +00:00
sender : & td . multiTx1 . FromAddress ,
recipient : & td . multiTx1 . ToAddress ,
chainIDOut : nil ,
chainIDIn : nil ,
transferType : nil ,
2023-04-21 11:59:29 +00:00
} , entries [ 7 ] )
// Test complete interval
filter . Period . EndTimestamp = trs [ 2 ] . Timestamp
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
require . Equal ( t , 5 , len ( entries ) )
2023-07-18 15:57:44 +00:00
2023-04-21 11:59:29 +00:00
// Check start and end content
require . Equal ( t , Entry {
2023-05-11 07:50:07 +00:00
payloadType : SimpleTransactionPT ,
2023-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 2 ] . ChainID , Hash : trs [ 2 ] . Hash , Address : trs [ 2 ] . To } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 2 ] . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( trs [ 2 ] . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & trs [ 2 ] . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "ETH" ) ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 2 ] . From ,
recipient : & trs [ 2 ] . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & trs [ 2 ] . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( trs [ 2 ] . Token . Address ) ,
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-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-06-14 19:43:28 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( td . multiTx1 . FromAmount ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( td . multiTx1 . ToAmount ) ) ,
2023-06-13 09:25:23 +00:00
tokenOut : tokenFromSymbol ( nil , td . multiTx1 . FromToken ) ,
tokenIn : tokenFromSymbol ( nil , td . multiTx1 . ToToken ) ,
2023-08-30 16:14:57 +00:00
symbolOut : common . NewAndSet ( "USDC" ) ,
symbolIn : common . NewAndSet ( "DAI" ) ,
2023-07-18 15:57:44 +00:00
sender : & td . multiTx1 . FromAddress ,
recipient : & td . multiTx1 . ToAddress ,
chainIDOut : nil ,
chainIDIn : nil ,
transferType : nil ,
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-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 2 ] . ChainID , Hash : trs [ 2 ] . Hash , Address : trs [ 2 ] . To } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 2 ] . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( trs [ 2 ] . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & trs [ 2 ] . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "ETH" ) ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 2 ] . From ,
recipient : & trs [ 2 ] . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & trs [ 2 ] . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( trs [ 2 ] . Token . Address ) ,
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-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : td . tr1 . ChainID , Hash : td . tr1 . Hash , Address : td . tr1 . To } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : td . tr1 . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( td . tr1 . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & td . tr1 . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "ETH" ) ,
2023-07-18 15:57:44 +00:00
sender : & td . tr1 . From ,
recipient : & td . tr1 . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & td . tr1 . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( td . tr1 . Token . Address ) ,
2023-04-21 11:59:29 +00:00
} , entries [ 6 ] )
}
func TestGetActivityEntriesCheckOffsetAndLimit ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-04-21 11:59:29 +00:00
defer close ( )
// Add 10 extractable transactions with timestamps 1-10
2023-06-13 09:25:23 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , deps . db , 1 , 10 )
2023-04-21 11:59:29 +00:00
for i := range trs {
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , deps . db , trs [ i ] . To , & trs [ i ] )
2023-04-21 11:59:29 +00:00
}
2023-09-12 10:19:15 +00:00
allAddresses := append ( fromTrs , toTrs ... )
2023-05-28 10:40:50 +00:00
2023-04-21 11:59:29 +00:00
var filter Filter
// Get all
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 8 ] . ChainID , Hash : trs [ 8 ] . Hash , Address : trs [ 8 ] . To } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 8 ] . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( trs [ 8 ] . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & trs [ 8 ] . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "ETH" ) ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 8 ] . From ,
recipient : & trs [ 8 ] . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & trs [ 8 ] . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( trs [ 8 ] . Token . Address ) ,
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-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 6 ] . ChainID , Hash : trs [ 6 ] . Hash , Address : trs [ 6 ] . To } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 6 ] . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( trs [ 6 ] . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & trs [ 6 ] . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "DAI" ) ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 6 ] . From ,
recipient : & trs [ 6 ] . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & trs [ 6 ] . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( trs [ 6 ] . Token . Address ) ,
2023-04-21 11:59:29 +00:00
} , entries [ 2 ] )
// Move window 2 entries forward
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 6 ] . ChainID , Hash : trs [ 6 ] . Hash , Address : trs [ 6 ] . To } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 6 ] . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( trs [ 6 ] . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & trs [ 6 ] . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "DAI" ) ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 6 ] . From ,
recipient : & trs [ 6 ] . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & trs [ 6 ] . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( trs [ 6 ] . Token . Address ) ,
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-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 4 ] . ChainID , Hash : trs [ 4 ] . Hash , Address : trs [ 4 ] . To } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 4 ] . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( trs [ 4 ] . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & trs [ 4 ] . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "USDC" ) ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 4 ] . From ,
recipient : & trs [ 4 ] . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & trs [ 4 ] . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( trs [ 4 ] . Token . Address ) ,
2023-04-21 11:59:29 +00:00
} , entries [ 2 ] )
// Move window 4 more entries to test filter cap
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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-07-04 22:21:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 2 ] . ChainID , Hash : trs [ 2 ] . Hash , Address : trs [ 2 ] . To } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 2 ] . Timestamp ,
2023-09-06 19:15:07 +00:00
activityType : ReceiveAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( trs [ 2 ] . Value ) ) ,
tokenOut : nil ,
tokenIn : TTrToToken ( t , & trs [ 2 ] . TestTransaction ) ,
symbolOut : nil ,
symbolIn : common . NewAndSet ( "USDC" ) ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 2 ] . From ,
recipient : & trs [ 2 ] . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : nil ,
chainIDIn : & trs [ 2 ] . ChainID ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( trs [ 2 ] . Token . Address ) ,
2023-04-21 11:59:29 +00:00
} , entries [ 0 ] )
}
2023-09-11 09:54:37 +00:00
func countTypes ( entries [ ] Entry ) ( sendCount , receiveCount , contractCount , mintCount , swapCount , buyCount , bridgeCount int ) {
2023-05-28 10:40:50 +00:00
for _ , entry := range entries {
switch entry . activityType {
case SendAT :
sendCount ++
case ReceiveAT :
receiveCount ++
case SwapAT :
swapCount ++
case BuyAT :
buyCount ++
case BridgeAT :
bridgeCount ++
2023-09-11 09:54:37 +00:00
case ContractDeploymentAT :
contractCount ++
case MintAT :
mintCount ++
2023-05-28 10:40:50 +00:00
}
}
return
}
2023-04-21 11:59:29 +00:00
func TestGetActivityEntriesFilterByType ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-04-21 11:59:29 +00:00
defer close ( )
// Adds 4 extractable transactions
2023-09-12 10:19:15 +00:00
td , tdFromAdds , tdToAddrs := fillTestData ( t , deps . db )
2023-05-28 10:40:50 +00:00
// Add 5 extractable transactions: one MultiTransactionSwap, two MultiTransactionBridge and two MultiTransactionSend
2023-06-14 16:10:20 +00:00
multiTxs := make ( [ ] transfer . TestMultiTransaction , 5 )
2023-09-12 10:19:15 +00:00
trs , fromAddrs , toAddrs := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , len ( multiTxs ) * 2 )
2023-06-14 16:10:20 +00:00
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
2024-03-12 09:15:30 +00:00
var lastMT common . 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-13 09:25:23 +00:00
lastMT = transfer . InsertTestMultiTransaction ( t , deps . db , & multiTxs [ i / 2 ] )
2023-04-21 11:59:29 +00:00
}
2023-06-14 16:10:20 +00:00
trs [ i ] . MultiTransactionID = lastMT
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , deps . db , trs [ i ] . To , & trs [ i ] )
2023-04-21 11:59:29 +00:00
}
2023-10-11 05:10:08 +00:00
trsSpecial , fromSpecial , toSpecial := transfer . GenerateTestTransfers ( t , deps . db , 100 , 3 )
2023-09-14 21:50:51 +00:00
// Here not to include the modified To and From addresses
allAddresses := append ( append ( append ( append ( append ( tdFromAdds , tdToAddrs ... ) , fromAddrs ... ) , toAddrs ... ) , fromSpecial ... ) , toSpecial ... )
2023-10-11 05:10:08 +00:00
// Insert MintAT Collectible
2023-09-11 09:54:37 +00:00
trsSpecial [ 0 ] . From = eth . HexToAddress ( "0x0" )
transfer . InsertTestTransferWithOptions ( t , deps . db , trsSpecial [ 0 ] . To , & trsSpecial [ 0 ] , & transfer . TestTransferOptions {
TokenAddress : eth . HexToAddress ( "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" ) ,
TokenID : ( big . NewInt ( 1318 ) ) ,
} )
2023-10-11 05:10:08 +00:00
// Insert MintAT Token
trsSpecial [ 1 ] . From = eth . HexToAddress ( "0x0" )
inputMethod , err := hex . DecodeString ( "1b5ee6ae" )
require . NoError ( t , err )
transfer . InsertTestTransferWithOptions ( t , deps . db , trsSpecial [ 1 ] . To , & trsSpecial [ 1 ] , & transfer . TestTransferOptions {
TokenAddress : eth . HexToAddress ( "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb49" ) ,
Tx : transfer . GenerateTxField ( inputMethod ) ,
} )
2023-09-11 09:54:37 +00:00
// Insert ContractDeploymentAt
2023-10-11 05:10:08 +00:00
trsSpecial [ 2 ] . To = eth . HexToAddress ( "0x0" )
transfer . InsertTestTransferWithOptions ( t , deps . db , trsSpecial [ 2 ] . From , & trsSpecial [ 2 ] , & transfer . TestTransferOptions {
NullifyAddresses : [ ] eth . Address { trsSpecial [ 2 ] . To } ,
2023-09-11 09:54:37 +00:00
} )
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-10-11 05:10:08 +00:00
addresses := [ ] eth . 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 , trsSpecial [ 0 ] . To , trsSpecial [ 1 ] . To , trsSpecial [ 2 ] . From }
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , addresses , false , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-10-11 05:10:08 +00:00
require . Equal ( t , 12 , len ( entries ) )
2023-05-11 07:50:07 +00:00
2023-04-21 11:59:29 +00:00
filter . Types = [ ] Type { SendAT , SwapAT }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , addresses , false , [ ] common . ChainID { } , filter , 0 , 15 )
2023-04-21 11:59:29 +00:00
require . NoError ( t , err )
2023-10-11 05:10:08 +00:00
2023-05-28 10:40:50 +00:00
// 3 from td Send + 2 trs MT Send + 1 (swap)
require . Equal ( t , 6 , len ( entries ) )
2023-09-11 09:54:37 +00:00
sendCount , receiveCount , contractCount , mintCount , swapCount , _ , bridgeCount := countTypes ( entries )
2023-05-28 10:40:50 +00:00
require . Equal ( t , 5 , sendCount )
require . Equal ( t , 0 , receiveCount )
2023-09-11 09:54:37 +00:00
require . Equal ( t , 0 , contractCount )
require . Equal ( t , 0 , mintCount )
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-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , addresses , false , [ ] 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
2023-09-11 09:54:37 +00:00
sendCount , receiveCount , contractCount , mintCount , swapCount , _ , bridgeCount = countTypes ( entries )
2023-05-28 10:40:50 +00:00
require . Equal ( t , 0 , sendCount )
2023-04-21 11:59:29 +00:00
require . Equal ( t , 1 , receiveCount )
2023-09-11 09:54:37 +00:00
require . Equal ( t , 0 , contractCount )
require . Equal ( t , 0 , mintCount )
2023-05-28 10:40:50 +00:00
require . Equal ( t , 0 , swapCount )
require . Equal ( t , 2 , bridgeCount )
2023-09-11 09:54:37 +00:00
filter . Types = [ ] Type { MintAT }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , addresses , false , [ ] common . ChainID { } , filter , 0 , 15 )
2023-09-11 09:54:37 +00:00
require . NoError ( t , err )
2023-10-11 05:10:08 +00:00
require . Equal ( t , 2 , len ( entries ) )
2023-09-11 09:54:37 +00:00
sendCount , receiveCount , contractCount , mintCount , swapCount , _ , bridgeCount = countTypes ( entries )
require . Equal ( t , 0 , sendCount )
require . Equal ( t , 0 , receiveCount )
require . Equal ( t , 0 , contractCount )
2023-10-11 05:10:08 +00:00
require . Equal ( t , 2 , mintCount )
2023-09-11 09:54:37 +00:00
require . Equal ( t , 0 , swapCount )
require . Equal ( t , 0 , bridgeCount )
filter . Types = [ ] Type { ContractDeploymentAT }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , addresses , false , [ ] common . ChainID { } , filter , 0 , 15 )
2023-09-11 09:54:37 +00:00
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
sendCount , receiveCount , contractCount , mintCount , swapCount , _ , bridgeCount = countTypes ( entries )
require . Equal ( t , 0 , sendCount )
require . Equal ( t , 0 , receiveCount )
require . Equal ( t , 1 , contractCount )
require . Equal ( t , 0 , mintCount )
require . Equal ( t , 0 , swapCount )
require . Equal ( t , 0 , bridgeCount )
2023-09-11 14:46:49 +00:00
// Filter with all addresses regression
filter . Types = [ ] Type { SendAT }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-09-11 14:46:49 +00:00
require . NoError ( t , err )
2023-09-14 21:50:51 +00:00
// We have 6 but one is not matched because is a receive, having owner the to address
2023-09-11 14:46:49 +00:00
require . Equal ( t , 5 , len ( entries ) )
2023-04-21 11:59:29 +00:00
}
2023-10-12 10:21:03 +00:00
func TestStatusMintCustomEvent ( t * testing . T ) {
deps , close := setupTestActivityDB ( t )
defer close ( )
td , fromTds , toTds := fillTestData ( t , deps . db )
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , 3 )
allAddresses := append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... )
trs [ 0 ] . From = eth . HexToAddress ( "0x0" )
transfer . InsertTestTransferWithOptions ( t , deps . db , trs [ 0 ] . To , & trs [ 0 ] , & transfer . TestTransferOptions {
TokenAddress : eth . HexToAddress ( "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" ) ,
Receipt : & types . Receipt {
Logs : [ ] * types . Log {
{ Topics : [ ] eth . Hash { eth . HexToHash ( "0xea667487ed28493de38fd2808b00affaee21d875a9e95aa01ef8352151292297" ) } } ,
{ Topics : [ ] eth . Hash { eth . HexToHash ( "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925" ) } } ,
} ,
} ,
} )
// StatusMint - 0x28c427b0611d99da5c4f7368abe57e86b045b483c4689ae93e90745802335b87
trs [ 1 ] . From = eth . HexToAddress ( "0x0" )
transfer . InsertTestTransferWithOptions ( t , deps . db , trs [ 1 ] . To , & trs [ 1 ] , & transfer . TestTransferOptions {
TokenAddress : eth . HexToAddress ( "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb49" ) ,
Receipt : & types . Receipt {
Logs : [ ] * types . Log {
{ Topics : [ ] eth . Hash { eth . HexToHash ( "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925" ) } } ,
{ Topics : [ ] eth . Hash { eth . HexToHash ( "0x28c427b0611d99da5c4f7368abe57e86b045b483c4689ae93e90745802335b87" ) } } ,
} ,
} ,
} )
// Log order should not matter
trs [ 2 ] . From = eth . HexToAddress ( "0x0" )
transfer . InsertTestTransferWithOptions ( t , deps . db , trs [ 2 ] . To , & trs [ 2 ] , & transfer . TestTransferOptions {
TokenAddress : eth . HexToAddress ( "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb49" ) ,
Receipt : & types . Receipt {
Logs : [ ] * types . Log {
{ Topics : [ ] eth . Hash { eth . HexToHash ( "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" ) } } ,
{ Topics : [ ] eth . Hash { eth . HexToHash ( "0x28c427b0611d99da5c4f7368abe57e86b045b483c4689ae93e90745802335b87" ) } } ,
{ Topics : [ ] eth . Hash { eth . HexToHash ( "0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925" ) } } ,
} ,
} ,
} )
var filter Filter
entries , err := getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
require . NoError ( t , err )
require . Equal ( t , 7 , len ( entries ) )
filter . Types = [ ] Type { MintAT }
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
require . NoError ( t , err )
require . Equal ( t , 2 , len ( entries ) )
require . Equal ( t , trs [ 2 ] . Hash , entries [ 0 ] . transaction . Hash )
require . Equal ( t , trs [ 1 ] . Hash , entries [ 1 ] . transaction . Hash )
}
2023-05-11 07:50:07 +00:00
func TestGetActivityEntriesFilterByAddresses ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-04-21 11:59:29 +00:00
defer close ( )
// Adds 4 extractable transactions
2023-06-13 09:25:23 +00:00
td , fromTds , toTds := fillTestData ( t , deps . db )
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , 6 )
2023-04-21 11:59:29 +00:00
for i := range trs {
2023-09-06 19:15:07 +00:00
transfer . InsertTestTransfer ( t , deps . db , trs [ i ] . From , & trs [ i ] )
2023-04-21 11:59:29 +00:00
}
2023-09-12 10:19:15 +00:00
allAddresses := append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... )
2023-05-28 10:40:50 +00:00
2023-04-21 11:59:29 +00:00
var filter Filter
2023-05-11 07:50:07 +00:00
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 10 , len ( entries ) )
2023-09-12 10:19:15 +00:00
addressesFilter := [ ] eth . Address { td . multiTx1 . ToAddress , td . multiTx2 . FromAddress , trs [ 1 ] . From , trs [ 4 ] . From , trs [ 3 ] . To }
2023-09-06 19:15:07 +00:00
// The td.multiTx1.ToAddress and trs[3].To are missing not having them as owner address
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , addressesFilter , false , [ ] 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 ,
2023-09-06 19:15:07 +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-09-06 19:15:07 +00:00
activityType : SendAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-09-06 19:15:07 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( trs [ 4 ] . Value ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
tokenOut : TTrToToken ( t , & trs [ 4 ] . TestTransaction ) ,
tokenIn : nil ,
symbolOut : common . NewAndSet ( "USDC" ) ,
symbolIn : nil ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 4 ] . From ,
recipient : & trs [ 4 ] . To ,
2023-09-06 19:15:07 +00:00
chainIDOut : & trs [ 4 ] . ChainID ,
chainIDIn : nil ,
2023-07-18 15:57:44 +00:00
transferType : expectedTokenType ( trs [ 4 ] . Token . Address ) ,
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-09-06 19:15:07 +00:00
transaction : & transfer . TransactionIdentity { ChainID : trs [ 1 ] . ChainID , Hash : trs [ 1 ] . Hash , Address : trs [ 1 ] . From } ,
2023-05-11 07:50:07 +00:00
id : 0 ,
timestamp : trs [ 1 ] . Timestamp ,
activityType : SendAT ,
2023-09-20 08:30:31 +00:00
activityStatus : FinalizedAS ,
2023-06-14 19:43:28 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( trs [ 1 ] . Value ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( 0 ) ) ,
2023-06-13 09:25:23 +00:00
tokenOut : TTrToToken ( t , & trs [ 1 ] . TestTransaction ) ,
tokenIn : nil ,
2023-08-30 16:14:57 +00:00
symbolOut : common . NewAndSet ( "ETH" ) ,
2023-08-28 09:02:05 +00:00
symbolIn : nil ,
2023-07-18 15:57:44 +00:00
sender : & trs [ 1 ] . From ,
recipient : & trs [ 1 ] . To ,
chainIDOut : & trs [ 1 ] . ChainID ,
chainIDIn : nil ,
transferType : expectedTokenType ( trs [ 1 ] . Token . Address ) ,
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-06-14 19:43:28 +00:00
amountOut : ( * hexutil . Big ) ( big . NewInt ( td . multiTx2 . FromAmount ) ) ,
amountIn : ( * hexutil . Big ) ( big . NewInt ( td . multiTx2 . ToAmount ) ) ,
2023-06-13 09:25:23 +00:00
tokenOut : tokenFromSymbol ( nil , td . multiTx2 . FromToken ) ,
tokenIn : tokenFromSymbol ( nil , td . multiTx2 . ToToken ) ,
2023-08-30 16:14:57 +00:00
symbolOut : common . NewAndSet ( "USDC" ) ,
symbolIn : common . NewAndSet ( "SNT" ) ,
2023-07-18 15:57:44 +00:00
sender : & td . multiTx2 . FromAddress ,
recipient : & td . multiTx2 . ToAddress ,
chainIDOut : nil ,
chainIDIn : nil ,
2023-04-21 11:59:29 +00:00
} , entries [ 2 ] )
}
2023-05-11 07:50:07 +00:00
func TestGetActivityEntriesFilterByStatus ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-05-11 07:50:07 +00:00
defer close ( )
2023-09-20 08:30:31 +00:00
// Adds 4 extractable transactions: 1 T, 1 T pending, 1 MT pending, 1 MT with 2xT finalized
2023-06-13 09:25:23 +00:00
td , fromTds , toTds := fillTestData ( t , deps . db )
2023-09-20 08:30:31 +00:00
// Add 7 extractable transactions: 1 pending, 1 Tr failed, 1 MT failed, 4 finalized
2023-06-13 09:25:23 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , 7 )
2023-06-14 16:10:20 +00:00
multiTx := transfer . GenerateTestSendMultiTransaction ( trs [ 6 ] )
2023-06-13 09:25:23 +00:00
failedMTID := transfer . InsertTestMultiTransaction ( t , deps . 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 {
2023-06-13 09:25:23 +00:00
transfer . InsertTestPendingTransaction ( t , deps . db , & trs [ i ] )
2023-05-28 10:40:50 +00:00
} else {
trs [ i ] . Success = i != 3 && i != 6
2023-09-20 08:30:31 +00:00
if trs [ i ] . Success && ( i == 2 || i == 5 ) {
// Finalize status depends on timestamp
trs [ i ] . Timestamp = mockupTime . Unix ( ) - 10
}
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , deps . db , trs [ i ] . To , & trs [ i ] )
2023-05-28 10:40:50 +00:00
}
2023-05-11 07:50:07 +00:00
}
2023-09-12 10:19:15 +00:00
allAddresses := append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... )
2023-05-28 10:40:50 +00:00
2023-05-11 07:50:07 +00:00
var filter Filter
2023-05-28 10:40:50 +00:00
filter . Statuses = allActivityStatusesFilter ( )
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-28 10:40:50 +00:00
require . NoError ( t , err )
2023-09-20 08:30:31 +00:00
require . Equal ( t , 2 , len ( entries ) )
2023-05-28 10:40:50 +00:00
filter . Statuses = [ ] Status { FinalizedAS }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-28 10:40:50 +00:00
require . NoError ( t , err )
2023-09-20 08:30:31 +00:00
require . Equal ( t , 4 , 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-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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 ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-05-11 07:50:07 +00:00
defer close ( )
2023-06-13 09:25:23 +00:00
// Adds 4 extractable transactions 2 transactions (ETH/Goerli, ETH/Optimism), one MT USDC to DAI and another MT USDC to SNT
td , fromTds , toTds := fillTestData ( t , deps . db )
// Add 9 transactions DAI/Goerli, ETH/Mainnet, ETH/Goerli, ETH/Optimism, USDC/Mainnet, USDC/Goerli, USDC/Optimism, SNT/Mainnet, DAI/Mainnet
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , 9 )
2023-05-11 07:50:07 +00:00
for i := range trs {
2023-06-13 09:25:23 +00:00
tokenAddr := transfer . TestTokens [ i ] . Address
trs [ i ] . ChainID = common . ChainID ( transfer . TestTokens [ i ] . ChainID )
2023-06-27 22:49:02 +00:00
transfer . InsertTestTransferWithOptions ( t , deps . db , trs [ i ] . To , & trs [ i ] , & transfer . TestTransferOptions {
TokenAddress : tokenAddr ,
} )
2023-05-11 07:50:07 +00:00
}
2023-09-12 10:19:15 +00:00
allAddresses := append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... )
2023-05-28 10:40:50 +00:00
2023-05-11 07:50:07 +00:00
var filter Filter
2023-06-13 09:25:23 +00:00
filter . FilterOutAssets = true
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] 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-13 09:25:23 +00:00
filter . FilterOutAssets = false
filter . Assets = allTokensFilter ( )
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-06-13 09:25:23 +00:00
require . Equal ( t , 13 , len ( entries ) )
2023-05-11 07:50:07 +00:00
2023-06-13 09:25:23 +00:00
// Native tokens are network agnostic, hence all are returned
filter . Assets = [ ] Token { { TokenType : Native , ChainID : common . ChainID ( transfer . EthMainnet . ChainID ) } }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-06-13 09:25:23 +00:00
require . Equal ( t , 5 , len ( entries ) )
2023-05-11 07:50:07 +00:00
2023-06-13 09:25:23 +00:00
// Test that it doesn't break the filter
filter . Assets = [ ] Token { { TokenType : Erc1155 } }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-06-13 09:25:23 +00:00
require . Equal ( t , 0 , len ( entries ) )
2023-05-11 07:50:07 +00:00
2023-06-13 09:25:23 +00:00
filter . Assets = [ ] Token { {
TokenType : Erc20 ,
ChainID : common . ChainID ( transfer . UsdcMainnet . ChainID ) ,
Address : transfer . UsdcMainnet . Address ,
} }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-06-13 09:25:23 +00:00
// Two MT for which ChainID is ignored and one transfer on the main net and the Goerli is ignored
require . Equal ( t , 3 , len ( entries ) )
2023-09-06 19:15:07 +00:00
require . Equal ( t , Erc20 , entries [ 0 ] . tokenIn . TokenType )
require . Equal ( t , transfer . UsdcMainnet . Address , entries [ 0 ] . tokenIn . Address )
require . Nil ( t , entries [ 0 ] . tokenOut )
2023-06-13 09:25:23 +00:00
// MT has only symbol, the first token is lookup by symbol for both entries
require . Equal ( t , Erc20 , entries [ 1 ] . tokenOut . TokenType )
require . Equal ( t , transfer . UsdcMainnet . Address , entries [ 1 ] . tokenOut . Address )
require . Equal ( t , Erc20 , entries [ 1 ] . tokenIn . TokenType )
require . Equal ( t , transfer . SntMainnet . Address , entries [ 1 ] . tokenIn . Address )
require . Equal ( t , Erc20 , entries [ 2 ] . tokenOut . TokenType )
require . Equal ( t , transfer . UsdcMainnet . Address , entries [ 1 ] . tokenOut . Address )
require . Equal ( t , Erc20 , entries [ 2 ] . tokenIn . TokenType )
require . Equal ( t , transfer . UsdcMainnet . Address , entries [ 1 ] . tokenOut . Address )
filter . Assets = [ ] Token { {
TokenType : Erc20 ,
ChainID : common . ChainID ( transfer . UsdcMainnet . ChainID ) ,
Address : transfer . UsdcMainnet . Address ,
} , {
TokenType : Erc20 ,
ChainID : common . ChainID ( transfer . UsdcGoerli . ChainID ) ,
Address : transfer . UsdcGoerli . Address ,
} }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-06-13 09:25:23 +00:00
require . NoError ( t , err )
// Two MT for which ChainID is ignored and two transfers on the main net and Goerli
require . Equal ( t , 4 , len ( entries ) )
2023-09-06 19:15:07 +00:00
require . Equal ( t , Erc20 , entries [ 0 ] . tokenIn . TokenType )
require . Equal ( t , transfer . UsdcGoerli . Address , entries [ 0 ] . tokenIn . Address )
require . Nil ( t , entries [ 0 ] . tokenOut )
2023-05-11 07:50:07 +00:00
}
2023-09-21 06:58:36 +00:00
func TestGetActivityEntriesFilterByCollectibles ( t * testing . T ) {
deps , close := setupTestActivityDB ( t )
defer close ( )
// Adds 4 extractable transactions 2 transactions (ETH/Goerli, ETH/Optimism), one MT USDC to DAI and another MT USDC to SNT
td , fromTds , toTds := fillTestData ( t , deps . db )
// Add 4 transactions with collectibles
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , 4 )
for i := range trs {
collectibleData := transfer . TestCollectibles [ i ]
trs [ i ] . ChainID = collectibleData . ChainID
transfer . InsertTestTransferWithOptions ( t , deps . db , trs [ i ] . To , & trs [ i ] , & transfer . TestTransferOptions {
TokenAddress : collectibleData . TokenAddress ,
TokenID : collectibleData . TokenID ,
} )
}
allAddresses := append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... )
var filter Filter
filter . FilterOutCollectibles = true
entries , err := getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
require . NoError ( t , err )
require . Equal ( t , 0 , len ( entries ) )
filter . FilterOutCollectibles = false
filter . Collectibles = allTokensFilter ( )
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
require . NoError ( t , err )
require . Equal ( t , 8 , len ( entries ) )
// Search for a specific collectible
filter . Collectibles = [ ] Token { tokenFromCollectible ( & transfer . TestCollectibles [ 0 ] ) }
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
require . Equal ( t , entries [ 0 ] . tokenIn . Address , transfer . TestCollectibles [ 0 ] . TokenAddress )
require . Equal ( t , entries [ 0 ] . tokenIn . TokenID , ( * hexutil . Big ) ( transfer . TestCollectibles [ 0 ] . TokenID ) )
// Search for a specific collectible
filter . Collectibles = [ ] Token { tokenFromCollectible ( & transfer . TestCollectibles [ 3 ] ) }
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
require . Equal ( t , entries [ 0 ] . tokenIn . Address , transfer . TestCollectibles [ 3 ] . TokenAddress )
require . Equal ( t , entries [ 0 ] . tokenIn . TokenID , ( * hexutil . Big ) ( transfer . TestCollectibles [ 3 ] . TokenID ) )
// Search for a multiple collectibles
filter . Collectibles = [ ] Token { tokenFromCollectible ( & transfer . TestCollectibles [ 1 ] ) , tokenFromCollectible ( & transfer . TestCollectibles [ 2 ] ) }
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
require . NoError ( t , err )
require . Equal ( t , 2 , len ( entries ) )
2024-02-22 18:46:21 +00:00
require . Equal ( t , entries [ 0 ] . tokenIn . Address , transfer . TestCollectibles [ 2 ] . TokenAddress )
require . True ( t , ( * big . Int ) ( entries [ 0 ] . tokenIn . TokenID ) . Cmp ( transfer . TestCollectibles [ 2 ] . TokenID ) == 0 )
require . Equal ( t , entries [ 1 ] . tokenIn . Address , transfer . TestCollectibles [ 1 ] . TokenAddress )
require . True ( t , ( * big . Int ) ( entries [ 1 ] . tokenIn . TokenID ) . Cmp ( transfer . TestCollectibles [ 1 ] . TokenID ) == 0 )
2023-09-21 06:58:36 +00:00
}
2023-05-11 07:50:07 +00:00
func TestGetActivityEntriesFilterByToAddresses ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-05-11 07:50:07 +00:00
defer close ( )
// Adds 4 extractable transactions
2023-06-13 09:25:23 +00:00
td , fromTds , toTds := fillTestData ( t , deps . db )
2023-05-11 07:50:07 +00:00
// Add 6 extractable transactions
2023-06-13 09:25:23 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , 6 )
2023-05-11 07:50:07 +00:00
for i := range trs {
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , deps . db , trs [ i ] . To , & trs [ i ] )
2023-05-11 07:50:07 +00:00
}
2023-09-12 10:19:15 +00:00
allAddresses := append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... )
2023-05-28 10:40:50 +00:00
2023-05-11 07:50:07 +00:00
var filter Filter
2023-09-12 10:19:15 +00:00
filter . CounterpartyAddresses = allAddresses
entries , err := getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 10 , len ( entries ) )
2023-07-10 14:56:08 +00:00
filter . CounterpartyAddresses = [ ] eth . Address { eth . HexToAddress ( "0x567890" ) }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 0 , len ( entries ) )
2023-07-10 14:56:08 +00:00
filter . CounterpartyAddresses = [ ] eth . Address { td . pendingTr . To , td . multiTx2 . ToAddress , trs [ 3 ] . To }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 3 , len ( entries ) )
2023-07-10 14:56:08 +00:00
filter . CounterpartyAddresses = [ ] eth . Address { td . tr1 . To , td . pendingTr . From , trs [ 3 ] . From , trs [ 5 ] . To }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 2 , len ( entries ) )
}
2023-07-25 16:03:33 +00:00
2023-05-11 07:50:07 +00:00
func TestGetActivityEntriesFilterByNetworks ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-05-11 07:50:07 +00:00
defer close ( )
// Adds 4 extractable transactions
2023-06-13 09:25:23 +00:00
td , fromTds , toTds := fillTestData ( t , deps . db )
2023-07-25 16:03:33 +00:00
chainToEntryCount := make ( map [ common . ChainID ] map [ int ] int )
recordPresence := func ( chainID common . ChainID , entry int ) {
if _ , ok := chainToEntryCount [ chainID ] ; ! ok {
chainToEntryCount [ chainID ] = make ( map [ int ] int )
chainToEntryCount [ chainID ] [ entry ] = 1
} else {
if _ , ok := chainToEntryCount [ chainID ] [ entry ] ; ! ok {
chainToEntryCount [ chainID ] [ entry ] = 1
} else {
chainToEntryCount [ chainID ] [ entry ] ++
}
}
}
recordPresence ( td . tr1 . ChainID , 0 )
recordPresence ( td . pendingTr . ChainID , 1 )
recordPresence ( td . multiTx1Tr1 . ChainID , 2 )
if td . multiTx1Tr2 . ChainID != td . multiTx1Tr1 . ChainID {
recordPresence ( td . multiTx1Tr2 . ChainID , 2 )
}
recordPresence ( td . multiTx2Tr1 . ChainID , 3 )
if td . multiTx2Tr2 . ChainID != td . multiTx2Tr1 . ChainID {
recordPresence ( td . multiTx2Tr2 . ChainID , 3 )
}
if td . multiTx2PendingTr . ChainID != td . multiTx2Tr1 . ChainID && td . multiTx2PendingTr . ChainID != td . multiTx2Tr2 . ChainID {
recordPresence ( td . multiTx2PendingTr . ChainID , 3 )
}
2023-05-11 07:50:07 +00:00
// Add 6 extractable transactions
2023-06-13 09:25:23 +00:00
trs , fromTrs , toTrs := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , 6 )
2023-05-11 07:50:07 +00:00
for i := range trs {
2023-07-25 16:03:33 +00:00
recordPresence ( trs [ i ] . ChainID , 4 + i )
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , deps . db , trs [ i ] . To , & trs [ i ] )
2023-05-11 07:50:07 +00:00
}
2023-09-12 10:19:15 +00:00
allAddresses := append ( append ( append ( fromTds , toTds ... ) , fromTrs ... ) , toTrs ... )
2023-05-11 07:50:07 +00:00
var filter Filter
chainIDs := allNetworksFilter ( )
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , allAddresses , true , 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-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , chainIDs , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-07-25 16:03:33 +00:00
require . Equal ( t , 0 , len ( entries ) )
2023-05-11 07:50:07 +00:00
2023-06-14 16:10:20 +00:00
chainIDs = [ ] common . ChainID { td . pendingTr . ChainID , td . multiTx2Tr1 . ChainID , trs [ 3 ] . ChainID }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , allAddresses , true , chainIDs , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
2023-07-25 16:03:33 +00:00
expectedResults := make ( map [ int ] int )
for _ , chainID := range chainIDs {
for entry := range chainToEntryCount [ chainID ] {
if _ , ok := expectedResults [ entry ] ; ! ok {
expectedResults [ entry ] ++
}
}
}
require . Equal ( t , len ( expectedResults ) , len ( entries ) )
}
func TestGetActivityEntriesFilterByNetworksOfSubTransactions ( t * testing . T ) {
deps , close := setupTestActivityDB ( t )
defer close ( )
// Add 6 extractable transactions
trs , _ , toTrs := transfer . GenerateTestTransfers ( t , deps . db , 0 , 5 )
trs [ 0 ] . ChainID = 1231
trs [ 1 ] . ChainID = 1232
trs [ 2 ] . ChainID = 1233
mt1 := transfer . GenerateTestBridgeMultiTransaction ( trs [ 0 ] , trs [ 1 ] )
trs [ 0 ] . MultiTransactionID = transfer . InsertTestMultiTransaction ( t , deps . db , & mt1 )
trs [ 1 ] . MultiTransactionID = mt1 . MultiTransactionID
trs [ 2 ] . MultiTransactionID = mt1 . MultiTransactionID
trs [ 3 ] . ChainID = 1234
mt2 := transfer . GenerateTestSwapMultiTransaction ( trs [ 3 ] , testutils . SntSymbol , 100 )
2023-09-06 08:03:50 +00:00
// insertMultiTransaction will insert 0 instead of NULL
mt2 . FromNetworkID = common . NewAndSet ( uint64 ( 0 ) )
mt2 . ToNetworkID = common . NewAndSet ( uint64 ( 0 ) )
2023-07-25 16:03:33 +00:00
trs [ 3 ] . MultiTransactionID = transfer . InsertTestMultiTransaction ( t , deps . db , & mt2 )
for i := range trs {
if i == 2 {
transfer . InsertTestPendingTransaction ( t , deps . db , & trs [ i ] )
} else {
transfer . InsertTestTransfer ( t , deps . db , trs [ i ] . To , & trs [ i ] )
}
}
var filter Filter
chainIDs := allNetworksFilter ( )
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , toTrs , false , chainIDs , filter , 0 , 15 )
2023-07-25 16:03:33 +00:00
require . NoError ( t , err )
require . Equal ( t , 3 , len ( entries ) )
chainIDs = [ ] common . ChainID { trs [ 0 ] . ChainID , trs [ 1 ] . ChainID }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , toTrs , false , chainIDs , filter , 0 , 15 )
2023-07-25 16:03:33 +00:00
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
require . Equal ( t , entries [ 0 ] . id , mt1 . MultiTransactionID )
2023-09-06 08:03:50 +00:00
// Filter by pending_transactions sub-transacitons
2023-07-25 16:03:33 +00:00
chainIDs = [ ] common . ChainID { trs [ 2 ] . ChainID }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , toTrs , false , chainIDs , filter , 0 , 15 )
2023-07-25 16:03:33 +00:00
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
require . Equal ( t , entries [ 0 ] . id , mt1 . MultiTransactionID )
2023-09-06 08:03:50 +00:00
chainIDs = [ ] common . ChainID { trs [ 3 ] . ChainID }
2023-09-12 10:19:15 +00:00
entries , err = getActivityEntries ( context . Background ( ) , deps , toTrs , false , chainIDs , filter , 0 , 15 )
2023-09-06 08:03:50 +00:00
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
require . Equal ( t , entries [ 0 ] . id , mt2 . MultiTransactionID )
2023-05-11 07:50:07 +00:00
}
func TestGetActivityEntriesCheckToAndFrom ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-05-11 07:50:07 +00:00
defer close ( )
2023-07-04 13:53:26 +00:00
// Adds 6 transactions from which 4 are filtered out
2023-06-13 09:25:23 +00:00
td , _ , _ := fillTestData ( t , deps . db )
2023-05-11 07:50:07 +00:00
// Add extra transactions to test To address
2023-06-13 09:25:23 +00:00
trs , _ , _ := transfer . GenerateTestTransfers ( t , deps . db , td . nextIndex , 2 )
2023-06-20 02:50:49 +00:00
transfer . InsertTestTransfer ( t , deps . db , trs [ 0 ] . To , & trs [ 0 ] )
2023-06-13 09:25:23 +00:00
transfer . InsertTestPendingTransaction ( t , deps . db , & trs [ 1 ] )
2023-05-11 07:50:07 +00:00
2023-09-06 19:15:07 +00:00
addresses := [ ] eth . Address { td . tr1 . To , td . pendingTr . To ,
td . multiTx1 . FromAddress , td . multiTx2 . FromAddress , trs [ 0 ] . To , trs [ 1 ] . To }
2023-05-11 07:50:07 +00:00
var filter Filter
2023-09-12 10:19:15 +00:00
entries , err := getActivityEntries ( context . Background ( ) , deps , addresses , false , [ ] common . ChainID { } , filter , 0 , 15 )
2023-05-11 07:50:07 +00:00
require . NoError ( t , err )
require . Equal ( t , 6 , len ( entries ) )
2023-09-06 19:15:07 +00:00
require . Equal ( t , ReceiveAT , entries [ 5 ] . activityType ) // td.tr1
2023-05-11 07:50:07 +00:00
require . NotEqual ( t , eth . Address { } , entries [ 5 ] . transaction . Address ) // td.tr1
2023-07-25 16:03:33 +00:00
require . Equal ( t , td . tr1 . To , * entries [ 5 ] . recipient ) // td.tr1
2023-05-11 07:50:07 +00:00
2023-09-06 19:15:07 +00:00
require . Equal ( t , ReceiveAT , entries [ 4 ] . activityType ) // td.pendingTr
2023-05-11 07:50:07 +00:00
// 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)
}
2023-05-28 10:40:50 +00:00
2023-06-08 23:52:45 +00:00
func TestGetActivityEntriesCheckContextCancellation ( t * testing . T ) {
2023-06-13 09:25:23 +00:00
deps , close := setupTestActivityDB ( t )
2023-06-08 23:52:45 +00:00
defer close ( )
2023-09-12 10:19:15 +00:00
_ , fromAddresses , toAddresses := fillTestData ( t , deps . db )
2023-06-08 23:52:45 +00:00
cancellableCtx , cancelFn := context . WithCancel ( context . Background ( ) )
cancelFn ( )
2023-09-12 10:19:15 +00:00
activities , err := getActivityEntries ( cancellableCtx , deps , append ( fromAddresses , toAddresses ... ) , true , [ ] common . ChainID { } , Filter { } , 0 , 10 )
2023-06-08 23:52:45 +00:00
require . ErrorIs ( t , err , context . Canceled )
require . Equal ( t , 0 , len ( activities ) )
}
2023-07-04 12:01:45 +00:00
func TestGetActivityEntriesNullAddresses ( t * testing . T ) {
deps , close := setupTestActivityDB ( t )
defer close ( )
trs , _ , _ := transfer . GenerateTestTransfers ( t , deps . db , 0 , 4 )
multiTx := transfer . GenerateTestBridgeMultiTransaction ( trs [ 0 ] , trs [ 1 ] )
2023-07-10 14:56:08 +00:00
multiTx . ToAddress = eth . Address { }
2023-07-04 12:01:45 +00:00
trs [ 0 ] . MultiTransactionID = transfer . InsertTestMultiTransaction ( t , deps . db , & multiTx )
trs [ 1 ] . MultiTransactionID = trs [ 0 ] . MultiTransactionID
for i := 0 ; i < 3 ; i ++ {
2023-09-12 10:19:15 +00:00
transfer . InsertTestTransferWithOptions ( t , deps . db , trs [ i ] . From , & trs [ i ] , & transfer . TestTransferOptions {
2023-07-10 14:56:08 +00:00
NullifyAddresses : [ ] eth . Address { trs [ i ] . To } ,
2023-07-04 12:01:45 +00:00
} )
}
2023-07-10 14:56:08 +00:00
trs [ 3 ] . To = eth . Address { }
2023-07-04 12:01:45 +00:00
transfer . InsertTestPendingTransaction ( t , deps . db , & trs [ 3 ] )
2023-09-12 10:19:15 +00:00
addresses := [ ] eth . Address { trs [ 0 ] . From , trs [ 1 ] . From , trs [ 2 ] . From , trs [ 3 ] . From }
2023-07-04 12:01:45 +00:00
2023-09-12 10:19:15 +00:00
activities , err := getActivityEntries ( context . Background ( ) , deps , addresses , false , allNetworksFilter ( ) , Filter { } , 0 , 10 )
2023-07-04 12:01:45 +00:00
require . NoError ( t , err )
require . Equal ( t , 3 , len ( activities ) )
}
2023-07-10 15:00:35 +00:00
2023-09-12 10:19:15 +00:00
func TestGetActivityEntries_ErrorIfNoAddress ( t * testing . T ) {
_ , err := getActivityEntries ( context . Background ( ) , FilterDependencies { } , [ ] eth . Address { } , true , [ ] common . ChainID { } , Filter { } , 0 , 10 )
require . EqualError ( t , err , "no addresses provided" )
}
2023-08-24 12:23:40 +00:00
func TestGetTxDetails ( t * testing . T ) {
deps , close := setupTestActivityDB ( t )
defer close ( )
// Adds 4 extractable transactions 2 transactions (ETH/Goerli, ETH/Optimism), one MT USDC to DAI and another MT USDC to SNT
td , _ , _ := fillTestData ( t , deps . db )
_ , err := getTxDetails ( context . Background ( ) , deps . db , "" )
require . EqualError ( t , err , "invalid tx id" )
details , err := getTxDetails ( context . Background ( ) , deps . db , td . tr1 . Hash . String ( ) )
require . NoError ( t , err )
require . Equal ( t , td . tr1 . Hash . String ( ) , details . ID )
require . Equal ( t , 0 , details . MultiTxID )
require . Equal ( t , td . tr1 . Nonce , details . Nonce )
2023-11-30 11:37:32 +00:00
require . Equal ( t , len ( details . ChainDetails ) , 1 )
require . Equal ( t , td . tr1 . ChainID , common . ChainID ( details . ChainDetails [ 0 ] . ChainID ) )
require . Equal ( t , td . tr1 . BlkNumber , details . ChainDetails [ 0 ] . BlockNumber )
require . Equal ( t , td . tr1 . Hash , details . ChainDetails [ 0 ] . Hash )
require . Equal ( t , td . tr1 . Contract , * details . ChainDetails [ 0 ] . Contract )
2023-08-24 12:23:40 +00:00
}
func TestGetMultiTxDetails ( t * testing . T ) {
deps , close := setupTestActivityDB ( t )
defer close ( )
// Adds 4 extractable transactions 2 transactions (ETH/Goerli, ETH/Optimism), one MT USDC to DAI and another MT USDC to SNT
td , _ , _ := fillTestData ( t , deps . db )
_ , err := getMultiTxDetails ( context . Background ( ) , deps . db , 0 )
require . EqualError ( t , err , "invalid tx id" )
details , err := getMultiTxDetails ( context . Background ( ) , deps . db , int ( td . multiTx1 . MultiTransactionID ) )
require . NoError ( t , err )
require . Equal ( t , "" , details . ID )
require . Equal ( t , int ( td . multiTx1 . MultiTransactionID ) , details . MultiTxID )
require . Equal ( t , td . multiTx1Tr2 . Nonce , details . Nonce )
2023-11-30 11:37:32 +00:00
require . Equal ( t , 2 , len ( details . ChainDetails ) )
require . Equal ( t , td . multiTx1Tr1 . ChainID , common . ChainID ( details . ChainDetails [ 0 ] . ChainID ) )
require . Equal ( t , td . multiTx1Tr1 . BlkNumber , details . ChainDetails [ 0 ] . BlockNumber )
require . Equal ( t , td . multiTx1Tr1 . Hash , details . ChainDetails [ 0 ] . Hash )
require . Equal ( t , td . multiTx1Tr1 . Contract , * details . ChainDetails [ 0 ] . Contract )
require . Equal ( t , td . multiTx1Tr2 . ChainID , common . ChainID ( details . ChainDetails [ 1 ] . ChainID ) )
require . Equal ( t , td . multiTx1Tr2 . BlkNumber , details . ChainDetails [ 1 ] . BlockNumber )
require . Equal ( t , td . multiTx1Tr2 . Hash , details . ChainDetails [ 1 ] . Hash )
require . Equal ( t , td . multiTx1Tr2 . Contract , * details . ChainDetails [ 1 ] . Contract )
2023-08-24 12:23:40 +00:00
}
2024-01-24 13:00:08 +00:00
func TestGetActivityEntriesSkipEthGasFeeOnlyTransfers ( t * testing . T ) {
deps , close := setupTestActivityDB ( t )
defer close ( )
to := eth . Address { 0x1 }
from := eth . Address { 0x2 }
hash := eth . Hash { 0x3 }
blkNum := int64 ( 1 )
chainID := common . ChainID ( 1 )
nonce := uint64 ( 1 )
// Insert 0-value gas-only ETH transfer as a result of token transfer's gas fee
transfer . InsertTestTransfer ( t , deps . db , to , & transfer . TestTransfer {
TestTransaction : transfer . TestTransaction {
ChainID : chainID ,
From : from ,
Hash : hash ,
BlkNumber : blkNum ,
Nonce : nonce ,
} ,
To : to ,
Value : 0 ,
} )
entries , err := getActivityEntries ( context . Background ( ) , deps , [ ] eth . Address { to } , true , [ ] common . ChainID { chainID } , Filter { } , 0 , 10 )
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
require . Equal ( t , hash , entries [ 0 ] . transaction . Hash )
// Insert token transfer
transfer . InsertTestTransferWithOptions ( t , deps . db , to ,
& transfer . TestTransfer {
TestTransaction : transfer . TestTransaction {
ChainID : chainID ,
From : from ,
Hash : hash ,
BlkNumber : blkNum ,
Nonce : nonce ,
} ,
To : to ,
Value : 1 ,
} ,
& transfer . TestTransferOptions {
TokenAddress : eth . Address { 0x4 } ,
} ,
)
// Gas-fee-only transfer should be removed, so we get only 1 transfer again
entries , err = getActivityEntries ( context . Background ( ) , deps , [ ] eth . Address { to } , true , [ ] common . ChainID { chainID } , Filter { } , 0 , 10 )
require . NoError ( t , err )
require . Equal ( t , 1 , len ( entries ) )
require . Equal ( t , contractTypeFromDBType ( "erc20" ) , entries [ 0 ] . transferType )
// Insert real 0-value ETH transfer
transfer . InsertTestTransfer ( t , deps . db , to , & transfer . TestTransfer {
TestTransaction : transfer . TestTransaction {
ChainID : chainID ,
From : from ,
Hash : eth . Hash { 0x5 } , // another hash
BlkNumber : blkNum ,
Nonce : nonce + 1 , // another nonce
} ,
To : to ,
Value : 0 , // 0-value as well
} )
entries , err = getActivityEntries ( context . Background ( ) , deps , [ ] eth . Address { to } , true , [ ] common . ChainID { chainID } , Filter { } , 0 , 10 )
require . NoError ( t , err )
require . Equal ( t , 2 , len ( entries ) )
}