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(vpool): complete genesis import export #959

Merged
merged 27 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
425fa2b
add genesis snapshots
AgentSmithMatrix Sep 20, 2022
94765e1
add pair into pair reserve snapshot
AgentSmithMatrix Sep 20, 2022
0cf99bf
include pair into snapshot
AgentSmithMatrix Sep 20, 2022
c43e54a
use new constructor part 1
AgentSmithMatrix Sep 20, 2022
db841fd
add snapshot pair
AgentSmithMatrix Sep 20, 2022
1bc521a
remove unused code
AgentSmithMatrix Sep 20, 2022
b4de2ec
replace constructor in SaveSnapshot
AgentSmithMatrix Sep 20, 2022
39a6919
fix lint
AgentSmithMatrix Sep 20, 2022
e9faeb1
use temporary reserve snapshot
AgentSmithMatrix Sep 20, 2022
4b338ec
add reserve snapshot
AgentSmithMatrix Sep 20, 2022
0696865
switch function
AgentSmithMatrix Sep 20, 2022
864678f
remove unused code
AgentSmithMatrix Sep 20, 2022
a953dd8
refactor usage on tests
AgentSmithMatrix Sep 20, 2022
e1f3021
use new constructor on prices test
AgentSmithMatrix Sep 20, 2022
7c1a302
remove usage of old function
AgentSmithMatrix Sep 20, 2022
cd79884
rename function to NewReserveSnapshot
AgentSmithMatrix Sep 20, 2022
2ab2d7f
set vpool genesis snapshots as not nullable
AgentSmithMatrix Sep 20, 2022
5df1119
use common.AssetPair for ReserveSnapshot
AgentSmithMatrix Sep 20, 2022
f17d8fc
SaveSnapshot accepts a snapshot instead of building it
AgentSmithMatrix Sep 20, 2022
2ff88cc
refavctor genesis test
AgentSmithMatrix Sep 20, 2022
81d8c9a
add genesis getAllSnapshots
AgentSmithMatrix Sep 20, 2022
4a98c73
add export and import snapshots
AgentSmithMatrix Sep 20, 2022
176294d
remove unused params
AgentSmithMatrix Sep 20, 2022
99d3934
add validate to snapshot
AgentSmithMatrix Sep 20, 2022
3348864
add genesis validate
AgentSmithMatrix Sep 20, 2022
bcef091
update changelog
AgentSmithMatrix Sep 20, 2022
b704e83
fix linter problems
AgentSmithMatrix Sep 21, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#894](https://github.com/NibiruChain/nibiru/pull/894) - add the collections package!
* [#897](https://github.com/NibiruChain/nibiru/pull/897) - x/pricefeed - use collections.
* [#933](https://github.com/NibiruChain/nibiru/pull/933) - refactor(perp): remove whitelist and simplify state keys
* [#959](https://github.com/NibiruChain/nibiru/pull/959) - feat(vpool): complete genesis import export
* removed Params from genesis.
* added pair into ReserveSnapshot type.
* added validation of snapshots and snapshots in genesis.

### API Breaking

Expand Down
2 changes: 1 addition & 1 deletion proto/vpool/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ option go_package = "github.com/NibiruChain/nibiru/x/vpool/types";
// GenesisState defines the vpool module's genesis state.
message GenesisState {
repeated VPool vpools = 1;
Params params = 2 [(gogoproto.nullable) = false];
repeated ReserveSnapshot snapshots = 3 [(gogoproto.nullable) = false];
}
7 changes: 2 additions & 5 deletions proto/vpool/v1/state.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ enum TwapCalcOption {
BASE_ASSET_SWAP = 3;
}

// Params defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;
}

// A virtual pool used only for price discovery of perpetual futures contracts.
// No real liquidity exists in this pool.
message VPool {
Expand Down Expand Up @@ -94,6 +89,8 @@ message CurrentTWAP {

// a snapshot of the vpool's reserves at a given point in time
message ReserveSnapshot {
common.AssetPair pair = 5 [(gogoproto.nullable) = false];

string base_asset_reserve = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false];
Expand Down
2 changes: 1 addition & 1 deletion x/epochs/keeper/epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (k Keeper) IterateEpochInfo(ctx sdk.Context, fn func(index int64, epochInfo

// AllEpochInfos iterate through epochs to return all epochs info.
func (k Keeper) AllEpochInfos(ctx sdk.Context) []types.EpochInfo {
epochs := []types.EpochInfo{}
var epochs []types.EpochInfo
k.IterateEpochInfo(ctx, func(index int64, epochInfo types.EpochInfo) (stop bool) {
epochs = append(epochs, epochInfo)
return false
Expand Down
13 changes: 11 additions & 2 deletions x/vpool/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/NibiruChain/nibiru/x/vpool/types"

"github.com/NibiruChain/nibiru/x/vpool/keeper"
)

// Called every block to store a snapshot of the vpool.
// EndBlocker Called every block to store a snapshot of the vpool.
func EndBlocker(ctx sdk.Context, k keeper.Keeper) []abci.ValidatorUpdate {
for _, pool := range k.GetAllPools(ctx) {
k.SaveSnapshot(ctx, pool.Pair, pool.QuoteAssetReserve, pool.BaseAssetReserve)
snapshot := types.NewReserveSnapshot(
pool.Pair,
pool.BaseAssetReserve,
pool.QuoteAssetReserve,
ctx.BlockTime(),
ctx.BlockHeight(),
)
k.SaveSnapshot(ctx, snapshot)
}
return []abci.ValidatorUpdate{}
}
26 changes: 14 additions & 12 deletions x/vpool/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ func TestSnapshotUpdates(t *testing.T) {
sdk.OneDec(),
sdk.NewDec(10),
)
expectedSnapshot := types.ReserveSnapshot{
BaseAssetReserve: sdk.NewDec(10),
QuoteAssetReserve: sdk.NewDec(10),
TimestampMs: ctx.BlockTime().UnixMilli(),
BlockNumber: ctx.BlockHeight(),
}
expectedSnapshot := types.NewReserveSnapshot(
common.Pair_BTC_NUSD,
sdk.NewDec(10),
sdk.NewDec(10),
ctx.BlockTime(),
ctx.BlockHeight(),
)

t.Log("run one block of 5 seconds")
runBlock(5 * time.Second)
Expand All @@ -61,12 +62,13 @@ func TestSnapshotUpdates(t *testing.T) {
false,
)
require.NoError(t, err)
expectedSnapshot = types.ReserveSnapshot{
QuoteAssetReserve: sdk.NewDec(20),
BaseAssetReserve: sdk.NewDec(5),
TimestampMs: ctx.BlockTime().UnixMilli(),
BlockNumber: ctx.BlockHeight(),
}
expectedSnapshot = types.NewReserveSnapshot(
common.Pair_BTC_NUSD,
sdk.NewDec(5),
sdk.NewDec(20),
ctx.BlockTime(),
ctx.BlockHeight(),
)

t.Log("run one block of 5 seconds")
runBlock(5 * time.Second)
Expand Down
6 changes: 6 additions & 0 deletions x/vpool/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
vp.MaxLeverage,
)
}

for _, snapshot := range genState.Snapshots {
k.SaveSnapshot(ctx, snapshot)
}
}

// ExportGenesis returns the capability module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
pools := k.GetAllPools(ctx)
snapshots := k.GetAllSnapshots(ctx)

var genState types.GenesisState
genState.Vpools = pools
genState.Snapshots = snapshots

return &genState
}
40 changes: 36 additions & 4 deletions x/vpool/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vpool_test

import (
"testing"
"time"

"github.com/NibiruChain/nibiru/simapp"

Expand All @@ -13,7 +14,6 @@ import (
"github.com/NibiruChain/nibiru/x/vpool/types"
)

// TODO: https://github.com/NibiruChain/nibiru/issues/475
func TestGenesis(t *testing.T) {
vpools := []*types.VPool{
{
Expand All @@ -38,7 +38,34 @@ func TestGenesis(t *testing.T) {
},
}

genesisState := types.GenesisState{Vpools: vpools}
snapshots := []types.ReserveSnapshot{
types.NewReserveSnapshot(
common.Pair_BTC_NUSD,
sdk.NewDec(1_000_000),
sdk.NewDec(60_000_000_000),
time.UnixMilli(123456),
1,
),
types.NewReserveSnapshot(
common.Pair_BTC_NUSD,
sdk.NewDec(2_000_000),
sdk.NewDec(50_000_000_000),
time.UnixMilli(223456),
2,
),
types.NewReserveSnapshot(
common.Pair_ETH_NUSD,
sdk.NewDec(1_000_000),
sdk.NewDec(50_000_000_000),
time.UnixMilli(223456),
2,
),
}

genesisState := types.GenesisState{
Vpools: vpools,
Snapshots: snapshots,
}

nibiruApp, ctx := simapp.NewTestNibiruAppAndContext(true)
k := nibiruApp.VpoolKeeper
Expand All @@ -50,8 +77,13 @@ func TestGenesis(t *testing.T) {

exportedGenesis := vpool.ExportGenesis(ctx, k)
require.Len(t, exportedGenesis.Vpools, 2)
require.Len(t, exportedGenesis.Snapshots, 5) // 3 from imported + 2 created when creating a pool

for _, pool := range genesisState.Vpools {
require.Contains(t, exportedGenesis.Vpools, pool)
}

for _, exportedVpool := range exportedGenesis.Vpools {
require.Contains(t, genesisState.Vpools, exportedVpool)
for _, snapshot := range genesisState.Snapshots {
require.Contains(t, exportedGenesis.Snapshots, snapshot)
}
}
Loading