Skip to content

Commit e3aec64

Browse files
fix(xrpl): add metadata typed fields
1 parent 6d25188 commit e3aec64

13 files changed

+134
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Refactored
1111

12-
- `TxResponse` `meta` field type changed to `TxMetadataBuilder`, enabling custom parsing for specific transactions metadata such as `Payment`, `NFTokenMint`, etc.
12+
- `TxResponse` `Meta` field type changed to `TxMetadataBuilder`, enabling custom parsing for specific transactions metadata such as `Payment`, `NFTokenMint`, etc.
1313

1414
## [v0.1.13]
1515

xrpl/transaction/metadata.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package transaction
22

33
import (
44
"github.com/Peersyst/xrpl-go/xrpl/ledger-entry-types"
5+
"github.com/Peersyst/xrpl-go/xrpl/transaction/types"
56
)
67

78
// TxMeta represents the metadata interface for a transaction.
@@ -21,7 +22,7 @@ type TxObjMeta struct {
2122
DeliveredAmount any `json:"delivered_amount,omitempty"`
2223

2324
// ParentBatchID is the hash of the parent Batch transaction when this transaction is executed as part of a batch.
24-
ParentBatchID *string `json:"ParentBatchID,omitempty"`
25+
ParentBatchID *types.BatchID `json:"ParentBatchID,omitempty"`
2526
}
2627

2728
// TxMeta implements the TxMeta interface for TxObjMeta.

xrpl/transaction/metadata_builder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ type TxMetadataBuilder struct {
1515
DeliveredAmount any `json:"delivered_amount,omitempty"`
1616

1717
// ParentBatchID is the hash of the parent Batch transaction when this transaction is executed as part of a batch.
18-
ParentBatchID *string `json:"ParentBatchID,omitempty"`
18+
ParentBatchID *types.BatchID `json:"ParentBatchID,omitempty"`
1919

2020
// rippled 1.11.0 or later.
2121
// Only available in: NFTokenMintMetadata, NFTokenAcceptOfferMetadata
2222
NFTokenID *types.NFTokenID `json:"nftoken_id,omitempty"`
2323

2424
// OfferID is a string of Amount is present.
2525
// Only available in: NFTokenMintMetadata, NFTokenCreateOfferMetadata
26-
OfferID *string `json:"offer_id,omitempty"`
26+
OfferID *types.OfferID `json:"offer_id,omitempty"`
2727

2828
// rippled 1.11.0 or later.
2929
// Only available in: NFTokenCancelOfferMetadata
3030
NFTokenIDs []types.NFTokenID `json:"nftoken_ids,omitempty"`
3131

3232
// Only available in: MPTokenIssuanceCreate
33-
MPTIssuanceID *string `json:"mpt_issuance_id,omitempty"`
33+
MPTIssuanceID *types.MPTIssuanceID `json:"mpt_issuance_id,omitempty"`
3434
}
3535

3636
// AsPaymentMetadata returns the PaymentMetadata.

xrpl/transaction/metadata_builder_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
)
99

1010
func TestTxMetadataBuilder_AsTxObjMeta(t *testing.T) {
11+
batchId := types.BatchID("ABCD1234")
12+
1113
tests := []struct {
1214
name string
1315
builder TxMetadataBuilder
@@ -28,7 +30,7 @@ func TestTxMetadataBuilder_AsTxObjMeta(t *testing.T) {
2830
TransactionIndex: 42,
2931
TransactionResult: "tesSUCCESS",
3032
DeliveredAmount: "1000000",
31-
ParentBatchID: stringPtr("ABCD1234"),
33+
ParentBatchID: &batchId,
3234
},
3335
expected: TxObjMeta{
3436
AffectedNodes: []AffectedNode{
@@ -43,7 +45,7 @@ func TestTxMetadataBuilder_AsTxObjMeta(t *testing.T) {
4345
TransactionIndex: 42,
4446
TransactionResult: "tesSUCCESS",
4547
DeliveredAmount: "1000000",
46-
ParentBatchID: stringPtr("ABCD1234"),
48+
ParentBatchID: &batchId,
4749
},
4850
},
4951
{
@@ -87,6 +89,8 @@ func TestTxMetadataBuilder_AsTxObjMeta(t *testing.T) {
8789
}
8890

8991
func TestTxMetadataBuilder_AsPaymentMetadata(t *testing.T) {
92+
batchID := types.BatchID("EFGH5678")
93+
9094
tests := []struct {
9195
name string
9296
builder TxMetadataBuilder
@@ -107,7 +111,7 @@ func TestTxMetadataBuilder_AsPaymentMetadata(t *testing.T) {
107111
TransactionIndex: 10,
108112
TransactionResult: "tesSUCCESS",
109113
DeliveredAmount: "5000000",
110-
ParentBatchID: stringPtr("EFGH5678"),
114+
ParentBatchID: &batchID,
111115
},
112116
expected: PaymentMetadata{
113117
TxObjMeta: TxObjMeta{
@@ -123,7 +127,7 @@ func TestTxMetadataBuilder_AsPaymentMetadata(t *testing.T) {
123127
TransactionIndex: 10,
124128
TransactionResult: "tesSUCCESS",
125129
DeliveredAmount: "5000000",
126-
ParentBatchID: stringPtr("EFGH5678"),
130+
ParentBatchID: &batchID,
127131
},
128132
},
129133
},
@@ -152,7 +156,7 @@ func TestTxMetadataBuilder_AsPaymentMetadata(t *testing.T) {
152156

153157
func TestTxMetadataBuilder_AsNFTokenMintMetadata(t *testing.T) {
154158
nftokenID := types.NFTokenID("000100001E962F495F07A990F4ED55ACCFEEF365DBAA76B6A048C0A200000007")
155-
offerID := "9C92E061381C1EF37A8CDE0E8FC35188BFC30B1883825042A64309AC09F4C36D"
159+
offerID := types.OfferID("9C92E061381C1EF37A8CDE0E8FC35188BFC30B1883825042A64309AC09F4C36D")
156160

157161
tests := []struct {
158162
name string
@@ -250,7 +254,7 @@ func TestTxMetadataBuilder_AsNFTokenMintMetadata(t *testing.T) {
250254
}
251255

252256
func TestTxMetadataBuilder_AsNFTokenCreateOfferMetadata(t *testing.T) {
253-
offerID := "68CD1F6F906494EA08C9CB5CAFA64DFA90D4E834B7151899B73231DE5A0C3B77"
257+
offerID := types.OfferID("68CD1F6F906494EA08C9CB5CAFA64DFA90D4E834B7151899B73231DE5A0C3B77")
254258

255259
tests := []struct {
256260
name string
@@ -504,7 +508,7 @@ func TestTxMetadataBuilder_AsNFTokenCancelOfferMetadata(t *testing.T) {
504508
}
505509

506510
func TestTxMetadataBuilder_AsMPTokenIssuanceCreateMetadata(t *testing.T) {
507-
mptIssuanceID := "MPT1234567890ABCDEF"
511+
mptIssuanceID := types.MPTIssuanceID("MPT1234567890ABCDEF")
508512

509513
tests := []struct {
510514
name string
@@ -581,8 +585,3 @@ func TestTxMetadataBuilder_AsMPTokenIssuanceCreateMetadata(t *testing.T) {
581585
})
582586
}
583587
}
584-
585-
// Helper function to create string pointers
586-
func stringPtr(s string) *string {
587-
return &s
588-
}

xrpl/transaction/mptoken_issuance_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929
// It extends from TxObjMeta.
3030
type MPTokenIssuanceCreateMetadata struct {
3131
TxObjMeta
32-
MPTIssuanceID *string `json:"mpt_issuance_id,omitempty"`
32+
MPTIssuanceID *types.MPTIssuanceID `json:"mpt_issuance_id,omitempty"`
3333
}
3434

3535
// MPTokenIssuanceCreate represents a transaction to create a new MPTokenIssuance object.

xrpl/transaction/nftoken_create_offer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type NFTokenCreateOfferMetadata struct {
2020
TxObjMeta
2121

2222
// OfferID is a string of Amount is present.
23-
OfferID *string `json:"offer_id,omitempty"`
23+
OfferID *types.OfferID `json:"offer_id,omitempty"`
2424
}
2525

2626
// NFTokenCreateOffer creates either a new Sell offer for an NFToken owned by the account executing the transaction, or a new Buy offer for an NFToken owned by another account.

xrpl/transaction/nftoken_mint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type NFTokenMintMetadata struct {
1515
NFTokenID *types.NFTokenID `json:"nftoken_id,omitempty"`
1616

1717
// OfferID is a string of Amount is present.
18-
OfferID *string `json:"offer_id,omitempty"`
18+
OfferID *types.OfferID `json:"offer_id,omitempty"`
1919
}
2020

2121
// NFTokenMint transaction creates a non-fungible token and adds it to the relevant NFTokenPage object of the NFTokenMinter as an NFToken object.

xrpl/transaction/types/batch_id.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package types
2+
3+
// BatchID represents a batch ID.
4+
type BatchID string
5+
6+
func (b *BatchID) String() string {
7+
return string(*b)
8+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package types
2+
3+
import "testing"
4+
5+
func TestBatchIDString(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
batchId BatchID
9+
want string
10+
}{
11+
{
12+
name: "Empty BatchID",
13+
batchId: BatchID(""),
14+
want: "",
15+
},
16+
{
17+
name: "Non-empty BatchID",
18+
batchId: BatchID("1234567890abcdef"),
19+
want: "1234567890abcdef",
20+
},
21+
}
22+
23+
for _, tt := range tests {
24+
t.Run(tt.name, func(t *testing.T) {
25+
if got := tt.batchId.String(); got != tt.want {
26+
t.Errorf("BatchID.String(), got: %v but we want %v", got, tt.want)
27+
}
28+
})
29+
}
30+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package types
2+
3+
// MPTIssuanceID represents an MPT issuance ID.
4+
type MPTIssuanceID string
5+
6+
func (mpt *MPTIssuanceID) String() string {
7+
return string(*mpt)
8+
}

0 commit comments

Comments
 (0)