Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [cheqd] add top_accounts module #510

Merged
merged 55 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
066ca31
init
huichiaotsou Dec 15, 2022
e8a5387
add schema; finish top accounts module structure
huichiaotsou Dec 15, 2022
4bd775a
add x/bank UpdateBalances
huichiaotsou Dec 15, 2022
3ae5026
return bank balances for updating top accounts table
huichiaotsou Dec 15, 2022
b007e03
add account balance table
huichiaotsou Dec 15, 2022
937d51e
comment out non x/bank codes for testing
huichiaotsou Dec 16, 2022
bdfff91
add GetDenom() to pricefeed
huichiaotsou Dec 16, 2022
159c276
store native token only and update top_accounts sum
huichiaotsou Dec 16, 2022
3c0fa00
start implementing x/staking
huichiaotsou Dec 19, 2022
001dc0e
redo bank, drop account_balance table and store directly to top_accou…
huichiaotsou Dec 19, 2022
4202d3f
refactor x/bank
huichiaotsou Dec 19, 2022
9e645b9
handleMsgDelegate
huichiaotsou Dec 19, 2022
8a61b5a
handle MsgBeginRedelegate
huichiaotsou Dec 19, 2022
68e7bf3
unbonding
huichiaotsou Dec 19, 2022
91edbcf
handle rewards
huichiaotsou Dec 19, 2022
98965b8
resume daily reward query
huichiaotsou Dec 19, 2022
41b859a
to implement parse Cmd & add hasura metadata & address foreign key & …
huichiaotsou Dec 19, 2022
97d830d
Merge branch 'cosmos/v0.44.x' into aaron/top_accounts_module
huichiaotsou Dec 19, 2022
7c1fb89
change log
huichiaotsou Dec 19, 2022
5376c06
rm index from staking refresh fn
huichiaotsou Dec 20, 2022
37c3f67
rm index from refresh fns
huichiaotsou Dec 20, 2022
24168ba
add hasura
huichiaotsou Dec 20, 2022
3739edd
test metadata
huichiaotsou Dec 20, 2022
65598f0
TestSaveTopAccountsBalance
huichiaotsou Dec 20, 2022
e2cfbb3
TestGetAccountBalanceSum
huichiaotsou Dec 20, 2022
08d4cc2
TestUpdateTopAccountsSum
huichiaotsou Dec 20, 2022
721b59a
go v1.18 in test file
huichiaotsou Dec 20, 2022
00d7d5f
no sec
huichiaotsou Dec 20, 2022
64bcfd6
no lint
huichiaotsou Dec 20, 2022
9331cbc
`(*Module).handleMsgWithdrawDelegatorReward` - `index` is unused (unp…
huichiaotsou Dec 20, 2022
1ee1a29
(*Module).refreshBalance - result 0 (func()) is always nil (unparam)
huichiaotsou Dec 20, 2022
4f1bc23
fix sql file spacing
huichiaotsou Dec 20, 2022
f7a66cc
rm database/bank.go
huichiaotsou Dec 20, 2022
d62399a
prettier
huichiaotsou Dec 20, 2022
1d9696e
Update modules/top_accounts/utils_refresh.go
huichiaotsou Dec 21, 2022
64d7c81
Update modules/top_accounts/utils_refresh.go
huichiaotsou Dec 21, 2022
4f99f3c
Update database/types/top_accounts.go
huichiaotsou Dec 21, 2022
2195d7f
Update database/types/top_accounts.go
huichiaotsou Dec 21, 2022
5a525fe
Update modules/staking/utils_delegations.go
huichiaotsou Dec 21, 2022
dcf3558
Update modules/staking/utils_redelegations.go
huichiaotsou Dec 21, 2022
b9afea8
Update modules/staking/utils_delegations.go
huichiaotsou Dec 21, 2022
b671200
Update modules/top_accounts/handle_periodic_operations.go
huichiaotsou Dec 21, 2022
35ced69
Update modules/top_accounts/utils_refresh.go
huichiaotsou Dec 21, 2022
6e0ef57
Update modules/top_accounts/utils_refresh.go
huichiaotsou Dec 21, 2022
74c7d86
Update modules/top_accounts/utils_refresh.go
huichiaotsou Dec 21, 2022
5737a7f
Update modules/top_accounts/utils_refresh.go
huichiaotsou Dec 21, 2022
540e9f3
Update CHANGELOG.md
huichiaotsou Dec 21, 2022
fc0e341
Update modules/top_accounts/handle_msg.go
huichiaotsou Dec 21, 2022
7345172
Update modules/top_accounts/handle_msg.go
huichiaotsou Dec 21, 2022
9fb9e6e
Update modules/top_accounts/handle_msg.go
huichiaotsou Dec 21, 2022
6a2fd58
Update CHANGELOG.md
huichiaotsou Dec 21, 2022
fc128c0
Update types/bank.go
huichiaotsou Dec 21, 2022
11a6b68
Update modules/top_accounts/utils_refresh.go
huichiaotsou Dec 21, 2022
583bf86
Update modules/top_accounts/handle_periodic_operations.go
huichiaotsou Dec 21, 2022
099b6a2
Merge branch 'chains/cheqd/mainnet' into aaron/top_accounts_module
mergify[bot] Dec 25, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.18
- name: Test & Create coverage report
run: make install test-unit stop-docker-test
- name: Upload cove coverage
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Unreleased
### Changes

#### Top Accounts Module
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved
- ([\#510](https://github.com/forbole/bdjuno/pull/510)) Implemented top accounts module to store chain native token's balance for ranking
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved


#### CI
- ([\#508](https://github.com/forbole/bdjuno/pull/508)) Upgrade workflow golangci version to v1.50.1

Expand Down
28 changes: 28 additions & 0 deletions cmd/bdjuno/cheqd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
didMsgs "github.com/cheqd/cheqd-node/x/cheqd/types"
resourceMsgs "github.com/cheqd/cheqd-node/x/resource/types"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/forbole/juno/v3/modules/messages"
)

// CheqdAddressesParser represents a MessageAddressesParser for the my custom module
// here, we're using a DID as the address
func CheqdAddressesParser(_ codec.Codec, cosmosMsg sdk.Msg) ([]string, error) {
switch msg := cosmosMsg.(type) {

case *didMsgs.MsgCreateDid:
return []string{msg.GetPayload().GetId()}, nil

case *didMsgs.MsgUpdateDid:
return []string{msg.GetPayload().GetId()}, nil

case *resourceMsgs.MsgCreateResource:
return []string{msg.GetPayload().GetId()}, nil

default:
return nil, messages.MessageNotSupported(cosmosMsg)
}
}
4 changes: 4 additions & 0 deletions cmd/bdjuno/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"github.com/cheqd/cheqd-node/app"

"github.com/cosmos/cosmos-sdk/types/module"
"github.com/forbole/juno/v3/cmd"
initcmd "github.com/forbole/juno/v3/cmd/init"
Expand Down Expand Up @@ -56,6 +58,7 @@ func main() {
func getBasicManagers() []module.BasicManager {
return []module.BasicManager{
gaiaapp.ModuleBasics,
app.ModuleBasics,
}
}

Expand All @@ -65,5 +68,6 @@ func getBasicManagers() []module.BasicManager {
func getAddressesParser() messages.MessageAddressesParser {
return messages.JoinMessageParsers(
messages.CosmosMessageAddressesParser,
CheqdAddressesParser,
)
}
16 changes: 7 additions & 9 deletions database/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

dbtypes "github.com/forbole/bdjuno/v3/database/types"

bddbtypes "github.com/forbole/bdjuno/v3/database/types"
)

func (suite *DbTestSuite) TestBigDipperDb_SaveSupply() {
Expand All @@ -18,9 +16,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveSupply() {
suite.Require().NoError(err)

// Verify the data
expected := bddbtypes.NewSupplyRow(dbtypes.NewDbCoins(original), 10)
expected := dbtypes.NewSupplyRow(dbtypes.NewDbCoins(original), 10)

var rows []bddbtypes.SupplyRow
var rows []dbtypes.SupplyRow
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM supply`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1, "supply table should contain only one row")
Expand All @@ -37,7 +35,7 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveSupply() {
suite.Require().NoError(err)

// Verify the data
rows = []bddbtypes.SupplyRow{}
rows = []dbtypes.SupplyRow{}
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM supply`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1, "supply table should contain only one row")
Expand All @@ -51,9 +49,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveSupply() {
suite.Require().NoError(err)

// Verify the data
expected = bddbtypes.NewSupplyRow(dbtypes.NewDbCoins(coins), 10)
expected = dbtypes.NewSupplyRow(dbtypes.NewDbCoins(coins), 10)

rows = []bddbtypes.SupplyRow{}
rows = []dbtypes.SupplyRow{}
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM supply`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1, "supply table should contain only one row")
Expand All @@ -67,9 +65,9 @@ func (suite *DbTestSuite) TestBigDipperDb_SaveSupply() {
suite.Require().NoError(err)

// Verify the data
expected = bddbtypes.NewSupplyRow(dbtypes.NewDbCoins(coins), 20)
expected = dbtypes.NewSupplyRow(dbtypes.NewDbCoins(coins), 20)

rows = []bddbtypes.SupplyRow{}
rows = []dbtypes.SupplyRow{}
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM supply`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1, "supply table should contain only one row")
Expand Down
11 changes: 11 additions & 0 deletions database/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ WHERE distribution_params.height <= excluded.height`

return nil
}

// GetDelegators returns the current delegators set
func (db *Db) GetDelegators() ([]string, error) {
var rows []string
err := db.Sqlx.Select(&rows, `SELECT DISTINCT (address) FROM top_accounts `)
if err != nil {
return nil, err
}

return rows, nil
}
2 changes: 1 addition & 1 deletion database/schema/02-bank.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ CREATE TABLE supply
height BIGINT NOT NULL,
CHECK (one_row_id)
);
CREATE INDEX supply_height_index ON supply (height);
CREATE INDEX supply_height_index ON supply (height);
11 changes: 11 additions & 0 deletions database/schema/13-top_accounts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE top_accounts
(
address TEXT NOT NULL REFERENCES account (address) PRIMARY KEY,
available BIGINT DEFAULT 0,
delegation BIGINT DEFAULT 0,
redelegation BIGINT DEFAULT 0,
unbonding BIGINT DEFAULT 0,
reward BIGINT DEFAULT 0,
sum BIGINT NOT NULL DEFAULT 0
);
CREATE INDEX top_accounts_sum_index ON top_accounts (sum);
54 changes: 54 additions & 0 deletions database/top_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package database

import (
"fmt"

"github.com/forbole/bdjuno/v3/types"
)

func (db *Db) SaveTopAccountsBalance(column string, bals []types.NativeTokenAmount) error {
if len(bals) == 0 {
return nil
}

stmt := fmt.Sprintf("INSERT INTO top_accounts (address, %s) VALUES ", column) //nolint

var params []interface{}

for i, bal := range bals {
bi := i * 2
stmt += fmt.Sprintf("($%d, $%d),", bi+1, bi+2)

params = append(params, bal.Address, bal.Balance.String())
}

stmt = stmt[:len(stmt)-1]
stmt += fmt.Sprintf("ON CONFLICT (address) DO UPDATE SET %s = excluded.%s ", column, column)

_, err := db.Sql.Exec(stmt, params...)
return err

}

func (db *Db) GetAccountBalanceSum(address string) (string, error) {
stmt := `SELECT
COALESCE(available,0) + COALESCE(delegation,0) + COALESCE(redelegation,0) + COALESCE(unbonding,0) + COALESCE(reward,0)
as sum FROM top_accounts WHERE address = $1
`
var rows []string
err := db.Sqlx.Select(&rows, stmt, address)
if err != nil || len(rows) == 0 {
return "0", err
}

return rows[0], nil
}

func (db *Db) UpdateTopAccountsSum(address, sum string) error {
stmt := `INSERT INTO top_accounts (address, sum) VALUES ($1, $2)
ON CONFLICT (address) DO UPDATE SET sum = excluded.sum`

_, err := db.Sql.Exec(stmt, address, sum)
return err

}
143 changes: 143 additions & 0 deletions database/top_accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package database_test

import (
sdk "github.com/cosmos/cosmos-sdk/types"
dbtypes "github.com/forbole/bdjuno/v3/database/types"
"github.com/forbole/bdjuno/v3/types"
)

func (suite *DbTestSuite) TestSaveTopAccountsBalance() {
suite.getAccount("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs")

// Test saving balances
amount := types.NewNativeTokenAmount(
"cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs",
sdk.NewInt(100),
10,
)

err := suite.database.SaveTopAccountsBalance("available", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("delegation", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("redelegation", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("unbonding", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("reward", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

err = suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", "500")
suite.Require().NoError(err)

// Verify data
expected := dbtypes.NewTopAccountsRow("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", 100, 100, 100, 100, 100, 500)

var rows []dbtypes.TopAccountsRow
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM top_accounts`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1)
suite.Require().True(expected.Equals(rows[0]))

// Test saving higher values
newAmount := types.NewNativeTokenAmount(
"cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs",
sdk.NewInt(200),
10,
)

err = suite.database.SaveTopAccountsBalance("available", []types.NativeTokenAmount{newAmount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("delegation", []types.NativeTokenAmount{newAmount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("redelegation", []types.NativeTokenAmount{newAmount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("unbonding", []types.NativeTokenAmount{newAmount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("reward", []types.NativeTokenAmount{newAmount})
suite.Require().NoError(err)

err = suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", "1000")
suite.Require().NoError(err)

// Verify data
expected = dbtypes.NewTopAccountsRow("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", 200, 200, 200, 200, 200, 1000)
err = suite.database.Sqlx.Select(&rows, `SELECT * FROM top_accounts`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1)
suite.Require().True(expected.Equals(rows[0]))

}

func (suite *DbTestSuite) TestGetAccountBalanceSum() {
suite.getAccount("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs")

// Store balances
amount := types.NewNativeTokenAmount(
"cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs",
sdk.NewInt(100),
10,
)

err := suite.database.SaveTopAccountsBalance("available", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("delegation", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("redelegation", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("unbonding", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

err = suite.database.SaveTopAccountsBalance("reward", []types.NativeTokenAmount{amount})
suite.Require().NoError(err)

// Verify Data
expectedSum := "500"
sum, err := suite.database.GetAccountBalanceSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs")
suite.Require().NoError(err)
suite.Require().Equal(expectedSum, sum)

// Verify getting 0 amount
expectedSum = "0"
sum, err = suite.database.GetAccountBalanceSum("")
suite.Require().NoError(err)
suite.Require().Equal(expectedSum, sum)
}

func (suite *DbTestSuite) TestUpdateTopAccountsSum() {
suite.getAccount("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs")

// Store top accounts sum
amount := "100"
err := suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", amount)
suite.Require().NoError(err)

// Verify data
var rows []string
err = suite.database.Sqlx.Select(&rows, `SELECT sum FROM top_accounts`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1)
suite.Require().Equal(amount, rows[0])

// Store different amount
amount = "200"
err = suite.database.UpdateTopAccountsSum("cosmos1z4hfrxvlgl4s8u4n5ngjcw8kdqrcv43599amxs", amount)
suite.Require().NoError(err)

// Verify data
err = suite.database.Sqlx.Select(&rows, `SELECT sum FROM top_accounts`)
suite.Require().NoError(err)
suite.Require().Len(rows, 1)
suite.Require().Equal(amount, rows[0])
}
34 changes: 34 additions & 0 deletions database/types/top_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package types

type TopAccountsRow struct {
Address string `db:"address"`
Available int64 `db:"available"`
Delegation int64 `db:"delegation"`
Redelegation int64 `db:"redelegation"`
Unbonding int64 `db:"unbonding"`
Reward int64 `db:"reward"`
Sum int64 `db:"sum"`
}

func NewTopAccountsRow(
address string, available, delegation, redelegation, unbonding, reward, sum int64,
) TopAccountsRow {
return TopAccountsRow{
Address: address,
Available: available,
Delegation: delegation,
Redelegation: redelegation,
Unbonding: unbonding,
Reward: reward,
Sum: sum,
}
}

// Equals return true if one TopAccountsRow representing the same row as the original one
func (a TopAccountsRow) Equals(b TopAccountsRow) bool {
return a.Address == b.Address &&
a.Available == b.Available &&
a.Redelegation == b.Redelegation &&
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved
a.Unbonding == b.Unbonding &&
a.Reward == b.Reward
huichiaotsou marked this conversation as resolved.
Show resolved Hide resolved
}
Loading