Skip to content

Commit

Permalink
Merge pull request #2450 from fluidity-money/develop-pending-winner-q…
Browse files Browse the repository at this point in the history
…ueue-array

convert `PendingWinners` queue to array
  • Loading branch information
eli-d authored Dec 14, 2023
2 parents 344a69d + 971b6fd commit f055fc4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
86 changes: 44 additions & 42 deletions cmd/microservice-user-transactions-aggregate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,49 +91,51 @@ func main() {
})

// pending winners have the same behaviour as winners
winners.PendingWinners(func (pendingWinner winnerTypes.PendingWinner) {
var (
network = pendingWinner.Network
transactionHash = pendingWinner.TransactionHash.String()
application = pendingWinner.Application.String()
usdWinAmount = pendingWinner.UsdWinAmount
senderAddress = pendingWinner.SenderAddress.String()
utility = pendingWinner.Utility
)

existingUserTransaction := user_actions.GetAggregatedUserTransactionByHash(network, transactionHash)
if existingUserTransaction == nil {
log.Fatal(func(k *log.Log) {
k.Format(
"Found a winner in transaction %v with no corresponding send!",
transactionHash,
)
})
}
// regardless of whether there's existing win data, always prefer to show
// an application if any logs in this transaction contain one
if existingUserTransaction.Application == "none" {
existingUserTransaction.Application = application
}

winningAmountFloat := usdWinAmount
winners.PendingWinners(func (pendingWinners []winnerTypes.PendingWinner) {
for _, pendingWinner := range pendingWinners {
var (
network = pendingWinner.Network
transactionHash = pendingWinner.TransactionHash.String()
application = pendingWinner.Application.String()
usdWinAmount = pendingWinner.UsdWinAmount
senderAddress = pendingWinner.SenderAddress.String()
utility = pendingWinner.Utility
)

existingUserTransaction := user_actions.GetAggregatedUserTransactionByHash(network, transactionHash)
if existingUserTransaction == nil {
log.Fatal(func(k *log.Log) {
k.Format(
"Found a winner in transaction %v with no corresponding send!",
transactionHash,
)
})
}
// regardless of whether there's existing win data, always prefer to show
// an application if any logs in this transaction contain one
if existingUserTransaction.Application == "none" {
existingUserTransaction.Application = application
}

winningAmountFloat := usdWinAmount

// no existing info, update all win-related fields
if existingUserTransaction.WinningAddress == "" && utility == "FLUID" {
existingUserTransaction.WinningAddress = senderAddress
existingUserTransaction.WinningAmount = winningAmountFloat
existingUserTransaction.RewardHash = transactionHash
existingUserTransaction.UtilityName = utility
}

existingUtility := existingUserTransaction.UtilityName

// update utility amount and name if unset
if utility != "FLUID" && (existingUtility == "FLUID" || existingUtility == "") {
existingUserTransaction.UtilityAmount = winningAmountFloat
existingUserTransaction.UtilityName = utility
}

// no existing info, update all win-related fields
if existingUserTransaction.WinningAddress == "" && utility == "FLUID" {
existingUserTransaction.WinningAddress = senderAddress
existingUserTransaction.WinningAmount = winningAmountFloat
existingUserTransaction.RewardHash = transactionHash
existingUserTransaction.UtilityName = utility
}

existingUtility := existingUserTransaction.UtilityName

// update utility amount and name if unset
if utility != "FLUID" && (existingUtility == "FLUID" || existingUtility == "") {
existingUserTransaction.UtilityAmount = winningAmountFloat
existingUserTransaction.UtilityName = utility
user_actions.UpdateAggregatedUserTransactionByHash(*existingUserTransaction, transactionHash)
}

user_actions.UpdateAggregatedUserTransactionByHash(*existingUserTransaction, transactionHash)
})
}
9 changes: 4 additions & 5 deletions lib/queues/winners/winners.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package winners

import (
"github.com/fluidity-money/fluidity-app/common/ethereum/fluidity"
"github.com/fluidity-money/fluidity-app/lib/databases/timescale/spooler"
"github.com/fluidity-money/fluidity-app/lib/queue"
"github.com/fluidity-money/fluidity-app/lib/types/network"
types "github.com/fluidity-money/fluidity-app/lib/types/winners"
Expand Down Expand Up @@ -80,12 +79,12 @@ func BlockedWinnersAll(f func(BlockedWinner)) {
})
}

func PendingWinners(f func(spooler.PendingWinner)) {
func PendingWinners(f func([]types.PendingWinner)) {
queue.GetMessages(TopicPendingWinners, func(message queue.Message) {
var pendingWinner types.PendingWinner
var pendingWinners []types.PendingWinner

message.Decode(&pendingWinner)
message.Decode(&pendingWinners)

f(pendingWinner)
f(pendingWinners)
})
}

0 comments on commit f055fc4

Please sign in to comment.