From 81a69f7cab4afc403c8016ee986bb2a842d2771c Mon Sep 17 00:00:00 2001 From: slandymani Date: Tue, 2 Apr 2024 15:35:21 +0300 Subject: [PATCH] handle balance changes --- modules/bank/handle_block.go | 59 ++++++++++++++++++++++++++++++++++++ modules/oracle/handle_msg.go | 4 +-- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 modules/bank/handle_block.go diff --git a/modules/bank/handle_block.go b/modules/bank/handle_block.go new file mode 100644 index 000000000..eabf4c8b6 --- /dev/null +++ b/modules/bank/handle_block.go @@ -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, + } +} diff --git a/modules/oracle/handle_msg.go b/modules/oracle/handle_msg.go index 637816d72..75a88ff13 100644 --- a/modules/oracle/handle_msg.go +++ b/modules/oracle/handle_msg.go @@ -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") @@ -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")