Skip to content

Commit

Permalink
handle balance changes
Browse files Browse the repository at this point in the history
  • Loading branch information
slandymani committed Apr 2, 2024
1 parent 4a668a6 commit 81a69f7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
59 changes: 59 additions & 0 deletions modules/bank/handle_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package bank

import (
tmctypes "github.com/cometbft/cometbft/rpc/core/types"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/forbole/bdjuno/v4/types"
juno "github.com/forbole/juno/v5/types"
)

func (m *Module) HandleBlock(
_ *tmctypes.ResultBlock, _ *tmctypes.ResultBlockResults, txs []*juno.Tx, _ *tmctypes.ResultValidators,
) error {
height, err := m.db.GetLastBlockHeight()
if err != nil {
return err
}

var addresses []string
addrMap := make(map[string]bool)

for _, tx := range txs {
for _, message := range tx.Body.Messages {
var stdMsg sdk.Msg
err := m.cdc.UnpackAny(message, &stdMsg)
if err != nil {
return err
}

addresses, _ := m.messageParser(m.cdc, stdMsg)
for _, address := range addresses {
addrMap[address] = true
}
}
}

for addr, _ := range addrMap {
addresses = append(addresses, addr)
}

balances, err := m.keeper.GetBalances(addresses, height)
if err != nil {
return err
}

bankBalances := make([]banktypes.Balance, len(balances))
for i, balance := range balances {
bankBalances[i] = convertBalance(balance)
}

return m.db.SaveAccountBalances(height, bankBalances)
}

func convertBalance(balance types.AccountBalance) banktypes.Balance {
return banktypes.Balance{
Address: balance.Address,
Coins: balance.Balance,
}
}
4 changes: 2 additions & 2 deletions modules/oracle/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (m *Module) handleMsgCreateOracleScript(oracleScriptID, height int64, times
return nil
}

func (m *Module) handleMsgEditOracleScript(height int64, timestamp string, msg *oracletypes.MsgEditOracleScript) error {
func (m *Module) handleMsgEditOracleScript(height int64, _ string, msg *oracletypes.MsgEditOracleScript) error {
err := m.db.EditOracleScript(height, msg)
if err != nil {
return errors.Wrap(err, "error while editing oracle script from MsgEditOracleScript")
Expand Down Expand Up @@ -131,7 +131,7 @@ func (m *Module) handleMsgRequestData(requestID, height int64, dataSourceIDs []i
return nil
}

func (m *Module) handleMsgReportData(msg *oracletypes.MsgReportData, txHash string, height, reportID int64, timestamp string) error {
func (m *Module) handleMsgReportData(msg *oracletypes.MsgReportData, txHash string, _, reportID int64, _ string) error {
countBeforeSaving, err := m.db.GetReportCount()
if err != nil {
return errors.Wrap(err, "error while saving data request from MsgReportData")
Expand Down

0 comments on commit 81a69f7

Please sign in to comment.