Skip to content

Commit

Permalink
Merge branch 'chains/realionetwork/testnet-v1' of https://github.com/…
Browse files Browse the repository at this point in the history
…decentrio/bdjuno into chains/realionetwork/testnet-v1
  • Loading branch information
neitdung committed Jan 10, 2025
2 parents 90c0187 + 421084b commit b6882cf
Show file tree
Hide file tree
Showing 8 changed files with 424 additions and 21 deletions.
78 changes: 73 additions & 5 deletions database/multistaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ func (db *Db) SaveUnbondingToken(height int64, multiStakingUnlocks []*multistaki
for _, entry := range entries {
denom := entry.UnlockingCoin.Denom
amount := entry.UnlockingCoin.Amount
if total[denom].IsNil() {
value, exists := total[denom]
if !exists {
total[denom] = amount
} else {
total[denom].Add(amount)
total[denom] = value.Add(amount)
}
}
}
Expand Down Expand Up @@ -126,16 +127,50 @@ WHERE token_unbonding.height <= excluded.height`
return nil
}

func (db *Db) SaveUnbondingToken2(height int64, total map[string]cosmossdk_io_math.Int) error {
if len(total) == 0 {
return nil
}

query := `INSERT INTO token_unbonding (denom, amount, height) VALUES`

var param []interface{}

i := 0
for denom, amount := range total {
vi := i * 3
query += fmt.Sprintf("($%d,$%d,$%d),", vi+1, vi+2, vi+3)

param = append(param, denom, amount.String(), height)
i++
}

query = query[:len(query)-1] // Remove trailing ","
query += `
ON CONFLICT (denom) DO UPDATE
SET amount = excluded.amount,
height = excluded.height
WHERE token_unbonding.height <= excluded.height`

_, err := db.SQL.Exec(query, param...)
if err != nil {
return fmt.Errorf("error while saving token_unbonding: %s", err)
}

return nil
}

func (db *Db) SaveBondedToken(height int64, multiStakingLocks []*multistakingtypes.MultiStakingLock) error {
total := make(map[string]cosmossdk_io_math.Int)

for _, msLock := range multiStakingLocks {
denom := msLock.LockedCoin.Denom
amount := msLock.LockedCoin.Amount
if total[denom].IsNil() {
value, exists := total[denom]
if !exists {
total[denom] = amount
} else {
total[denom].Add(amount)
total[denom] = value.Add(amount)
}
}

Expand Down Expand Up @@ -167,7 +202,40 @@ WHERE token_bonded.height <= excluded.height`
if err != nil {
return fmt.Errorf("error while saving token_bonded: %s", err)
}


return nil
}

func (db *Db) SaveBondedToken2(height int64, total map[string]cosmossdk_io_math.Int) error {
if len(total) == 0 {
return nil
}

query := `INSERT INTO token_bonded (denom, amount, height) VALUES`

var param []interface{}

i := 0
for denom, amount := range total {
vi := i * 3
query += fmt.Sprintf("($%d,$%d,$%d),", vi+1, vi+2, vi+3)

param = append(param, denom, amount.String(), height)
i++
}

query = query[:len(query)-1] // Remove trailing ","
query += `
ON CONFLICT (denom) DO UPDATE
SET amount = excluded.amount,
height = excluded.height
WHERE token_bonded.height <= excluded.height`

_, err := db.SQL.Exec(query, param...)
if err != nil {
return fmt.Errorf("error while saving token_bonded: %s", err)
}

return nil
}

Expand Down
28 changes: 28 additions & 0 deletions database/types/multistaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ func (coin *MSCoin) Scan(src interface{}) error {
return nil
}

func (coin *MSCoin) getDenom() string {
return coin.Denom
}

func (coin *MSCoin) getAmount() string {
return coin.Amount
}

type MSCoins []*MSCoin

// Scan implements sql.Scanner
Expand Down Expand Up @@ -157,3 +165,23 @@ func (coins *UnlockEntries) Scan(src interface{}) error {
*coins = coinsV
return nil
}

type LockRow struct {
StakerAddr string `db:"staker_addr"`
ValAddr string `db:"val_addr"`
MsLock *MSCoins `db:"ms_lock"`
Height int64 `db:"height"`
}

type UnlockRow struct {
StakerAddr string `db:"staker_addr"`
ValAddr string `db:"val_addr"`
Entries *UnlockEntries `db:"unlock_entry"`
Height int64 `db:"height"`
}

type MSTokenRow struct {
Denom string `db:"denom"`
Amount string `db:"amount"`
Height int64 `db:"height"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@ package multistaking
import (
"fmt"

"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"

"github.com/forbole/callisto/v4/modules/utils"
)

// RegisterPeriodicOperations implements modules.Module
func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error {
log.Debug().Str("module", "multistaking").Msg("setting up periodic tasks")

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)
}
// RunAdditionalOperations implements modules.AdditionalOperationsModule
func (m *Module) RunAdditionalOperations() error {

return nil
return m.UpdateMultiStaking()
}

func (m *Module) UpdateMultiStaking() error {
Expand Down
Loading

0 comments on commit b6882cf

Please sign in to comment.