From aa0b5e41c045ec79c88367e0fdb7d1b602669a37 Mon Sep 17 00:00:00 2001 From: neitdung Date: Tue, 7 Jan 2025 17:23:30 +0700 Subject: [PATCH] handle by msg --- modules/{bank => asset}/handle_msg.go | 2 +- modules/asset/handle_periodic_operations.go | 17 ++++------- modules/asset/module.go | 2 ++ modules/bank/handle_block.go | 2 +- modules/bank/module.go | 2 -- modules/multistaking/handle_block.go | 10 ------- .../handle_periodic_operations.go | 2 +- modules/staking/handle_msg.go | 30 ++++++++++++++++++- 8 files changed, 40 insertions(+), 27 deletions(-) rename modules/{bank => asset}/handle_msg.go (99%) diff --git a/modules/bank/handle_msg.go b/modules/asset/handle_msg.go similarity index 99% rename from modules/bank/handle_msg.go rename to modules/asset/handle_msg.go index 7457b86d8..e7832767f 100644 --- a/modules/bank/handle_msg.go +++ b/modules/asset/handle_msg.go @@ -1,4 +1,4 @@ -package bank +package asset import ( "fmt" diff --git a/modules/asset/handle_periodic_operations.go b/modules/asset/handle_periodic_operations.go index dea4be1fd..636e14454 100644 --- a/modules/asset/handle_periodic_operations.go +++ b/modules/asset/handle_periodic_operations.go @@ -7,7 +7,6 @@ import ( "github.com/go-co-op/gocron" "github.com/rs/zerolog/log" - "github.com/forbole/callisto/v4/modules/utils" "github.com/forbole/callisto/v4/types" assettypes "github.com/realiotech/realio-network/x/asset/types" ) @@ -15,18 +14,17 @@ import ( // RegisterPeriodicOperations implements modules.Module func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error { log.Debug().Str("module", "asset").Msg("setting up periodic tasks") - fmt.Println("vlin0") - if _, err := scheduler.Every(10).Minutes().Do(func() { - utils.WatchMethod(m.UpdateTokens) - }); err != nil { - return fmt.Errorf("error while setting up asset token periodic operation: %s", err) + + err := m.InitAllTokens() + if err != nil { + return fmt.Errorf("error while initAllTokens: %s", err) } return nil } -// UpdateSupply updates the supply of all the tokens -func (m *Module) UpdateTokens() error { +// InitAllTokens init the supply of all the tokens +func (m *Module) InitAllTokens() error { log.Trace().Str("module", "bank").Str("operation", "total supply"). Msg("updating total supply") @@ -34,13 +32,11 @@ func (m *Module) UpdateTokens() error { if err != nil { return fmt.Errorf("error while getting latest block height: %s", err) } - fmt.Println("vlin1") tokens, err := m.source.GetTokens(block.Height) if err != nil { return err } - fmt.Println("vlin2", len(tokens)) return m.updateAllTokens(block.Height, tokens) } @@ -51,7 +47,6 @@ func (m *Module) updateAllTokens(height int64, tokens []assettypes.Token) error Msg("updating token unit") for _, token := range tokens { - fmt.Println("vlin3", token.Symbol) lowerCaseSymbol := strings.ToLower(token.Symbol) baseDenom := fmt.Sprintf("a%s", lowerCaseSymbol) tokenUnit := types.NewTokenUnit(baseDenom, 18, nil, "") diff --git a/modules/asset/module.go b/modules/asset/module.go index 91969100f..81165cabf 100644 --- a/modules/asset/module.go +++ b/modules/asset/module.go @@ -11,6 +11,8 @@ import ( var ( _ modules.Module = &Module{} _ modules.PeriodicOperationsModule = &Module{} + _ modules.MessageModule = &Module{} + _ modules.AuthzMessageModule = &Module{} ) // Module represents the x/staking module diff --git a/modules/bank/handle_block.go b/modules/bank/handle_block.go index b3a3b03c6..069aa3120 100644 --- a/modules/bank/handle_block.go +++ b/modules/bank/handle_block.go @@ -91,7 +91,7 @@ func (m *Module) UpdateBalance(addresses []string, height int64) error { if err != nil { return err } - + err = m.db.SaveAccountBalances(accountBalances, height) return err } diff --git a/modules/bank/module.go b/modules/bank/module.go index c7993a703..a29bbb5e0 100644 --- a/modules/bank/module.go +++ b/modules/bank/module.go @@ -14,8 +14,6 @@ import ( var ( _ modules.Module = &Module{} _ modules.PeriodicOperationsModule = &Module{} - _ modules.MessageModule = &Module{} - _ modules.AuthzMessageModule = &Module{} _ modules.BlockModule = &Module{} ) diff --git a/modules/multistaking/handle_block.go b/modules/multistaking/handle_block.go index 27a9379fe..60917db71 100644 --- a/modules/multistaking/handle_block.go +++ b/modules/multistaking/handle_block.go @@ -44,16 +44,6 @@ func (m *Module) updateTxsByEvent(height int64, events []abci.Event) error { msEvents = append(msEvents, msEvent) } - case stakingtypes.EventTypeRedelegate: - valAddr, _ := juno.FindAttributeByKey(event, stakingtypes.AttributeKeyValidator) - delAddr, _ := juno.FindAttributeByKey(event, stakingtypes.AttributeKeyDelegator) - amount, _ := juno.FindAttributeByKey(event, sdk.AttributeKeyAmount) - msEvent, err := dbtypes.NewMSEvent("redelegate", valAddr.Value, delAddr.Value, amount.Value) - - if err == nil { - msEvents = append(msEvents, msEvent) - } - case stakingtypes.EventTypeUnbond: valAddr, _ := juno.FindAttributeByKey(event, stakingtypes.AttributeKeyValidator) delAddr, _ := juno.FindAttributeByKey(event, stakingtypes.AttributeKeyDelegator) diff --git a/modules/multistaking/handle_periodic_operations.go b/modules/multistaking/handle_periodic_operations.go index 5fdc20071..9133222b8 100644 --- a/modules/multistaking/handle_periodic_operations.go +++ b/modules/multistaking/handle_periodic_operations.go @@ -13,7 +13,7 @@ import ( func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error { log.Debug().Str("module", "multistaking").Msg("setting up periodic tasks") - if _, err := scheduler.Every(10).Minutes().Do(func() { + if _, err := scheduler.Every(5).Hour().Do(func() { utils.WatchMethod(m.UpdateMultiStaking) }); err != nil { return fmt.Errorf("error while setting up multistaking token periodic operation: %s", err) diff --git a/modules/staking/handle_msg.go b/modules/staking/handle_msg.go index 866cbbd1b..750695c35 100644 --- a/modules/staking/handle_msg.go +++ b/modules/staking/handle_msg.go @@ -8,7 +8,9 @@ import ( juno "github.com/forbole/juno/v6/types" + dbtypes "github.com/forbole/callisto/v4/database/types" "github.com/forbole/callisto/v4/utils" + multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types" ) var msgFilter = map[string]bool{ @@ -48,6 +50,12 @@ func (m *Module) HandleMsg(_ int, msg juno.Message, tx *juno.Transaction) error return m.UpdateValidatorStatuses() case "/cosmos.staking.v1beta1.MsgBeginRedelegate": + cosmosMsg := utils.UnpackMessage(m.cdc, msg.GetBytes(), &stakingtypes.MsgBeginRedelegate{}) + err := m.updateStakingEvent(int64(tx.Height), cosmosMsg) + if err != nil { + return err + } + return m.UpdateValidatorStatuses() case "/cosmos.staking.v1beta1.MsgUndelegate": @@ -67,7 +75,15 @@ func (m *Module) handleMsgCreateValidator(height int64, msg *stakingtypes.MsgCre if err != nil { return fmt.Errorf("error while refreshing validator from MsgCreateValidator: %s", err) } - return nil + + var infos []multistakingtypes.ValidatorInfo + validatorInfo := multistakingtypes.ValidatorInfo{ + OperatorAddress: msg.ValidatorAddress, + BondDenom: msg.Value.Denom, + } + infos = append(infos, validatorInfo) + + return m.db.SaveValidatorDenom(height, infos) } // handleEditValidator handles MsgEditValidator utils, updating the validator info @@ -79,3 +95,15 @@ func (m *Module) handleEditValidator(height int64, msg *stakingtypes.MsgEditVali return nil } + +func (m *Module) updateStakingEvent(height int64, msg *stakingtypes.MsgBeginRedelegate) error { + var msEvents []dbtypes.MSEvent + event := dbtypes.MSEvent{ + Name: "redelegate", + ValAddr: msg.ValidatorSrcAddress, + DelAddr: msg.DelegatorAddress, + Amount: msg.Amount.String(), + } + msEvents = append(msEvents, event) + return m.db.SaveMSEvent(msEvents, height) +}