@@ -71,11 +71,11 @@ func ProcessTokenTransferEventsFromTransaction(tx ingest.LedgerTransaction) ([]*
71
71
func ProcessTokenTransferEventsFromOperation (opIndex uint32 , op xdr.Operation , opResult xdr.OperationResult , tx ingest.LedgerTransaction ) ([]* TokenTransferEvent , error ) {
72
72
switch op .Body .Type {
73
73
case xdr .OperationTypeCreateAccount :
74
- return accountCreateEvents (opIndex , op , opResult , tx )
74
+ return accountCreateEvents (tx , opIndex , op )
75
75
case xdr .OperationTypeAccountMerge :
76
- return mergeAccountEvents (opIndex , op , opResult , tx )
76
+ return mergeAccountEvents (tx , opIndex , op , opResult )
77
77
case xdr .OperationTypePayment :
78
- return paymentEvents (opIndex , op . Body . MustPaymentOp (), opResult . Tr . MustPaymentResult (), tx )
78
+ return paymentEvents (tx , opIndex , op )
79
79
case xdr .OperationTypeCreateClaimableBalance :
80
80
return createClaimableBalanceEvents (opIndex , op .Body .MustCreateClaimableBalanceOp (), opResult .Tr .MustCreateClaimableBalanceResult (), tx )
81
81
case xdr .OperationTypeClaimClaimableBalance :
@@ -120,40 +120,65 @@ func generateFeeEvent(tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error
120
120
postBalance := change .Post .Data .MustAccount ().Balance
121
121
accId := change .Pre .Data .MustAccount ().AccountId
122
122
amt := amount .String (postBalance - preBalance )
123
- event := NewFeeEvent (tx .Ledger .LedgerSequence (), tx .Ledger .ClosedAt (), tx .Hash .HexString (), address . AddressFromAccountId (accId ), amt , asset .NewNativeAsset ())
123
+ event := NewFeeEvent (tx .Ledger .LedgerSequence (), tx .Ledger .ClosedAt (), tx .Hash .HexString (), addressFromAccountId (accId ), amt , asset .NewNativeAsset ())
124
124
events = append (events , event )
125
125
}
126
126
return events , nil
127
127
}
128
128
129
129
// Function stubs
130
- func accountCreateEvents (opIndex uint32 , op xdr.Operation , result xdr. OperationResult , tx ingest. LedgerTransaction ) ([]* TokenTransferEvent , error ) {
130
+ func accountCreateEvents (tx ingest. LedgerTransaction , opIndex uint32 , op xdr.Operation ) ([]* TokenTransferEvent , error ) {
131
131
srcAcc := sourceAccount (tx , op )
132
-
133
132
createAccountOp := op .Body .MustCreateAccountOp ()
134
133
destAcc , amt := createAccountOp .Destination , amount .String (createAccountOp .StartingBalance )
135
134
meta := NewEventMeta (tx , & opIndex , nil )
136
- event := NewTransferEvent (meta , address . AddressFromAccount (srcAcc ), address . AddressFromAccountId (destAcc ), amt , asset .NewNativeAsset ())
135
+ event := NewTransferEvent (meta , addressFromAccount (srcAcc ), addressFromAccountId (destAcc ), amt , asset .NewNativeAsset ())
137
136
return []* TokenTransferEvent {event }, nil // Just one event will be generated
138
137
}
139
138
140
- func mergeAccountEvents (opIndex uint32 , op xdr.Operation , result xdr.OperationResult , tx ingest. LedgerTransaction ) ([]* TokenTransferEvent , error ) {
139
+ func mergeAccountEvents (tx ingest. LedgerTransaction , opIndex uint32 , op xdr.Operation , result xdr.OperationResult ) ([]* TokenTransferEvent , error ) {
141
140
res := result .Tr .MustAccountMergeResult ()
142
141
// If there is no transfer of XLM from source account to destination (i.e src account is empty), then no need to generate a transfer event
143
142
if res .SourceAccountBalance == nil {
144
143
return nil , nil
145
144
}
146
-
147
145
srcAcc := sourceAccount (tx , op )
148
146
destAcc := op .Body .MustDestination ()
149
147
amt := amount .String (* res .SourceAccountBalance )
150
148
meta := NewEventMeta (tx , & opIndex , nil )
151
- event := NewTransferEvent (meta , address . AddressFromAccount (srcAcc ), address . AddressFromAccount (destAcc ), amt , asset .NewNativeAsset ())
149
+ event := NewTransferEvent (meta , addressFromAccount (srcAcc ), addressFromAccount (destAcc ), amt , asset .NewNativeAsset ())
152
150
return []* TokenTransferEvent {event }, nil // Just one event will be generated
153
151
}
154
152
155
- func paymentEvents (opIndex uint32 , op xdr.PaymentOp , result xdr.PaymentResult , tx ingest.LedgerTransaction ) ([]* TokenTransferEvent , error ) {
156
- return nil , nil
153
+ func paymentEvents (tx ingest.LedgerTransaction , opIndex uint32 , op xdr.Operation ) ([]* TokenTransferEvent , error ) {
154
+ paymentOp := op .Body .MustPaymentOp ()
155
+ srcAcc := sourceAccount (tx , op )
156
+ destAcc := paymentOp .Destination
157
+ sAddress := addressFromAccount (srcAcc )
158
+ dAddress := addressFromAccount (destAcc )
159
+ amt := amount .String (paymentOp .Amount )
160
+ var as * asset.Asset
161
+ meta := NewEventMeta (tx , & opIndex , nil )
162
+ var event * TokenTransferEvent
163
+ 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 ())
167
+ } 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
+ }
180
+ }
181
+ return []* TokenTransferEvent {event }, nil
157
182
}
158
183
159
184
func createClaimableBalanceEvents (opIndex uint32 , op xdr.CreateClaimableBalanceOp , result xdr.CreateClaimableBalanceResult , tx ingest.LedgerTransaction ) ([]* TokenTransferEvent , error ) {
@@ -217,3 +242,36 @@ func sourceAccount(tx ingest.LedgerTransaction, op xdr.Operation) xdr.MuxedAccou
217
242
res := tx .Envelope .SourceAccount ()
218
243
return res
219
244
}
245
+
246
+ func addressFromAccount (account xdr.MuxedAccount ) * address.Address {
247
+ addr := & address.Address {}
248
+ switch account .Type {
249
+ case xdr .CryptoKeyTypeKeyTypeEd25519 :
250
+ addr .AddressType = address .AddressType_ADDRESS_TYPE_ACCOUNT
251
+ case xdr .CryptoKeyTypeKeyTypeMuxedEd25519 :
252
+ addr .AddressType = address .AddressType_ADDRESS_TYPE_MUXED_ACCOUNT
253
+ }
254
+ addr .StrKey = account .Address ()
255
+ return addr
256
+ }
257
+
258
+ func addressFromAccountId (account xdr.AccountId ) * address.Address {
259
+ return & address.Address {
260
+ AddressType : address .AddressType_ADDRESS_TYPE_ACCOUNT ,
261
+ StrKey : account .Address (),
262
+ }
263
+ }
264
+
265
+ func addressFromLpHash (lpHash xdr.PoolId ) * address.Address {
266
+ return & address.Address {
267
+ AddressType : address .AddressType_ADDRESS_TYPE_LIQUIDITY_POOL ,
268
+ StrKey : xdr .Hash (lpHash ).HexString (), // replace with strkey
269
+ }
270
+ }
271
+
272
+ func addressFromClaimableBalanceId (cb xdr.ClaimableBalanceId ) * address.Address {
273
+ return & address.Address {
274
+ AddressType : address .AddressType_ADDRESS_TYPE_LIQUIDITY_POOL ,
275
+ StrKey : cb .MustV0 ().HexString (), //replace with strkey
276
+ }
277
+ }
0 commit comments