Skip to content

Commit 36946d4

Browse files
committed
Add support for simple payment
1 parent f76543a commit 36946d4

File tree

3 files changed

+113
-60
lines changed

3 files changed

+113
-60
lines changed

ingest/address/address.go

-22
This file was deleted.

ingest/processors/token_transfer/token_transfer_processor.go

+96-38
Original file line numberDiff line numberDiff line change
@@ -71,37 +71,37 @@ func ProcessTokenTransferEventsFromTransaction(tx ingest.LedgerTransaction) ([]*
7171
func ProcessTokenTransferEventsFromOperation(opIndex uint32, op xdr.Operation, opResult xdr.OperationResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
7272
switch op.Body.Type {
7373
case xdr.OperationTypeCreateAccount:
74-
return accountCreateEvents(opIndex, op, opResult, tx)
74+
return accountCreateEvents(tx, opIndex, op)
7575
case xdr.OperationTypeAccountMerge:
76-
return mergeAccountEvents(opIndex, op, opResult, tx)
76+
return mergeAccountEvents(tx, opIndex, op, opResult)
7777
case xdr.OperationTypePayment:
78-
return paymentEvents(opIndex, op.Body.MustPaymentOp(), opResult.Tr.MustPaymentResult(), tx)
78+
return paymentEvents(tx, opIndex, op)
7979
case xdr.OperationTypeCreateClaimableBalance:
80-
return createClaimableBalanceEvents(opIndex, op.Body.MustCreateClaimableBalanceOp(), opResult.Tr.MustCreateClaimableBalanceResult(), tx)
80+
return createClaimableBalanceEvents(tx, opIndex, op.Body.MustCreateClaimableBalanceOp(), opResult.Tr.MustCreateClaimableBalanceResult())
8181
case xdr.OperationTypeClaimClaimableBalance:
82-
return claimClaimableBalanceEvents(opIndex, op.Body.MustClaimClaimableBalanceOp(), opResult.Tr.MustClaimClaimableBalanceResult(), tx)
82+
return claimClaimableBalanceEvents(tx, opIndex, op.Body.MustClaimClaimableBalanceOp(), opResult.Tr.MustClaimClaimableBalanceResult())
8383
case xdr.OperationTypeClawback:
84-
return clawbackEvents(opIndex, op.Body.MustClawbackOp(), opResult.Tr.MustClawbackResult(), tx)
84+
return clawbackEvents(tx, opIndex, op.Body.MustClawbackOp(), opResult.Tr.MustClawbackResult())
8585
case xdr.OperationTypeClawbackClaimableBalance:
86-
return clawbackClaimableBalanceEvents(opIndex, op.Body.MustClawbackClaimableBalanceOp(), opResult.Tr.MustClawbackClaimableBalanceResult(), tx)
86+
return clawbackClaimableBalanceEvents(tx, opIndex, op.Body.MustClawbackClaimableBalanceOp(), opResult.Tr.MustClawbackClaimableBalanceResult())
8787
case xdr.OperationTypeAllowTrust:
88-
return allowTrustEvents(opIndex, op.Body.MustAllowTrustOp(), opResult.Tr.MustAllowTrustResult(), tx)
88+
return allowTrustEvents(tx, opIndex, op.Body.MustAllowTrustOp(), opResult.Tr.MustAllowTrustResult())
8989
case xdr.OperationTypeSetTrustLineFlags:
90-
return setTrustLineFlagsEvents(opIndex, op.Body.MustSetTrustLineFlagsOp(), opResult.Tr.MustSetTrustLineFlagsResult(), tx)
90+
return setTrustLineFlagsEvents(tx, opIndex, op.Body.MustSetTrustLineFlagsOp(), opResult.Tr.MustSetTrustLineFlagsResult())
9191
case xdr.OperationTypeLiquidityPoolDeposit:
92-
return liquidityPoolDepositEvents(opIndex, op.Body.MustLiquidityPoolDepositOp(), opResult.Tr.MustLiquidityPoolDepositResult(), tx)
92+
return liquidityPoolDepositEvents(tx, opIndex, op.Body.MustLiquidityPoolDepositOp(), opResult.Tr.MustLiquidityPoolDepositResult())
9393
case xdr.OperationTypeLiquidityPoolWithdraw:
94-
return liquidityPoolWithdrawEvents(opIndex, op.Body.MustLiquidityPoolWithdrawOp(), opResult.Tr.MustLiquidityPoolWithdrawResult(), tx)
94+
return liquidityPoolWithdrawEvents(tx, opIndex, op.Body.MustLiquidityPoolWithdrawOp(), opResult.Tr.MustLiquidityPoolWithdrawResult())
9595
case xdr.OperationTypeManageBuyOffer:
96-
return manageBuyOfferEvents(opIndex, op.Body.MustManageBuyOfferOp(), opResult.Tr.MustManageBuyOfferResult(), tx)
96+
return manageBuyOfferEvents(tx, opIndex, op.Body.MustManageBuyOfferOp(), opResult.Tr.MustManageBuyOfferResult())
9797
case xdr.OperationTypeManageSellOffer:
98-
return manageSellOfferEvents(opIndex, op.Body.MustManageSellOfferOp(), opResult.Tr.MustManageSellOfferResult(), tx)
98+
return manageSellOfferEvents(tx, opIndex, op.Body.MustManageSellOfferOp(), opResult.Tr.MustManageSellOfferResult())
9999
case xdr.OperationTypeCreatePassiveSellOffer:
100-
return createPassiveSellOfferEvents(opIndex, op.Body.MustCreatePassiveSellOfferOp(), opResult.Tr.MustCreatePassiveSellOfferResult(), tx)
100+
return createPassiveSellOfferEvents(tx, opIndex, op.Body.MustCreatePassiveSellOfferOp(), opResult.Tr.MustCreatePassiveSellOfferResult())
101101
case xdr.OperationTypePathPaymentStrictSend:
102-
return pathPaymentStrictSendEvents(opIndex, op.Body.MustPathPaymentStrictSendOp(), opResult.Tr.MustPathPaymentStrictSendResult(), tx)
102+
return pathPaymentStrictSendEvents(tx, opIndex, op.Body.MustPathPaymentStrictSendOp(), opResult.Tr.MustPathPaymentStrictSendResult())
103103
case xdr.OperationTypePathPaymentStrictReceive:
104-
return pathPaymentStrictReceiveEvents(opIndex, op.Body.MustPathPaymentStrictReceiveOp(), opResult.Tr.MustPathPaymentStrictReceiveResult(), tx)
104+
return pathPaymentStrictReceiveEvents(tx, opIndex, op.Body.MustPathPaymentStrictReceiveOp(), opResult.Tr.MustPathPaymentStrictReceiveResult())
105105
default:
106106
return nil, nil
107107
}
@@ -120,91 +120,116 @@ 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(), address.AddressFromAccountId(accId), amt, asset.NewNativeAsset())
123+
event := NewFeeEvent(tx.Ledger.LedgerSequence(), tx.Ledger.ClosedAt(), tx.Hash.HexString(), addressFromAccountId(accId), amt, asset.NewNativeAsset())
124124
events = append(events, event)
125125
}
126126
return events, nil
127127
}
128128

129129
// 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) {
131131
srcAcc := sourceAccount(tx, op)
132-
133132
createAccountOp := op.Body.MustCreateAccountOp()
134133
destAcc, amt := createAccountOp.Destination, amount.String(createAccountOp.StartingBalance)
135134
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())
137136
return []*TokenTransferEvent{event}, nil // Just one event will be generated
138137
}
139138

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) {
141140
res := result.Tr.MustAccountMergeResult()
142141
// 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
143142
if res.SourceAccountBalance == nil {
144143
return nil, nil
145144
}
146-
147145
srcAcc := sourceAccount(tx, op)
148146
destAcc := op.Body.MustDestination()
149147
amt := amount.String(*res.SourceAccountBalance)
150148
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())
152150
return []*TokenTransferEvent{event}, nil // Just one event will be generated
153151
}
154152

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
157182
}
158183

159-
func createClaimableBalanceEvents(opIndex uint32, op xdr.CreateClaimableBalanceOp, result xdr.CreateClaimableBalanceResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
184+
func createClaimableBalanceEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.CreateClaimableBalanceOp, result xdr.CreateClaimableBalanceResult) ([]*TokenTransferEvent, error) {
160185
return nil, nil
161186
}
162187

163-
func claimClaimableBalanceEvents(opIndex uint32, op xdr.ClaimClaimableBalanceOp, result xdr.ClaimClaimableBalanceResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
188+
func claimClaimableBalanceEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.ClaimClaimableBalanceOp, result xdr.ClaimClaimableBalanceResult) ([]*TokenTransferEvent, error) {
164189
return nil, nil
165190
}
166191

167-
func clawbackEvents(opIndex uint32, op xdr.ClawbackOp, result xdr.ClawbackResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
192+
func clawbackEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.ClawbackOp, result xdr.ClawbackResult) ([]*TokenTransferEvent, error) {
168193
return nil, nil
169194
}
170195

171-
func clawbackClaimableBalanceEvents(opIndex uint32, op xdr.ClawbackClaimableBalanceOp, result xdr.ClawbackClaimableBalanceResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
196+
func clawbackClaimableBalanceEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.ClawbackClaimableBalanceOp, result xdr.ClawbackClaimableBalanceResult) ([]*TokenTransferEvent, error) {
172197
return nil, nil
173198
}
174199

175-
func allowTrustEvents(opIndex uint32, op xdr.AllowTrustOp, result xdr.AllowTrustResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
200+
func allowTrustEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.AllowTrustOp, result xdr.AllowTrustResult) ([]*TokenTransferEvent, error) {
176201
return nil, nil
177202
}
178203

179-
func setTrustLineFlagsEvents(opIndex uint32, op xdr.SetTrustLineFlagsOp, result xdr.SetTrustLineFlagsResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
204+
func setTrustLineFlagsEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.SetTrustLineFlagsOp, result xdr.SetTrustLineFlagsResult) ([]*TokenTransferEvent, error) {
180205
return nil, nil
181206
}
182207

183-
func liquidityPoolDepositEvents(opIndex uint32, op xdr.LiquidityPoolDepositOp, result xdr.LiquidityPoolDepositResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
208+
func liquidityPoolDepositEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.LiquidityPoolDepositOp, result xdr.LiquidityPoolDepositResult) ([]*TokenTransferEvent, error) {
184209
return nil, nil
185210
}
186211

187-
func liquidityPoolWithdrawEvents(opIndex uint32, op xdr.LiquidityPoolWithdrawOp, result xdr.LiquidityPoolWithdrawResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
212+
func liquidityPoolWithdrawEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.LiquidityPoolWithdrawOp, result xdr.LiquidityPoolWithdrawResult) ([]*TokenTransferEvent, error) {
188213
return nil, nil
189214
}
190215

191-
func manageBuyOfferEvents(opIndex uint32, op xdr.ManageBuyOfferOp, result xdr.ManageBuyOfferResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
216+
func manageBuyOfferEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.ManageBuyOfferOp, result xdr.ManageBuyOfferResult) ([]*TokenTransferEvent, error) {
192217
return nil, nil
193218
}
194219

195-
func manageSellOfferEvents(opIndex uint32, op xdr.ManageSellOfferOp, result xdr.ManageSellOfferResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
220+
func manageSellOfferEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.ManageSellOfferOp, result xdr.ManageSellOfferResult) ([]*TokenTransferEvent, error) {
196221
return nil, nil
197222
}
198223

199-
func createPassiveSellOfferEvents(opIndex uint32, op xdr.CreatePassiveSellOfferOp, result xdr.ManageSellOfferResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
224+
func createPassiveSellOfferEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.CreatePassiveSellOfferOp, result xdr.ManageSellOfferResult) ([]*TokenTransferEvent, error) {
200225
return nil, nil
201226
}
202227

203-
func pathPaymentStrictSendEvents(opIndex uint32, op xdr.PathPaymentStrictSendOp, result xdr.PathPaymentStrictSendResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
228+
func pathPaymentStrictSendEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.PathPaymentStrictSendOp, result xdr.PathPaymentStrictSendResult) ([]*TokenTransferEvent, error) {
204229
return nil, nil
205230
}
206231

207-
func pathPaymentStrictReceiveEvents(opIndex uint32, op xdr.PathPaymentStrictReceiveOp, result xdr.PathPaymentStrictReceiveResult, tx ingest.LedgerTransaction) ([]*TokenTransferEvent, error) {
232+
func pathPaymentStrictReceiveEvents(tx ingest.LedgerTransaction, opIndex uint32, op xdr.PathPaymentStrictReceiveOp, result xdr.PathPaymentStrictReceiveResult) ([]*TokenTransferEvent, error) {
208233
return nil, nil
209234
}
210235

@@ -217,3 +242,36 @@ func sourceAccount(tx ingest.LedgerTransaction, op xdr.Operation) xdr.MuxedAccou
217242
res := tx.Envelope.SourceAccount()
218243
return res
219244
}
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+
}

xdr/asset.go

+17
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,19 @@ func (a *Asset) GetIssuer() string {
423423
}
424424
}
425425

426+
func (a *Asset) GetIssuerAccountId() (AccountId, error) {
427+
var addr AccountId
428+
switch a.Type {
429+
case AssetTypeAssetTypeNative:
430+
return AccountId{}, errors.New("native Asset has no issuer")
431+
case AssetTypeAssetTypeCreditAlphanum4:
432+
addr = (*a.AlphaNum4).Issuer
433+
case AssetTypeAssetTypeCreditAlphanum12:
434+
addr = (*a.AlphaNum12).Issuer
435+
}
436+
return addr, nil
437+
}
438+
426439
func (a *Asset) LessThan(b Asset) bool {
427440
if a.Type != b.Type {
428441
return int32(a.Type) < int32(b.Type)
@@ -455,3 +468,7 @@ func (a Asset) ContractID(passphrase string) ([32]byte, error) {
455468
}
456469
return sha256.Sum256(xdrPreImageBytes), nil
457470
}
471+
472+
func (a Asset) IsNative() bool {
473+
return a.Type == AssetTypeAssetTypeNative
474+
}

0 commit comments

Comments
 (0)