From c13d9547d88d7d7e2a3f2b94c8579ad6be662d37 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Thu, 18 Aug 2022 21:57:00 +0700 Subject: [PATCH 01/19] get unbonding delegations --- modules/staking/utils_staking_pool.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index 21991fce9..0ad042d9a 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/forbole/bdjuno/v3/types" + // sdk "github.com/cosmos/cosmos-sdk/types" ) func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { @@ -12,5 +13,30 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { return nil, fmt.Errorf("error while getting staking pool: %s", err) } + validatorsList, err := m.db.GetValidators() + if err != nil { + return nil, err + } + + var totalUnbondingTokens int64 + for _, validator := range validatorsList { + unbondingDelegations, err := m.source.GetUnbondingDelegationsFromValidator( + height, + validator.GetOperator(), + nil, + ) + if err != nil { + return nil, err + } + + for _, unbonding := range unbondingDelegations.UnbondingResponses { + for _, entry := range unbonding.Entries { + totalUnbondingTokens = totalUnbondingTokens + entry.Balance.Int64() + } + } + } + + fmt.Printf("\n \n totalUnbondingTokens %v \n \n", totalUnbondingTokens) + return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, height), nil } From d8c60eb656f84d01a431222f7668cddc49c870ec Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Thu, 18 Aug 2022 21:58:07 +0700 Subject: [PATCH 02/19] update err log --- modules/staking/utils_staking_pool.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index 0ad042d9a..66c8cd647 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -15,7 +15,7 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { validatorsList, err := m.db.GetValidators() if err != nil { - return nil, err + return nil, fmt.Errorf("error while getting validators list: %s", err) } var totalUnbondingTokens int64 @@ -26,7 +26,7 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { nil, ) if err != nil { - return nil, err + return nil, fmt.Errorf("error while getting unbonding delegations: %s", err) } for _, unbonding := range unbondingDelegations.UnbondingResponses { From 48b22625195fd78caaeadf9a96b3b28f9268b5a5 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Thu, 18 Aug 2022 22:02:07 +0700 Subject: [PATCH 03/19] add unbonding_tokens to schema --- database/schema/03-staking.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/database/schema/03-staking.sql b/database/schema/03-staking.sql index c5b9ecb4f..c1ae718e5 100644 --- a/database/schema/03-staking.sql +++ b/database/schema/03-staking.sql @@ -16,6 +16,7 @@ CREATE TABLE staking_pool one_row_id BOOLEAN NOT NULL DEFAULT TRUE PRIMARY KEY, bonded_tokens TEXT NOT NULL, not_bonded_tokens TEXT NOT NULL, + unbonding_tokens TEXT NOT NULL, height BIGINT NOT NULL, CHECK (one_row_id) ); From 2641c162907e058cc6e601f8d7ec3ee8e185de7d Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Thu, 18 Aug 2022 22:08:22 +0700 Subject: [PATCH 04/19] Add UnbondingTokens to Pool inst --- database/staking_pool.go | 5 +++-- database/types/staking_pool.go | 5 ++++- types/staking_pool_params.go | 4 +++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/database/staking_pool.go b/database/staking_pool.go index 4e2e6eae2..7a2477186 100644 --- a/database/staking_pool.go +++ b/database/staking_pool.go @@ -9,15 +9,16 @@ import ( // SaveStakingPool allows to save for the given height the given stakingtypes pool func (db *Db) SaveStakingPool(pool *types.Pool) error { stmt := ` -INSERT INTO staking_pool (bonded_tokens, not_bonded_tokens, height) +INSERT INTO staking_pool (bonded_tokens, not_bonded_tokens, unbonding_tokens, height) VALUES ($1, $2, $3) ON CONFLICT (one_row_id) DO UPDATE SET bonded_tokens = excluded.bonded_tokens, not_bonded_tokens = excluded.not_bonded_tokens, + unbonding_tokens = excluded.unbonding_tokens, height = excluded.height WHERE staking_pool.height <= excluded.height` - _, err := db.Sql.Exec(stmt, pool.BondedTokens.String(), pool.NotBondedTokens.String(), pool.Height) + _, err := db.Sql.Exec(stmt, pool.BondedTokens.String(), pool.NotBondedTokens.String(), pool.UnbondingTokens.String(), pool.Height) if err != nil { return fmt.Errorf("error while storing staking pool: %s", err) } diff --git a/database/types/staking_pool.go b/database/types/staking_pool.go index 0fae3620a..373262e88 100644 --- a/database/types/staking_pool.go +++ b/database/types/staking_pool.go @@ -5,15 +5,17 @@ type StakingPoolRow struct { OneRowID bool `db:"one_row_id"` BondedTokens int64 `db:"bonded_tokens"` NotBondedTokens int64 `db:"not_bonded_tokens"` + UnbondingTokens int64 `db:"unbonding_tokens"` Height int64 `db:"height"` } // NewStakingPoolRow allows to easily create a new StakingPoolRow -func NewStakingPoolRow(bondedTokens, notBondedTokens int64, height int64) StakingPoolRow { +func NewStakingPoolRow(bondedTokens, notBondedTokens, unbondingTokens int64, height int64) StakingPoolRow { return StakingPoolRow{ OneRowID: true, BondedTokens: bondedTokens, NotBondedTokens: notBondedTokens, + UnbondingTokens: unbondingTokens, Height: height, } } @@ -22,5 +24,6 @@ func NewStakingPoolRow(bondedTokens, notBondedTokens int64, height int64) Stakin func (r StakingPoolRow) Equal(s StakingPoolRow) bool { return r.BondedTokens == s.BondedTokens && r.NotBondedTokens == s.NotBondedTokens && + r.UnbondingTokens == s.UnbondingTokens && r.Height == s.Height } diff --git a/types/staking_pool_params.go b/types/staking_pool_params.go index ced553090..164302fae 100644 --- a/types/staking_pool_params.go +++ b/types/staking_pool_params.go @@ -9,14 +9,16 @@ import ( type Pool struct { BondedTokens sdk.Int NotBondedTokens sdk.Int + UnbondingTokens sdk.Int Height int64 } // NewPool allows to build a new Pool instance -func NewPool(bondedTokens, notBondedTokens sdk.Int, height int64) *Pool { +func NewPool(bondedTokens, notBondedTokens, unbondingTokens sdk.Int, height int64) *Pool { return &Pool{ BondedTokens: bondedTokens, NotBondedTokens: notBondedTokens, + UnbondingTokens: unbondingTokens, Height: height, } } From bf50f9793e606b815ffe1584694c8963802c0edb Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Thu, 18 Aug 2022 22:20:26 +0700 Subject: [PATCH 05/19] unbonding tokens --- modules/staking/utils_staking_pool.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index 66c8cd647..6ebca3411 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -3,8 +3,8 @@ package staking import ( "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/forbole/bdjuno/v3/types" - // sdk "github.com/cosmos/cosmos-sdk/types" ) func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { @@ -18,7 +18,7 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { return nil, fmt.Errorf("error while getting validators list: %s", err) } - var totalUnbondingTokens int64 + var unbondingTokens int64 for _, validator := range validatorsList { unbondingDelegations, err := m.source.GetUnbondingDelegationsFromValidator( height, @@ -31,12 +31,12 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { for _, unbonding := range unbondingDelegations.UnbondingResponses { for _, entry := range unbonding.Entries { - totalUnbondingTokens = totalUnbondingTokens + entry.Balance.Int64() + unbondingTokens += entry.Balance.Int64() } } } - fmt.Printf("\n \n totalUnbondingTokens %v \n \n", totalUnbondingTokens) + fmt.Printf("\n \n totalUnbondingTokens %v \n \n", unbondingTokens) - return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, height), nil + return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, sdk.NewInt(unbondingTokens), height), nil } From b4ea933471c6a2dc1bb31f5276926940bb460aa6 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Thu, 18 Aug 2022 22:52:57 +0700 Subject: [PATCH 06/19] add staked_not_bonded_tokens column --- database/schema/03-staking.sql | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/database/schema/03-staking.sql b/database/schema/03-staking.sql index c1ae718e5..f2f1eb491 100644 --- a/database/schema/03-staking.sql +++ b/database/schema/03-staking.sql @@ -13,11 +13,12 @@ CREATE INDEX staking_params_height_index ON staking_params (height); CREATE TABLE staking_pool ( - one_row_id BOOLEAN NOT NULL DEFAULT TRUE PRIMARY KEY, - bonded_tokens TEXT NOT NULL, - not_bonded_tokens TEXT NOT NULL, - unbonding_tokens TEXT NOT NULL, - height BIGINT NOT NULL, + one_row_id BOOLEAN NOT NULL DEFAULT TRUE PRIMARY KEY, + bonded_tokens TEXT NOT NULL, + not_bonded_tokens TEXT NOT NULL, + unbonding_tokens TEXT NOT NULL, + staked_not_bonded_tokens TEXT NOT NULL, + height BIGINT NOT NULL, CHECK (one_row_id) ); CREATE INDEX staking_pool_height_index ON staking_pool (height); From cab1d4368202390ccb49755f0c6b7da57accdc0d Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Thu, 18 Aug 2022 22:53:39 +0700 Subject: [PATCH 07/19] updated types --- database/staking_pool.go | 10 ++++++++-- database/types/staking_pool.go | 25 ++++++++++++++----------- types/staking_pool_params.go | 20 +++++++++++--------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/database/staking_pool.go b/database/staking_pool.go index 7a2477186..b5c807566 100644 --- a/database/staking_pool.go +++ b/database/staking_pool.go @@ -9,16 +9,22 @@ import ( // SaveStakingPool allows to save for the given height the given stakingtypes pool func (db *Db) SaveStakingPool(pool *types.Pool) error { stmt := ` -INSERT INTO staking_pool (bonded_tokens, not_bonded_tokens, unbonding_tokens, height) +INSERT INTO staking_pool (bonded_tokens, not_bonded_tokens, unbonding_tokens, staked_not_bonded_tokens, height) VALUES ($1, $2, $3) ON CONFLICT (one_row_id) DO UPDATE SET bonded_tokens = excluded.bonded_tokens, not_bonded_tokens = excluded.not_bonded_tokens, unbonding_tokens = excluded.unbonding_tokens, + staked_not_bonded_tokens = excluded.staked_not_bonded_tokens, height = excluded.height WHERE staking_pool.height <= excluded.height` - _, err := db.Sql.Exec(stmt, pool.BondedTokens.String(), pool.NotBondedTokens.String(), pool.UnbondingTokens.String(), pool.Height) + _, err := db.Sql.Exec(stmt, + pool.BondedTokens.String(), + pool.NotBondedTokens.String(), + pool.UnbondingTokens.String(), + pool.StakedNotBondedTokens.String(), + pool.Height) if err != nil { return fmt.Errorf("error while storing staking pool: %s", err) } diff --git a/database/types/staking_pool.go b/database/types/staking_pool.go index 373262e88..b97b96a0c 100644 --- a/database/types/staking_pool.go +++ b/database/types/staking_pool.go @@ -2,21 +2,23 @@ package types // StakingPoolRow represents a single row inside the staking_pool table type StakingPoolRow struct { - OneRowID bool `db:"one_row_id"` - BondedTokens int64 `db:"bonded_tokens"` - NotBondedTokens int64 `db:"not_bonded_tokens"` - UnbondingTokens int64 `db:"unbonding_tokens"` - Height int64 `db:"height"` + OneRowID bool `db:"one_row_id"` + BondedTokens int64 `db:"bonded_tokens"` + NotBondedTokens int64 `db:"not_bonded_tokens"` + UnbondingTokens int64 `db:"unbonding_tokens"` + StakedNotBondedTokens int64 `db:"staked_not_bonded_tokens"` + Height int64 `db:"height"` } // NewStakingPoolRow allows to easily create a new StakingPoolRow -func NewStakingPoolRow(bondedTokens, notBondedTokens, unbondingTokens int64, height int64) StakingPoolRow { +func NewStakingPoolRow(bondedTokens, notBondedTokens, unbondingTokens, stakedNotBondedTokens int64, height int64) StakingPoolRow { return StakingPoolRow{ - OneRowID: true, - BondedTokens: bondedTokens, - NotBondedTokens: notBondedTokens, - UnbondingTokens: unbondingTokens, - Height: height, + OneRowID: true, + BondedTokens: bondedTokens, + NotBondedTokens: notBondedTokens, + UnbondingTokens: unbondingTokens, + StakedNotBondedTokens: stakedNotBondedTokens, + Height: height, } } @@ -25,5 +27,6 @@ func (r StakingPoolRow) Equal(s StakingPoolRow) bool { return r.BondedTokens == s.BondedTokens && r.NotBondedTokens == s.NotBondedTokens && r.UnbondingTokens == s.UnbondingTokens && + r.StakedNotBondedTokens == s.StakedNotBondedTokens && r.Height == s.Height } diff --git a/types/staking_pool_params.go b/types/staking_pool_params.go index 164302fae..708ed1ed0 100644 --- a/types/staking_pool_params.go +++ b/types/staking_pool_params.go @@ -7,19 +7,21 @@ import ( // Pool contains the data of the staking pool at the given height type Pool struct { - BondedTokens sdk.Int - NotBondedTokens sdk.Int - UnbondingTokens sdk.Int - Height int64 + BondedTokens sdk.Int + NotBondedTokens sdk.Int + UnbondingTokens sdk.Int + StakedNotBondedTokens sdk.Int + Height int64 } // NewPool allows to build a new Pool instance -func NewPool(bondedTokens, notBondedTokens, unbondingTokens sdk.Int, height int64) *Pool { +func NewPool(bondedTokens, notBondedTokens, unbondingTokens, stakedNotBondedTokens sdk.Int, height int64) *Pool { return &Pool{ - BondedTokens: bondedTokens, - NotBondedTokens: notBondedTokens, - UnbondingTokens: unbondingTokens, - Height: height, + BondedTokens: bondedTokens, + NotBondedTokens: notBondedTokens, + UnbondingTokens: unbondingTokens, + StakedNotBondedTokens: stakedNotBondedTokens, + Height: height, } } From 44dbf769893fd47ab6ca3e53e704de03502fac7f Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Thu, 18 Aug 2022 22:57:54 +0700 Subject: [PATCH 08/19] add staked not bonded tokens calc --- modules/staking/utils_staking_pool.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index 6ebca3411..4bf89b492 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -36,7 +36,9 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { } } + stakedNotBondedTokens := pool.NotBondedTokens.Sub(sdk.NewInt(unbondingTokens)) + fmt.Printf("\n \n totalUnbondingTokens %v \n \n", unbondingTokens) - return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, sdk.NewInt(unbondingTokens), height), nil + return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, sdk.NewInt(unbondingTokens), stakedNotBondedTokens, height), nil } From 547b6e7cdd773e2a01f213c22a7d2c54965b8f14 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Fri, 19 Aug 2022 17:33:13 +0700 Subject: [PATCH 09/19] update staking pool test --- database/staking_pool.go | 2 +- database/staking_pool_test.go | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/database/staking_pool.go b/database/staking_pool.go index b5c807566..4b8637239 100644 --- a/database/staking_pool.go +++ b/database/staking_pool.go @@ -10,7 +10,7 @@ import ( func (db *Db) SaveStakingPool(pool *types.Pool) error { stmt := ` INSERT INTO staking_pool (bonded_tokens, not_bonded_tokens, unbonding_tokens, staked_not_bonded_tokens, height) -VALUES ($1, $2, $3) +VALUES ($1, $2, $3, $4, $5) ON CONFLICT (one_row_id) DO UPDATE SET bonded_tokens = excluded.bonded_tokens, not_bonded_tokens = excluded.not_bonded_tokens, diff --git a/database/staking_pool_test.go b/database/staking_pool_test.go index 290bd66cf..49b13f7b4 100644 --- a/database/staking_pool_test.go +++ b/database/staking_pool_test.go @@ -9,12 +9,12 @@ import ( func (suite *DbTestSuite) TestBigDipperDb_SaveStakingPool() { // Save the data - original := types.NewPool(sdk.NewInt(50), sdk.NewInt(100), 10) + original := types.NewPool(sdk.NewInt(50), sdk.NewInt(100), sdk.NewInt(5), sdk.NewInt(1), 10) err := suite.database.SaveStakingPool(original) suite.Require().NoError(err) // Verify the data - expected := dbtypes.NewStakingPoolRow(50, 100, 10) + expected := dbtypes.NewStakingPoolRow(50, 100, 5, 1, 10) var rows []dbtypes.StakingPoolRow err = suite.database.Sqlx.Select(&rows, `SELECT * FROM staking_pool`) @@ -25,7 +25,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveStakingPool() { // ---------------------------------------------------------------------------------------------------------------- // Try updating using a lower height - pool := types.NewPool(sdk.NewInt(1), sdk.NewInt(1), 8) + pool := types.NewPool(sdk.NewInt(1), sdk.NewInt(1), sdk.NewInt(1), sdk.NewInt(1), 8) err = suite.database.SaveStakingPool(pool) suite.Require().NoError(err) @@ -39,12 +39,12 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveStakingPool() { // ---------------------------------------------------------------------------------------------------------------- // Try updating with the same height - pool = types.NewPool(sdk.NewInt(1), sdk.NewInt(1), 10) + pool = types.NewPool(sdk.NewInt(1), sdk.NewInt(1), sdk.NewInt(1), sdk.NewInt(1), 10) err = suite.database.SaveStakingPool(pool) suite.Require().NoError(err) // Verify the data - expected = dbtypes.NewStakingPoolRow(1, 1, 10) + expected = dbtypes.NewStakingPoolRow(1, 1, 1, 1, 10) rows = []dbtypes.StakingPoolRow{} err = suite.database.Sqlx.Select(&rows, `SELECT * FROM staking_pool`) @@ -55,12 +55,12 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveStakingPool() { // ---------------------------------------------------------------------------------------------------------------- // Try updating with a higher height - pool = types.NewPool(sdk.NewInt(1000000), sdk.NewInt(1000000), 20) + pool = types.NewPool(sdk.NewInt(1000000), sdk.NewInt(1000000), sdk.NewInt(20), sdk.NewInt(15), 20) err = suite.database.SaveStakingPool(pool) suite.Require().NoError(err) // Verify the data - expected = dbtypes.NewStakingPoolRow(1000000, 1000000, 20) + expected = dbtypes.NewStakingPoolRow(1000000, 1000000, 20, 15, 20) rows = []dbtypes.StakingPoolRow{} err = suite.database.Sqlx.Select(&rows, `SELECT * FROM staking_pool`) From 14ca88776226ea7cd3cd0ac50c0967f4090fefcc Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Fri, 19 Aug 2022 17:49:37 +0700 Subject: [PATCH 10/19] mv staking pool to periodic ops --- modules/staking/handle_block.go | 23 --------- modules/staking/handle_periodic_operations.go | 48 +++++++++++++++++++ modules/staking/module.go | 9 ++-- 3 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 modules/staking/handle_periodic_operations.go diff --git a/modules/staking/handle_block.go b/modules/staking/handle_block.go index b6dbcc6a0..65732df9c 100644 --- a/modules/staking/handle_block.go +++ b/modules/staking/handle_block.go @@ -33,9 +33,6 @@ func (m *Module) HandleBlock( // Updated the double sign evidences go m.updateDoubleSignEvidence(block.Block.Height, block.Block.Evidence.Evidence) - // Update the staking pool - go m.updateStakingPool(block.Block.Height) - return nil } @@ -123,23 +120,3 @@ func (m *Module) updateDoubleSignEvidence(height int64, evidenceList tmtypes.Evi } } - -// updateStakingPool reads from the LCD the current staking pool and stores its value inside the database -func (m *Module) updateStakingPool(height int64) { - log.Debug().Str("module", "staking").Int64("height", height). - Msg("updating staking pool") - - pool, err := m.GetStakingPool(height) - if err != nil { - log.Error().Str("module", "staking").Err(err).Int64("height", height). - Msg("error while getting staking pool") - return - } - - err = m.db.SaveStakingPool(pool) - if err != nil { - log.Error().Str("module", "staking").Err(err).Int64("height", height). - Msg("error while saving staking pool") - return - } -} diff --git a/modules/staking/handle_periodic_operations.go b/modules/staking/handle_periodic_operations.go new file mode 100644 index 000000000..395042f77 --- /dev/null +++ b/modules/staking/handle_periodic_operations.go @@ -0,0 +1,48 @@ +package staking + +import ( + "fmt" + + "github.com/go-co-op/gocron" + "github.com/rs/zerolog/log" + + "github.com/forbole/bdjuno/v3/modules/utils" +) + +// RegisterPeriodicOperations implements modules.PeriodicOperationsModule +func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error { + log.Debug().Str("module", "staking").Msg("setting up periodic tasks") + + // Update the staking pool every 5 mins + if _, err := scheduler.Every(5).Minutes().Do(func() { + utils.WatchMethod(m.updateStakingPool) + }); err != nil { + return fmt.Errorf("error while scheduling staking pool periodic operation: %s", err) + } + + return nil +} + +// updateStakingPool reads from the LCD the current staking pool and stores its value inside the database +func (m *Module) updateStakingPool() error { + height, err := m.db.GetLastBlockHeight() + if err != nil { + return fmt.Errorf("error while getting latest block height: %s", err) + } + log.Debug().Str("module", "staking").Int64("height", height). + Msg("updating staking pool") + + pool, err := m.GetStakingPool(height) + if err != nil { + return fmt.Errorf("error while getting staking pool: %s", err) + + } + + err = m.db.SaveStakingPool(pool) + if err != nil { + return fmt.Errorf("error while saving staking pool: %s", err) + + } + + return nil +} diff --git a/modules/staking/module.go b/modules/staking/module.go index 10f42c6be..c6c0d58aa 100644 --- a/modules/staking/module.go +++ b/modules/staking/module.go @@ -9,10 +9,11 @@ import ( ) var ( - _ modules.Module = &Module{} - _ modules.GenesisModule = &Module{} - _ modules.BlockModule = &Module{} - _ modules.MessageModule = &Module{} + _ modules.Module = &Module{} + _ modules.GenesisModule = &Module{} + _ modules.BlockModule = &Module{} + _ modules.MessageModule = &Module{} + _ modules.PeriodicOperationsModule = &Module{} ) // Module represents the x/staking module From 31bd614758fb87ee0f0edb0efed8d224e63ac121 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Fri, 19 Aug 2022 17:51:58 +0700 Subject: [PATCH 11/19] updated utils --- modules/staking/utils_staking_pool.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index 4bf89b492..23202c3d1 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -25,20 +25,22 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { validator.GetOperator(), nil, ) + if err != nil { - return nil, fmt.Errorf("error while getting unbonding delegations: %s", err) + // Do nothing + continue } + // calculate total unbonding tokens value for _, unbonding := range unbondingDelegations.UnbondingResponses { for _, entry := range unbonding.Entries { + // add to total unbonding value unbondingTokens += entry.Balance.Int64() } } } - stakedNotBondedTokens := pool.NotBondedTokens.Sub(sdk.NewInt(unbondingTokens)) - - fmt.Printf("\n \n totalUnbondingTokens %v \n \n", unbondingTokens) + stakedNotBondedTokens := pool.NotBondedTokens.Int64() - unbondingTokens - return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, sdk.NewInt(unbondingTokens), stakedNotBondedTokens, height), nil + return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, sdk.NewInt(unbondingTokens), sdk.NewInt(stakedNotBondedTokens), height), nil } From c449d377f953a373b7e7bb2c9499bf4c3044a0a7 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Fri, 19 Aug 2022 18:03:22 +0700 Subject: [PATCH 12/19] updated hasura metadata --- .../metadata/databases/bdjuno/tables/public_staking_pool.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/hasura/metadata/databases/bdjuno/tables/public_staking_pool.yaml b/hasura/metadata/databases/bdjuno/tables/public_staking_pool.yaml index b2dfa5e66..0f7c65e9e 100644 --- a/hasura/metadata/databases/bdjuno/tables/public_staking_pool.yaml +++ b/hasura/metadata/databases/bdjuno/tables/public_staking_pool.yaml @@ -8,5 +8,7 @@ select_permissions: - height - bonded_tokens - not_bonded_tokens + - unbonding_tokens + - staked_not_bonded_tokens filter: {} role: anonymous From 7e7f26abb101c3a0d9ecfe8b8db1e5fef82ef5b1 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Fri, 19 Aug 2022 19:05:06 +0700 Subject: [PATCH 13/19] updated comments --- modules/staking/utils_staking_pool.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index 23202c3d1..68617d7a4 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -31,7 +31,7 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { continue } - // calculate total unbonding tokens value + // calculate total value of unbonding tokens for _, unbonding := range unbondingDelegations.UnbondingResponses { for _, entry := range unbonding.Entries { // add to total unbonding value @@ -40,6 +40,7 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { } } + // calculate total value of staked tokens that are not bonded stakedNotBondedTokens := pool.NotBondedTokens.Int64() - unbondingTokens return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, sdk.NewInt(unbondingTokens), sdk.NewInt(stakedNotBondedTokens), height), nil From d936230af5c1e4090b966f5dcf3619c60ad2c2a2 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Fri, 19 Aug 2022 19:09:08 +0700 Subject: [PATCH 14/19] added CHANGELOG.md entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce15182fb..b1bf851a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Changes #### Staking Module - ([\#443](https://github.com/forbole/bdjuno/pull/443)) Remove tombstone status from staking module(already stored in slashing module) +- ([\#455](https://github.com/forbole/bdjuno/pull/455)) Added `unbonding_tokens` and `staked_not_bonded_tokens` values to staking pool table + ## Version v3.2.0 ### Changes From 874896c37ca1604a85a644f7fbc0e17635aa7660 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Fri, 19 Aug 2022 19:17:29 +0700 Subject: [PATCH 15/19] fix test --- database/gov_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/database/gov_test.go b/database/gov_test.go index 6050bfaa3..51a56c09c 100644 --- a/database/gov_test.go +++ b/database/gov_test.go @@ -508,6 +508,8 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalStakingPoolSnapshot() { snapshot := types.NewProposalStakingPoolSnapshot(1, types.NewPool( sdk.NewInt(100), sdk.NewInt(200), + sdk.NewInt(20), + sdk.NewInt(30), 10, )) err := suite.database.SaveProposalStakingPoolSnapshot(snapshot) @@ -530,6 +532,8 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalStakingPoolSnapshot() { err = suite.database.SaveProposalStakingPoolSnapshot(types.NewProposalStakingPoolSnapshot(1, types.NewPool( sdk.NewInt(200), sdk.NewInt(500), + sdk.NewInt(14), + sdk.NewInt(10), 9, ))) suite.Require().NoError(err) @@ -551,6 +555,8 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalStakingPoolSnapshot() { err = suite.database.SaveProposalStakingPoolSnapshot(types.NewProposalStakingPoolSnapshot(1, types.NewPool( sdk.NewInt(500), sdk.NewInt(1000), + sdk.NewInt(20), + sdk.NewInt(30), 10, ))) suite.Require().NoError(err) @@ -572,6 +578,8 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveProposalStakingPoolSnapshot() { err = suite.database.SaveProposalStakingPoolSnapshot(types.NewProposalStakingPoolSnapshot(1, types.NewPool( sdk.NewInt(1000), sdk.NewInt(2000), + sdk.NewInt(80), + sdk.NewInt(40), 11, ))) suite.Require().NoError(err) From 38a995f89775b89c318ba15d6fae855935d4bfc1 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Tue, 23 Aug 2022 17:09:02 +0700 Subject: [PATCH 16/19] update unbondingTokens type to sdk.Int --- modules/staking/utils_staking_pool.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index 68617d7a4..432005977 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -18,7 +18,7 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { return nil, fmt.Errorf("error while getting validators list: %s", err) } - var unbondingTokens int64 + var unbondingTokens sdk.Int for _, validator := range validatorsList { unbondingDelegations, err := m.source.GetUnbondingDelegationsFromValidator( height, @@ -35,13 +35,13 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { for _, unbonding := range unbondingDelegations.UnbondingResponses { for _, entry := range unbonding.Entries { // add to total unbonding value - unbondingTokens += entry.Balance.Int64() + unbondingTokens = unbondingTokens.Add(entry.Balance) } } } // calculate total value of staked tokens that are not bonded - stakedNotBondedTokens := pool.NotBondedTokens.Int64() - unbondingTokens + stakedNotBondedTokens := pool.NotBondedTokens.Sub(unbondingTokens) - return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, sdk.NewInt(unbondingTokens), sdk.NewInt(stakedNotBondedTokens), height), nil + return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, unbondingTokens, stakedNotBondedTokens, height), nil } From e4973973a71c8cf924ed2417f808acdcf1178915 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Tue, 23 Aug 2022 17:25:56 +0700 Subject: [PATCH 17/19] updated staking pool query --- modules/staking/utils_staking_pool.go | 32 ++++++++++++++++++--------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index 432005977..c53b90f2d 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -4,6 +4,8 @@ import ( "fmt" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/query" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/forbole/bdjuno/v3/types" ) @@ -20,19 +22,11 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { var unbondingTokens sdk.Int for _, validator := range validatorsList { - unbondingDelegations, err := m.source.GetUnbondingDelegationsFromValidator( - height, - validator.GetOperator(), - nil, - ) - - if err != nil { - // Do nothing - continue - } + // get list of all unbonding delegations for each validator + unbondingDelegations := m.getTotalUnbondingDelegationsFromValidator(height, validator.GetOperator()) // calculate total value of unbonding tokens - for _, unbonding := range unbondingDelegations.UnbondingResponses { + for _, unbonding := range unbondingDelegations { for _, entry := range unbonding.Entries { // add to total unbonding value unbondingTokens = unbondingTokens.Add(entry.Balance) @@ -45,3 +39,19 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { return types.NewPool(pool.BondedTokens, pool.NotBondedTokens, unbondingTokens, stakedNotBondedTokens, height), nil } + +func (m *Module) getTotalUnbondingDelegationsFromValidator(height int64, valOperatorAddress string) []stakingtypes.UnbondingDelegation { + var unbondingDelegations []stakingtypes.UnbondingDelegation + var nextKey []byte + var stop = false + for !stop { + res, _ := m.source.GetUnbondingDelegationsFromValidator(height, + valOperatorAddress, + &query.PageRequest{Key: nextKey}, + ) + nextKey = res.Pagination.NextKey + stop = len(res.Pagination.NextKey) == 0 + unbondingDelegations = append(unbondingDelegations, res.UnbondingResponses...) + } + return unbondingDelegations +} From a28e90cfa3a0b5da598aa9a55819b9eea3c81dd0 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Tue, 23 Aug 2022 18:15:28 +0700 Subject: [PATCH 18/19] updated unbonding tokens query --- modules/staking/utils_staking_pool.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index c53b90f2d..2edc8cc3b 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -20,18 +20,20 @@ func (m *Module) GetStakingPool(height int64) (*types.Pool, error) { return nil, fmt.Errorf("error while getting validators list: %s", err) } - var unbondingTokens sdk.Int + var unbondingTokens = sdk.NewInt(0) + for _, validator := range validatorsList { // get list of all unbonding delegations for each validator unbondingDelegations := m.getTotalUnbondingDelegationsFromValidator(height, validator.GetOperator()) - - // calculate total value of unbonding tokens - for _, unbonding := range unbondingDelegations { - for _, entry := range unbonding.Entries { - // add to total unbonding value - unbondingTokens = unbondingTokens.Add(entry.Balance) + if len(unbondingDelegations) > 0 { + // calculate total value of unbonding tokens + for _, unbonding := range unbondingDelegations { + for _, entry := range unbonding.Entries { + unbondingTokens = unbondingTokens.Add(entry.Balance) + } } } + } // calculate total value of staked tokens that are not bonded From d99de0b5d263080e25c76431ee8dd02d5ad82025 Mon Sep 17 00:00:00 2001 From: Magic Cat Date: Tue, 23 Aug 2022 19:08:23 +0700 Subject: [PATCH 19/19] handle error --- modules/staking/utils_staking_pool.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/staking/utils_staking_pool.go b/modules/staking/utils_staking_pool.go index 2edc8cc3b..9164ff6ed 100644 --- a/modules/staking/utils_staking_pool.go +++ b/modules/staking/utils_staking_pool.go @@ -47,10 +47,14 @@ func (m *Module) getTotalUnbondingDelegationsFromValidator(height int64, valOper var nextKey []byte var stop = false for !stop { - res, _ := m.source.GetUnbondingDelegationsFromValidator(height, + res, err := m.source.GetUnbondingDelegationsFromValidator(height, valOperatorAddress, &query.PageRequest{Key: nextKey}, ) + if err != nil { + return []stakingtypes.UnbondingDelegation{} + } + nextKey = res.Pagination.NextKey stop = len(res.Pagination.NextKey) == 0 unbondingDelegations = append(unbondingDelegations, res.UnbondingResponses...)