Repository: trustwallet/blockatlas
Pull request #951 (2020-03-03 02:50:21)

[Observer] Fix getDirection bug for TokenTransfer and NativeTokenTransfer


Patch:
diff --git a/observer/observer.go b/observer/observer.go
index a9b076e..658ec8f 100644
--- a/observer/observer.go
+++ b/observer/observer.go
@@ -88,8 +88,19 @@ func getDirection(tx blockatlas.Tx, address string) blockatlas.Direction {
 		addressSet := mapset.NewSet(address)
 		return bitcoin.InferDirection(&tx, addressSet)
 	}
-	if address == tx.To {
-		if tx.From == tx.To {
+	switch meta := tx.Meta.(type) {
+	case *blockatlas.TokenTransfer:
+		return determineDirection(address, meta.From, meta.To)
+	case *blockatlas.NativeTokenTransfer:
+		return determineDirection(address, meta.From, meta.To)
+	default:
+		return determineDirection(address, tx.From, tx.To)
+	}
+}
+
+func determineDirection(address, from, to string) blockatlas.Direction {
+	if address == to {
+		if from == to {
 			return blockatlas.DirectionSelf
 		}
 		return blockatlas.DirectionIncoming


Test patch:
diff --git a/observer/observer_test.go b/observer/observer_test.go
index c01027a..91c4c32 100644
--- a/observer/observer_test.go
+++ b/observer/observer_test.go
@@ -171,6 +171,66 @@ func Test_getDirection(t *testing.T) {
 				}, "3BMEXVshYmWqc8qcQLyBQPgRgAPfogWdJ4",
 			}, blockatlas.DirectionIncoming,
 		},
+		{"Test NativeTokenTransfer Direction Self",
+			args{
+				blockatlas.Tx{
+					Meta: &blockatlas.NativeTokenTransfer{
+						From:     "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+						To:       "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+					},
+				}, "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+			}, blockatlas.DirectionSelf,
+		},
+		{"Test NativeTokenTransfer Direction Outgoing",
+			args{
+				blockatlas.Tx{
+					Meta: &blockatlas.NativeTokenTransfer{
+						From:     "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+						To:       "0x74c8199372c584DAB8b14c519bc8BC8C622F37b7",
+					},
+				}, "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+			}, blockatlas.DirectionOutgoing,
+		},
+		{"Test NativeTokenTransfer Direction Incoming",
+			args{
+				blockatlas.Tx{
+					Meta: &blockatlas.NativeTokenTransfer{
+						From:     "0x74c8199372c584DAB8b14c519bc8BC8C622F37b7",
+						To:       "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+					},
+				}, "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+			}, blockatlas.DirectionIncoming,
+		},
+		{"Test TokenTransfer Direction Self",
+			args{
+				blockatlas.Tx{
+					Meta: &blockatlas.TokenTransfer{
+						From:     "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+						To:       "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+					},
+				}, "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+			}, blockatlas.DirectionSelf,
+		},
+		{"Test TokenTransfer Direction Outgoing",
+			args{
+				blockatlas.Tx{
+					Meta: &blockatlas.TokenTransfer{
+						From:     "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+						To:       "0x74c8199372c584DAB8b14c519bc8BC8C622F37b7",
+					},
+				}, "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+			}, blockatlas.DirectionOutgoing,
+		},
+		{"Test TokenTransfer Direction Incoming",
+			args{
+				blockatlas.Tx{
+					Meta: &blockatlas.TokenTransfer{
+						From:     "0x74c8199372c584DAB8b14c519bc8BC8C622F37b7",
+						To:       "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+					},
+				}, "0x5574Cd97432cEd0D7Caf58ac3c4fEDB2061C98fB",
+			}, blockatlas.DirectionIncoming,
+		},
 	}
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {