Skip to content

Commit 17eccbb

Browse files
committed
refactor: use *uint64 for IntendedReceivingAmount to store NULL for non-payments
Non-payment transactions now store NULL instead of 0, distinguishing "not a payment" from "zero-drop payment". Also fix stale doc comment on transactionStatus, add doc comment to TransactionStatus field, and remove duplicate DestinationAddressHash assignment.
1 parent ceda142 commit 17eccbb

5 files changed

Lines changed: 28 additions & 20 deletions

File tree

internal/xrp/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ func (c *xrpClient) GetBlockResult(ctx context.Context, blockNum uint64) (*index
190190
return nil, fmt.Errorf("isNativePayment: %w", err)
191191
}
192192
transactions[i].IsNativePayment = isNative
193-
transactions[i].DestinationAddressHash = tx.destinationAddressHash()
194193

195194
transactions[i].IntendedReceivingAmount, err = tx.intendedReceivingAmount()
196195
if err != nil {

internal/xrp/entities.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import (
66

77
// Transaction is the database model for an XRP ledger payment transaction.
88
type Transaction struct {
9-
Hash string `gorm:"primaryKey;type:varchar(64)"`
10-
BlockNumber uint64 `gorm:"index;index:idx_payref_block,priority:2;index:idx_dest_block,priority:2"`
11-
Timestamp uint64 `gorm:"index"`
9+
Hash string `gorm:"primaryKey;type:varchar(64)"`
10+
BlockNumber uint64 `gorm:"index;index:idx_payref_block,priority:2;index:idx_dest_block,priority:2"`
11+
Timestamp uint64 `gorm:"index"`
12+
// TransactionStatus is true when the transaction result is tesSUCCESS or a
13+
// destination-related failure (tecDST_TAG_NEEDED, tecNO_DST, tecNO_DST_INSUF_XRP,
14+
// tecNO_PERMISSION), and false for all other results.
1215
TransactionStatus bool `gorm:"index"`
1316
PaymentReference string `gorm:"index:idx_payref_block,priority:1;type:varchar(64);default:null"`
1417
Response string `gorm:"type:varchar"`
1518
IsNativePayment bool `gorm:"index"`
16-
IntendedReceivingAmount uint64 `gorm:"index:idx_payref_block,priority:3;index:idx_dest_block,priority:3"`
19+
IntendedReceivingAmount *uint64 `gorm:"index:idx_payref_block,priority:3;index:idx_dest_block,priority:3"`
1720
DestinationAddressHash *string `gorm:"index:idx_dest_block,priority:1;type:varchar(64);default:null"`
1821
SourceAddressesRoot string `gorm:"index;type:varchar(64);default:null"`
1922
Sequence uint64 `gorm:"index:idx_source_sequence,priority:2"`

internal/xrp/transaction.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ func (tx *transaction) destinationAddressHash() *string {
105105
return nil
106106
}
107107

108-
// transactionStatus returns a numeric status code derived from the transaction's
109-
// TransactionResult metadata: 0 for success, 1 for destination-related failures,
110-
// and 2 for all other results.
108+
// transactionStatus returns true when the transaction result is tesSUCCESS or a
109+
// destination-related failure (tecDST_TAG_NEEDED, tecNO_DST, tecNO_DST_INSUF_XRP,
110+
// tecNO_PERMISSION), and false for all other results.
111111
func (tx *transaction) transactionStatus() (bool, error) {
112112
var meta struct {
113113
TransactionResult string `json:"TransactionResult"`
@@ -130,23 +130,24 @@ func (tx *transaction) transactionStatus() (bool, error) {
130130
}
131131

132132
// intendedReceivingAmount returns the native XRP amount in drops that the
133-
// sender intended to deliver. Returns 0 for non-native (token) payments.
134-
func (tx *transaction) intendedReceivingAmount() (uint64, error) {
133+
// sender intended to deliver. Returns a pointer to 0 for non-native (token) payments.
134+
func (tx *transaction) intendedReceivingAmount() (*uint64, error) {
135135
if len(tx.Amount) == 0 || tx.Amount[0] != '"' {
136-
return 0, nil
136+
zero := uint64(0)
137+
return &zero, nil
137138
}
138139

139140
var s string
140141
if err := json.Unmarshal(tx.Amount, &s); err != nil {
141-
return 0, fmt.Errorf("unmarshal amount: %w", err)
142+
return nil, fmt.Errorf("unmarshal amount: %w", err)
142143
}
143144

144145
drops, err := strconv.ParseUint(s, 10, 64)
145146
if err != nil {
146-
return 0, fmt.Errorf("parse drops: %w", err)
147+
return nil, fmt.Errorf("parse drops: %w", err)
147148
}
148149

149-
return drops, nil
150+
return &drops, nil
150151
}
151152

152153
// sourceAddressesRoot computes the Merkle root of the double-Keccak256-hashed addresses

internal/xrp/transaction_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,27 @@ func TestIntendedReceivingAmount(t *testing.T) {
4949
tests := []struct {
5050
name string
5151
amount string
52-
expected uint64
52+
expected *uint64
5353
}{
5454
{
5555
name: "native XRP drops",
5656
amount: `"15000000"`,
57-
expected: 15000000,
57+
expected: uint64Ptr(15000000),
5858
},
5959
{
6060
name: "large native amount",
6161
amount: `"100000000000000000"`,
62-
expected: 100000000000000000,
62+
expected: uint64Ptr(100000000000000000),
6363
},
6464
{
6565
name: "token amount object",
6666
amount: `{"currency":"USD","issuer":"rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn","value":"100"}`,
67-
expected: 0,
67+
expected: uint64Ptr(0),
6868
},
6969
{
7070
name: "empty amount",
7171
amount: ``,
72-
expected: 0,
72+
expected: uint64Ptr(0),
7373
},
7474
}
7575

@@ -287,3 +287,7 @@ func TestDestinationAddressHash(t *testing.T) {
287287
func ptr(s string) *string {
288288
return &s
289289
}
290+
291+
func uint64Ptr(v uint64) *uint64 {
292+
return &v
293+
}

internal/xrp/xrp_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ func TestXrp(t *testing.T) {
8585
require.Nil(t, tx.DestinationTag)
8686

8787
require.NotNil(t, tx.DestinationAddressHash)
88-
require.NotZero(t, tx.IntendedReceivingAmount)
88+
require.NotNil(t, tx.IntendedReceivingAmount)
89+
require.NotZero(t, *tx.IntendedReceivingAmount)
8990
require.True(t, tx.TransactionStatus)
9091

9192
require.Empty(t, blockResult.Events)

0 commit comments

Comments
 (0)