From 425fa2bb46644a86cb41b5646233044d39467e30 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 12:22:23 +0200 Subject: [PATCH 01/27] add genesis snapshots --- proto/vpool/v1/genesis.proto | 1 + x/epochs/keeper/epoch.go | 2 +- x/vpool/types/genesis.pb.go | 91 ++++++++++++++++++++++++++++++------ 3 files changed, 80 insertions(+), 14 deletions(-) diff --git a/proto/vpool/v1/genesis.proto b/proto/vpool/v1/genesis.proto index 291b757ac..1ea695db6 100644 --- a/proto/vpool/v1/genesis.proto +++ b/proto/vpool/v1/genesis.proto @@ -10,4 +10,5 @@ option go_package = "github.com/NibiruChain/nibiru/x/vpool/types"; message GenesisState { repeated VPool vpools = 1; Params params = 2 [(gogoproto.nullable) = false]; + repeated ReserveSnapshot snapshots = 3; } diff --git a/x/epochs/keeper/epoch.go b/x/epochs/keeper/epoch.go index 64dbbf6d5..e52e2f18f 100644 --- a/x/epochs/keeper/epoch.go +++ b/x/epochs/keeper/epoch.go @@ -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 diff --git a/x/vpool/types/genesis.pb.go b/x/vpool/types/genesis.pb.go index 6e0147871..4a5ecc0c5 100644 --- a/x/vpool/types/genesis.pb.go +++ b/x/vpool/types/genesis.pb.go @@ -25,8 +25,9 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the vpool module's genesis state. type GenesisState struct { - Vpools []*VPool `protobuf:"bytes,1,rep,name=vpools,proto3" json:"vpools,omitempty"` - Params Params `protobuf:"bytes,2,opt,name=params,proto3" json:"params"` + Vpools []*VPool `protobuf:"bytes,1,rep,name=vpools,proto3" json:"vpools,omitempty"` + Params Params `protobuf:"bytes,2,opt,name=params,proto3" json:"params"` + Snapshots []*ReserveSnapshot `protobuf:"bytes,3,rep,name=snapshots,proto3" json:"snapshots,omitempty"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -76,6 +77,13 @@ func (m *GenesisState) GetParams() Params { return Params{} } +func (m *GenesisState) GetSnapshots() []*ReserveSnapshot { + if m != nil { + return m.Snapshots + } + return nil +} + func init() { proto.RegisterType((*GenesisState)(nil), "nibiru.vpool.v1.GenesisState") } @@ -83,21 +91,24 @@ func init() { func init() { proto.RegisterFile("vpool/v1/genesis.proto", fileDescriptor_fc3ffc8cca622811) } var fileDescriptor_fc3ffc8cca622811 = []byte{ - // 222 bytes of a gzipped FileDescriptorProto + // 257 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2b, 0x2b, 0xc8, 0xcf, 0xcf, 0xd1, 0x2f, 0x33, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0xcf, 0xcb, 0x4c, 0xca, 0x2c, 0x2a, 0xd5, 0x03, 0x4b, 0xeb, 0x95, 0x19, 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0xe5, 0xf4, 0x41, 0x2c, 0x88, 0x32, 0x29, 0x11, 0xb8, - 0xf6, 0xe2, 0x92, 0xc4, 0x92, 0x54, 0x88, 0xa8, 0x52, 0x29, 0x17, 0x8f, 0x3b, 0xc4, 0xb4, 0x60, - 0x90, 0xa8, 0x90, 0x1e, 0x17, 0x1b, 0x58, 0x5d, 0xb1, 0x04, 0xa3, 0x02, 0xb3, 0x06, 0xb7, 0x91, - 0x98, 0x1e, 0x9a, 0xe9, 0x7a, 0x61, 0x01, 0xf9, 0xf9, 0x39, 0x41, 0x50, 0x55, 0x42, 0xa6, 0x5c, - 0x6c, 0x05, 0x89, 0x45, 0x89, 0xb9, 0xc5, 0x12, 0x4c, 0x0a, 0x8c, 0x1a, 0xdc, 0x46, 0xe2, 0x18, - 0xea, 0x03, 0xc0, 0xd2, 0x4e, 0x2c, 0x27, 0xee, 0xc9, 0x33, 0x04, 0x41, 0x15, 0x3b, 0xb9, 0x9e, - 0x78, 0x24, 0xc7, 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x13, 0x1e, 0xcb, 0x31, - 0x5c, 0x78, 0x2c, 0xc7, 0x70, 0xe3, 0xb1, 0x1c, 0x43, 0x94, 0x76, 0x7a, 0x66, 0x49, 0x46, 0x69, - 0x92, 0x5e, 0x72, 0x7e, 0xae, 0xbe, 0x1f, 0xd8, 0x28, 0xe7, 0x8c, 0xc4, 0xcc, 0x3c, 0x7d, 0x88, - 0xb1, 0xfa, 0x15, 0xfa, 0x10, 0x6f, 0x94, 0x54, 0x16, 0xa4, 0x16, 0x27, 0xb1, 0x81, 0x3d, 0x61, - 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x70, 0xde, 0x93, 0x17, 0x1b, 0x01, 0x00, 0x00, + 0xf6, 0xe2, 0x92, 0xc4, 0x92, 0x54, 0x88, 0xa8, 0xd2, 0x56, 0x46, 0x2e, 0x1e, 0x77, 0x88, 0x71, + 0xc1, 0x20, 0x61, 0x21, 0x3d, 0x2e, 0x36, 0xb0, 0xc2, 0x62, 0x09, 0x46, 0x05, 0x66, 0x0d, 0x6e, + 0x23, 0x31, 0x3d, 0x34, 0xe3, 0xf5, 0xc2, 0x02, 0xf2, 0xf3, 0x73, 0x82, 0xa0, 0xaa, 0x84, 0x4c, + 0xb9, 0xd8, 0x0a, 0x12, 0x8b, 0x12, 0x73, 0x8b, 0x25, 0x98, 0x14, 0x18, 0x35, 0xb8, 0x8d, 0xc4, + 0x31, 0xd4, 0x07, 0x80, 0xa5, 0x9d, 0x58, 0x4e, 0xdc, 0x93, 0x67, 0x08, 0x82, 0x2a, 0x16, 0xb2, + 0xe3, 0xe2, 0x2c, 0xce, 0x4b, 0x2c, 0x28, 0xce, 0xc8, 0x2f, 0x29, 0x96, 0x60, 0x06, 0xdb, 0xa4, + 0x80, 0xa1, 0x33, 0x28, 0xb5, 0x38, 0xb5, 0xa8, 0x2c, 0x35, 0x18, 0xaa, 0x30, 0x08, 0xa1, 0xc5, + 0xc9, 0xf5, 0xc4, 0x23, 0x39, 0xc6, 0x0b, 0x8f, 0xe4, 0x18, 0x1f, 0x3c, 0x92, 0x63, 0x9c, 0xf0, + 0x58, 0x8e, 0xe1, 0xc2, 0x63, 0x39, 0x86, 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0xb4, 0xd3, 0x33, 0x4b, + 0x32, 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xf5, 0xfd, 0xc0, 0x06, 0x3a, 0x67, 0x24, 0x66, 0xe6, + 0xe9, 0x43, 0x0c, 0xd7, 0xaf, 0xd0, 0x87, 0x84, 0x43, 0x49, 0x65, 0x41, 0x6a, 0x71, 0x12, 0x1b, + 0x38, 0x14, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xd7, 0x3c, 0x39, 0xfe, 0x5c, 0x01, 0x00, + 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -120,6 +131,20 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Snapshots) > 0 { + for iNdEx := len(m.Snapshots) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.Snapshots[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x1a + } + } { size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -172,6 +197,12 @@ func (m *GenesisState) Size() (n int) { } l = m.Params.Size() n += 1 + l + sovGenesis(uint64(l)) + if len(m.Snapshots) > 0 { + for _, e := range m.Snapshots { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } return n } @@ -277,6 +308,40 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Snapshots", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Snapshots = append(m.Snapshots, &ReserveSnapshot{}) + if err := m.Snapshots[len(m.Snapshots)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) From 94765e143da68dbdeaf236f4bb8871067b02e0a8 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 14:51:29 +0200 Subject: [PATCH 02/27] add pair into pair reserve snapshot --- proto/vpool/v1/state.proto | 2 + x/vpool/genesis_test.go | 15 ++++ x/vpool/types/state.pb.go | 159 ++++++++++++++++++++++++------------- 3 files changed, 122 insertions(+), 54 deletions(-) diff --git a/proto/vpool/v1/state.proto b/proto/vpool/v1/state.proto index 9bfd49a67..f3cbf3c4a 100644 --- a/proto/vpool/v1/state.proto +++ b/proto/vpool/v1/state.proto @@ -94,6 +94,8 @@ message CurrentTWAP { // a snapshot of the vpool's reserves at a given point in time message ReserveSnapshot { + string pair = 5; + string base_asset_reserve = 1 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false]; diff --git a/x/vpool/genesis_test.go b/x/vpool/genesis_test.go index a2e451018..8b1ad9720 100644 --- a/x/vpool/genesis_test.go +++ b/x/vpool/genesis_test.go @@ -38,6 +38,21 @@ func TestGenesis(t *testing.T) { }, } + //snapshots := []*types.ReserveSnapshot{ + // { + // BaseAssetReserve: sdk.NewDec(1_000_000), + // QuoteAssetReserve: sdk.NewDec(60_000_000_000), + // TimestampMs: 123456, + // BlockNumber: 1, + // }, + // { + // BaseAssetReserve: sdk.NewDec(2_000_000), + // QuoteAssetReserve: sdk.NewDec(50_000_000_000), + // TimestampMs: 123456, + // BlockNumber: 2, + // }, + //} + genesisState := types.GenesisState{Vpools: vpools} nibiruApp, ctx := simapp.NewTestNibiruAppAndContext(true) diff --git a/x/vpool/types/state.pb.go b/x/vpool/types/state.pb.go index a7bed374a..5ef0cc370 100644 --- a/x/vpool/types/state.pb.go +++ b/x/vpool/types/state.pb.go @@ -236,6 +236,7 @@ func (m *CurrentTWAP) GetPairID() string { // a snapshot of the vpool's reserves at a given point in time type ReserveSnapshot struct { + Pair string `protobuf:"bytes,5,opt,name=pair,proto3" json:"pair,omitempty"` BaseAssetReserve github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=base_asset_reserve,json=baseAssetReserve,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"base_asset_reserve"` // quote asset is usually the margin asset, e.g. NUSD QuoteAssetReserve github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=quote_asset_reserve,json=quoteAssetReserve,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"quote_asset_reserve"` @@ -277,6 +278,13 @@ func (m *ReserveSnapshot) XXX_DiscardUnknown() { var xxx_messageInfo_ReserveSnapshot proto.InternalMessageInfo +func (m *ReserveSnapshot) GetPair() string { + if m != nil { + return m.Pair + } + return "" +} + func (m *ReserveSnapshot) GetTimestampMs() int64 { if m != nil { return m.TimestampMs @@ -384,60 +392,60 @@ func init() { func init() { proto.RegisterFile("vpool/v1/state.proto", fileDescriptor_e9da3afd19017067) } var fileDescriptor_e9da3afd19017067 = []byte{ - // 835 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x95, 0xcf, 0x8f, 0x1b, 0x35, - 0x14, 0xc7, 0x93, 0x6c, 0x36, 0xdd, 0xbc, 0xd9, 0x6e, 0x52, 0xef, 0x2e, 0x4d, 0x0b, 0x4a, 0x96, - 0x20, 0xa1, 0xaa, 0x88, 0x44, 0x2d, 0xb7, 0xde, 0xf2, 0xab, 0x28, 0x52, 0xb2, 0x33, 0x9d, 0xa4, - 0xad, 0x54, 0x21, 0x2c, 0x67, 0xe2, 0x66, 0xad, 0x8c, 0xed, 0xc1, 0xe3, 0xa4, 0xe9, 0x7f, 0xc1, - 0x05, 0xa9, 0x47, 0xfe, 0x14, 0x8e, 0x3d, 0xf6, 0x88, 0x38, 0xac, 0x60, 0xf7, 0x3f, 0xe0, 0x2f, - 0x40, 0xf6, 0x0c, 0x90, 0x50, 0x38, 0x30, 0x82, 0xd3, 0xc4, 0xdf, 0xf7, 0xe6, 0xf3, 0x75, 0xde, - 0xbc, 0x67, 0xc3, 0xc9, 0x3a, 0x92, 0x32, 0x6c, 0xaf, 0x1f, 0xb4, 0x63, 0x4d, 0x34, 0x6d, 0x45, - 0x4a, 0x6a, 0x89, 0x2a, 0x82, 0xcd, 0x98, 0x5a, 0xb5, 0x6c, 0xb0, 0xb5, 0x7e, 0x70, 0xf7, 0x4e, - 0x20, 0x63, 0x2e, 0x63, 0x6c, 0xc3, 0xed, 0x64, 0x91, 0xe4, 0xde, 0x3d, 0x59, 0xc8, 0x85, 0x4c, - 0x74, 0xf3, 0x2b, 0x55, 0x8f, 0x03, 0xc9, 0xb9, 0x14, 0xed, 0xe4, 0x91, 0x88, 0xcd, 0x23, 0x28, - 0x79, 0x44, 0x11, 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4e, 0xd4, 0xc2, 0xfd, 0x05, 0x1c, 0x4d, 0x5f, 0x91, 0xa8, 0x47, 0xc2, 0xc0, - 0x8d, 0x2c, 0xf3, 0x0c, 0x3e, 0x32, 0xe3, 0x8c, 0x7b, 0x9d, 0x51, 0x0f, 0xbb, 0xde, 0xdf, 0xa0, - 0x0f, 0xa0, 0x38, 0xf1, 0xdc, 0x69, 0xc2, 0x7c, 0xf2, 0xd4, 0x9d, 0x0e, 0x70, 0x67, 0x32, 0x19, - 0x4c, 0xf1, 0xe4, 0x79, 0xc7, 0xab, 0x16, 0xd0, 0x31, 0x54, 0xba, 0x9d, 0xc9, 0x8e, 0xb8, 0xd7, - 0x1d, 0xbc, 0xbd, 0xaa, 0xe7, 0xdf, 0x5d, 0xd5, 0xf3, 0x3f, 0x5f, 0xd5, 0xf3, 0xdf, 0x5e, 0xd7, - 0x73, 0xef, 0xae, 0xeb, 0xb9, 0x1f, 0xaf, 0xeb, 0xb9, 0x17, 0x9f, 0x6d, 0x15, 0xf1, 0xdc, 0xde, - 0x8e, 0xbd, 0x0b, 0xc2, 0x44, 0x3b, 0xb9, 0x29, 0xdb, 0x9b, 0x76, 0x72, 0x91, 0xdb, 0x6a, 0xce, - 0x4a, 0xf6, 0xbe, 0xfd, 0xe2, 0xb7, 0x00, 0x00, 0x00, 0xff, 0xff, 0x96, 0xf5, 0x9a, 0x49, 0xde, - 0x07, 0x00, 0x00, + // 838 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x95, 0xcf, 0x6e, 0x1b, 0x37, + 0x10, 0xc6, 0x25, 0x59, 0x56, 0xac, 0x59, 0xc7, 0x52, 0x68, 0xbb, 0x51, 0xd2, 0x42, 0x72, 0x55, + 0xa0, 0x08, 0x52, 0x54, 0x42, 0xd2, 0x5b, 0x6e, 0xfa, 0x97, 0x42, 0x80, 0xe4, 0xdd, 0xac, 0x94, + 0x04, 0x08, 0x8a, 0x12, 0xd4, 0x8a, 0x91, 0x09, 0x2d, 0xc9, 0x2d, 0x97, 0x52, 0x94, 0xb7, 0xe8, + 0x31, 0xc7, 0x5e, 0xfb, 0x16, 0x3d, 0xe6, 0x98, 0x63, 0xd1, 0x83, 0xd1, 0xda, 0x6f, 0xd0, 0x27, + 0x28, 0xc8, 0xdd, 0xa6, 0x72, 0x5d, 0x14, 0xe8, 0xa2, 0x39, 0xad, 0xf8, 0xcd, 0xec, 0xef, 0xa3, + 0x66, 0x67, 0x48, 0x38, 0x5a, 0x47, 0x52, 0x86, 0xed, 0xf5, 0x83, 0x76, 0xac, 0x89, 0xa6, 0xad, + 0x48, 0x49, 0x2d, 0x51, 0x45, 0xb0, 0x19, 0x53, 0xab, 0x96, 0x0d, 0xb6, 0xd6, 0x0f, 0xee, 0xde, + 0x09, 0x64, 0xcc, 0x65, 0x8c, 0x6d, 0xb8, 0x9d, 0x2c, 0x92, 0xdc, 0xbb, 0x47, 0x0b, 0xb9, 0x90, + 0x89, 0x6e, 0x7e, 0xa5, 0xea, 0x61, 0x20, 0x39, 0x97, 0xa2, 0x9d, 0x3c, 0x12, 0xb1, 0x79, 0x00, + 0x25, 0x8f, 0x28, 0xc2, 0xe3, 0x47, 0xc5, 0x37, 0x3f, 0x34, 0x72, 0xcd, 0xdf, 0x76, 0x61, 0xf7, + 0x99, 0x27, 0x65, 0x88, 0x1e, 0x42, 0x31, 0x22, 0x4c, 0xd5, 0xf2, 0x27, 0xf9, 0x7b, 0xce, 0xc3, + 0x5a, 0x2b, 0xf5, 0x4f, 0xdf, 0xee, 0xc4, 0x31, 0xd5, 0x1e, 0x61, 0xaa, 0x5b, 0x7c, 0x7b, 0xde, + 0xc8, 0xf9, 0x36, 0x17, 0x7d, 0x03, 0x68, 0x46, 0x62, 0x8a, 0x89, 0x89, 0x62, 0x45, 0x63, 0xaa, + 0xd6, 0xb4, 0x56, 0x38, 0xc9, 0xdf, 0x2b, 0x77, 0x5b, 0x26, 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 0xe7, 0x7f, 0xbd, 0xa8, 0xe7, + 0xbf, 0xbf, 0xac, 0xe7, 0xde, 0x5d, 0xd6, 0x73, 0x3f, 0x5f, 0xd6, 0x73, 0x2f, 0xbe, 0xd8, 0x2a, + 0xe2, 0xa9, 0xbd, 0x31, 0x7b, 0x67, 0x84, 0x89, 0x76, 0x72, 0x7b, 0xb6, 0x37, 0xed, 0xe4, 0x72, + 0xb7, 0xd5, 0x9c, 0x95, 0xec, 0x1d, 0xfc, 0xd5, 0x1f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xef, 0xc2, + 0x0a, 0x5c, 0xf2, 0x07, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -646,6 +654,13 @@ func (m *ReserveSnapshot) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.Pair) > 0 { + i -= len(m.Pair) + copy(dAtA[i:], m.Pair) + i = encodeVarintState(dAtA, i, uint64(len(m.Pair))) + i-- + dAtA[i] = 0x2a + } if m.BlockNumber != 0 { i = encodeVarintState(dAtA, i, uint64(m.BlockNumber)) i-- @@ -828,6 +843,10 @@ func (m *ReserveSnapshot) Size() (n int) { if m.BlockNumber != 0 { n += 1 + sovState(uint64(m.BlockNumber)) } + l = len(m.Pair) + if l > 0 { + n += 1 + l + sovState(uint64(l)) + } return n } @@ -1555,6 +1574,38 @@ func (m *ReserveSnapshot) Unmarshal(dAtA []byte) error { break } } + case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Pair", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowState + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthState + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthState + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Pair = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipState(dAtA[iNdEx:]) From 0cf99bf9649a0818317790a4fa1772c137b33bbe Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 15:31:00 +0200 Subject: [PATCH 03/27] include pair into snapshot --- x/vpool/abci.go | 2 +- x/vpool/abci_test.go | 2 ++ x/vpool/keeper/snapshot.go | 1 + x/vpool/keeper/snapshot_test.go | 4 ++++ 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/x/vpool/abci.go b/x/vpool/abci.go index 57167ab2c..ca2e6eee1 100644 --- a/x/vpool/abci.go +++ b/x/vpool/abci.go @@ -7,7 +7,7 @@ import ( "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) diff --git a/x/vpool/abci_test.go b/x/vpool/abci_test.go index 041f82dc3..74cf71800 100644 --- a/x/vpool/abci_test.go +++ b/x/vpool/abci_test.go @@ -39,6 +39,7 @@ func TestSnapshotUpdates(t *testing.T) { sdk.NewDec(10), ) expectedSnapshot := types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), BaseAssetReserve: sdk.NewDec(10), QuoteAssetReserve: sdk.NewDec(10), TimestampMs: ctx.BlockTime().UnixMilli(), @@ -62,6 +63,7 @@ func TestSnapshotUpdates(t *testing.T) { ) require.NoError(t, err) expectedSnapshot = types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), QuoteAssetReserve: sdk.NewDec(20), BaseAssetReserve: sdk.NewDec(5), TimestampMs: ctx.BlockTime().UnixMilli(), diff --git a/x/vpool/keeper/snapshot.go b/x/vpool/keeper/snapshot.go index 768f17190..1a8840595 100644 --- a/x/vpool/keeper/snapshot.go +++ b/x/vpool/keeper/snapshot.go @@ -35,6 +35,7 @@ func (k Keeper) SaveSnapshot( baseAssetReserve sdk.Dec, ) { snapshot := &types.ReserveSnapshot{ + Pair: pair.String(), BaseAssetReserve: baseAssetReserve, QuoteAssetReserve: quoteAssetReserve, TimestampMs: ctx.BlockTime().UnixMilli(), diff --git a/x/vpool/keeper/snapshot_test.go b/x/vpool/keeper/snapshot_test.go index 7b0545fe9..7e6c041bf 100644 --- a/x/vpool/keeper/snapshot_test.go +++ b/x/vpool/keeper/snapshot_test.go @@ -37,6 +37,7 @@ func TestGetReserveSnapshotMultiplePairs(t *testing.T) { require.NoError(t, err) require.Equal(t, types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), BaseAssetReserve: sdk.OneDec(), QuoteAssetReserve: sdk.OneDec(), TimestampMs: genesisTime.UnixMilli(), @@ -58,6 +59,7 @@ func TestSaveSnapshot(t *testing.T) { require.NoError(t, err) require.Equal(t, types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), BaseAssetReserve: sdk.OneDec(), QuoteAssetReserve: sdk.OneDec(), TimestampMs: ctx.BlockTime().UnixMilli(), @@ -85,6 +87,7 @@ func TestGetSnapshot(t *testing.T) { snapshot, err := vpoolKeeper.GetSnapshot(ctx, common.Pair_BTC_NUSD, 1) require.NoError(t, err) require.Equal(t, types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), BaseAssetReserve: sdk.OneDec(), QuoteAssetReserve: sdk.OneDec(), TimestampMs: ctx.BlockTime().UnixMilli(), @@ -104,6 +107,7 @@ func TestGetSnapshot(t *testing.T) { snapshot, err = vpoolKeeper.GetSnapshot(ctx, common.Pair_BTC_NUSD, 2) require.NoError(t, err) require.Equal(t, types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), BaseAssetReserve: sdk.NewDec(2), QuoteAssetReserve: sdk.NewDec(2), TimestampMs: ctx.BlockTime().UnixMilli(), From c43e54a468b75255bbdefef9d112f9cef9b7b556 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 21:30:13 +0200 Subject: [PATCH 04/27] use new constructor part 1 --- x/vpool/abci_test.go | 26 ++++++++++++-------------- x/vpool/types/snapshot.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 x/vpool/types/snapshot.go diff --git a/x/vpool/abci_test.go b/x/vpool/abci_test.go index 74cf71800..89b5debaa 100644 --- a/x/vpool/abci_test.go +++ b/x/vpool/abci_test.go @@ -38,13 +38,12 @@ func TestSnapshotUpdates(t *testing.T) { sdk.OneDec(), sdk.NewDec(10), ) - expectedSnapshot := types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), - BaseAssetReserve: sdk.NewDec(10), - QuoteAssetReserve: sdk.NewDec(10), - TimestampMs: ctx.BlockTime().UnixMilli(), - BlockNumber: ctx.BlockHeight(), - } + expectedSnapshot := types.NewReserveSnapshot( + ctx, + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(10), + ) t.Log("run one block of 5 seconds") runBlock(5 * time.Second) @@ -62,13 +61,12 @@ func TestSnapshotUpdates(t *testing.T) { false, ) require.NoError(t, err) - expectedSnapshot = types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), - QuoteAssetReserve: sdk.NewDec(20), - BaseAssetReserve: sdk.NewDec(5), - TimestampMs: ctx.BlockTime().UnixMilli(), - BlockNumber: ctx.BlockHeight(), - } + expectedSnapshot = types.NewReserveSnapshot( + ctx, + common.Pair_BTC_NUSD, + sdk.NewDec(5), + sdk.NewDec(20), + ) t.Log("run one block of 5 seconds") runBlock(5 * time.Second) diff --git a/x/vpool/types/snapshot.go b/x/vpool/types/snapshot.go new file mode 100644 index 000000000..e0a34b5ee --- /dev/null +++ b/x/vpool/types/snapshot.go @@ -0,0 +1,16 @@ +package types + +import ( + "github.com/NibiruChain/nibiru/x/common" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func NewReserveSnapshot(ctx sdk.Context, pair common.AssetPair, baseAssetReserve, quoteAssetReserve sdk.Dec) ReserveSnapshot { + return ReserveSnapshot{ + Pair: pair.String(), + BaseAssetReserve: baseAssetReserve, + QuoteAssetReserve: quoteAssetReserve, + TimestampMs: ctx.BlockTime().UnixMilli(), + BlockNumber: ctx.BlockHeight(), + } +} From db841fdbf6dec46d94d47c5146f25dbedf2bb4db Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 21:39:38 +0200 Subject: [PATCH 05/27] add snapshot pair --- x/vpool/keeper/keeper_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/x/vpool/keeper/keeper_test.go b/x/vpool/keeper/keeper_test.go index 85a18b050..6dcdb55db 100644 --- a/x/vpool/keeper/keeper_test.go +++ b/x/vpool/keeper/keeper_test.go @@ -592,12 +592,14 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("15"), }, prevSnapshot: &types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), QuoteAssetReserve: sdk.NewDec(1000), BaseAssetReserve: sdk.OneDec(), TimestampMs: 0, BlockNumber: 0, }, latestSnapshot: &types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), QuoteAssetReserve: sdk.NewDec(1002), BaseAssetReserve: sdk.OneDec(), TimestampMs: 1, @@ -619,6 +621,7 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("15"), }, prevSnapshot: &types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), QuoteAssetReserve: sdk.NewDec(1000), BaseAssetReserve: sdk.OneDec(), TimestampMs: 0, @@ -641,6 +644,7 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("15"), }, prevSnapshot: &types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), QuoteAssetReserve: sdk.NewDec(1000), BaseAssetReserve: sdk.OneDec(), TimestampMs: 0, @@ -663,12 +667,14 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("15"), }, prevSnapshot: &types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), QuoteAssetReserve: sdk.NewDec(1000), BaseAssetReserve: sdk.OneDec(), TimestampMs: 0, BlockNumber: 0, }, latestSnapshot: &types.ReserveSnapshot{ + Pair: common.Pair_BTC_NUSD.String(), QuoteAssetReserve: sdk.NewDec(1002), BaseAssetReserve: sdk.OneDec(), TimestampMs: 1, From 1bc521a04a5bd59e255b0db349b4363af667347d Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 21:41:33 +0200 Subject: [PATCH 06/27] remove unused code --- x/vpool/keeper/keeper_test.go | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/x/vpool/keeper/keeper_test.go b/x/vpool/keeper/keeper_test.go index 6dcdb55db..c7a44fb31 100644 --- a/x/vpool/keeper/keeper_test.go +++ b/x/vpool/keeper/keeper_test.go @@ -738,12 +738,6 @@ func TestGetMaintenanceMarginRatio(t *testing.T) { MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.42"), MaxLeverage: sdk.OneDec(), }, - snapshot: types.ReserveSnapshot{ - QuoteAssetReserve: sdk.NewDec(1000), - BaseAssetReserve: sdk.OneDec(), - TimestampMs: 0, - BlockNumber: 0, - }, expectedMaintenanceMarginRatio: sdk.MustNewDecFromStr("0.42"), }, { @@ -758,12 +752,6 @@ func TestGetMaintenanceMarginRatio(t *testing.T) { MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.4242"), MaxLeverage: sdk.OneDec(), }, - snapshot: types.ReserveSnapshot{ - QuoteAssetReserve: sdk.NewDec(1000), - BaseAssetReserve: sdk.OneDec(), - TimestampMs: 0, - BlockNumber: 0, - }, expectedMaintenanceMarginRatio: sdk.MustNewDecFromStr("0.4242"), }, } From b4de2ec6e4a9c9d006a1a2dc0c26ac2051f8f2b3 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 21:47:54 +0200 Subject: [PATCH 07/27] replace constructor in SaveSnapshot --- x/vpool/keeper/snapshot.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/x/vpool/keeper/snapshot.go b/x/vpool/keeper/snapshot.go index 1a8840595..5fcce6e3e 100644 --- a/x/vpool/keeper/snapshot.go +++ b/x/vpool/keeper/snapshot.go @@ -34,16 +34,15 @@ func (k Keeper) SaveSnapshot( quoteAssetReserve sdk.Dec, baseAssetReserve sdk.Dec, ) { - snapshot := &types.ReserveSnapshot{ - Pair: pair.String(), - BaseAssetReserve: baseAssetReserve, - QuoteAssetReserve: quoteAssetReserve, - TimestampMs: ctx.BlockTime().UnixMilli(), - BlockNumber: ctx.BlockHeight(), - } + snapshot := types.NewReserveSnapshot( + ctx, + pair, + baseAssetReserve, + quoteAssetReserve, + ) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SnapshotsKeyPrefix) - store.Set(types.GetSnapshotKey(pair, uint64(ctx.BlockHeight())), k.codec.MustMarshal(snapshot)) + store.Set(types.GetSnapshotKey(pair, uint64(ctx.BlockHeight())), k.codec.MustMarshal(&snapshot)) } // GetLatestReserveSnapshot returns the last snapshot that was saved From 39a6919acf646921db3df28906d34a5ff22cf445 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 21:50:15 +0200 Subject: [PATCH 08/27] fix lint --- x/vpool/types/snapshot.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x/vpool/types/snapshot.go b/x/vpool/types/snapshot.go index e0a34b5ee..96bf1250a 100644 --- a/x/vpool/types/snapshot.go +++ b/x/vpool/types/snapshot.go @@ -1,8 +1,9 @@ package types import ( - "github.com/NibiruChain/nibiru/x/common" sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/NibiruChain/nibiru/x/common" ) func NewReserveSnapshot(ctx sdk.Context, pair common.AssetPair, baseAssetReserve, quoteAssetReserve sdk.Dec) ReserveSnapshot { From e9faeb1d1fafa8659fad93c5baf2095d843d4858 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 22:00:03 +0200 Subject: [PATCH 09/27] use temporary reserve snapshot --- x/vpool/abci_test.go | 5 +++-- x/vpool/types/snapshot.go | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/x/vpool/abci_test.go b/x/vpool/abci_test.go index 89b5debaa..067e977ff 100644 --- a/x/vpool/abci_test.go +++ b/x/vpool/abci_test.go @@ -38,11 +38,12 @@ func TestSnapshotUpdates(t *testing.T) { sdk.OneDec(), sdk.NewDec(10), ) - expectedSnapshot := types.NewReserveSnapshot( - ctx, + expectedSnapshot := types.NewReserveSnapshotWithoutCtx( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(10), + ctx.BlockTime(), + ctx.BlockHeight(), ) t.Log("run one block of 5 seconds") diff --git a/x/vpool/types/snapshot.go b/x/vpool/types/snapshot.go index 96bf1250a..2c1ed73ea 100644 --- a/x/vpool/types/snapshot.go +++ b/x/vpool/types/snapshot.go @@ -2,11 +2,16 @@ package types import ( sdk "github.com/cosmos/cosmos-sdk/types" + "time" "github.com/NibiruChain/nibiru/x/common" ) -func NewReserveSnapshot(ctx sdk.Context, pair common.AssetPair, baseAssetReserve, quoteAssetReserve sdk.Dec) ReserveSnapshot { +func NewReserveSnapshot( + ctx sdk.Context, + pair common.AssetPair, + baseAssetReserve, quoteAssetReserve sdk.Dec, +) ReserveSnapshot { return ReserveSnapshot{ Pair: pair.String(), BaseAssetReserve: baseAssetReserve, @@ -15,3 +20,18 @@ func NewReserveSnapshot(ctx sdk.Context, pair common.AssetPair, baseAssetReserve BlockNumber: ctx.BlockHeight(), } } + +func NewReserveSnapshotWithoutCtx( + pair common.AssetPair, + baseAssetReserve, quoteAssetReserve sdk.Dec, + blockTime time.Time, + blockHeight int64, +) ReserveSnapshot { + return ReserveSnapshot{ + Pair: pair.String(), + BaseAssetReserve: baseAssetReserve, + QuoteAssetReserve: quoteAssetReserve, + TimestampMs: blockTime.UnixMilli(), + BlockNumber: blockHeight, + } +} From 4b338eccb5331e02dc6bdb8185ba95d479ac6fcf Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 22:10:49 +0200 Subject: [PATCH 10/27] add reserve snapshot --- x/vpool/keeper/snapshot_test.go | 73 ++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 32 deletions(-) diff --git a/x/vpool/keeper/snapshot_test.go b/x/vpool/keeper/snapshot_test.go index 7e6c041bf..58566508c 100644 --- a/x/vpool/keeper/snapshot_test.go +++ b/x/vpool/keeper/snapshot_test.go @@ -36,13 +36,13 @@ func TestGetReserveSnapshotMultiplePairs(t *testing.T) { snapshot, err := vpoolKeeper.GetLatestReserveSnapshot(ctx, common.Pair_BTC_NUSD) require.NoError(t, err) require.Equal(t, - types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), - BaseAssetReserve: sdk.OneDec(), - QuoteAssetReserve: sdk.OneDec(), - TimestampMs: genesisTime.UnixMilli(), - BlockNumber: 1, - }, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.OneDec(), + sdk.OneDec(), + genesisTime, + 1, + ), snapshot, ) } @@ -58,13 +58,13 @@ func TestSaveSnapshot(t *testing.T) { snapshot, err := vpoolKeeper.GetLatestReserveSnapshot(ctx, common.Pair_BTC_NUSD) require.NoError(t, err) require.Equal(t, - types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), - BaseAssetReserve: sdk.OneDec(), - QuoteAssetReserve: sdk.OneDec(), - TimestampMs: ctx.BlockTime().UnixMilli(), - BlockNumber: 1, - }, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.OneDec(), + sdk.OneDec(), + ctx.BlockTime(), + 1, + ), snapshot, ) } @@ -86,13 +86,16 @@ func TestGetSnapshot(t *testing.T) { t.Log("Check snapshot 1") snapshot, err := vpoolKeeper.GetSnapshot(ctx, common.Pair_BTC_NUSD, 1) require.NoError(t, err) - require.Equal(t, types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), - BaseAssetReserve: sdk.OneDec(), - QuoteAssetReserve: sdk.OneDec(), - TimestampMs: ctx.BlockTime().UnixMilli(), - BlockNumber: 1, - }, snapshot) + require.Equal(t, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.OneDec(), + sdk.OneDec(), + ctx.BlockTime(), + 1, + ), + snapshot, + ) t.Log("Save snapshot 2") ctx = ctx.WithBlockHeight(2).WithBlockTime(time.Now().Add(5 * time.Second)) @@ -106,13 +109,16 @@ func TestGetSnapshot(t *testing.T) { t.Log("Fetch snapshot 2") snapshot, err = vpoolKeeper.GetSnapshot(ctx, common.Pair_BTC_NUSD, 2) require.NoError(t, err) - require.Equal(t, types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), - BaseAssetReserve: sdk.NewDec(2), - QuoteAssetReserve: sdk.NewDec(2), - TimestampMs: ctx.BlockTime().UnixMilli(), - BlockNumber: 2, - }, snapshot) + require.Equal(t, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(2), + sdk.NewDec(2), + ctx.BlockTime(), + 2, + ), + snapshot, + ) } func TestGetSnapshotPrice(t *testing.T) { @@ -179,10 +185,13 @@ func TestGetSnapshotPrice(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - snapshot := types.ReserveSnapshot{ - QuoteAssetReserve: tc.quoteAssetReserve, - BaseAssetReserve: tc.baseAssetReserve, - } + snapshot := types.NewReserveSnapshotWithoutCtx( + tc.pair, + tc.baseAssetReserve, + tc.quoteAssetReserve, + time.Now(), + 1, + ) snapshotPriceOpts := snapshotPriceOptions{ pair: tc.pair, From 069686529a6588879be5a8af8a9e9f54a9cb7976 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 22:13:39 +0200 Subject: [PATCH 11/27] switch function --- x/vpool/keeper/keeper_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/x/vpool/keeper/keeper_test.go b/x/vpool/keeper/keeper_test.go index c7a44fb31..58f44aba8 100644 --- a/x/vpool/keeper/keeper_test.go +++ b/x/vpool/keeper/keeper_test.go @@ -471,12 +471,13 @@ func TestIsOverFluctuationLimit(t *testing.T) { MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"), MaxLeverage: sdk.MustNewDecFromStr("15"), }, - snapshot: types.ReserveSnapshot{ - QuoteAssetReserve: sdk.NewDec(1000), - BaseAssetReserve: sdk.OneDec(), - TimestampMs: 0, - BlockNumber: 0, - }, + snapshot: types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.OneDec(), + sdk.NewDec(1000), + time.Now(), + 0, + ), isOverLimit: false, }, { From 864678fcd34434c66a8bdc3fa505378387b463e2 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 22:16:54 +0200 Subject: [PATCH 12/27] remove unused code --- x/vpool/keeper/keeper_test.go | 45 ++++++++--------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/x/vpool/keeper/keeper_test.go b/x/vpool/keeper/keeper_test.go index 58f44aba8..be478606c 100644 --- a/x/vpool/keeper/keeper_test.go +++ b/x/vpool/keeper/keeper_test.go @@ -453,9 +453,8 @@ func TestGetVpools(t *testing.T) { func TestIsOverFluctuationLimit(t *testing.T) { tests := []struct { - name string - pool types.VPool - snapshot types.ReserveSnapshot + name string + pool types.VPool isOverLimit bool }{ @@ -471,13 +470,6 @@ func TestIsOverFluctuationLimit(t *testing.T) { MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"), MaxLeverage: sdk.MustNewDecFromStr("15"), }, - snapshot: types.NewReserveSnapshotWithoutCtx( - common.Pair_BTC_NUSD, - sdk.OneDec(), - sdk.NewDec(1000), - time.Now(), - 0, - ), isOverLimit: false, }, { @@ -492,12 +484,6 @@ func TestIsOverFluctuationLimit(t *testing.T) { MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"), MaxLeverage: sdk.MustNewDecFromStr("15"), }, - snapshot: types.ReserveSnapshot{ - QuoteAssetReserve: sdk.NewDec(1000), - BaseAssetReserve: sdk.OneDec(), - TimestampMs: 0, - BlockNumber: 0, - }, isOverLimit: false, }, { @@ -512,12 +498,6 @@ func TestIsOverFluctuationLimit(t *testing.T) { MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"), MaxLeverage: sdk.MustNewDecFromStr("15"), }, - snapshot: types.ReserveSnapshot{ - QuoteAssetReserve: sdk.NewDec(1000), - BaseAssetReserve: sdk.OneDec(), - TimestampMs: 0, - BlockNumber: 0, - }, isOverLimit: false, }, { @@ -532,12 +512,6 @@ func TestIsOverFluctuationLimit(t *testing.T) { MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"), MaxLeverage: sdk.MustNewDecFromStr("15"), }, - snapshot: types.ReserveSnapshot{ - QuoteAssetReserve: sdk.NewDec(1000), - BaseAssetReserve: sdk.OneDec(), - TimestampMs: 0, - BlockNumber: 0, - }, isOverLimit: true, }, { @@ -552,12 +526,6 @@ func TestIsOverFluctuationLimit(t *testing.T) { MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.0625"), MaxLeverage: sdk.MustNewDecFromStr("15"), }, - snapshot: types.ReserveSnapshot{ - QuoteAssetReserve: sdk.NewDec(1000), - BaseAssetReserve: sdk.OneDec(), - TimestampMs: 0, - BlockNumber: 0, - }, isOverLimit: true, }, } @@ -565,7 +533,14 @@ func TestIsOverFluctuationLimit(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - assert.EqualValues(t, tc.isOverLimit, isOverFluctuationLimit(&tc.pool, tc.snapshot)) + snapshot := types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.OneDec(), + sdk.NewDec(1000), + time.Now(), + 0, + ) + assert.EqualValues(t, tc.isOverLimit, isOverFluctuationLimit(&tc.pool, snapshot)) }) } } From a953dd8d9cb287efaaedfd31e061c40c62a8db6a Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 22:32:46 +0200 Subject: [PATCH 13/27] refactor usage on tests --- x/vpool/keeper/prices_test.go | 39 +++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/x/vpool/keeper/prices_test.go b/x/vpool/keeper/prices_test.go index 53f8dd658..390cdc763 100644 --- a/x/vpool/keeper/prices_test.go +++ b/x/vpool/keeper/prices_test.go @@ -242,24 +242,27 @@ func TestCalcTwap(t *testing.T) { name: "spot price twap calc, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - { - QuoteAssetReserve: sdk.NewDec(90), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 10, - BlockNumber: 1, - }, - { - QuoteAssetReserve: sdk.NewDec(85), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 20, - BlockNumber: 2, - }, - { - QuoteAssetReserve: sdk.NewDec(95), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 30, - BlockNumber: 3, - }, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(90), + time.UnixMilli(10), + 1, + ), + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(85), + time.UnixMilli(20), + 2, + ), + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(95), + time.UnixMilli(30), + 3, + ), }, currentBlockTime: time.UnixMilli(30), currentBlockHeight: 3, From e1f3021f143bcdcbcce336a7c79d2df7a3f91bcd Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 22:40:12 +0200 Subject: [PATCH 14/27] use new constructor on prices test --- x/vpool/keeper/prices_test.go | 143 ++++++++++++++++++---------------- 1 file changed, 77 insertions(+), 66 deletions(-) diff --git a/x/vpool/keeper/prices_test.go b/x/vpool/keeper/prices_test.go index 390cdc763..6cded9d85 100644 --- a/x/vpool/keeper/prices_test.go +++ b/x/vpool/keeper/prices_test.go @@ -274,24 +274,27 @@ func TestCalcTwap(t *testing.T) { name: "spot price twap calc, t=[11,35]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - { - QuoteAssetReserve: sdk.NewDec(90), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 10, - BlockNumber: 1, - }, - { - QuoteAssetReserve: sdk.NewDec(85), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 20, - BlockNumber: 2, - }, - { - QuoteAssetReserve: sdk.NewDec(95), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 30, - BlockNumber: 3, - }, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(90), + time.UnixMilli(10), + 1, + ), + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(85), + time.UnixMilli(20), + 2, + ), + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(95), + time.UnixMilli(30), + 3, + ), }, currentBlockTime: time.UnixMilli(35), currentBlockHeight: 4, @@ -303,18 +306,20 @@ func TestCalcTwap(t *testing.T) { name: "quote asset swap twap calc, add to pool, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - { - QuoteAssetReserve: sdk.NewDec(30), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 10, - BlockNumber: 1, - }, - { - QuoteAssetReserve: sdk.NewDec(40), - BaseAssetReserve: sdk.MustNewDecFromStr("7.5"), - TimestampMs: 20, - BlockNumber: 2, - }, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(30), + time.UnixMilli(10), + 1, + ), + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.MustNewDecFromStr("7.5"), + sdk.NewDec(40), + time.UnixMilli(20), + 2, + ), }, currentBlockTime: time.UnixMilli(30), currentBlockHeight: 3, @@ -328,18 +333,20 @@ func TestCalcTwap(t *testing.T) { name: "quote asset swap twap calc, remove from pool, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - { - QuoteAssetReserve: sdk.NewDec(60), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 10, - BlockNumber: 1, - }, - { - QuoteAssetReserve: sdk.NewDec(50), - BaseAssetReserve: sdk.NewDec(12), - TimestampMs: 20, - BlockNumber: 2, - }, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(60), + time.UnixMilli(10), + 1, + ), + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(12), + sdk.NewDec(50), + time.UnixMilli(20), + 2, + ), }, currentBlockTime: time.UnixMilli(30), currentBlockHeight: 3, @@ -353,18 +360,20 @@ func TestCalcTwap(t *testing.T) { name: "base asset swap twap calc, add to pool, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - { - QuoteAssetReserve: sdk.NewDec(60), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 10, - BlockNumber: 1, - }, - { - QuoteAssetReserve: sdk.NewDec(30), - BaseAssetReserve: sdk.NewDec(20), - TimestampMs: 20, - BlockNumber: 2, - }, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(60), + time.UnixMilli(10), + 1, + ), + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(20), + sdk.NewDec(30), + time.UnixMilli(20), + 2, + ), }, currentBlockTime: time.UnixMilli(30), currentBlockHeight: 3, @@ -378,18 +387,20 @@ func TestCalcTwap(t *testing.T) { name: "base asset swap twap calc, remove from pool, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - { - QuoteAssetReserve: sdk.NewDec(60), - BaseAssetReserve: sdk.NewDec(10), - TimestampMs: 10, - BlockNumber: 1, - }, - { - QuoteAssetReserve: sdk.NewDec(75), - BaseAssetReserve: sdk.NewDec(8), - TimestampMs: 20, - BlockNumber: 2, - }, + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(10), + sdk.NewDec(60), + time.UnixMilli(10), + 1, + ), + types.NewReserveSnapshotWithoutCtx( + common.Pair_BTC_NUSD, + sdk.NewDec(8), + sdk.NewDec(75), + time.UnixMilli(20), + 2, + ), }, currentBlockTime: time.UnixMilli(30), currentBlockHeight: 3, From 7c1a302dd11c0340e5df8df652c42416e1f37ce3 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 22:50:58 +0200 Subject: [PATCH 15/27] remove usage of old function --- x/vpool/abci_test.go | 5 +++-- x/vpool/keeper/keeper_test.go | 5 ++--- x/vpool/keeper/snapshot.go | 5 +++-- x/vpool/types/snapshot.go | 14 -------------- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/x/vpool/abci_test.go b/x/vpool/abci_test.go index 067e977ff..1a81015fd 100644 --- a/x/vpool/abci_test.go +++ b/x/vpool/abci_test.go @@ -62,11 +62,12 @@ func TestSnapshotUpdates(t *testing.T) { false, ) require.NoError(t, err) - expectedSnapshot = types.NewReserveSnapshot( - ctx, + expectedSnapshot = types.NewReserveSnapshotWithoutCtx( common.Pair_BTC_NUSD, sdk.NewDec(5), sdk.NewDec(20), + ctx.BlockTime(), + ctx.BlockHeight(), ) t.Log("run one block of 5 seconds") diff --git a/x/vpool/keeper/keeper_test.go b/x/vpool/keeper/keeper_test.go index be478606c..e09ccb1c1 100644 --- a/x/vpool/keeper/keeper_test.go +++ b/x/vpool/keeper/keeper_test.go @@ -696,9 +696,8 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { func TestGetMaintenanceMarginRatio(t *testing.T) { tests := []struct { - name string - pool *types.VPool - snapshot types.ReserveSnapshot + name string + pool *types.VPool expectedMaintenanceMarginRatio sdk.Dec }{ diff --git a/x/vpool/keeper/snapshot.go b/x/vpool/keeper/snapshot.go index 5fcce6e3e..117d29be2 100644 --- a/x/vpool/keeper/snapshot.go +++ b/x/vpool/keeper/snapshot.go @@ -34,11 +34,12 @@ func (k Keeper) SaveSnapshot( quoteAssetReserve sdk.Dec, baseAssetReserve sdk.Dec, ) { - snapshot := types.NewReserveSnapshot( - ctx, + snapshot := types.NewReserveSnapshotWithoutCtx( pair, baseAssetReserve, quoteAssetReserve, + ctx.BlockTime(), + ctx.BlockHeight(), ) store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SnapshotsKeyPrefix) diff --git a/x/vpool/types/snapshot.go b/x/vpool/types/snapshot.go index 2c1ed73ea..57e23bed4 100644 --- a/x/vpool/types/snapshot.go +++ b/x/vpool/types/snapshot.go @@ -7,20 +7,6 @@ import ( "github.com/NibiruChain/nibiru/x/common" ) -func NewReserveSnapshot( - ctx sdk.Context, - pair common.AssetPair, - baseAssetReserve, quoteAssetReserve sdk.Dec, -) ReserveSnapshot { - return ReserveSnapshot{ - Pair: pair.String(), - BaseAssetReserve: baseAssetReserve, - QuoteAssetReserve: quoteAssetReserve, - TimestampMs: ctx.BlockTime().UnixMilli(), - BlockNumber: ctx.BlockHeight(), - } -} - func NewReserveSnapshotWithoutCtx( pair common.AssetPair, baseAssetReserve, quoteAssetReserve sdk.Dec, From cd798849831b74d78c4ec89f6d93cfad785c4b89 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 22:53:41 +0200 Subject: [PATCH 16/27] rename function to NewReserveSnapshot --- x/vpool/abci_test.go | 4 ++-- x/vpool/keeper/keeper_test.go | 2 +- x/vpool/keeper/prices_test.go | 28 ++++++++++++++-------------- x/vpool/keeper/snapshot.go | 2 +- x/vpool/keeper/snapshot_test.go | 10 +++++----- x/vpool/types/snapshot.go | 2 +- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/x/vpool/abci_test.go b/x/vpool/abci_test.go index 1a81015fd..d083db4bb 100644 --- a/x/vpool/abci_test.go +++ b/x/vpool/abci_test.go @@ -38,7 +38,7 @@ func TestSnapshotUpdates(t *testing.T) { sdk.OneDec(), sdk.NewDec(10), ) - expectedSnapshot := types.NewReserveSnapshotWithoutCtx( + expectedSnapshot := types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(10), @@ -62,7 +62,7 @@ func TestSnapshotUpdates(t *testing.T) { false, ) require.NoError(t, err) - expectedSnapshot = types.NewReserveSnapshotWithoutCtx( + expectedSnapshot = types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(5), sdk.NewDec(20), diff --git a/x/vpool/keeper/keeper_test.go b/x/vpool/keeper/keeper_test.go index e09ccb1c1..36b6b547a 100644 --- a/x/vpool/keeper/keeper_test.go +++ b/x/vpool/keeper/keeper_test.go @@ -533,7 +533,7 @@ func TestIsOverFluctuationLimit(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - snapshot := types.NewReserveSnapshotWithoutCtx( + snapshot := types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.OneDec(), sdk.NewDec(1000), diff --git a/x/vpool/keeper/prices_test.go b/x/vpool/keeper/prices_test.go index 6cded9d85..c07b0a36d 100644 --- a/x/vpool/keeper/prices_test.go +++ b/x/vpool/keeper/prices_test.go @@ -242,21 +242,21 @@ func TestCalcTwap(t *testing.T) { name: "spot price twap calc, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(90), time.UnixMilli(10), 1, ), - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(85), time.UnixMilli(20), 2, ), - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(95), @@ -274,21 +274,21 @@ func TestCalcTwap(t *testing.T) { name: "spot price twap calc, t=[11,35]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(90), time.UnixMilli(10), 1, ), - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(85), time.UnixMilli(20), 2, ), - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(95), @@ -306,14 +306,14 @@ func TestCalcTwap(t *testing.T) { name: "quote asset swap twap calc, add to pool, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(30), time.UnixMilli(10), 1, ), - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.MustNewDecFromStr("7.5"), sdk.NewDec(40), @@ -333,14 +333,14 @@ func TestCalcTwap(t *testing.T) { name: "quote asset swap twap calc, remove from pool, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(60), time.UnixMilli(10), 1, ), - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(12), sdk.NewDec(50), @@ -360,14 +360,14 @@ func TestCalcTwap(t *testing.T) { name: "base asset swap twap calc, add to pool, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(60), time.UnixMilli(10), 1, ), - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(20), sdk.NewDec(30), @@ -387,14 +387,14 @@ func TestCalcTwap(t *testing.T) { name: "base asset swap twap calc, remove from pool, t=[10,30]", pair: common.Pair_BTC_NUSD, reserveSnapshots: []types.ReserveSnapshot{ - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(10), sdk.NewDec(60), time.UnixMilli(10), 1, ), - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(8), sdk.NewDec(75), diff --git a/x/vpool/keeper/snapshot.go b/x/vpool/keeper/snapshot.go index 117d29be2..ceac85bed 100644 --- a/x/vpool/keeper/snapshot.go +++ b/x/vpool/keeper/snapshot.go @@ -34,7 +34,7 @@ func (k Keeper) SaveSnapshot( quoteAssetReserve sdk.Dec, baseAssetReserve sdk.Dec, ) { - snapshot := types.NewReserveSnapshotWithoutCtx( + snapshot := types.NewReserveSnapshot( pair, baseAssetReserve, quoteAssetReserve, diff --git a/x/vpool/keeper/snapshot_test.go b/x/vpool/keeper/snapshot_test.go index 58566508c..8f5010443 100644 --- a/x/vpool/keeper/snapshot_test.go +++ b/x/vpool/keeper/snapshot_test.go @@ -36,7 +36,7 @@ func TestGetReserveSnapshotMultiplePairs(t *testing.T) { snapshot, err := vpoolKeeper.GetLatestReserveSnapshot(ctx, common.Pair_BTC_NUSD) require.NoError(t, err) require.Equal(t, - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.OneDec(), sdk.OneDec(), @@ -58,7 +58,7 @@ func TestSaveSnapshot(t *testing.T) { snapshot, err := vpoolKeeper.GetLatestReserveSnapshot(ctx, common.Pair_BTC_NUSD) require.NoError(t, err) require.Equal(t, - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.OneDec(), sdk.OneDec(), @@ -87,7 +87,7 @@ func TestGetSnapshot(t *testing.T) { snapshot, err := vpoolKeeper.GetSnapshot(ctx, common.Pair_BTC_NUSD, 1) require.NoError(t, err) require.Equal(t, - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.OneDec(), sdk.OneDec(), @@ -110,7 +110,7 @@ func TestGetSnapshot(t *testing.T) { snapshot, err = vpoolKeeper.GetSnapshot(ctx, common.Pair_BTC_NUSD, 2) require.NoError(t, err) require.Equal(t, - types.NewReserveSnapshotWithoutCtx( + types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(2), sdk.NewDec(2), @@ -185,7 +185,7 @@ func TestGetSnapshotPrice(t *testing.T) { for _, tc := range tests { tc := tc t.Run(tc.name, func(t *testing.T) { - snapshot := types.NewReserveSnapshotWithoutCtx( + snapshot := types.NewReserveSnapshot( tc.pair, tc.baseAssetReserve, tc.quoteAssetReserve, diff --git a/x/vpool/types/snapshot.go b/x/vpool/types/snapshot.go index 57e23bed4..016f78a8a 100644 --- a/x/vpool/types/snapshot.go +++ b/x/vpool/types/snapshot.go @@ -7,7 +7,7 @@ import ( "github.com/NibiruChain/nibiru/x/common" ) -func NewReserveSnapshotWithoutCtx( +func NewReserveSnapshot( pair common.AssetPair, baseAssetReserve, quoteAssetReserve sdk.Dec, blockTime time.Time, From 2ab2d7ff4fe6c97cc9e3bd8df2b605a40c29548f Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 23:01:15 +0200 Subject: [PATCH 17/27] set vpool genesis snapshots as not nullable --- proto/vpool/v1/genesis.proto | 2 +- x/vpool/genesis_test.go | 36 +++++++++++++++++++++--------------- x/vpool/types/genesis.pb.go | 32 ++++++++++++++++---------------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/proto/vpool/v1/genesis.proto b/proto/vpool/v1/genesis.proto index 1ea695db6..466648848 100644 --- a/proto/vpool/v1/genesis.proto +++ b/proto/vpool/v1/genesis.proto @@ -10,5 +10,5 @@ option go_package = "github.com/NibiruChain/nibiru/x/vpool/types"; message GenesisState { repeated VPool vpools = 1; Params params = 2 [(gogoproto.nullable) = false]; - repeated ReserveSnapshot snapshots = 3; + repeated ReserveSnapshot snapshots = 3 [(gogoproto.nullable) = false]; } diff --git a/x/vpool/genesis_test.go b/x/vpool/genesis_test.go index 8b1ad9720..adcc63375 100644 --- a/x/vpool/genesis_test.go +++ b/x/vpool/genesis_test.go @@ -2,6 +2,7 @@ package vpool_test import ( "testing" + "time" "github.com/NibiruChain/nibiru/simapp" @@ -38,22 +39,27 @@ func TestGenesis(t *testing.T) { }, } - //snapshots := []*types.ReserveSnapshot{ - // { - // BaseAssetReserve: sdk.NewDec(1_000_000), - // QuoteAssetReserve: sdk.NewDec(60_000_000_000), - // TimestampMs: 123456, - // BlockNumber: 1, - // }, - // { - // BaseAssetReserve: sdk.NewDec(2_000_000), - // QuoteAssetReserve: sdk.NewDec(50_000_000_000), - // TimestampMs: 123456, - // BlockNumber: 2, - // }, - //} + 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, + ), + } - genesisState := types.GenesisState{Vpools: vpools} + genesisState := types.GenesisState{ + Vpools: vpools, + Snapshots: snapshots, + } nibiruApp, ctx := simapp.NewTestNibiruAppAndContext(true) k := nibiruApp.VpoolKeeper diff --git a/x/vpool/types/genesis.pb.go b/x/vpool/types/genesis.pb.go index 4a5ecc0c5..f57253aee 100644 --- a/x/vpool/types/genesis.pb.go +++ b/x/vpool/types/genesis.pb.go @@ -25,9 +25,9 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the vpool module's genesis state. type GenesisState struct { - Vpools []*VPool `protobuf:"bytes,1,rep,name=vpools,proto3" json:"vpools,omitempty"` - Params Params `protobuf:"bytes,2,opt,name=params,proto3" json:"params"` - Snapshots []*ReserveSnapshot `protobuf:"bytes,3,rep,name=snapshots,proto3" json:"snapshots,omitempty"` + Vpools []*VPool `protobuf:"bytes,1,rep,name=vpools,proto3" json:"vpools,omitempty"` + Params Params `protobuf:"bytes,2,opt,name=params,proto3" json:"params"` + Snapshots []ReserveSnapshot `protobuf:"bytes,3,rep,name=snapshots,proto3" json:"snapshots"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -77,7 +77,7 @@ func (m *GenesisState) GetParams() Params { return Params{} } -func (m *GenesisState) GetSnapshots() []*ReserveSnapshot { +func (m *GenesisState) GetSnapshots() []ReserveSnapshot { if m != nil { return m.Snapshots } @@ -91,24 +91,24 @@ func init() { func init() { proto.RegisterFile("vpool/v1/genesis.proto", fileDescriptor_fc3ffc8cca622811) } var fileDescriptor_fc3ffc8cca622811 = []byte{ - // 257 bytes of a gzipped FileDescriptorProto + // 258 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2b, 0x2b, 0xc8, 0xcf, 0xcf, 0xd1, 0x2f, 0x33, 0xd4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x92, 0x63, 0x9c, 0xf0, - 0x58, 0x8e, 0xe1, 0xc2, 0x63, 0x39, 0x86, 0x1b, 0x8f, 0xe5, 0x18, 0xa2, 0xb4, 0xd3, 0x33, 0x4b, - 0x32, 0x4a, 0x93, 0xf4, 0x92, 0xf3, 0x73, 0xf5, 0xfd, 0xc0, 0x06, 0x3a, 0x67, 0x24, 0x66, 0xe6, - 0xe9, 0x43, 0x0c, 0xd7, 0xaf, 0xd0, 0x87, 0x84, 0x43, 0x49, 0x65, 0x41, 0x6a, 0x71, 0x12, 0x1b, - 0x38, 0x14, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xd7, 0x3c, 0x39, 0xfe, 0x5c, 0x01, 0x00, - 0x00, + 0x31, 0xd4, 0x07, 0x80, 0xa5, 0x9d, 0x58, 0x4e, 0xdc, 0x93, 0x67, 0x08, 0x82, 0x2a, 0x16, 0x72, + 0xe1, 0xe2, 0x2c, 0xce, 0x4b, 0x2c, 0x28, 0xce, 0xc8, 0x2f, 0x29, 0x96, 0x60, 0x06, 0xdb, 0xa4, + 0x80, 0xa1, 0x33, 0x28, 0xb5, 0x38, 0xb5, 0xa8, 0x2c, 0x35, 0x18, 0xaa, 0x10, 0x6a, 0x04, 0x42, + 0xa3, 0x93, 0xeb, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, 0xc7, 0x38, + 0xe1, 0xb1, 0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, 0x44, 0x69, 0xa7, 0x67, + 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xfb, 0x81, 0x8d, 0x75, 0xce, 0x48, 0xcc, + 0xcc, 0xd3, 0x87, 0x58, 0xa1, 0x5f, 0xa1, 0x0f, 0x09, 0x8d, 0x92, 0xca, 0x82, 0xd4, 0xe2, 0x24, + 0x36, 0x70, 0x58, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xa4, 0x71, 0xa3, 0x10, 0x62, 0x01, + 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -337,7 +337,7 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Snapshots = append(m.Snapshots, &ReserveSnapshot{}) + m.Snapshots = append(m.Snapshots, ReserveSnapshot{}) if err := m.Snapshots[len(m.Snapshots)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } From 5df111932360d244fd4bf4270f5caefa02f30b08 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 23:33:27 +0200 Subject: [PATCH 18/27] use common.AssetPair for ReserveSnapshot --- proto/vpool/v1/state.proto | 2 +- x/vpool/keeper/keeper_test.go | 12 +-- x/vpool/types/snapshot.go | 2 +- x/vpool/types/state.pb.go | 148 +++++++++++++++++----------------- 4 files changed, 83 insertions(+), 81 deletions(-) diff --git a/proto/vpool/v1/state.proto b/proto/vpool/v1/state.proto index f3cbf3c4a..4198bf934 100644 --- a/proto/vpool/v1/state.proto +++ b/proto/vpool/v1/state.proto @@ -94,7 +94,7 @@ message CurrentTWAP { // a snapshot of the vpool's reserves at a given point in time message ReserveSnapshot { - string pair = 5; + common.AssetPair pair = 5 [(gogoproto.nullable) = false]; string base_asset_reserve = 1 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", diff --git a/x/vpool/keeper/keeper_test.go b/x/vpool/keeper/keeper_test.go index 36b6b547a..49b062dc8 100644 --- a/x/vpool/keeper/keeper_test.go +++ b/x/vpool/keeper/keeper_test.go @@ -568,14 +568,14 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("15"), }, prevSnapshot: &types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), + Pair: common.Pair_BTC_NUSD, QuoteAssetReserve: sdk.NewDec(1000), BaseAssetReserve: sdk.OneDec(), TimestampMs: 0, BlockNumber: 0, }, latestSnapshot: &types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), + Pair: common.Pair_BTC_NUSD, QuoteAssetReserve: sdk.NewDec(1002), BaseAssetReserve: sdk.OneDec(), TimestampMs: 1, @@ -597,7 +597,7 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("15"), }, prevSnapshot: &types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), + Pair: common.Pair_BTC_NUSD, QuoteAssetReserve: sdk.NewDec(1000), BaseAssetReserve: sdk.OneDec(), TimestampMs: 0, @@ -620,7 +620,7 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("15"), }, prevSnapshot: &types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), + Pair: common.Pair_BTC_NUSD, QuoteAssetReserve: sdk.NewDec(1000), BaseAssetReserve: sdk.OneDec(), TimestampMs: 0, @@ -643,14 +643,14 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("15"), }, prevSnapshot: &types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), + Pair: common.Pair_BTC_NUSD, QuoteAssetReserve: sdk.NewDec(1000), BaseAssetReserve: sdk.OneDec(), TimestampMs: 0, BlockNumber: 0, }, latestSnapshot: &types.ReserveSnapshot{ - Pair: common.Pair_BTC_NUSD.String(), + Pair: common.Pair_BTC_NUSD, QuoteAssetReserve: sdk.NewDec(1002), BaseAssetReserve: sdk.OneDec(), TimestampMs: 1, diff --git a/x/vpool/types/snapshot.go b/x/vpool/types/snapshot.go index 016f78a8a..753f7f365 100644 --- a/x/vpool/types/snapshot.go +++ b/x/vpool/types/snapshot.go @@ -14,7 +14,7 @@ func NewReserveSnapshot( blockHeight int64, ) ReserveSnapshot { return ReserveSnapshot{ - Pair: pair.String(), + Pair: pair, BaseAssetReserve: baseAssetReserve, QuoteAssetReserve: quoteAssetReserve, TimestampMs: blockTime.UnixMilli(), diff --git a/x/vpool/types/state.pb.go b/x/vpool/types/state.pb.go index 5ef0cc370..fa30a0065 100644 --- a/x/vpool/types/state.pb.go +++ b/x/vpool/types/state.pb.go @@ -236,7 +236,7 @@ func (m *CurrentTWAP) GetPairID() string { // a snapshot of the vpool's reserves at a given point in time type ReserveSnapshot struct { - Pair string `protobuf:"bytes,5,opt,name=pair,proto3" json:"pair,omitempty"` + Pair common.AssetPair `protobuf:"bytes,5,opt,name=pair,proto3" json:"pair"` BaseAssetReserve github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,1,opt,name=base_asset_reserve,json=baseAssetReserve,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"base_asset_reserve"` // quote asset is usually the margin asset, e.g. NUSD QuoteAssetReserve github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=quote_asset_reserve,json=quoteAssetReserve,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"quote_asset_reserve"` @@ -278,11 +278,11 @@ func (m *ReserveSnapshot) XXX_DiscardUnknown() { var xxx_messageInfo_ReserveSnapshot proto.InternalMessageInfo -func (m *ReserveSnapshot) GetPair() string { +func (m *ReserveSnapshot) GetPair() common.AssetPair { if m != nil { return m.Pair } - return "" + return common.AssetPair{} } func (m *ReserveSnapshot) GetTimestampMs() int64 { @@ -392,60 +392,60 @@ func init() { func init() { proto.RegisterFile("vpool/v1/state.proto", fileDescriptor_e9da3afd19017067) } var fileDescriptor_e9da3afd19017067 = []byte{ - // 838 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x95, 0xcf, 0x6e, 0x1b, 0x37, - 0x10, 0xc6, 0x25, 0x59, 0x56, 0xac, 0x59, 0xc7, 0x52, 0x68, 0xbb, 0x51, 0xd2, 0x42, 0x72, 0x55, - 0xa0, 0x08, 0x52, 0x54, 0x42, 0xd2, 0x5b, 0x6e, 0xfa, 0x97, 0x42, 0x80, 0xe4, 0xdd, 0xac, 0x94, - 0x04, 0x08, 0x8a, 0x12, 0xd4, 0x8a, 0x91, 0x09, 0x2d, 0xc9, 0x2d, 0x97, 0x52, 0x94, 0xb7, 0xe8, - 0x31, 0xc7, 0x5e, 0xfb, 0x16, 0x3d, 0xe6, 0x98, 0x63, 0xd1, 0x83, 0xd1, 0xda, 0x6f, 0xd0, 0x27, - 0x28, 0xc8, 0xdd, 0xa6, 0x72, 0x5d, 0x14, 0xe8, 0xa2, 0x39, 0xad, 0xf8, 0xcd, 0xec, 0xef, 0xa3, - 0x66, 0x67, 0x48, 0x38, 0x5a, 0x47, 0x52, 0x86, 0xed, 0xf5, 0x83, 0x76, 0xac, 0x89, 0xa6, 0xad, - 0x48, 0x49, 0x2d, 0x51, 0x45, 0xb0, 0x19, 0x53, 0xab, 0x96, 0x0d, 0xb6, 0xd6, 0x0f, 0xee, 0xde, - 0x09, 0x64, 0xcc, 0x65, 0x8c, 0x6d, 0xb8, 0x9d, 0x2c, 0x92, 0xdc, 0xbb, 0x47, 0x0b, 0xb9, 0x90, - 0x89, 0x6e, 0x7e, 0xa5, 0xea, 0x61, 0x20, 0x39, 0x97, 0xa2, 0x9d, 0x3c, 0x12, 0xb1, 0x79, 0x00, - 0x25, 0x8f, 0x28, 0xc2, 0xe3, 0x47, 0xc5, 0x37, 0x3f, 0x34, 0x72, 0xcd, 0xdf, 0x76, 0x61, 0xf7, - 0x99, 0x27, 0x65, 0x88, 0x1e, 0x42, 0x31, 0x22, 0x4c, 0xd5, 0xf2, 0x27, 0xf9, 0x7b, 0xce, 0xc3, - 0x5a, 0x2b, 0xf5, 0x4f, 0xdf, 0xee, 0xc4, 0x31, 0xd5, 0x1e, 0x61, 0xaa, 0x5b, 0x7c, 0x7b, 0xde, - 0xc8, 0xf9, 0x36, 0x17,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xe4, 0x79, 0xc7, 0xab, 0x16, 0xd0, 0x21, 0x54, 0xba, 0x9d, 0xc9, - 0x15, 0x71, 0xa7, 0x3b, 0x78, 0x7b, 0x51, 0xcf, 0xbf, 0xbb, 0xa8, 0xe7, 0x7f, 0xbd, 0xa8, 0xe7, - 0xbf, 0xbf, 0xac, 0xe7, 0xde, 0x5d, 0xd6, 0x73, 0x3f, 0x5f, 0xd6, 0x73, 0x2f, 0xbe, 0xd8, 0x2a, - 0xe2, 0xa9, 0xbd, 0x31, 0x7b, 0x67, 0x84, 0x89, 0x76, 0x72, 0x7b, 0xb6, 0x37, 0xed, 0xe4, 0x72, - 0xb7, 0xd5, 0x9c, 0x95, 0xec, 0x1d, 0xfc, 0xd5, 0x1f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xef, 0xc2, - 0x0a, 0x5c, 0xf2, 0x07, 0x00, 0x00, + // 839 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x95, 0x4f, 0x8f, 0x1a, 0xb7, + 0x1b, 0xc7, 0x81, 0x05, 0xb2, 0x3c, 0xb3, 0x59, 0x88, 0x77, 0xf7, 0x17, 0x92, 0x5f, 0x05, 0x5b, + 0x2a, 0x55, 0x51, 0xaa, 0x82, 0x92, 0xde, 0x72, 0xe3, 0x5f, 0x2a, 0x24, 0xd8, 0x99, 0x0c, 0x24, + 0x91, 0xa2, 0xaa, 0x96, 0x19, 0x1c, 0xd6, 0x62, 0x6c, 0x4f, 0x3d, 0x86, 0x90, 0x77, 0xd1, 0x63, + 0x8e, 0x7d, 0x29, 0x3d, 0xa6, 0xb7, 0x1c, 0xab, 0x1e, 0x56, 0xed, 0xee, 0x3b, 0xe8, 0x2b, 0xa8, + 0xec, 0x99, 0xa6, 0x6c, 0xd3, 0x4b, 0xa7, 0xed, 0x69, 0xf0, 0xf7, 0x79, 0xe6, 0xf3, 0x35, 0xcf, + 0x3c, 0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65, 0x7b, 0x47, 0x7f, 0xf1, 0x5b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xab, + 0x79, 0x54, 0x6a, 0x12, 0x08, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -654,13 +654,16 @@ func (m *ReserveSnapshot) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if len(m.Pair) > 0 { - i -= len(m.Pair) - copy(dAtA[i:], m.Pair) - i = encodeVarintState(dAtA, i, uint64(len(m.Pair))) - i-- - dAtA[i] = 0x2a + { + size, err := m.Pair.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintState(dAtA, i, uint64(size)) } + i-- + dAtA[i] = 0x2a if m.BlockNumber != 0 { i = encodeVarintState(dAtA, i, uint64(m.BlockNumber)) i-- @@ -843,10 +846,8 @@ func (m *ReserveSnapshot) Size() (n int) { if m.BlockNumber != 0 { n += 1 + sovState(uint64(m.BlockNumber)) } - l = len(m.Pair) - if l > 0 { - n += 1 + l + sovState(uint64(l)) - } + l = m.Pair.Size() + n += 1 + l + sovState(uint64(l)) return n } @@ -1578,7 +1579,7 @@ func (m *ReserveSnapshot) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Pair", wireType) } - var stringLen uint64 + var msglen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowState @@ -1588,23 +1589,24 @@ func (m *ReserveSnapshot) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + msglen |= int(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { + if msglen < 0 { return ErrInvalidLengthState } - postIndex := iNdEx + intStringLen + postIndex := iNdEx + msglen if postIndex < 0 { return ErrInvalidLengthState } if postIndex > l { return io.ErrUnexpectedEOF } - m.Pair = string(dAtA[iNdEx:postIndex]) + if err := m.Pair.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } iNdEx = postIndex default: iNdEx = preIndex From f17d8fc2ad04aefcdf7cbe5a0423b0bd00a9ec06 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 23:48:32 +0200 Subject: [PATCH 19/27] SaveSnapshot accepts a snapshot instead of building it --- x/vpool/abci.go | 10 ++++++++- x/vpool/keeper/keeper_test.go | 16 ++++++++++++-- x/vpool/keeper/pool_state.go | 12 +++++++++- x/vpool/keeper/prices_test.go | 11 +++++++--- x/vpool/keeper/snapshot.go | 14 ++---------- x/vpool/keeper/snapshot_test.go | 39 +++++++++++++++++++++++++++------ 6 files changed, 76 insertions(+), 26 deletions(-) diff --git a/x/vpool/abci.go b/x/vpool/abci.go index ca2e6eee1..2b42615af 100644 --- a/x/vpool/abci.go +++ b/x/vpool/abci.go @@ -1,6 +1,7 @@ package vpool import ( + "github.com/NibiruChain/nibiru/x/vpool/types" sdk "github.com/cosmos/cosmos-sdk/types" abci "github.com/tendermint/tendermint/abci/types" @@ -10,7 +11,14 @@ import ( // 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{} } diff --git a/x/vpool/keeper/keeper_test.go b/x/vpool/keeper/keeper_test.go index 49b062dc8..457d2db84 100644 --- a/x/vpool/keeper/keeper_test.go +++ b/x/vpool/keeper/keeper_test.go @@ -671,13 +671,25 @@ func TestCheckFluctuationLimitRatio(t *testing.T) { vpoolKeeper.savePool(ctx, tc.pool) t.Log("save snapshot 0") + ctx = ctx.WithBlockHeight(tc.prevSnapshot.BlockNumber).WithBlockTime(time.UnixMilli(tc.prevSnapshot.TimestampMs)) - vpoolKeeper.SaveSnapshot(ctx, common.Pair_BTC_NUSD, tc.prevSnapshot.QuoteAssetReserve, tc.prevSnapshot.BaseAssetReserve) + snapshot := types.NewReserveSnapshot( + common.Pair_BTC_NUSD, tc.prevSnapshot.BaseAssetReserve, tc.prevSnapshot.QuoteAssetReserve, ctx.BlockTime(), ctx.BlockHeight(), + ) + vpoolKeeper.SaveSnapshot(ctx, snapshot) if tc.latestSnapshot != nil { t.Log("save snapshot 1") ctx = ctx.WithBlockHeight(tc.latestSnapshot.BlockNumber).WithBlockTime(time.UnixMilli(tc.latestSnapshot.TimestampMs)) - vpoolKeeper.SaveSnapshot(ctx, common.Pair_BTC_NUSD, tc.latestSnapshot.QuoteAssetReserve, tc.latestSnapshot.BaseAssetReserve) + + snapshot := types.NewReserveSnapshot( + common.Pair_BTC_NUSD, + tc.latestSnapshot.BaseAssetReserve, + tc.latestSnapshot.QuoteAssetReserve, + ctx.BlockTime(), + ctx.BlockHeight(), + ) + vpoolKeeper.SaveSnapshot(ctx, snapshot) } t.Log("check fluctuation limit") diff --git a/x/vpool/keeper/pool_state.go b/x/vpool/keeper/pool_state.go index 2e7a2a116..969cc075a 100644 --- a/x/vpool/keeper/pool_state.go +++ b/x/vpool/keeper/pool_state.go @@ -32,7 +32,17 @@ func (k Keeper) CreatePool( MaintenanceMarginRatio: maintenanceMarginRatio, MaxLeverage: maxLeverage, }) - k.SaveSnapshot(ctx, pair, quoteAssetReserve, baseAssetReserve) + + k.SaveSnapshot( + ctx, + types.NewReserveSnapshot( + pair, + baseAssetReserve, + quoteAssetReserve, + ctx.BlockTime(), + ctx.BlockHeight(), + ), + ) } // getPool returns the pool from database diff --git a/x/vpool/keeper/prices_test.go b/x/vpool/keeper/prices_test.go index c07b0a36d..867d2c8fe 100644 --- a/x/vpool/keeper/prices_test.go +++ b/x/vpool/keeper/prices_test.go @@ -447,11 +447,16 @@ func TestCalcTwap(t *testing.T) { for _, snapshot := range tc.reserveSnapshots { ctx = ctx.WithBlockHeight(snapshot.BlockNumber).WithBlockTime(time.UnixMilli(snapshot.TimestampMs)) + snapshot := types.NewReserveSnapshot( + common.Pair_BTC_NUSD, + snapshot.BaseAssetReserve, + snapshot.QuoteAssetReserve, + ctx.BlockTime(), + ctx.BlockHeight(), + ) vpoolKeeper.SaveSnapshot( ctx, - tc.pair, - snapshot.QuoteAssetReserve, - snapshot.BaseAssetReserve, + snapshot, ) } ctx = ctx.WithBlockTime(tc.currentBlockTime).WithBlockHeight(tc.currentBlockHeight) diff --git a/x/vpool/keeper/snapshot.go b/x/vpool/keeper/snapshot.go index ceac85bed..0f4a528c1 100644 --- a/x/vpool/keeper/snapshot.go +++ b/x/vpool/keeper/snapshot.go @@ -30,20 +30,10 @@ func (k Keeper) GetSnapshot(ctx sdk.Context, pair common.AssetPair, blockHeight func (k Keeper) SaveSnapshot( ctx sdk.Context, - pair common.AssetPair, - quoteAssetReserve sdk.Dec, - baseAssetReserve sdk.Dec, + snapshot types.ReserveSnapshot, ) { - snapshot := types.NewReserveSnapshot( - pair, - baseAssetReserve, - quoteAssetReserve, - ctx.BlockTime(), - ctx.BlockHeight(), - ) - store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SnapshotsKeyPrefix) - store.Set(types.GetSnapshotKey(pair, uint64(ctx.BlockHeight())), k.codec.MustMarshal(&snapshot)) + store.Set(types.GetSnapshotKey(snapshot.Pair, uint64(ctx.BlockHeight())), k.codec.MustMarshal(&snapshot)) } // GetLatestReserveSnapshot returns the last snapshot that was saved diff --git a/x/vpool/keeper/snapshot_test.go b/x/vpool/keeper/snapshot_test.go index 8f5010443..7ceb1998f 100644 --- a/x/vpool/keeper/snapshot_test.go +++ b/x/vpool/keeper/snapshot_test.go @@ -28,9 +28,26 @@ func TestGetReserveSnapshotMultiplePairs(t *testing.T) { mock.NewMockPricefeedKeeper(gomock.NewController(t)), ) ctx = ctx.WithBlockHeight(1).WithBlockTime(genesisTime) + snapshot := types.NewReserveSnapshot( + common.Pair_BTC_NUSD, + sdk.OneDec(), + sdk.OneDec(), + ctx.BlockTime(), + ctx.BlockHeight(), + ) + vpoolKeeper.SaveSnapshot(ctx, snapshot) - vpoolKeeper.SaveSnapshot(ctx, common.Pair_BTC_NUSD, sdk.OneDec(), sdk.OneDec()) - vpoolKeeper.SaveSnapshot(ctx, common.Pair_ETH_NUSD, sdk.NewDec(2), sdk.NewDec(2)) + snapshot = types.NewReserveSnapshot( + common.Pair_ETH_NUSD, + sdk.NewDec(2), + sdk.NewDec(2), + ctx.BlockTime(), + ctx.BlockHeight(), + ) + vpoolKeeper.SaveSnapshot( + ctx, + snapshot, + ) ctx = ctx.WithBlockHeight(2).WithBlockTime(genesisTime.Add(5 * time.Second)) snapshot, err := vpoolKeeper.GetLatestReserveSnapshot(ctx, common.Pair_BTC_NUSD) @@ -53,7 +70,8 @@ func TestSaveSnapshot(t *testing.T) { ) ctx = ctx.WithBlockHeight(1).WithBlockTime(time.Now()) - vpoolKeeper.SaveSnapshot(ctx, common.Pair_BTC_NUSD, sdk.OneDec(), sdk.OneDec()) + snapshot := types.NewReserveSnapshot(common.Pair_BTC_NUSD, sdk.OneDec(), sdk.OneDec(), ctx.BlockTime(), ctx.BlockHeight()) + vpoolKeeper.SaveSnapshot(ctx, snapshot) snapshot, err := vpoolKeeper.GetLatestReserveSnapshot(ctx, common.Pair_BTC_NUSD) require.NoError(t, err) @@ -76,12 +94,14 @@ func TestGetSnapshot(t *testing.T) { t.Log("Save snapshot 1") ctx = ctx.WithBlockHeight(1).WithBlockTime(time.Now()) - vpoolKeeper.SaveSnapshot( - ctx, + snapshot := types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.OneDec(), sdk.OneDec(), + ctx.BlockTime(), + ctx.BlockHeight(), ) + vpoolKeeper.SaveSnapshot(ctx, snapshot) t.Log("Check snapshot 1") snapshot, err := vpoolKeeper.GetSnapshot(ctx, common.Pair_BTC_NUSD, 1) @@ -99,11 +119,16 @@ func TestGetSnapshot(t *testing.T) { t.Log("Save snapshot 2") ctx = ctx.WithBlockHeight(2).WithBlockTime(time.Now().Add(5 * time.Second)) - vpoolKeeper.SaveSnapshot( - ctx, + snapshot = types.NewReserveSnapshot( common.Pair_BTC_NUSD, sdk.NewDec(2), sdk.NewDec(2), + ctx.BlockTime(), + ctx.BlockHeight(), + ) + vpoolKeeper.SaveSnapshot( + ctx, + snapshot, ) t.Log("Fetch snapshot 2") From 2ff88ccb67bea4ceebf72035b4d91adaf4b424ae Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Tue, 20 Sep 2022 23:55:40 +0200 Subject: [PATCH 20/27] refavctor genesis test --- x/vpool/genesis.go | 4 ++++ x/vpool/genesis_test.go | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/x/vpool/genesis.go b/x/vpool/genesis.go index b356e401c..eb067776a 100644 --- a/x/vpool/genesis.go +++ b/x/vpool/genesis.go @@ -23,6 +23,10 @@ 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. diff --git a/x/vpool/genesis_test.go b/x/vpool/genesis_test.go index adcc63375..b796c76ec 100644 --- a/x/vpool/genesis_test.go +++ b/x/vpool/genesis_test.go @@ -72,7 +72,7 @@ func TestGenesis(t *testing.T) { exportedGenesis := vpool.ExportGenesis(ctx, k) require.Len(t, exportedGenesis.Vpools, 2) - for _, exportedVpool := range exportedGenesis.Vpools { - require.Contains(t, genesisState.Vpools, exportedVpool) + for _, pool := range genesisState.Vpools { + require.Contains(t, exportedGenesis.Vpools, pool) } } From 81d8c9a44bab2830e8e63bd2e9a0a79fe31a0062 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 21 Sep 2022 00:11:22 +0200 Subject: [PATCH 21/27] add genesis getAllSnapshots --- x/vpool/genesis_test.go | 4 ++++ x/vpool/keeper/snapshot.go | 21 ++++++++++++++++++-- x/vpool/keeper/snapshot_test.go | 34 +++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/x/vpool/genesis_test.go b/x/vpool/genesis_test.go index b796c76ec..6a6605820 100644 --- a/x/vpool/genesis_test.go +++ b/x/vpool/genesis_test.go @@ -75,4 +75,8 @@ func TestGenesis(t *testing.T) { for _, pool := range genesisState.Vpools { require.Contains(t, exportedGenesis.Vpools, pool) } + + //for _, snapshot := range genesisState.Snapshots { + // require.Contains(t, exportedGenesis.Snapshots, snapshot) + //} } diff --git a/x/vpool/keeper/snapshot.go b/x/vpool/keeper/snapshot.go index 0f4a528c1..9aeff3d61 100644 --- a/x/vpool/keeper/snapshot.go +++ b/x/vpool/keeper/snapshot.go @@ -2,7 +2,6 @@ package keeper import ( "fmt" - "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" @@ -33,7 +32,7 @@ func (k Keeper) SaveSnapshot( snapshot types.ReserveSnapshot, ) { store := prefix.NewStore(ctx.KVStore(k.storeKey), types.SnapshotsKeyPrefix) - store.Set(types.GetSnapshotKey(snapshot.Pair, uint64(ctx.BlockHeight())), k.codec.MustMarshal(&snapshot)) + store.Set(types.GetSnapshotKey(snapshot.Pair, uint64(snapshot.BlockNumber)), k.codec.MustMarshal(&snapshot)) } // GetLatestReserveSnapshot returns the last snapshot that was saved @@ -53,6 +52,24 @@ func (k Keeper) GetLatestReserveSnapshot(ctx sdk.Context, pair common.AssetPair) return types.ReserveSnapshot{}, types.ErrNoLastSnapshotSaved } +// GetAllSnapshots returns all pools that exist. +func (k Keeper) GetAllSnapshots(ctx sdk.Context) []types.ReserveSnapshot { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, types.SnapshotsKeyPrefix) + + var snapshots []types.ReserveSnapshot + for ; iterator.Valid(); iterator.Next() { + bz := iterator.Value() + + var snapshot types.ReserveSnapshot + k.codec.MustUnmarshal(bz, &snapshot) + + snapshots = append(snapshots, snapshot) + } + + return snapshots +} + /* An object parameter for getPriceWithSnapshot(). diff --git a/x/vpool/keeper/snapshot_test.go b/x/vpool/keeper/snapshot_test.go index 7ceb1998f..0199c14ad 100644 --- a/x/vpool/keeper/snapshot_test.go +++ b/x/vpool/keeper/snapshot_test.go @@ -146,6 +146,40 @@ func TestGetSnapshot(t *testing.T) { ) } +func TestGetAllSnapshots(t *testing.T) { + vpoolKeeper, ctx := VpoolKeeper(t, + mock.NewMockPricefeedKeeper(gomock.NewController(t)), + ) + + 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, + ), + } + + for _, snapshot := range snapshots { + vpoolKeeper.SaveSnapshot(ctx, snapshot) + } + + savedSnapshots := vpoolKeeper.GetAllSnapshots(ctx) + require.Len(t, savedSnapshots, 2) + + for _, snapshot := range snapshots { + require.Contains(t, savedSnapshots, snapshot) + } +} + func TestGetSnapshotPrice(t *testing.T) { tests := []struct { name string From 4a98c732f9a4ad7cad21965008b8ac27679bc120 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 21 Sep 2022 00:27:41 +0200 Subject: [PATCH 22/27] add export and import snapshots --- x/vpool/genesis.go | 2 ++ x/vpool/genesis_test.go | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/x/vpool/genesis.go b/x/vpool/genesis.go index eb067776a..4f782407c 100644 --- a/x/vpool/genesis.go +++ b/x/vpool/genesis.go @@ -32,9 +32,11 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState) // 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 } diff --git a/x/vpool/genesis_test.go b/x/vpool/genesis_test.go index 6a6605820..68f940753 100644 --- a/x/vpool/genesis_test.go +++ b/x/vpool/genesis_test.go @@ -14,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{ { @@ -54,6 +53,13 @@ func TestGenesis(t *testing.T) { 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{ @@ -71,12 +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 _, snapshot := range genesisState.Snapshots { - // require.Contains(t, exportedGenesis.Snapshots, snapshot) - //} + for _, snapshot := range genesisState.Snapshots { + require.Contains(t, exportedGenesis.Snapshots, snapshot) + } } From 176294da33ed205e03bda2dea17573167f464647 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 21 Sep 2022 00:42:06 +0200 Subject: [PATCH 23/27] remove unused params --- proto/vpool/v1/genesis.proto | 1 - proto/vpool/v1/state.proto | 5 - x/vpool/types/genesis.go | 8 +- x/vpool/types/genesis.pb.go | 79 ++---------- x/vpool/types/genesis_test.go | 3 - x/vpool/types/params.go | 39 ------ x/vpool/types/state.pb.go | 234 +++++++++------------------------- 7 files changed, 73 insertions(+), 296 deletions(-) delete mode 100644 x/vpool/types/params.go diff --git a/proto/vpool/v1/genesis.proto b/proto/vpool/v1/genesis.proto index 466648848..6d386511f 100644 --- a/proto/vpool/v1/genesis.proto +++ b/proto/vpool/v1/genesis.proto @@ -9,6 +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]; } diff --git a/proto/vpool/v1/state.proto b/proto/vpool/v1/state.proto index 4198bf934..faacbf495 100644 --- a/proto/vpool/v1/state.proto +++ b/proto/vpool/v1/state.proto @@ -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 { diff --git a/x/vpool/types/genesis.go b/x/vpool/types/genesis.go index 592855119..a45b2acea 100644 --- a/x/vpool/types/genesis.go +++ b/x/vpool/types/genesis.go @@ -10,8 +10,8 @@ import ( // DefaultGenesis returns the default Capability genesis state func DefaultGenesis() *GenesisState { return &GenesisState{ - Params: DefaultParams(), - Vpools: []*VPool{}, + Vpools: []*VPool{}, + Snapshots: []ReserveSnapshot{}, } } @@ -30,8 +30,8 @@ func (gs GenesisState) Validate() error { } vpools[pair] = struct{}{} } - // validate params - return gs.Params.Validate() + + return nil } func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.RawMessage) *GenesisState { diff --git a/x/vpool/types/genesis.pb.go b/x/vpool/types/genesis.pb.go index f57253aee..cfc1b1cd3 100644 --- a/x/vpool/types/genesis.pb.go +++ b/x/vpool/types/genesis.pb.go @@ -26,7 +26,6 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // GenesisState defines the vpool module's genesis state. type GenesisState struct { Vpools []*VPool `protobuf:"bytes,1,rep,name=vpools,proto3" json:"vpools,omitempty"` - Params Params `protobuf:"bytes,2,opt,name=params,proto3" json:"params"` Snapshots []ReserveSnapshot `protobuf:"bytes,3,rep,name=snapshots,proto3" json:"snapshots"` } @@ -70,13 +69,6 @@ func (m *GenesisState) GetVpools() []*VPool { return nil } -func (m *GenesisState) GetParams() Params { - if m != nil { - return m.Params - } - return Params{} -} - func (m *GenesisState) GetSnapshots() []ReserveSnapshot { if m != nil { return m.Snapshots @@ -91,24 +83,22 @@ func init() { func init() { proto.RegisterFile("vpool/v1/genesis.proto", fileDescriptor_fc3ffc8cca622811) } var fileDescriptor_fc3ffc8cca622811 = []byte{ - // 258 bytes of a gzipped FileDescriptorProto + // 230 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2b, 0x2b, 0xc8, 0xcf, 0xcf, 0xd1, 0x2f, 0x33, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0xcf, 0xcb, 0x4c, 0xca, 0x2c, 0x2a, 0xd5, 0x03, 0x4b, 0xeb, 0x95, 0x19, 0x4a, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0xe5, 0xf4, 0x41, 0x2c, 0x88, 0x32, 0x29, 0x11, 0xb8, - 0xf6, 0xe2, 0x92, 0xc4, 0x92, 0x54, 0x88, 0xa8, 0xd2, 0x6e, 0x46, 0x2e, 0x1e, 0x77, 0x88, 0x71, - 0xc1, 0x20, 0x61, 0x21, 0x3d, 0x2e, 0x36, 0xb0, 0xc2, 0x62, 0x09, 0x46, 0x05, 0x66, 0x0d, 0x6e, - 0x23, 0x31, 0x3d, 0x34, 0xe3, 0xf5, 0xc2, 0x02, 0xf2, 0xf3, 0x73, 0x82, 0xa0, 0xaa, 0x84, 0x4c, - 0xb9, 0xd8, 0x0a, 0x12, 0x8b, 0x12, 0x73, 0x8b, 0x25, 0x98, 0x14, 0x18, 0x35, 0xb8, 0x8d, 0xc4, - 0x31, 0xd4, 0x07, 0x80, 0xa5, 0x9d, 0x58, 0x4e, 0xdc, 0x93, 0x67, 0x08, 0x82, 0x2a, 0x16, 0x72, - 0xe1, 0xe2, 0x2c, 0xce, 0x4b, 0x2c, 0x28, 0xce, 0xc8, 0x2f, 0x29, 0x96, 0x60, 0x06, 0xdb, 0xa4, - 0x80, 0xa1, 0x33, 0x28, 0xb5, 0x38, 0xb5, 0xa8, 0x2c, 0x35, 0x18, 0xaa, 0x10, 0x6a, 0x04, 0x42, - 0xa3, 0x93, 0xeb, 0x89, 0x47, 0x72, 0x8c, 0x17, 0x1e, 0xc9, 0x31, 0x3e, 0x78, 0x24, 0xc7, 0x38, - 0xe1, 0xb1, 0x1c, 0xc3, 0x85, 0xc7, 0x72, 0x0c, 0x37, 0x1e, 0xcb, 0x31, 0x44, 0x69, 0xa7, 0x67, - 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xfb, 0x81, 0x8d, 0x75, 0xce, 0x48, 0xcc, - 0xcc, 0xd3, 0x87, 0x58, 0xa1, 0x5f, 0xa1, 0x0f, 0x09, 0x8d, 0x92, 0xca, 0x82, 0xd4, 0xe2, 0x24, - 0x36, 0x70, 0x58, 0x18, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xa4, 0x71, 0xa3, 0x10, 0x62, 0x01, - 0x00, 0x00, + 0xf6, 0xe2, 0x92, 0xc4, 0x92, 0x54, 0x88, 0xa8, 0x52, 0x0b, 0x23, 0x17, 0x8f, 0x3b, 0xc4, 0xb8, + 0x60, 0x90, 0xb0, 0x90, 0x1e, 0x17, 0x1b, 0x58, 0x61, 0xb1, 0x04, 0xa3, 0x02, 0xb3, 0x06, 0xb7, + 0x91, 0x98, 0x1e, 0x9a, 0xf1, 0x7a, 0x61, 0x01, 0xf9, 0xf9, 0x39, 0x41, 0x50, 0x55, 0x42, 0x2e, + 0x5c, 0x9c, 0xc5, 0x79, 0x89, 0x05, 0xc5, 0x19, 0xf9, 0x25, 0xc5, 0x12, 0xcc, 0x60, 0x2d, 0x0a, + 0x18, 0x5a, 0x82, 0x52, 0x8b, 0x53, 0x8b, 0xca, 0x52, 0x83, 0xa1, 0x0a, 0x9d, 0x58, 0x4e, 0xdc, + 0x93, 0x67, 0x08, 0x42, 0x68, 0x74, 0x72, 0x3d, 0xf1, 0x48, 0x8e, 0xf1, 0xc2, 0x23, 0x39, 0xc6, + 0x07, 0x8f, 0xe4, 0x18, 0x27, 0x3c, 0x96, 0x63, 0xb8, 0xf0, 0x58, 0x8e, 0xe1, 0xc6, 0x63, 0x39, + 0x86, 0x28, 0xed, 0xf4, 0xcc, 0x92, 0x8c, 0xd2, 0x24, 0xbd, 0xe4, 0xfc, 0x5c, 0x7d, 0x3f, 0xb0, + 0xb1, 0xce, 0x19, 0x89, 0x99, 0x79, 0xfa, 0x10, 0x2b, 0xf4, 0x2b, 0xf4, 0x21, 0xde, 0x2a, 0xa9, + 0x2c, 0x48, 0x2d, 0x4e, 0x62, 0x03, 0x7b, 0xca, 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0xae, 0x12, + 0xc1, 0x9f, 0x2b, 0x01, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -145,16 +135,6 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { dAtA[i] = 0x1a } } - { - size, err := m.Params.MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintGenesis(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 if len(m.Vpools) > 0 { for iNdEx := len(m.Vpools) - 1; iNdEx >= 0; iNdEx-- { { @@ -195,8 +175,6 @@ func (m *GenesisState) Size() (n int) { n += 1 + l + sovGenesis(uint64(l)) } } - l = m.Params.Size() - n += 1 + l + sovGenesis(uint64(l)) if len(m.Snapshots) > 0 { for _, e := range m.Snapshots { l = e.Size() @@ -275,39 +253,6 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex - case 2: - if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Params", wireType) - } - var msglen int - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowGenesis - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - msglen |= int(b&0x7F) << shift - if b < 0x80 { - break - } - } - if msglen < 0 { - return ErrInvalidLengthGenesis - } - postIndex := iNdEx + msglen - if postIndex < 0 { - return ErrInvalidLengthGenesis - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.Params.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex case 3: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Snapshots", wireType) diff --git a/x/vpool/types/genesis_test.go b/x/vpool/types/genesis_test.go index 7c204790c..93d6241e1 100644 --- a/x/vpool/types/genesis_test.go +++ b/x/vpool/types/genesis_test.go @@ -39,7 +39,6 @@ func TestGenesisState_Validate(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("10"), }, }, - Params: Params{}, }, wantErr: false, }, @@ -67,7 +66,6 @@ func TestGenesisState_Validate(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("0"), }, }, - Params: Params{}, }, wantErr: true, }, @@ -105,7 +103,6 @@ func TestGenesisState_Validate(t *testing.T) { MaxLeverage: sdk.MustNewDecFromStr("10"), }, }, - Params: Params{}, }, wantErr: true, }, diff --git a/x/vpool/types/params.go b/x/vpool/types/params.go deleted file mode 100644 index 918919ad5..000000000 --- a/x/vpool/types/params.go +++ /dev/null @@ -1,39 +0,0 @@ -package types - -import ( - paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" - "gopkg.in/yaml.v2" -) - -var _ paramtypes.ParamSet = (*Params)(nil) - -// ParamKeyTable the param key table for launch module -func ParamKeyTable() paramtypes.KeyTable { - return paramtypes.NewKeyTable().RegisterParamSet(&Params{}) -} - -// NewParams creates a new Params instance -func NewParams() Params { - return Params{} -} - -// DefaultParams returns a default set of parameters -func DefaultParams() Params { - return Params{} -} - -// ParamSetPairs get the params.ParamSet -func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { - return paramtypes.ParamSetPairs{} -} - -// Validate validates the set of params -func (p Params) Validate() error { - return nil -} - -// String implements the Stringer interface. -func (p Params) String() string { - out, _ := yaml.Marshal(p) - return string(out) -} diff --git a/x/vpool/types/state.pb.go b/x/vpool/types/state.pb.go index fa30a0065..85efbb5ea 100644 --- a/x/vpool/types/state.pb.go +++ b/x/vpool/types/state.pb.go @@ -89,42 +89,6 @@ func (TwapCalcOption) EnumDescriptor() ([]byte, []int) { return fileDescriptor_e9da3afd19017067, []int{1} } -// Params defines the parameters for the module. -type Params struct { -} - -func (m *Params) Reset() { *m = Params{} } -func (*Params) ProtoMessage() {} -func (*Params) Descriptor() ([]byte, []int) { - return fileDescriptor_e9da3afd19017067, []int{0} -} -func (m *Params) XXX_Unmarshal(b []byte) error { - return m.Unmarshal(b) -} -func (m *Params) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { - if deterministic { - return xxx_messageInfo_Params.Marshal(b, m, deterministic) - } else { - b = b[:cap(b)] - n, err := m.MarshalToSizedBuffer(b) - if err != nil { - return nil, err - } - return b[:n], nil - } -} -func (m *Params) XXX_Merge(src proto.Message) { - xxx_messageInfo_Params.Merge(m, src) -} -func (m *Params) XXX_Size() int { - return m.Size() -} -func (m *Params) XXX_DiscardUnknown() { - xxx_messageInfo_Params.DiscardUnknown(m) -} - -var xxx_messageInfo_Params proto.InternalMessageInfo - // A virtual pool used only for price discovery of perpetual futures contracts. // No real liquidity exists in this pool. type VPool struct { @@ -150,7 +114,7 @@ func (m *VPool) Reset() { *m = VPool{} } func (m *VPool) String() string { return proto.CompactTextString(m) } func (*VPool) ProtoMessage() {} func (*VPool) Descriptor() ([]byte, []int) { - return fileDescriptor_e9da3afd19017067, []int{1} + return fileDescriptor_e9da3afd19017067, []int{0} } func (m *VPool) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -198,7 +162,7 @@ func (m *CurrentTWAP) Reset() { *m = CurrentTWAP{} } func (m *CurrentTWAP) String() string { return proto.CompactTextString(m) } func (*CurrentTWAP) ProtoMessage() {} func (*CurrentTWAP) Descriptor() ([]byte, []int) { - return fileDescriptor_e9da3afd19017067, []int{2} + return fileDescriptor_e9da3afd19017067, []int{1} } func (m *CurrentTWAP) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -249,7 +213,7 @@ func (m *ReserveSnapshot) Reset() { *m = ReserveSnapshot{} } func (m *ReserveSnapshot) String() string { return proto.CompactTextString(m) } func (*ReserveSnapshot) ProtoMessage() {} func (*ReserveSnapshot) Descriptor() ([]byte, []int) { - return fileDescriptor_e9da3afd19017067, []int{3} + return fileDescriptor_e9da3afd19017067, []int{2} } func (m *ReserveSnapshot) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -322,7 +286,7 @@ func (m *PoolPrices) Reset() { *m = PoolPrices{} } func (m *PoolPrices) String() string { return proto.CompactTextString(m) } func (*PoolPrices) ProtoMessage() {} func (*PoolPrices) Descriptor() ([]byte, []int) { - return fileDescriptor_e9da3afd19017067, []int{4} + return fileDescriptor_e9da3afd19017067, []int{3} } func (m *PoolPrices) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -382,7 +346,6 @@ func (m *PoolPrices) GetBlockNumber() int64 { func init() { proto.RegisterEnum("nibiru.vpool.v1.Direction", Direction_name, Direction_value) proto.RegisterEnum("nibiru.vpool.v1.TwapCalcOption", TwapCalcOption_name, TwapCalcOption_value) - proto.RegisterType((*Params)(nil), "nibiru.vpool.v1.Params") proto.RegisterType((*VPool)(nil), "nibiru.vpool.v1.VPool") proto.RegisterType((*CurrentTWAP)(nil), "nibiru.vpool.v1.CurrentTWAP") proto.RegisterType((*ReserveSnapshot)(nil), "nibiru.vpool.v1.ReserveSnapshot") @@ -392,83 +355,59 @@ func init() { func init() { proto.RegisterFile("vpool/v1/state.proto", fileDescriptor_e9da3afd19017067) } var fileDescriptor_e9da3afd19017067 = []byte{ - // 839 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x95, 0x4f, 0x8f, 0x1a, 0xb7, - 0x1b, 0xc7, 0x81, 0x05, 0xb2, 0x3c, 0xb3, 0x59, 0x88, 0x77, 0xf7, 0x17, 0x92, 0x5f, 0x05, 0x5b, - 0x2a, 0x55, 0x51, 0xaa, 0x82, 0x92, 0xde, 0x72, 0xe3, 0x5f, 0x2a, 0x24, 0xd8, 0x99, 0x0c, 0x24, - 0x91, 0xa2, 0xaa, 0x96, 0x19, 0x1c, 0xd6, 0x62, 0x6c, 0x4f, 0x3d, 0x86, 0x90, 0x77, 0xd1, 0x63, - 0x8e, 0x7d, 0x29, 0x3d, 0xa6, 0xb7, 0x1c, 0xab, 0x1e, 0x56, 0xed, 0xee, 0x3b, 0xe8, 0x2b, 0xa8, - 0xec, 0x99, 0xa6, 0x6c, 0xd3, 0x4b, 0xa7, 0xed, 0x69, 0xf0, 0xf7, 0x79, 0xe6, 0xf3, 0x35, 0xcf, - 0x3c, 0x7e, 0x0c, 0xc7, 0x9b, 0x48, 0xca, 0xb0, 0xb3, 0x79, 0xd0, 0x89, 0x35, 0xd1, 0xb4, 0x1d, - 0x29, 0xa9, 0x25, 0xaa, 0x0a, 0x36, 0x67, 0x6a, 0xdd, 0xb6, 0xc1, 0xf6, 0xe6, 0xc1, 0xdd, 0x3b, - 0x81, 0x8c, 0xb9, 0x8c, 0xb1, 0x0d, 0x77, 0x92, 0x45, 0x92, 0x7b, 0xf7, 0x78, 0x29, 0x97, 0x32, - 0xd1, 0xcd, 0xaf, 0x54, 0x3d, 0x0a, 0x24, 0xe7, 0x52, 0x74, 0x92, 0x47, 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79, 0x74, 0x0c, 0x35, 0x7f, 0x38, - 0x71, 0x9f, 0x0d, 0xf1, 0x63, 0xdf, 0x9d, 0x24, 0x6a, 0xe1, 0xfe, 0x12, 0x0e, 0x67, 0xaf, 0x48, - 0xd4, 0x27, 0x61, 0xe0, 0x46, 0x96, 0x79, 0x0a, 0x1f, 0x99, 0x11, 0x80, 0xfb, 0xdd, 0x71, 0x1f, - 0xbb, 0xde, 0x5f, 0xa0, 0xf7, 0xa1, 0x38, 0xf5, 0xdc, 0x59, 0xc2, 0x7c, 0xf2, 0xd4, 0x9d, 0x0d, - 0x71, 0x77, 0x3a, 0x1d, 0xce, 0xf0, 0xf4, 0x79, 0xd7, 0xab, 0x15, 0xd0, 0x11, 0x54, 0x7b, 0xdd, - 0xe9, 0x35, 0x71, 0xaf, 0x37, 0x7c, 0x7b, 0xd9, 0xc8, 0xbf, 0xbb, 0x6c, 0xe4, 0x7f, 0xbe, 0x6c, - 0xe4, 0xbf, 0xbd, 0x6a, 0xe4, 0xde, 0x5d, 0x35, 0x72, 0x3f, 0x5e, 0x35, 0x72, 0x2f, 0x3e, 0xdb, - 0x29, 0xe2, 0x99, 0x3d, 0x48, 0xfd, 0x73, 0xc2, 0x44, 0x27, 0x39, 0x54, 0x9d, 0x6d, 0x27, 0xb9, - 0xfc, 0x6d, 0x35, 0xe7, 0x65, 0x7b, 0x47, 0x7f, 0xf1, 0x5b, 0x00, 0x00, 0x00, 0xff, 0xff, 0xab, - 0x79, 0x54, 0x6a, 0x12, 0x08, 0x00, 0x00, -} - -func (m *Params) Marshal() (dAtA []byte, err error) { - size := m.Size() - dAtA = make([]byte, size) - n, err := m.MarshalToSizedBuffer(dAtA[:size]) - if err != nil { - return nil, err - } - return dAtA[:n], nil -} - -func (m *Params) MarshalTo(dAtA []byte) (int, error) { - size := m.Size() - return m.MarshalToSizedBuffer(dAtA[:size]) -} - -func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { - i := len(dAtA) - _ = i - var l int - _ = l - return len(dAtA) - i, nil + // 822 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x95, 0xdf, 0x6e, 0x1b, 0x45, + 0x14, 0xc6, 0x6d, 0x27, 0x4e, 0xe3, 0xb3, 0x69, 0xec, 0x4e, 0x12, 0xea, 0x16, 0x64, 0x07, 0x23, + 0xa1, 0xaa, 0x08, 0x5b, 0x2d, 0x77, 0xdc, 0xf9, 0x5f, 0x91, 0x25, 0x3b, 0xbb, 0x5d, 0xbb, 0xad, + 0x54, 0x21, 0x46, 0xe3, 0xf5, 0xd4, 0x19, 0x79, 0x67, 0x66, 0x99, 0x9d, 0x75, 0xdd, 0xb7, 0xe0, + 0x92, 0xc7, 0xe1, 0xb2, 0xdc, 0xf5, 0x12, 0x71, 0x11, 0x41, 0xf2, 0x06, 0x3c, 0x01, 0x9a, 0xd9, + 0xa5, 0x38, 0x84, 0x1b, 0x16, 0xb8, 0x5a, 0xcf, 0x77, 0x8e, 0x7f, 0xdf, 0xec, 0xd9, 0x33, 0x67, + 0xe0, 0x78, 0x1d, 0x49, 0x19, 0x76, 0xd6, 0x8f, 0x3a, 0xb1, 0x26, 0x9a, 0xb6, 0x23, 0x25, 0xb5, + 0x44, 0x55, 0xc1, 0xe6, 0x4c, 0x25, 0x6d, 0x1b, 0x6c, 0xaf, 0x1f, 0xdd, 0xbf, 0x17, 0xc8, 0x98, + 0xcb, 0x18, 0xdb, 0x70, 0x27, 0x5d, 0xa4, 0xb9, 0xf7, 0x8f, 0x97, 0x72, 0x29, 0x53, 0xdd, 0xfc, + 0xca, 0xd4, 0xa3, 0x40, 0x72, 0x2e, 0x45, 0x27, 0x7d, 0xa4, 0x62, 0xeb, 0xd7, 0x32, 0x94, 0x9f, + 0x7b, 0x52, 0x86, 0xe8, 0x31, 0xec, 0x46, 0x84, 0xa9, 0x7a, 0xf1, 0xb4, 0xf8, 0xc0, 0x79, 0x5c, +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xa7, 0x9e, 0x3b, 0x4b, 0x99, 0x4f, 0x9f, 0xb9, 0xb3, 0x21, + 0xee, 0x4e, 0xa7, 0xc3, 0x19, 0x9e, 0xbe, 0xe8, 0x7a, 0xb5, 0x12, 0x3a, 0x82, 0x6a, 0xaf, 0x3b, + 0xbd, 0x26, 0xee, 0xf4, 0x86, 0x6f, 0x2f, 0x1b, 0xc5, 0x77, 0x97, 0x8d, 0xe2, 0x2f, 0x97, 0x8d, + 0xe2, 0x77, 0x57, 0x8d, 0xc2, 0xbb, 0xab, 0x46, 0xe1, 0xa7, 0xab, 0x46, 0xe1, 0xe5, 0x67, 0x5b, + 0x45, 0x3c, 0xb3, 0x07, 0xa9, 0x7f, 0x4e, 0x98, 0xe8, 0xa4, 0x87, 0xaa, 0xb3, 0xe9, 0xa4, 0x97, + 0xbd, 0xad, 0xe6, 0x7c, 0xcf, 0xde, 0xc9, 0x5f, 0xfc, 0x1e, 0x00, 0x00, 0xff, 0xff, 0xc2, 0x60, + 0x79, 0x45, 0x02, 0x08, 0x00, 0x00, } func (m *VPool) Marshal() (dAtA []byte, err error) { @@ -777,15 +716,6 @@ func encodeVarintState(dAtA []byte, offset int, v uint64) int { dAtA[offset] = uint8(v) return base } -func (m *Params) Size() (n int) { - if m == nil { - return 0 - } - var l int - _ = l - return n -} - func (m *VPool) Size() (n int) { if m == nil { return 0 @@ -885,56 +815,6 @@ func sovState(x uint64) (n int) { func sozState(x uint64) (n int) { return sovState(uint64((x << 1) ^ uint64((int64(x) >> 63)))) } -func (m *Params) Unmarshal(dAtA []byte) error { - l := len(dAtA) - iNdEx := 0 - for iNdEx < l { - preIndex := iNdEx - var wire uint64 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowState - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - wire |= uint64(b&0x7F) << shift - if b < 0x80 { - break - } - } - fieldNum := int32(wire >> 3) - wireType := int(wire & 0x7) - if wireType == 4 { - return fmt.Errorf("proto: Params: wiretype end group for non-group") - } - if fieldNum <= 0 { - return fmt.Errorf("proto: Params: illegal tag %d (wire type %d)", fieldNum, wire) - } - switch fieldNum { - default: - iNdEx = preIndex - skippy, err := skipState(dAtA[iNdEx:]) - if err != nil { - return err - } - if (skippy < 0) || (iNdEx+skippy) < 0 { - return ErrInvalidLengthState - } - if (iNdEx + skippy) > l { - return io.ErrUnexpectedEOF - } - iNdEx += skippy - } - } - - if iNdEx > l { - return io.ErrUnexpectedEOF - } - return nil -} func (m *VPool) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 From 99d3934109891f21b377bed8158a6ba67360795b Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 21 Sep 2022 01:15:44 +0200 Subject: [PATCH 24/27] add validate to snapshot --- x/vpool/types/snapshot.go | 26 +++++++++ x/vpool/types/snapshot_test.go | 97 ++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 x/vpool/types/snapshot_test.go diff --git a/x/vpool/types/snapshot.go b/x/vpool/types/snapshot.go index 753f7f365..5060d4126 100644 --- a/x/vpool/types/snapshot.go +++ b/x/vpool/types/snapshot.go @@ -1,6 +1,7 @@ package types import ( + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "time" @@ -21,3 +22,28 @@ func NewReserveSnapshot( BlockNumber: blockHeight, } } + +func (s ReserveSnapshot) Validate() error { + err := s.Pair.Validate() + if err != nil { + return err + } + + if s.BaseAssetReserve.IsNegative() { + return fmt.Errorf("base asset reserve from snapshot cannot be negative: %d", s.BaseAssetReserve) + } + + if s.QuoteAssetReserve.IsNegative() { + return fmt.Errorf("quote asset reserve from snapshot cannot be negative: %d", s.QuoteAssetReserve) + } + + if s.TimestampMs < 0 { + return fmt.Errorf("timestamp from snapshot cannot be negative: %d", s.TimestampMs) + } + + if s.BlockNumber < 0 { + return fmt.Errorf("blocknumber from snapshot cannot be negative: %d", s.BlockNumber) + } + + return nil +} diff --git a/x/vpool/types/snapshot_test.go b/x/vpool/types/snapshot_test.go new file mode 100644 index 000000000..564a96831 --- /dev/null +++ b/x/vpool/types/snapshot_test.go @@ -0,0 +1,97 @@ +package types + +import ( + "github.com/NibiruChain/nibiru/x/common" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + "testing" + "time" +) + +func TestSnapshotValidate(t *testing.T) { + tests := []struct { + name string + snapshot ReserveSnapshot + shouldErr bool + }{ + { + name: "happy path", + snapshot: ReserveSnapshot{ + Pair: common.Pair_ETH_NUSD, + BaseAssetReserve: sdk.OneDec(), + QuoteAssetReserve: sdk.OneDec(), + TimestampMs: time.Now().UnixMilli(), + BlockNumber: 1, + }, + shouldErr: false, + }, + { + name: "invalid pair", + snapshot: ReserveSnapshot{ + Pair: common.MustNewAssetPair("$443ยท:fjka"), + BaseAssetReserve: sdk.OneDec(), + QuoteAssetReserve: sdk.OneDec(), + TimestampMs: time.Now().UnixMilli(), + BlockNumber: 1, + }, + shouldErr: true, + }, + { + name: "base asset negative", + snapshot: ReserveSnapshot{ + Pair: common.Pair_ETH_NUSD, + BaseAssetReserve: sdk.NewDec(-1), + QuoteAssetReserve: sdk.OneDec(), + TimestampMs: time.Now().UnixMilli(), + BlockNumber: 1, + }, + shouldErr: true, + }, + { + name: "quote asset negative", + snapshot: ReserveSnapshot{ + Pair: common.Pair_ETH_NUSD, + BaseAssetReserve: sdk.ZeroDec(), + QuoteAssetReserve: sdk.NewDec(-1), + TimestampMs: time.Now().UnixMilli(), + BlockNumber: 1, + }, + shouldErr: true, + }, + { + name: "timestamp negative", + snapshot: ReserveSnapshot{ + Pair: common.Pair_ETH_NUSD, + BaseAssetReserve: sdk.ZeroDec(), + QuoteAssetReserve: sdk.ZeroDec(), + TimestampMs: -1, + BlockNumber: 1, + }, + shouldErr: true, + }, + { + name: "blocknumber negative", + snapshot: ReserveSnapshot{ + Pair: common.Pair_ETH_NUSD, + BaseAssetReserve: sdk.ZeroDec(), + QuoteAssetReserve: sdk.ZeroDec(), + TimestampMs: 1, + BlockNumber: -1, + }, + shouldErr: true, + }, + } + + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + err := tc.snapshot.Validate() + if tc.shouldErr { + require.Error(t, err) + } else { + require.Nil(t, err) + } + }) + } + +} From 334886494bdd271e9485aba2f2fb03d5deb45bdc Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 21 Sep 2022 01:26:47 +0200 Subject: [PATCH 25/27] add genesis validate --- x/vpool/types/genesis.go | 6 ++++++ x/vpool/types/genesis_test.go | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/x/vpool/types/genesis.go b/x/vpool/types/genesis.go index a45b2acea..625b67e99 100644 --- a/x/vpool/types/genesis.go +++ b/x/vpool/types/genesis.go @@ -31,6 +31,12 @@ func (gs GenesisState) Validate() error { vpools[pair] = struct{}{} } + for _, snapshot := range gs.Snapshots { + if err := snapshot.Validate(); err != nil { + return err + } + } + return nil } diff --git a/x/vpool/types/genesis_test.go b/x/vpool/types/genesis_test.go index 93d6241e1..52f0bd743 100644 --- a/x/vpool/types/genesis_test.go +++ b/x/vpool/types/genesis_test.go @@ -106,6 +106,42 @@ func TestGenesisState_Validate(t *testing.T) { }, wantErr: true, }, + "invalid snapshot": { + genesis: &GenesisState{ + Vpools: []*VPool{ + { + Pair: common.MustNewAssetPair("btc:usd"), + BaseAssetReserve: sdk.MustNewDecFromStr("100000"), + QuoteAssetReserve: sdk.MustNewDecFromStr("100000"), + TradeLimitRatio: sdk.MustNewDecFromStr("0.5"), + FluctuationLimitRatio: sdk.MustNewDecFromStr("0.5"), + MaxOracleSpreadRatio: sdk.MustNewDecFromStr("0.5"), + MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.05"), + MaxLeverage: sdk.MustNewDecFromStr("10"), + }, + { + Pair: common.MustNewAssetPair("eth:usd"), + BaseAssetReserve: sdk.MustNewDecFromStr("100000"), + QuoteAssetReserve: sdk.MustNewDecFromStr("100000"), + TradeLimitRatio: sdk.MustNewDecFromStr("0.5"), + FluctuationLimitRatio: sdk.MustNewDecFromStr("0.5"), + MaxOracleSpreadRatio: sdk.MustNewDecFromStr("0.5"), + MaintenanceMarginRatio: sdk.MustNewDecFromStr("0.05"), + MaxLeverage: sdk.MustNewDecFromStr("10"), + }, + }, + Snapshots: []ReserveSnapshot{ + { + Pair: common.Pair_ETH_NUSD, + BaseAssetReserve: sdk.OneDec(), + QuoteAssetReserve: sdk.OneDec(), + TimestampMs: -1, + BlockNumber: 0, + }, + }, + }, + wantErr: true, + }, } for name, tc := range cases { From bcef0919c321a257b7411da5d1d92dcf6768c43f Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 21 Sep 2022 01:30:39 +0200 Subject: [PATCH 26/27] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 261702af3..48fc05d30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From b704e839081c36210d1f7f4712ecf1f7d89c0c02 Mon Sep 17 00:00:00 2001 From: Agent Smith Date: Wed, 21 Sep 2022 08:32:21 +0200 Subject: [PATCH 27/27] fix linter problems --- x/vpool/abci.go | 3 ++- x/vpool/keeper/snapshot.go | 1 + x/vpool/types/snapshot.go | 3 ++- x/vpool/types/snapshot_test.go | 9 +++++---- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/x/vpool/abci.go b/x/vpool/abci.go index 2b42615af..c834dc609 100644 --- a/x/vpool/abci.go +++ b/x/vpool/abci.go @@ -1,10 +1,11 @@ package vpool import ( - "github.com/NibiruChain/nibiru/x/vpool/types" 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" ) diff --git a/x/vpool/keeper/snapshot.go b/x/vpool/keeper/snapshot.go index 9aeff3d61..c455d8ac0 100644 --- a/x/vpool/keeper/snapshot.go +++ b/x/vpool/keeper/snapshot.go @@ -2,6 +2,7 @@ package keeper import ( "fmt" + "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" diff --git a/x/vpool/types/snapshot.go b/x/vpool/types/snapshot.go index 5060d4126..3575bc662 100644 --- a/x/vpool/types/snapshot.go +++ b/x/vpool/types/snapshot.go @@ -2,9 +2,10 @@ package types import ( "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" "time" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/NibiruChain/nibiru/x/common" ) diff --git a/x/vpool/types/snapshot_test.go b/x/vpool/types/snapshot_test.go index 564a96831..924ad8613 100644 --- a/x/vpool/types/snapshot_test.go +++ b/x/vpool/types/snapshot_test.go @@ -1,11 +1,13 @@ package types import ( - "github.com/NibiruChain/nibiru/x/common" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/require" "testing" "time" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" + + "github.com/NibiruChain/nibiru/x/common" ) func TestSnapshotValidate(t *testing.T) { @@ -93,5 +95,4 @@ func TestSnapshotValidate(t *testing.T) { } }) } - }