@@ -18,6 +18,7 @@ package sql_test
1818
1919import (
2020 "context"
21+ "math/big"
2122 "testing"
2223
2324 "github.com/ipfs/go-cid"
@@ -35,15 +36,20 @@ import (
3536 "github.com/ethereum/go-ethereum/statediff/indexer/models"
3637 "github.com/ethereum/go-ethereum/statediff/indexer/shared"
3738 "github.com/ethereum/go-ethereum/statediff/indexer/test_helpers"
39+ sdtypes "github.com/ethereum/go-ethereum/statediff/types"
3840)
3941
40- func setupPGX (t * testing.T ) {
42+ func setupPGXIndexer (t * testing.T ) {
4143 db , err = postgres .SetupPGXDB ()
4244 if err != nil {
4345 t .Fatal (err )
4446 }
4547 ind , err = sql .NewStateDiffIndexer (context .Background (), mocks .TestConfig , db )
4648 require .NoError (t , err )
49+ }
50+
51+ func setupPGX (t * testing.T ) {
52+ setupPGXIndexer (t )
4753 var tx interfaces.Batch
4854 tx , err = ind .PushBlock (
4955 mockBlock ,
@@ -557,3 +563,298 @@ func TestPGXIndexer(t *testing.T) {
557563 test_helpers .ExpectEqual (t , data , []byte {})
558564 })
559565}
566+
567+ func TestPGXWatchAddressMethods (t * testing.T ) {
568+ setupPGXIndexer (t )
569+ defer tearDown (t )
570+ defer checkTxClosure (t , 1 , 0 , 1 )
571+
572+ type res struct {
573+ Address string `db:"address"`
574+ CreatedAt uint64 `db:"created_at"`
575+ WatchedAt uint64 `db:"watched_at"`
576+ LastFilledAt uint64 `db:"last_filled_at"`
577+ }
578+ pgStr := "SELECT * FROM eth_meta.watched_addresses"
579+
580+ t .Run ("Insert watched addresses" , func (t * testing.T ) {
581+ args := []sdtypes.WatchAddressArg {
582+ {
583+ Address : contract1Address ,
584+ CreatedAt : contract1CreatedAt ,
585+ },
586+ {
587+ Address : contract2Address ,
588+ CreatedAt : contract2CreatedAt ,
589+ },
590+ }
591+ expectedData := []res {
592+ {
593+ Address : contract1Address ,
594+ CreatedAt : contract1CreatedAt ,
595+ WatchedAt : watchedAt1 ,
596+ LastFilledAt : lastFilledAt ,
597+ },
598+ {
599+ Address : contract2Address ,
600+ CreatedAt : contract2CreatedAt ,
601+ WatchedAt : watchedAt1 ,
602+ LastFilledAt : lastFilledAt ,
603+ },
604+ }
605+
606+ ind .InsertWatchedAddresses (args , big .NewInt (int64 (watchedAt1 )))
607+
608+ rows := []res {}
609+ err = db .Select (context .Background (), & rows , pgStr )
610+ if err != nil {
611+ t .Fatal (err )
612+ }
613+
614+ expectTrue (t , len (rows ) == len (expectedData ))
615+ for idx , row := range rows {
616+ test_helpers .ExpectEqual (t , row , expectedData [idx ])
617+ }
618+ })
619+
620+ t .Run ("Insert watched addresses (some already watched)" , func (t * testing.T ) {
621+ args := []sdtypes.WatchAddressArg {
622+ {
623+ Address : contract3Address ,
624+ CreatedAt : contract3CreatedAt ,
625+ },
626+ {
627+ Address : contract2Address ,
628+ CreatedAt : contract2CreatedAt ,
629+ },
630+ }
631+ expectedData := []res {
632+ {
633+ Address : contract1Address ,
634+ CreatedAt : contract1CreatedAt ,
635+ WatchedAt : watchedAt1 ,
636+ LastFilledAt : lastFilledAt ,
637+ },
638+ {
639+ Address : contract2Address ,
640+ CreatedAt : contract2CreatedAt ,
641+ WatchedAt : watchedAt1 ,
642+ LastFilledAt : lastFilledAt ,
643+ },
644+ {
645+ Address : contract3Address ,
646+ CreatedAt : contract3CreatedAt ,
647+ WatchedAt : watchedAt2 ,
648+ LastFilledAt : lastFilledAt ,
649+ },
650+ }
651+
652+ ind .InsertWatchedAddresses (args , big .NewInt (int64 (watchedAt2 )))
653+
654+ rows := []res {}
655+ err = db .Select (context .Background (), & rows , pgStr )
656+ if err != nil {
657+ t .Fatal (err )
658+ }
659+
660+ expectTrue (t , len (rows ) == len (expectedData ))
661+ for idx , row := range rows {
662+ test_helpers .ExpectEqual (t , row , expectedData [idx ])
663+ }
664+ })
665+
666+ t .Run ("Remove watched addresses" , func (t * testing.T ) {
667+ args := []sdtypes.WatchAddressArg {
668+ {
669+ Address : contract3Address ,
670+ CreatedAt : contract3CreatedAt ,
671+ },
672+ {
673+ Address : contract2Address ,
674+ CreatedAt : contract2CreatedAt ,
675+ },
676+ }
677+ expectedData := []res {
678+ {
679+ Address : contract1Address ,
680+ CreatedAt : contract1CreatedAt ,
681+ WatchedAt : watchedAt1 ,
682+ LastFilledAt : lastFilledAt ,
683+ },
684+ }
685+
686+ ind .RemoveWatchedAddresses (args )
687+
688+ rows := []res {}
689+ err = db .Select (context .Background (), & rows , pgStr )
690+ if err != nil {
691+ t .Fatal (err )
692+ }
693+
694+ expectTrue (t , len (rows ) == len (expectedData ))
695+ for idx , row := range rows {
696+ test_helpers .ExpectEqual (t , row , expectedData [idx ])
697+ }
698+ })
699+
700+ t .Run ("Remove watched addresses (some non-watched)" , func (t * testing.T ) {
701+ args := []sdtypes.WatchAddressArg {
702+ {
703+ Address : contract1Address ,
704+ CreatedAt : contract1CreatedAt ,
705+ },
706+ {
707+ Address : contract2Address ,
708+ CreatedAt : contract2CreatedAt ,
709+ },
710+ }
711+ expectedData := []res {}
712+
713+ ind .RemoveWatchedAddresses (args )
714+
715+ rows := []res {}
716+ err = db .Select (context .Background (), & rows , pgStr )
717+ if err != nil {
718+ t .Fatal (err )
719+ }
720+
721+ expectTrue (t , len (rows ) == len (expectedData ))
722+ for idx , row := range rows {
723+ test_helpers .ExpectEqual (t , row , expectedData [idx ])
724+ }
725+ })
726+
727+ t .Run ("Set watched addresses" , func (t * testing.T ) {
728+ args := []sdtypes.WatchAddressArg {
729+ {
730+ Address : contract1Address ,
731+ CreatedAt : contract1CreatedAt ,
732+ },
733+ {
734+ Address : contract2Address ,
735+ CreatedAt : contract2CreatedAt ,
736+ },
737+ {
738+ Address : contract3Address ,
739+ CreatedAt : contract3CreatedAt ,
740+ },
741+ }
742+ expectedData := []res {
743+ {
744+ Address : contract1Address ,
745+ CreatedAt : contract1CreatedAt ,
746+ WatchedAt : watchedAt2 ,
747+ LastFilledAt : lastFilledAt ,
748+ },
749+ {
750+ Address : contract2Address ,
751+ CreatedAt : contract2CreatedAt ,
752+ WatchedAt : watchedAt2 ,
753+ LastFilledAt : lastFilledAt ,
754+ },
755+ {
756+ Address : contract3Address ,
757+ CreatedAt : contract3CreatedAt ,
758+ WatchedAt : watchedAt2 ,
759+ LastFilledAt : lastFilledAt ,
760+ },
761+ }
762+
763+ ind .SetWatchedAddresses (args , big .NewInt (int64 (watchedAt2 )))
764+
765+ rows := []res {}
766+ err = db .Select (context .Background (), & rows , pgStr )
767+ if err != nil {
768+ t .Fatal (err )
769+ }
770+
771+ expectTrue (t , len (rows ) == len (expectedData ))
772+ for idx , row := range rows {
773+ test_helpers .ExpectEqual (t , row , expectedData [idx ])
774+ }
775+ })
776+
777+ t .Run ("Set watched addresses (some already watched)" , func (t * testing.T ) {
778+ args := []sdtypes.WatchAddressArg {
779+ {
780+ Address : contract4Address ,
781+ CreatedAt : contract4CreatedAt ,
782+ },
783+ {
784+ Address : contract2Address ,
785+ CreatedAt : contract2CreatedAt ,
786+ },
787+ {
788+ Address : contract3Address ,
789+ CreatedAt : contract3CreatedAt ,
790+ },
791+ }
792+ expectedData := []res {
793+ {
794+ Address : contract4Address ,
795+ CreatedAt : contract4CreatedAt ,
796+ WatchedAt : watchedAt3 ,
797+ LastFilledAt : lastFilledAt ,
798+ },
799+ {
800+ Address : contract2Address ,
801+ CreatedAt : contract2CreatedAt ,
802+ WatchedAt : watchedAt3 ,
803+ LastFilledAt : lastFilledAt ,
804+ },
805+ {
806+ Address : contract3Address ,
807+ CreatedAt : contract3CreatedAt ,
808+ WatchedAt : watchedAt3 ,
809+ LastFilledAt : lastFilledAt ,
810+ },
811+ }
812+
813+ ind .SetWatchedAddresses (args , big .NewInt (int64 (watchedAt3 )))
814+
815+ rows := []res {}
816+ err = db .Select (context .Background (), & rows , pgStr )
817+ if err != nil {
818+ t .Fatal (err )
819+ }
820+
821+ expectTrue (t , len (rows ) == len (expectedData ))
822+ for idx , row := range rows {
823+ test_helpers .ExpectEqual (t , row , expectedData [idx ])
824+ }
825+ })
826+
827+ t .Run ("Clear watched addresses" , func (t * testing.T ) {
828+ expectedData := []res {}
829+
830+ ind .ClearWatchedAddresses ()
831+
832+ rows := []res {}
833+ err = db .Select (context .Background (), & rows , pgStr )
834+ if err != nil {
835+ t .Fatal (err )
836+ }
837+
838+ expectTrue (t , len (rows ) == len (expectedData ))
839+ for idx , row := range rows {
840+ test_helpers .ExpectEqual (t , row , expectedData [idx ])
841+ }
842+ })
843+
844+ t .Run ("Clear watched addresses (empty table)" , func (t * testing.T ) {
845+ expectedData := []res {}
846+
847+ ind .ClearWatchedAddresses ()
848+
849+ rows := []res {}
850+ err = db .Select (context .Background (), & rows , pgStr )
851+ if err != nil {
852+ t .Fatal (err )
853+ }
854+
855+ expectTrue (t , len (rows ) == len (expectedData ))
856+ for idx , row := range rows {
857+ test_helpers .ExpectEqual (t , row , expectedData [idx ])
858+ }
859+ })
860+ }
0 commit comments