Skip to content

Commit ce4a993

Browse files
committed
fix: standardize memo type handling in Stellar transactions
- Updated tests and implementation to use the new MemoType type for memoType fields in Stellar transactions. - Replaced string comparisons with MemoType constants to ensure consistency and type safety across the codebase. - Adjusted related tests to reflect the changes in memoType assertions.
1 parent eb30c77 commit ce4a993

5 files changed

Lines changed: 33 additions & 13 deletions

File tree

internal/indexer/stellar.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,11 @@ func (s *StellarIndexer) convertPayment(
428428

429429
fee := decimal.Zero
430430
memo := ""
431-
memoType := ""
431+
var memoType types.MemoType
432432
if txDetail != nil {
433433
fee = stroopsToXLM(strings.TrimSpace(txDetail.FeeCharged))
434434
memo = strings.TrimSpace(txDetail.Memo)
435-
memoType = strings.TrimSpace(txDetail.MemoType)
435+
memoType = types.MemoType(strings.TrimSpace(txDetail.MemoType))
436436
}
437437

438438
tx := types.Transaction{
@@ -700,7 +700,7 @@ func (s *StellarIndexer) newOperationTransaction(
700700
tx.Memo = memo
701701
}
702702
if memoType := strings.TrimSpace(txDetail.MemoType); memoType != "" {
703-
tx.MemoType = memoType
703+
tx.MemoType = types.MemoType(memoType)
704704
}
705705
}
706706
return tx

internal/indexer/stellar_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func TestStellarGetBlock_ParsesNativeAndIssuedPaymentsWithMemo(t *testing.T) {
374374
memo := nativeTx.Memo
375375
assert.Equal(t, "memo-1", memo)
376376
memoType := nativeTx.MemoType
377-
assert.Equal(t, "text", memoType)
377+
assert.Equal(t, types.MemoTypeText, memoType)
378378
assert.Equal(t, "0.00001", nativeTx.TxFee.String())
379379

380380
tokenTx := block.Transactions[1]
@@ -1094,7 +1094,7 @@ func TestStellarMainnetFetchAndParseTransactions(t *testing.T) {
10941094
require.NotNil(t, payment)
10951095
assert.Equal(t, formatStellarAsset(payment.AssetIssuer, payment.AssetCode), tx.AssetAddress)
10961096
assert.Equal(t, "pspb:4441859", tx.Memo)
1097-
assert.Equal(t, "text", tx.MemoType)
1097+
assert.Equal(t, types.MemoTypeText, tx.MemoType)
10981098
},
10991099
},
11001100
{
@@ -1348,6 +1348,6 @@ func TestStellarMainnetBatchPaymentProducesMultipleTransactions(t *testing.T) {
13481348
for _, tx := range converted {
13491349
assert.Equal(t, batchTxHash, tx.TxHash)
13501350
assert.Equal(t, "pspb:4441859", tx.Memo)
1351-
assert.Equal(t, "text", tx.MemoType)
1351+
assert.Equal(t, types.MemoTypeText, tx.MemoType)
13521352
}
13531353
}

internal/worker/base_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func TestBaseWorkerEmitBlock_AppliesNativeOutgoingOverrides(t *testing.T) {
196196
Amount: "5.0000000",
197197
Type: constant.TxTypeTokenTransfer,
198198
AssetAddress: "GISSUER:USDC",
199-
MemoType: "text",
199+
MemoType: types.MemoTypeText,
200200
Metadata: map[string]any{
201201
testSourceTypeKey: string(constant.TxTypeNativeTransfer),
202202
testSourceAmountKey: "7.5000000",
@@ -217,7 +217,7 @@ func TestBaseWorkerEmitBlock_AppliesNativeOutgoingOverrides(t *testing.T) {
217217
if outTx.Type != constant.TxTypeNativeTransfer || outTx.Amount != "7.5000000" || outTx.AssetAddress != "" {
218218
t.Fatalf("outgoing native override should clear receiver-side asset, got type=%s amount=%s asset=%s", outTx.Type, outTx.Amount, outTx.AssetAddress)
219219
}
220-
if outTx.MemoType != "text" {
220+
if outTx.MemoType != types.MemoTypeText {
221221
t.Fatalf("outgoing tx should preserve unrelated metadata")
222222
}
223223
if outTx.GetMetadataString(testSourceTypeKey) != "" || outTx.GetMetadataString(testSourceAmountKey) != "" || outTx.GetMetadataString(testSourceAssetKey) != "" {

pkg/common/types/types.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ import (
1111
"github.com/shopspring/decimal"
1212
)
1313

14+
// MemoType represents a chain-specific memo type.
15+
//
16+
// For Stellar this maps to the underlying XDR memo type:
17+
// - "none" -> MEMO_NONE
18+
// - "text" -> MEMO_TEXT
19+
// - "id" -> MEMO_ID
20+
// - "hash" -> MEMO_HASH
21+
// - "return" -> MEMO_RETURN (32-byte refund hash)
22+
//
23+
// Other chains may reuse these values or leave MemoType empty.
24+
type MemoType string
25+
26+
const (
27+
MemoTypeNone MemoType = "none"
28+
MemoTypeText MemoType = "text"
29+
MemoTypeID MemoType = "id"
30+
MemoTypeHash MemoType = "hash"
31+
MemoTypeReturn MemoType = "return"
32+
)
33+
1434
type Block struct {
1535
Number uint64 `json:"number"`
1636
Hash string `json:"hash"`
@@ -60,7 +80,7 @@ type Transaction struct {
6080
Direction string `json:"direction"` // "in" (deposit) or "out" (withdrawal)
6181
DestinationTag string `json:"destinationTag,omitempty"`
6282
Memo string `json:"memo,omitempty"`
63-
MemoType string `json:"memoType,omitempty"`
83+
MemoType MemoType `json:"memoType,omitempty"`
6484
Metadata map[string]any `json:"metadata,omitempty"`
6585
}
6686

@@ -192,7 +212,7 @@ func (t Transaction) Hash() string {
192212
builder.WriteByte('|')
193213
builder.WriteString(memo)
194214
}
195-
if memoType := strings.TrimSpace(t.MemoType); memoType != "" {
215+
if memoType := strings.TrimSpace(string(t.MemoType)); memoType != "" {
196216
builder.WriteByte('|')
197217
builder.WriteString(memoType)
198218
}

pkg/common/types/types_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestTransactionBinaryRoundTripPreservesRoutingFields(t *testing.T) {
2828
}
2929
tx.DestinationTag = "12345"
3030
tx.Memo = "invoice-42"
31-
tx.MemoType = "text"
31+
tx.MemoType = MemoTypeText
3232
tx.SetMetadata("custom_routing_note", "internal")
3333

3434
data, err := tx.MarshalBinary()
@@ -38,7 +38,7 @@ func TestTransactionBinaryRoundTripPreservesRoutingFields(t *testing.T) {
3838
require.NoError(t, decoded.UnmarshalBinary(data))
3939
require.Equal(t, "12345", decoded.DestinationTag)
4040
require.Equal(t, "invoice-42", decoded.Memo)
41-
require.Equal(t, "text", decoded.MemoType)
41+
require.Equal(t, MemoTypeText, decoded.MemoType)
4242
require.Equal(t, "internal", decoded.GetMetadataString("custom_routing_note"))
4343
}
4444

@@ -59,7 +59,7 @@ func TestTransactionHashIncludesRoutingFields(t *testing.T) {
5959

6060
withMemo := base
6161
withMemo.Memo = "memo"
62-
withMemo.MemoType = "text"
62+
withMemo.MemoType = MemoTypeText
6363

6464
require.NotEqual(t, base.Hash(), withTag.Hash())
6565
require.NotEqual(t, base.Hash(), withMemo.Hash())

0 commit comments

Comments
 (0)