forked from realiotech/bdjuno
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2c3d308
add num holder for token
vietanh2k c8fa8cd
add table token_holder
vietanh2k f71b30a
add save account balance, asset token
vietanh2k 2239fc0
add multistaking, txs event
vietanh2k 107339b
add token bonded, unbonding
vietanh2k b207b24
add sql multistaking
vietanh2k aa0b5e4
handle by msg
vietanh2k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not enough arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed