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

update callisto: module asset, module multistaking, account balance, token holder #1

Merged
merged 7 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
61 changes: 61 additions & 0 deletions database/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
dbtypes "github.com/forbole/callisto/v4/database/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/forbole/callisto/v4/types"
"github.com/lib/pq"
)

Expand All @@ -26,3 +27,63 @@ WHERE supply.height <= excluded.height`

return nil
}

func (db *Db) SaveTokenHolder(tokens map[string]int, height int64) error {
if len(tokens) == 0 {
return nil
}

query := `INSERT INTO token_holder (denom, num_holder, height) VALUES`

var param []interface{}
i := 0
for denom, amount := range tokens {
vi := i * 3
query += fmt.Sprintf("($%d,$%d,$%d),", vi+1, vi+2, vi+3)
param = append(param, denom, amount, height)
i++
}

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

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

return nil
}

func (db *Db) SaveAccountBalances(accountBalances []types.AccountBalance, height int64) error {
if len(accountBalances) == 0 {
return nil
}

query := `INSERT INTO balance (address, balances, height) VALUES`

var param []interface{}
for i, accountBalance := range accountBalances {
vi := i * 3
query += fmt.Sprintf("($%d,$%d,$%d),", vi+1, vi+2, vi+3)
param = append(param, accountBalance.Address, pq.Array(dbtypes.NewDbCoins(accountBalance.Balance)), height)
}

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

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

return nil
}
225 changes: 225 additions & 0 deletions database/multistaking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package database

import (
"fmt"

cosmossdk_io_math "cosmossdk.io/math"
dbtypes "github.com/forbole/callisto/v4/database/types"

"github.com/lib/pq"
multistakingtypes "github.com/realio-tech/multi-staking-module/x/multi-staking/types"
)

func (db *Db) SaveMultiStakingLocks(height int64, multiStakingLocks []*multistakingtypes.MultiStakingLock) error {
if len(multiStakingLocks) == 0 {
return nil
}

query := `INSERT INTO ms_locks (staker_addr, val_addr, ms_lock, height) VALUES`

var param []interface{}

for i, msLock := range multiStakingLocks {
vi := i * 4
query += fmt.Sprintf("($%d,$%d,$%d,$%d),", vi+1, vi+2, vi+3, vi+4)
mStakerAddr := msLock.LockID.MultiStakerAddr
valAddr := msLock.LockID.ValAddr
msCoin := dbtypes.NewMSCoin(msLock.LockedCoin)
var mscoins dbtypes.MSCoins
mscoins = append(mscoins, &msCoin)
param = append(param, mStakerAddr, valAddr, pq.Array(mscoins), height)
}

query = query[:len(query)-1] // Remove trailing ","
query += `
ON CONFLICT (staker_addr, val_addr) DO UPDATE
SET ms_lock = excluded.ms_lock,
height = excluded.height
WHERE ms_locks.height <= excluded.height`

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

return nil
}

func (db *Db) SaveMultiStakingUnlocks(height int64, multiStakingUnlocks []*multistakingtypes.MultiStakingUnlock) error {
if len(multiStakingUnlocks) == 0 {
return nil
}

query := `INSERT INTO ms_unlocks (staker_addr, val_addr, unlock_entry, height) VALUES`

var param []interface{}

for i, msUnlock := range multiStakingUnlocks {
vi := i * 4
query += fmt.Sprintf("($%d,$%d,$%d,$%d),", vi+1, vi+2, vi+3, vi+4)
mStakerAddr := msUnlock.UnlockID.MultiStakerAddr
valAddr := msUnlock.UnlockID.ValAddr
entries := msUnlock.Entries
param = append(param, mStakerAddr, valAddr, pq.Array(dbtypes.NewUnlockEntries(entries)), height)
}

query = query[:len(query)-1] // Remove trailing ","
query += `
ON CONFLICT (staker_addr, val_addr) DO UPDATE
SET unlock_entry = excluded.unlock_entry,
height = excluded.height
WHERE ms_unlocks.height <= excluded.height`

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

return nil
}

func (db *Db) SaveUnbondingToken(height int64, multiStakingUnlocks []*multistakingtypes.MultiStakingUnlock) error {
total := make(map[string]cosmossdk_io_math.Int)

for _, msUnlock := range multiStakingUnlocks {
entries := msUnlock.Entries
for _, entry := range entries {
denom := entry.UnlockingCoin.Denom
amount := entry.UnlockingCoin.Amount
if total[denom].IsNil() {
total[denom] = amount
} else {
total[denom].Add(amount)
}
}
}

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() {
total[denom] = amount
} else {
total[denom].Add(amount)
}
}

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
}

func (db *Db) SaveValidatorDenom(height int64, validatorInfo []multistakingtypes.ValidatorInfo) error {
if len(validatorInfo) == 0 {
return nil
}

query := `INSERT INTO validator_denom (val_addr, denom, height) VALUES`

var param []interface{}
for i, info := range validatorInfo {
vi := i * 3
query += fmt.Sprintf("($%d,$%d,$%d),", vi+1, vi+2, vi+3)
param = append(param, info.OperatorAddress, info.BondDenom, height)
}

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

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

return nil
}

func (db *Db) SaveMSEvent(msEvents []dbtypes.MSEvent, height int64) error {
if len(msEvents) == 0 {
return nil
}

query := `INSERT INTO ms_event (height, name, val_addr, del_addr, amount) VALUES`

var param []interface{}
for i, msEvent := range msEvents {
vi := i * 5
query += fmt.Sprintf("($%d,$%d,$%d),", vi+1, vi+2, vi+3, vi+4, vi+5)

Check failure on line 213 in database/multistaking.go

View workflow job for this annotation

GitHub Actions / Unit-tests

fmt.Sprintf call needs 3 args but has 5 args
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not enough arguments

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

param = append(param, height, msEvent.Name, msEvent.ValAddr, msEvent.DelAddr, msEvent.Amount)
}

query = query[:len(query)-1] // Remove trailing ","

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

return nil
}
16 changes: 15 additions & 1 deletion database/schema/01-auth.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ CREATE TYPE COIN AS
amount TEXT
);

CREATE TYPE MS_UNLOCK_ENTRY AS
(
creation_height BIGINT,
unlock_coin COIN
);

/* ---- AUTH/ VESTING ACCOUNT ---- */
CREATE TABLE vesting_account
(
Expand All @@ -31,4 +37,12 @@ CREATE TABLE vesting_period
period_order BIGINT NOT NULL,
length BIGINT NOT NULL,
amount COIN[] NOT NULL DEFAULT '{}'
);
);

CREATE TABLE balance
(
address TEXT NOT NULL PRIMARY KEY,
balances COIN[] NOT NULL,
height BIGINT NOT NULL
);
CREATE INDEX balance_height_index ON balance (height);
11 changes: 10 additions & 1 deletion database/schema/02-bank.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ 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);

CREATE TABLE token_holder
(
denom TEXT NOT NULL PRIMARY KEY,
num_holder BIGINT NOT NULL,
height BIGINT NOT NULL
);

CREATE INDEX token_holder_height_index ON token_holder (height);
Loading
Loading