Skip to content

Commit

Permalink
handle by msg
Browse files Browse the repository at this point in the history
  • Loading branch information
vietanh2k committed Jan 7, 2025
1 parent b207b24 commit aa0b5e4
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion modules/bank/handle_msg.go → modules/asset/handle_msg.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bank
package asset

import (
"fmt"
Expand Down
17 changes: 6 additions & 11 deletions modules/asset/handle_periodic_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,36 @@ 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"
)

// 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")

block, err := m.db.GetLastBlockHeightAndTimestamp()
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)
}
Expand All @@ -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, "")
Expand Down
2 changes: 2 additions & 0 deletions modules/asset/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
var (
_ modules.Module = &Module{}
_ modules.PeriodicOperationsModule = &Module{}
_ modules.MessageModule = &Module{}
_ modules.AuthzMessageModule = &Module{}
)

// Module represents the x/staking module
Expand Down
2 changes: 1 addition & 1 deletion modules/bank/handle_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 0 additions & 2 deletions modules/bank/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
var (
_ modules.Module = &Module{}
_ modules.PeriodicOperationsModule = &Module{}
_ modules.MessageModule = &Module{}
_ modules.AuthzMessageModule = &Module{}
_ modules.BlockModule = &Module{}
)

Expand Down
10 changes: 0 additions & 10 deletions modules/multistaking/handle_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion modules/multistaking/handle_periodic_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 29 additions & 1 deletion modules/staking/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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":
Expand All @@ -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
Expand All @@ -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)
}

0 comments on commit aa0b5e4

Please sign in to comment.