@@ -237,7 +237,7 @@ func (s *StellarIndexer) convertLedger(
237237 }
238238
239239 var effects []stellar.Effect
240- if strings . EqualFold ( strings . TrimSpace ( operation .Type ), "create_claimable_balance" ) {
240+ if stellarOperationNeedsEffects ( operation .Type ) {
241241 effects , err = s .getCachedOperationEffects (ctx , effectsByOperation , effectsFetched , operation .ID )
242242 if err != nil {
243243 return nil , err
@@ -316,6 +316,15 @@ func (s *StellarIndexer) shouldProcessOperation(operation stellar.Operation) boo
316316 }
317317}
318318
319+ func stellarOperationNeedsEffects (operationType string ) bool {
320+ switch strings .ToLower (strings .TrimSpace (operationType )) {
321+ case "create_claimable_balance" , "claim_claimable_balance" :
322+ return true
323+ default :
324+ return false
325+ }
326+ }
327+
319328func (s * StellarIndexer ) getCachedTransactionDetail (
320329 ctx context.Context ,
321330 cache map [string ]* stellar.Transaction ,
@@ -512,9 +521,9 @@ func (s *StellarIndexer) convertOperation(
512521 case "create_claimable_balance" :
513522 return s .convertCreateClaimableBalanceOperation (operation , effects , txDetail , ledgerSequence , blockHash , transferIndex , ledgerTimestamp )
514523 case "claim_claimable_balance" :
515- return s .convertClaimClaimableBalanceOperation (operation , txDetail , ledgerSequence , blockHash , transferIndex , ledgerTimestamp )
524+ return s .convertClaimClaimableBalanceOperation (operation , effects , txDetail , ledgerSequence , blockHash , transferIndex , ledgerTimestamp )
516525 case "clawback_claimable_balance" :
517- return s .convertClawbackClaimableBalanceOperation (operation , txDetail , ledgerSequence , blockHash , transferIndex , ledgerTimestamp )
526+ return s .convertClawbackClaimableBalanceOperation (operation , effects , txDetail , ledgerSequence , blockHash , transferIndex , ledgerTimestamp )
518527 default :
519528 return types.Transaction {}, false , nil
520529 }
@@ -595,14 +604,13 @@ func (s *StellarIndexer) convertCreateClaimableBalanceOperation(
595604 return types.Transaction {}, false , err
596605 }
597606
598- shouldEmit := s .anyMonitoredAddress ( claimants ) || ( s . config .TwoWayIndexing && s .isMonitoredAddress (sourceAccount ) )
607+ shouldEmit := s .config .TwoWayIndexing && s .isMonitoredAddress (sourceAccount )
599608 if ! shouldEmit {
600609 return types.Transaction {}, false , nil
601610 }
602611
603612 tx := s .newOperationTransaction (operation , txDetail , ledgerSequence , blockHash , transferIndex , ledgerTimestamp )
604613 tx .FromAddress = sourceAccount
605- tx .ToAddress = stellarClaimableBalanceAddress (balanceID )
606614 tx .AssetAddress = assetAddress
607615 tx .Amount = amount
608616 tx .Type = txType
@@ -616,6 +624,7 @@ func (s *StellarIndexer) convertCreateClaimableBalanceOperation(
616624
617625func (s * StellarIndexer ) convertClaimClaimableBalanceOperation (
618626 operation stellar.Operation ,
627+ effects []stellar.Effect ,
619628 txDetail * stellar.Transaction ,
620629 ledgerSequence uint64 ,
621630 blockHash string ,
@@ -627,14 +636,25 @@ func (s *StellarIndexer) convertClaimClaimableBalanceOperation(
627636 return types.Transaction {}, false , nil
628637 }
629638 state , found , err := s .loadClaimableBalanceState (balanceID )
630- if err != nil || ! found {
639+ if err != nil {
631640 return types.Transaction {}, false , err
632641 }
633- if err := s .deleteClaimableBalanceState (balanceID ); err != nil {
634- return types.Transaction {}, false , err
642+ if found {
643+ if err := s .deleteClaimableBalanceState (balanceID ); err != nil {
644+ return types.Transaction {}, false , err
645+ }
646+ } else {
647+ state , found = stellarClaimableBalanceStateFromEffect (findStellarEffect (effects , "claimable_balance_claimed" ))
648+ if ! found {
649+ return types.Transaction {}, false , nil
650+ }
635651 }
636652
637653 claimant := normalizeStellarAddress (operation .Claimant )
654+ if claimant != "" {
655+ state .Claimants = []string {claimant }
656+ }
657+
638658 if claimant == "" || ! s .isMonitoredAddress (claimant ) {
639659 return types.Transaction {}, false , nil
640660 }
@@ -645,7 +665,6 @@ func (s *StellarIndexer) convertClaimClaimableBalanceOperation(
645665 }
646666
647667 tx := s .newOperationTransaction (operation , txDetail , ledgerSequence , blockHash , transferIndex , ledgerTimestamp )
648- tx .FromAddress = stellarClaimableBalanceAddress (balanceID )
649668 tx .ToAddress = claimant
650669 tx .AssetAddress = assetAddress
651670 tx .Amount = strings .TrimSpace (state .Amount )
@@ -657,6 +676,7 @@ func (s *StellarIndexer) convertClaimClaimableBalanceOperation(
657676
658677func (s * StellarIndexer ) convertClawbackClaimableBalanceOperation (
659678 operation stellar.Operation ,
679+ effects []stellar.Effect ,
660680 txDetail * stellar.Transaction ,
661681 ledgerSequence uint64 ,
662682 blockHash string ,
@@ -667,32 +687,16 @@ func (s *StellarIndexer) convertClawbackClaimableBalanceOperation(
667687 if balanceID == "" {
668688 return types.Transaction {}, false , nil
669689 }
670- state , found , err := s .loadClaimableBalanceState (balanceID )
671- if err != nil || ! found {
672- return types.Transaction {}, false , err
673- }
674- if err := s .deleteClaimableBalanceState (balanceID ); err != nil {
690+ _ , found , err := s .loadClaimableBalanceState (balanceID )
691+ if err != nil {
675692 return types.Transaction {}, false , err
676693 }
677-
678- if ! s .anyMonitoredAddress (state .Claimants ) {
679- return types.Transaction {}, false , nil
680- }
681-
682- txType , assetAddress , ok := stellarAssetFromOperation (state .Asset )
683- if ! ok || strings .TrimSpace (state .Amount ) == "" {
684- return types.Transaction {}, false , nil
694+ if found {
695+ if err := s .deleteClaimableBalanceState (balanceID ); err != nil {
696+ return types.Transaction {}, false , err
697+ }
685698 }
686-
687- tx := s .newOperationTransaction (operation , txDetail , ledgerSequence , blockHash , transferIndex , ledgerTimestamp )
688- tx .FromAddress = stellarClaimableBalanceAddress (balanceID )
689- tx .ToAddress = stellarBurnAddress
690- tx .AssetAddress = assetAddress
691- tx .Amount = strings .TrimSpace (state .Amount )
692- tx .Type = txType
693- tx .SetMetadata (types .MetadataKeySubtype , "clawback_claimable_balance" )
694- tx .SetMetadata (types .MetadataKeyClaimableID , balanceID )
695- return tx , true , nil
699+ return types.Transaction {}, false , nil
696700}
697701
698702func (s * StellarIndexer ) newOperationTransaction (
@@ -746,7 +750,7 @@ func (s *StellarIndexer) newOperationTransaction(
746750func (s * StellarIndexer ) fetchEffectsForOperations (ctx context.Context , operations []stellar.Operation ) (map [string ][]stellar.Effect , error ) {
747751 effectsByOperation := make (map [string ][]stellar.Effect )
748752 for _ , operation := range operations {
749- if ! strings . EqualFold ( strings . TrimSpace ( operation .Type ), "create_claimable_balance" ) {
753+ if ! stellarOperationNeedsEffects ( operation .Type ) {
750754 continue
751755 }
752756 operationID := strings .TrimSpace (operation .ID )
@@ -859,6 +863,24 @@ func findStellarEffect(effects []stellar.Effect, effectType string) *stellar.Eff
859863 return nil
860864}
861865
866+ func stellarClaimableBalanceStateFromEffect (effect * stellar.Effect ) (stellarClaimableBalanceState , bool ) {
867+ if effect == nil {
868+ return stellarClaimableBalanceState {}, false
869+ }
870+
871+ state := stellarClaimableBalanceState {
872+ Asset : strings .TrimSpace (effect .Asset ),
873+ Amount : strings .TrimSpace (effect .Amount ),
874+ }
875+ if claimant := normalizeStellarAddress (effect .Account ); claimant != "" {
876+ state .Claimants = []string {claimant }
877+ }
878+ if state .Asset == "" || state .Amount == "" {
879+ return stellarClaimableBalanceState {}, false
880+ }
881+ return state , true
882+ }
883+
862884func stellarTransferIndex (payment stellar.Payment , paymentIndex int ) string {
863885 if pagingToken := strings .TrimSpace (payment .PagingToken ); pagingToken != "" {
864886 return pagingToken
0 commit comments