Skip to content

Commit

Permalink
Tipping Snapshot Updates (#1921)
Browse files Browse the repository at this point in the history
snapshot up total ammount per currency in the user and channel streams
  • Loading branch information
texuf authored Dec 30, 2024
1 parent dd546f4 commit cd68b46
Show file tree
Hide file tree
Showing 8 changed files with 882 additions and 634 deletions.
58 changes: 58 additions & 0 deletions core/node/events/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

"google.golang.org/protobuf/proto"

"github.com/ethereum/go-ethereum/common"

. "github.com/river-build/river/core/node/base"
"github.com/river-build/river/core/node/events/migrations"
. "github.com/river-build/river/core/node/protocol"
Expand Down Expand Up @@ -323,6 +325,44 @@ func update_Snapshot_User(iSnapshot *Snapshot, userPayload *UserPayload) error {
return nil
case *UserPayload_UserMembershipAction_:
return nil
case *UserPayload_BlockchainTransaction:
// for sent transactions, sum up things like tips sent
switch transactionContent := content.BlockchainTransaction.Content.(type) {
case nil:
return nil
case *BlockchainTransaction_Tip_:
if snapshot.UserContent.TipsSent == nil {
snapshot.UserContent.TipsSent = make(map[string]uint64)
}
currencyAddress := common.BytesToAddress(transactionContent.Tip.GetEvent().GetCurrency())
currency := currencyAddress.Hex()
if _, ok := snapshot.UserContent.TipsSent[currency]; !ok {
snapshot.UserContent.TipsSent[currency] = 0
}
snapshot.UserContent.TipsSent[currency] += transactionContent.Tip.GetEvent().GetAmount()
return nil
default:
return RiverError(Err_INVALID_ARGUMENT, fmt.Sprintf("unknown blockchain transaction type %T", transactionContent))
}
case *UserPayload_ReceivedBlockchainTransaction_:
// for received transactions, sum up things like tips received
switch transactionContent := content.ReceivedBlockchainTransaction.Transaction.Content.(type) {
case nil:
return nil
case *BlockchainTransaction_Tip_:
if snapshot.UserContent.TipsReceived == nil {
snapshot.UserContent.TipsReceived = make(map[string]uint64)
}
currencyAddress := common.BytesToAddress(transactionContent.Tip.GetEvent().GetCurrency())
currency := currencyAddress.Hex()
if _, ok := snapshot.UserContent.TipsReceived[currency]; !ok {
snapshot.UserContent.TipsReceived[currency] = 0
}
snapshot.UserContent.TipsReceived[currency] += transactionContent.Tip.GetEvent().GetAmount()
return nil
default:
return RiverError(Err_INVALID_ARGUMENT, fmt.Sprintf("unknown received blockchain transaction type %T", transactionContent))
}
default:
return RiverError(Err_INVALID_ARGUMENT, fmt.Sprintf("unknown user payload type %T", userPayload.Content))
}
Expand Down Expand Up @@ -549,6 +589,24 @@ func update_Snapshot_Member(
case *MemberPayload_EncryptionAlgorithm_:
snapshot.EncryptionAlgorithm.Algorithm = content.EncryptionAlgorithm.Algorithm
return nil
case *MemberPayload_MemberBlockchainTransaction_:
switch transactionContent := content.MemberBlockchainTransaction.Transaction.Content.(type) {
case nil:
return nil
case *BlockchainTransaction_Tip_:
if snapshot.Tips == nil {
snapshot.Tips = make(map[string]uint64)
}
currencyAddress := common.BytesToAddress(transactionContent.Tip.GetEvent().GetCurrency())
currency := currencyAddress.Hex()
if _, ok := snapshot.Tips[currency]; !ok {
snapshot.Tips[currency] = 0
}
snapshot.Tips[currency] += transactionContent.Tip.GetEvent().GetAmount()
return nil
default:
return RiverError(Err_INVALID_ARGUMENT, fmt.Sprintf("unknown member blockchain transaction type %T", transactionContent))
}
default:
return RiverError(Err_INVALID_ARGUMENT, fmt.Sprintf("unknown membership payload type %T", memberPayload.Content))
}
Expand Down
Loading

0 comments on commit cd68b46

Please sign in to comment.