Repository: trustwallet/blockatlas
Pull request #995 (2020-03-31 20:08:36)

Fix Tezos Delegation direction
Fixes https://github.com/trustwallet/blockatlas/issues/994


Patch:
diff --git a/platform/tezos/model.go b/platform/tezos/model.go
index 451b39c..836b4fb 100644
--- a/platform/tezos/model.go
+++ b/platform/tezos/model.go
@@ -67,16 +67,18 @@ func (t *Transaction) ErrorMsg() string {
 	}
 }
 
-func (t *Transaction) Title() blockatlas.KeyTitle {
-	if t.Delegate == "" && t.Receiver != "" {
-		return blockatlas.AnyActionDelegation
+func (t *Transaction) Title(address string) (blockatlas.KeyTitle, bool) {
+	if t.Type == TxTypeDelegation {
+		if address == t.Sender && t.Delegate != "" && t.Receiver == "" {
+			return blockatlas.AnyActionDelegation, true
+		}
+
+		if address == t.Sender && t.Delegate == "" && t.Receiver != "" {
+			return blockatlas.AnyActionUndelegation, true
+		}
 	}
 
-	if t.Delegate != "" && t.Receiver == "" {
-		return blockatlas.AnyActionUndelegation
-	}
-
-	return blockatlas.AnyActionDelegation
+	return "unsupported title", false
 }
 
 func (t *Transaction) BlockTimestamp() int64 {
@@ -88,14 +90,14 @@ func (t *Transaction) BlockTimestamp() int64 {
 	return unix
 }
 
-func (t *Transaction) TransferType() blockatlas.TransactionType {
+func (t *Transaction) TransferType() (blockatlas.TransactionType, bool) {
 	switch t.Type {
 	case TxTypeTransaction:
-		return blockatlas.TxTransfer
+		return blockatlas.TxTransfer, true
 	case TxTypeDelegation:
-		return blockatlas.TxAnyAction
+		return blockatlas.TxAnyAction, true
 	default:
-		return ""
+		return "unsupported type", false
 	}
 }
 
diff --git a/platform/tezos/transaction.go b/platform/tezos/transaction.go
index 73347a0..023e84e 100644
--- a/platform/tezos/transaction.go
+++ b/platform/tezos/transaction.go
@@ -31,7 +31,13 @@ func NormalizeTxs(srcTxs []Transaction, address string) (txs []blockatlas.Tx) {
 
 // NormalizeTx converts a Tezos transaction into the generic model
 func NormalizeTx(srcTx Transaction, address string) (blockatlas.Tx, bool) {
-	var tx = blockatlas.Tx{
+	var tx blockatlas.Tx
+	tt, ok := srcTx.TransferType()
+	if !ok {
+		return tx, false
+	}
+
+	tx = blockatlas.Tx{
 		Block:  srcTx.Height,
 		Coin:   coin.XTZ,
 		Date:   srcTx.BlockTimestamp(),
@@ -41,18 +47,22 @@ func NormalizeTx(srcTx Transaction, address string) (blockatlas.Tx, bool) {
 		ID:     srcTx.Hash,
 		Status: srcTx.Status(),
 		To:     srcTx.GetReceiver(),
-		Type:   srcTx.TransferType(),
+		Type:   tt,
 	}
 	if address != "" {
 		tx.Direction = srcTx.Direction(address)
 	}
 
 	value := blockatlas.Amount(numbers.DecimalExp(numbers.Float64toString(srcTx.Volume), 6))
-	switch srcTx.TransferType() {
+	switch tt {
 	case blockatlas.TxAnyAction:
+		title, ok := srcTx.Title(address)
+		if !ok {
+			return tx, false
+		}
 		tx.Meta = blockatlas.AnyAction{
 			Coin:     coin.Tezos().ID,
-			Title:    srcTx.Title(),
+			Title:    title,
 			Key:      blockatlas.KeyStakeDelegate,
 			Name:     coin.Tezos().Name,
 			Symbol:   coin.Tezos().Symbol,


Test patch:
diff --git a/platform/tezos/model_test.go b/platform/tezos/model_test.go
index 7243c1a..153adfc 100644
--- a/platform/tezos/model_test.go
+++ b/platform/tezos/model_test.go
@@ -45,19 +45,23 @@ func TestTransaction_Status(t *testing.T) {
 	}
 
 	testsTitle := []struct {
-		name string
-		in   Transaction
-		out  blockatlas.KeyTitle
+		name    string
+		address string
+		in      Transaction
+		out     blockatlas.KeyTitle
 	}{
-		{"Delegation", Transaction{Delegate: "", Receiver: addr1}, blockatlas.AnyActionDelegation},
-		{"Undelegation", Transaction{Delegate: addr1, Receiver: ""}, blockatlas.AnyActionUndelegation},
-		{"Delegation", Transaction{Delegate: "", Receiver: ""}, blockatlas.AnyActionDelegation},
-		{"Delegation", Transaction{Delegate: addr1, Receiver: addr1}, blockatlas.AnyActionDelegation},
+		{"Delegation title", addr1, Transaction{Sender: addr1, Delegate: addr2, Receiver: "", Type: TxTypeDelegation}, blockatlas.AnyActionDelegation},
+		{"Undelegation title", addr1, Transaction{Sender: addr1, Delegate: "", Receiver: addr2, Type: TxTypeDelegation}, blockatlas.AnyActionUndelegation},
+		{"Unsupported title", addr1, Transaction{Sender: addr1, Delegate: addr1, Receiver: addr1}, "unsupported title"},
+		{"Unsupported title", addr1, Transaction{Sender: addr1, Delegate: addr2, Receiver: addr1}, "unsupported title"},
+		{"Unsupported title", addr1, Transaction{Sender: addr1, Delegate: addr1, Receiver: addr2}, "unsupported title"},
+		{"Unsupported title", addr1, Transaction{Sender: addr1, Delegate: addr2, Receiver: addr2}, "unsupported title"},
 	}
 
 	for _, tt := range testsTitle {
 		t.Run(tt.name, func(t *testing.T) {
-			assert.Equal(t, tt.out, tt.in.Title())
+			title, _ := tt.in.Title(tt.address)
+			assert.Equal(t, tt.out, title)
 		})
 	}
 
@@ -75,20 +79,20 @@ func TestTransaction_Status(t *testing.T) {
 		})
 	}
 
-	testsKind := []struct {
+	testsTransferType := []struct {
 		name string
 		in   Transaction
 		out  blockatlas.TransactionType
 	}{
-		{"Type should be transaction", Transaction{Type: "transaction"}, blockatlas.TxTransfer},
-		{"Type should be delegation", Transaction{Type: "delegation"}, blockatlas.TxAnyAction},
-		{"Type unsupported", Transaction{Type: "bake"}, ""},
-		{"Type endorsement", Transaction{Type: "endorsement"}, ""},
+		{"Type should be transaction", Transaction{Type: "transaction",}, blockatlas.TxTransfer},
+		{"Type should be delegation", Transaction{Type: "delegation",}, blockatlas.TxAnyAction},
+		{"Type unsupported", Transaction{Type: "bake"}, "unsupported type"},
 	}
 
-	for _, tt := range testsKind {
+	for _, tt := range testsTransferType {
 		t.Run(tt.name, func(t *testing.T) {
-			assert.Equal(t, tt.out, tt.in.TransferType())
+			transferType, _ := tt.in.TransferType()
+			assert.Equal(t, tt.out, transferType)
 		})
 	}