-
Notifications
You must be signed in to change notification settings - Fork 226
/
distribution.go
54 lines (44 loc) · 1.56 KB
/
distribution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package database
import (
"encoding/json"
"fmt"
dbtypes "github.com/forbole/bdjuno/v2/database/types"
"github.com/forbole/bdjuno/v2/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lib/pq"
)
// SaveCommunityPool allows to save for the given height the given total amount of coins
func (db *Db) SaveCommunityPool(coin sdk.DecCoins, height int64) error {
query := `
INSERT INTO community_pool(coins, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET coins = excluded.coins,
height = excluded.height
WHERE community_pool.height <= excluded.height`
_, err := db.Sql.Exec(query, pq.Array(dbtypes.NewDbDecCoins(coin)), height)
if err != nil {
return fmt.Errorf("error while storing community pool: %s", err)
}
return nil
}
// -------------------------------------------------------------------------------------------------------------------
// SaveDistributionParams allows to store the given distribution parameters inside the database
func (db *Db) SaveDistributionParams(params *types.DistributionParams) error {
paramsBz, err := json.Marshal(¶ms.Params)
if err != nil {
return fmt.Errorf("error while marshaling params: %s", err)
}
stmt := `
INSERT INTO distribution_params (params, height)
VALUES ($1, $2)
ON CONFLICT (one_row_id) DO UPDATE
SET params = excluded.params,
height = excluded.height
WHERE distribution_params.height <= excluded.height`
_, err = db.Sql.Exec(stmt, string(paramsBz), params.Height)
if err != nil {
return fmt.Errorf("error while storing distribution params: %s", err)
}
return nil
}