Asset stats (/assets) don't include claimable balances. We probably should add number of claimable balances holding a given asset with the sum amount. This can be either part of https://github.com/stellar/go/pull/3454 or in a separate PR.
Initial thought was to add this into the AssetStatsProcessor. The process being roughly:
However, that might have the same issue as https://github.com/stellar/go/issues/3380 and https://github.com/stellar/go/issues/3306, where the ingest.Change doesn't include any information about the claimable balance's creator. 馃 The difference being that in the processor we have access to the DB, so we could query that info from the history_claimable_balance_transactions table. But I don't like the idea of having the ingesters be coupled via the db.
Second thought, is we could do it dynamically in the /assets action. But, that means to load the /assets endpoint we also have to query all the current claimable balances, and do some math on the fly to calculate the new balances and accounts. We do the same for issuers already, but those are guaranteed to be 1:1 with assets. I worry about that having performance implications if there are a lot of claimable balances on-the-go.
@paulbellamy I believe you should be able to implement it in the AssetStatsProcessor. You don't need to have any logic to check who created the claimable balance or how the claimable balance was claimed or clawed back.
When you create a claimable balance I think there will be two ingest.Change produced from that operation. The first change will correspond to the trustline balance change of the creator. The other change will correspond to the event of a new claimable balance being introduced in the ledger.
Similarly when you claim or clawback a claimable balance, that operation should produce two ingest.Change instances. The first change will correspond to a trustline balance increasing. The other change will correspond to the claimable balance being removed from the ledger.
I think these are the only cases you need to handle:
// only handle changes involving claimable balances
switch {
case change.Pre == nil && change.Post != nil:
// Created
newCb := change.Post.Data.MustClaimableBalance()
// increase claimable balance stats
case change.Pre != nil && change.Post != nil:
// claimable balances are immutable so this case should never happen
case change.Pre != nil && change.Post == nil:
// Removed
oldCb := change.Pre.Data.MustClaimableBalance()
// decrease claimable balance stats
default:
return errors.New("Invalid io.Change: change.Pre == nil && change.Post == nil")
}
^ What @tamirms said, just one quick note on:
// claimable balances are immutable so this case should never happen
I think there's one case in which it's possible: when the sponsor is changed. But this won't affect the amount nor the asset.
Most helpful comment
@paulbellamy I believe you should be able to implement it in the AssetStatsProcessor. You don't need to have any logic to check who created the claimable balance or how the claimable balance was claimed or clawed back.
When you create a claimable balance I think there will be two
ingest.Changeproduced from that operation. The first change will correspond to the trustline balance change of the creator. The other change will correspond to the event of a new claimable balance being introduced in the ledger.Similarly when you claim or clawback a claimable balance, that operation should produce two
ingest.Changeinstances. The first change will correspond to a trustline balance increasing. The other change will correspond to the claimable balance being removed from the ledger.I think these are the only cases you need to handle: