Skip to content

Commit 571679c

Browse files
committedFeb 20, 2025
Add unit tests for createAccount, mergeAccount, simple Payment
1 parent b97f95c commit 571679c

File tree

2 files changed

+526
-40
lines changed

2 files changed

+526
-40
lines changed
 

‎ingest/processors/token_transfer/token_transfer_processor.go

+46-40
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package token_transfer
33
import (
44
"github.com/stellar/go/amount"
55
"github.com/stellar/go/ingest"
6-
"github.com/stellar/go/ingest/address"
7-
"github.com/stellar/go/ingest/asset"
6+
addressProto "github.com/stellar/go/ingest/address"
7+
assetProto "github.com/stellar/go/ingest/asset"
88
"github.com/stellar/go/support/errors"
99
"github.com/stellar/go/xdr"
1010
"io"
@@ -120,19 +120,19 @@ func generateFeeEvent(tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error
120120
postBalance := change.Post.Data.MustAccount().Balance
121121
accId := change.Pre.Data.MustAccount().AccountId
122122
amt := amount.String(postBalance - preBalance)
123-
event := NewFeeEvent(tx.Ledger.LedgerSequence(), tx.Ledger.ClosedAt(), tx.Hash.HexString(), addressFromAccountId(accId), amt, asset.NewNativeAsset())
123+
event := NewFeeEvent(tx.Ledger.LedgerSequence(), tx.Ledger.ClosedAt(), tx.Hash.HexString(), protoAddressFromAccountId(accId), amt, assetProto.NewNativeAsset())
124124
events = append(events, event)
125125
}
126126
return events, nil
127127
}
128128

129129
// Function stubs
130130
func accountCreateEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.Operation) ([]*TokenTransferEvent, error) {
131-
srcAcc := sourceAccount(tx, op)
131+
srcAcc := operationSourceAccount(tx, op)
132132
createAccountOp := op.Body.MustCreateAccountOp()
133133
destAcc, amt := createAccountOp.Destination, amount.String(createAccountOp.StartingBalance)
134134
meta := NewEventMeta(tx, &opIndex, nil)
135-
event := NewTransferEvent(meta, addressFromAccount(srcAcc), addressFromAccountId(destAcc), amt, asset.NewNativeAsset())
135+
event := NewTransferEvent(meta, protoAddressFromAccount(srcAcc), protoAddressFromAccountId(destAcc), amt, assetProto.NewNativeAsset())
136136
return []*TokenTransferEvent{event}, nil // Just one event will be generated
137137
}
138138

@@ -142,41 +142,47 @@ func mergeAccountEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.Oper
142142
if res.SourceAccountBalance == nil {
143143
return nil, nil
144144
}
145-
srcAcc := sourceAccount(tx, op)
145+
srcAcc := operationSourceAccount(tx, op)
146146
destAcc := op.Body.MustDestination()
147147
amt := amount.String(*res.SourceAccountBalance)
148148
meta := NewEventMeta(tx, &opIndex, nil)
149-
event := NewTransferEvent(meta, addressFromAccount(srcAcc), addressFromAccount(destAcc), amt, asset.NewNativeAsset())
149+
event := NewTransferEvent(meta, protoAddressFromAccount(srcAcc), protoAddressFromAccount(destAcc), amt, assetProto.NewNativeAsset())
150150
return []*TokenTransferEvent{event}, nil // Just one event will be generated
151151
}
152152

153+
// Depending on the asset - if src or dest account == issuer of asset, then mint/burn event, else transfer event
154+
func mintOrBurnOrTransferEvent(asset xdr.Asset, srcAcc xdr.MuxedAccount, destAcc xdr.MuxedAccount, amt string, meta *EventMeta) *TokenTransferEvent {
155+
protoAsset := assetProto.NewIssuedAsset(asset.GetCode(), asset.GetIssuer())
156+
var event *TokenTransferEvent
157+
sAddress := protoAddressFromAccount(srcAcc)
158+
dAddress := protoAddressFromAccount(destAcc)
159+
assetIssuerAccountId, _ := asset.GetIssuerAccountId()
160+
if assetIssuerAccountId.Equals(srcAcc.ToAccountId()) {
161+
// Mint event
162+
event = NewMintEvent(meta, dAddress, amt, protoAsset)
163+
} else if assetIssuerAccountId.Equals(destAcc.ToAccountId()) {
164+
// Burn event
165+
event = NewBurnEvent(meta, sAddress, amt, protoAsset)
166+
} else {
167+
// Regular transfer
168+
event = NewTransferEvent(meta, sAddress, dAddress, amt, protoAsset)
169+
}
170+
return event
171+
}
172+
153173
func paymentEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.Operation) ([]*TokenTransferEvent, error) {
154174
paymentOp := op.Body.MustPaymentOp()
155-
srcAcc := sourceAccount(tx, op)
175+
srcAcc := operationSourceAccount(tx, op)
156176
destAcc := paymentOp.Destination
157-
sAddress := addressFromAccount(srcAcc)
158-
dAddress := addressFromAccount(destAcc)
159177
amt := amount.String(paymentOp.Amount)
160-
var as *asset.Asset
161178
meta := NewEventMeta(tx, &opIndex, nil)
179+
162180
var event *TokenTransferEvent
163181
if paymentOp.Asset.IsNative() {
164-
as = asset.NewNativeAsset()
165-
// If native asset, it is always a regular transfer
166-
event = NewTransferEvent(meta, sAddress, dAddress, amt, asset.NewNativeAsset())
182+
// If native assetProto, it is always a regular transfer
183+
event = NewTransferEvent(meta, protoAddressFromAccount(srcAcc), protoAddressFromAccount(destAcc), amt, assetProto.NewNativeAsset())
167184
} else {
168-
as = asset.NewIssuedAsset(paymentOp.Asset.GetCode(), paymentOp.Asset.GetIssuer())
169-
assetIssuerAccountId, _ := paymentOp.Asset.GetIssuerAccountId()
170-
if assetIssuerAccountId.Equals(srcAcc.ToAccountId()) {
171-
// Mint event
172-
event = NewMintEvent(meta, dAddress, amt, as)
173-
} else if assetIssuerAccountId.Equals(destAcc.ToAccountId()) {
174-
// Burn event
175-
event = NewBurnEvent(meta, sAddress, amt, as)
176-
} else {
177-
// Regular transfer
178-
event = NewTransferEvent(meta, sAddress, dAddress, amt, as)
179-
}
185+
event = mintOrBurnOrTransferEvent(paymentOp.Asset, srcAcc, destAcc, amt, meta)
180186
}
181187
return []*TokenTransferEvent{event}, nil
182188
}
@@ -234,7 +240,7 @@ func pathPaymentStrictReceiveEvents(tx ingest.LedgerTransaction, opIndex uint32,
234240
}
235241

236242
// Helper functions
237-
func sourceAccount(tx ingest.LedgerTransaction, op xdr.Operation) xdr.MuxedAccount {
243+
func operationSourceAccount(tx ingest.LedgerTransaction, op xdr.Operation) xdr.MuxedAccount {
238244
acc := op.SourceAccount
239245
if acc != nil {
240246
return *acc
@@ -243,35 +249,35 @@ func sourceAccount(tx ingest.LedgerTransaction, op xdr.Operation) xdr.MuxedAccou
243249
return res
244250
}
245251

246-
func addressFromAccount(account xdr.MuxedAccount) *address.Address {
247-
addr := &address.Address{}
252+
func protoAddressFromAccount(account xdr.MuxedAccount) *addressProto.Address {
253+
addr := &addressProto.Address{}
248254
switch account.Type {
249255
case xdr.CryptoKeyTypeKeyTypeEd25519:
250-
addr.AddressType = address.AddressType_ADDRESS_TYPE_ACCOUNT
256+
addr.AddressType = addressProto.AddressType_ADDRESS_TYPE_ACCOUNT
251257
case xdr.CryptoKeyTypeKeyTypeMuxedEd25519:
252-
addr.AddressType = address.AddressType_ADDRESS_TYPE_MUXED_ACCOUNT
258+
addr.AddressType = addressProto.AddressType_ADDRESS_TYPE_MUXED_ACCOUNT
253259
}
254260
addr.StrKey = account.Address()
255261
return addr
256262
}
257263

258-
func addressFromAccountId(account xdr.AccountId) *address.Address {
259-
return &address.Address{
260-
AddressType: address.AddressType_ADDRESS_TYPE_ACCOUNT,
264+
func protoAddressFromAccountId(account xdr.AccountId) *addressProto.Address {
265+
return &addressProto.Address{
266+
AddressType: addressProto.AddressType_ADDRESS_TYPE_ACCOUNT,
261267
StrKey: account.Address(),
262268
}
263269
}
264270

265-
func addressFromLpHash(lpHash xdr.PoolId) *address.Address {
266-
return &address.Address{
267-
AddressType: address.AddressType_ADDRESS_TYPE_LIQUIDITY_POOL,
271+
func protoAddressFromLpHash(lpHash xdr.PoolId) *addressProto.Address {
272+
return &addressProto.Address{
273+
AddressType: addressProto.AddressType_ADDRESS_TYPE_LIQUIDITY_POOL,
268274
StrKey: xdr.Hash(lpHash).HexString(), // replace with strkey
269275
}
270276
}
271277

272-
func addressFromClaimableBalanceId(cb xdr.ClaimableBalanceId) *address.Address {
273-
return &address.Address{
274-
AddressType: address.AddressType_ADDRESS_TYPE_LIQUIDITY_POOL,
278+
func protoAddressFromClaimableBalanceId(cb xdr.ClaimableBalanceId) *addressProto.Address {
279+
return &addressProto.Address{
280+
AddressType: addressProto.AddressType_ADDRESS_TYPE_LIQUIDITY_POOL,
275281
StrKey: cb.MustV0().HexString(), //replace with strkey
276282
}
277283
}

0 commit comments

Comments
 (0)