Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(perp): Make the collateral denom a stateful collections.Item #1656

Merged
merged 25 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
25291a9
feat: add collateral statefull
matthiasmatt Oct 31, 2023
65a2496
chore: changelog
matthiasmatt Oct 31, 2023
ab46623
test: fix tests wip
matthiasmatt Nov 1, 2023
fa9cacf
fix: fix tests
matthiasmatt Nov 1, 2023
b10c7fa
feat: add admin function call for update collateral
matthiasmatt Nov 2, 2023
41c9231
feat: added update collateral to genesis export and loads for test an…
matthiasmatt Nov 2, 2023
4d5b5fb
Merge branch 'master' into mat/make-quote-collection
matthiasmatt Nov 2, 2023
62fcc8d
fix: lint
matthiasmatt Nov 2, 2023
b34931b
Merge branch 'mat/make-quote-collection' of github.com:NibiruChain/ni…
matthiasmatt Nov 2, 2023
f212a13
fix: rename collateralDenom
matthiasmatt Nov 2, 2023
42254fa
fix: fix weird *&
matthiasmatt Nov 2, 2023
da65180
fix: fix margin tests
matthiasmatt Nov 2, 2023
64923cd
fix: fix account prefix mismatch
matthiasmatt Nov 2, 2023
74e147c
fix: fix when no collateral is in genesis
matthiasmatt Nov 2, 2023
5dd9116
fix: remove collateral type and use the token factory one
matthiasmatt Nov 3, 2023
d8c5355
Merge branch 'master' into mat/make-quote-collection
matthiasmatt Nov 6, 2023
9e835f8
Merge branch 'master' into mat/make-quote-collection
matthiasmatt Nov 7, 2023
681f1fa
feat(common): implement StringValueEncoder
Unique-Divine Nov 15, 2023
0065ae0
fix(perp): use string instead of TFDenom for the collateral denom + f…
Unique-Divine Nov 15, 2023
b561255
#wip: all passing tests
Unique-Divine Nov 15, 2023
991e5d2
Merge branch 'mat/make-quote-collection' of https://github.com/Nibiru…
Unique-Divine Nov 15, 2023
15fe848
Merge branch 'master' into mat/make-quote-collection
Unique-Divine Nov 15, 2023
5ce3f35
refactor: linter blank lines
Unique-Divine Nov 15, 2023
2dd5807
feat(perp): make a msg server method gated by sudo
Unique-Divine Nov 15, 2023
32aacfd
refactor: linter
Unique-Divine Nov 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1620](https://github.com/NibiruChain/nibiru/pull/1620) - Token factory transaction messages for Mint and Burn
* [#1573](https://github.com/NibiruChain/nibiru/pull/1573) - feat(perp): Close markets and compute settlement price
* [#1632](https://github.com/NibiruChain/nibiru/pull/1632) - feat(perp): Add settle position transaction
* [#1656](https://github.com/NibiruChain/nibiru/pull/1656) - feat(perp): Make the collateral denom a stateful collections.Item
* [#1670](https://github.com/NibiruChain/nibiru/pull/1670) - feat(inflation): Make inflation polynomial

### State Machine Breaking
Expand Down Expand Up @@ -785,4 +786,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Testing

* [#695](https://github.com/NibiruChain/nibiru/pull/695) Add `OpenPosition` integration tests.
* [#692](https://github.com/NibiruChain/nibiru/pull/692) Add test coverage for Perp MsgServer methods.
* [#692](https://github.com/NibiruChain/nibiru/pull/692) Add test coverage for Perp MsgServer methods.
9 changes: 5 additions & 4 deletions app/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,13 +364,14 @@ func (app *NibiruApp) InitKeepers(
appCodec, keys[epochstypes.StoreKey],
)

app.SudoKeeper = keeper.NewKeeper(
appCodec, keys[sudotypes.StoreKey],
)

app.PerpKeeperV2 = perpkeeper.NewKeeper(
appCodec, keys[perptypes.StoreKey],
app.AccountKeeper, app.BankKeeper, app.OracleKeeper, app.EpochsKeeper,
)

app.SudoKeeper = keeper.NewKeeper(
appCodec, keys[sudotypes.StoreKey],
app.SudoKeeper,
)

app.InflationKeeper = inflationkeeper.NewKeeper(
Expand Down
9 changes: 6 additions & 3 deletions proto/nibiru/perp/v2/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ syntax = "proto3";

package nibiru.perp.v2;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/v1beta1/coin.proto";
import "google/api/annotations.proto";
import "gogoproto/gogo.proto";
import "nibiru/perp/v2/state.proto";

option go_package = "github.com/NibiruChain/nibiru/x/perp/v2/types";
Expand Down Expand Up @@ -33,6 +33,9 @@ message GenesisState {
];
}

// For testing purposes, we allow the collateral to be set at genesis
string collateral_denom = 11;

repeated TraderVolume trader_volumes = 7 [ (gogoproto.nullable) = false ];

message Discount {
Expand Down Expand Up @@ -81,4 +84,4 @@ message GenesisPosition {
uint64 version = 2;

Position position = 3 [ (gogoproto.nullable) = false ];
}
}
14 changes: 14 additions & 0 deletions proto/nibiru/perp/v2/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ service Msg {

rpc DonateToEcosystemFund(MsgDonateToEcosystemFund)
returns (MsgDonateToEcosystemFundResponse) {}

rpc ChangeCollateralDenom(MsgChangeCollateralDenom)
returns (MsgChangeCollateralDenomResponse) {}
}

// -------------------------- Settle Position --------------------------
Expand Down Expand Up @@ -328,3 +331,14 @@ message MsgDonateToEcosystemFund {
}

message MsgDonateToEcosystemFundResponse {}

// ----------------------- MsgChangeCollateralDenom -----------------------

// MsgChangeCollateralDenom: Changes the collateral denom for the module.
// [Admin] Only callable by sudoers.
message MsgChangeCollateralDenom {
string sender = 1;
string new_denom = 2;
}

message MsgChangeCollateralDenomResponse {}
12 changes: 7 additions & 5 deletions wasmbinding/exec_perp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (s *TestSuitePerpExecutor) SetupSuite() {
})
coins := sdk.NewCoins(
sdk.NewCoin(denoms.NIBI, sdk.NewInt(1_000_000)),
sdk.NewCoin(denoms.NUSD, sdk.NewInt(420_000*69)),
sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(420_000*69)),
sdk.NewCoin(denoms.USDT, sdk.NewInt(420_000*69)),
)
s.NoError(testapp.FundAccount(nibiru.BankKeeper, ctx, sender, coins))
Expand All @@ -66,6 +66,7 @@ func (s *TestSuitePerpExecutor) SetupSuite() {
s.exec = &wasmbinding.ExecutorPerp{
PerpV2: nibiru.PerpKeeperV2,
}
s.nibiru.PerpKeeperV2.Collateral.Set(s.ctx, perpv2types.TestingCollateralDenomNUSD)
s.NoError(testapp.FundAccount(nibiru.BankKeeper, ctx, s.contractPerp, coins))

s.OnSetupEnd()
Expand All @@ -79,7 +80,8 @@ func (s *TestSuitePerpExecutor) OnSetupEnd() {
// Happy path coverage of MarketOrder, AddMargin, RemoveMargin, and ClosePosition
func (s *TestSuitePerpExecutor) TestOpenAddRemoveClose() {
pair := asset.MustNewPair(s.happyFields.Pair)
margin := sdk.NewCoin(denoms.NUSD, sdk.NewInt(69))

margin := sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(69))
incorrectMargin := sdk.NewCoin(denoms.USDT, sdk.NewInt(69))

for _, err := range []error{
Expand Down Expand Up @@ -243,7 +245,7 @@ func (s *TestSuitePerpExecutor) DoInsuranceFundWithdrawTest(
s.nibiru.BankKeeper,
s.ctx,
perpv2types.PerpEFModuleAccount,
sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(420))),
sdk.NewCoins(sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(420))),
)
s.NoError(err)

Expand Down Expand Up @@ -330,7 +332,7 @@ func (s *TestSuitePerpExecutor) DoSetMarketEnabledTest(
}

func (s *TestSuitePerpExecutor) TestSadPath_InsuranceFundWithdraw() {
fundsToWithdraw := sdk.NewCoin(denoms.NUSD, sdk.NewInt(69_000))
fundsToWithdraw := sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(69_000))

err := s.DoInsuranceFundWithdrawTest(fundsToWithdraw.Amount, s.contractDeployer)
s.Error(err)
Expand All @@ -339,7 +341,7 @@ func (s *TestSuitePerpExecutor) TestSadPath_InsuranceFundWithdraw() {
func (s *TestSuitePerpExecutor) TestSadPaths_InvalidPair() {
sadPair := asset.Pair("ftt:ust:doge")
pair := sadPair
margin := sdk.NewCoin(denoms.NUSD, sdk.NewInt(69))
margin := sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(69))

for _, err := range []error{
s.DoMarketOrderTest(pair),
Expand Down
11 changes: 7 additions & 4 deletions wasmbinding/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ func (s *TestSuiteExecutor) SetupSuite() {
ChainID: "nibiru-wasmnet-1",
Time: time.Now().UTC(),
})
nibiru.PerpKeeperV2.Collateral.Set(ctx, perpv2types.TestingCollateralDenomNUSD)

coins := sdk.NewCoins(
sdk.NewCoin(denoms.NIBI, sdk.NewInt(10_000_000)),
sdk.NewCoin(denoms.NUSD, sdk.NewInt(420_000*69)),
sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(420_000*69)),
)

s.NoError(testapp.FundAccount(nibiru.BankKeeper, ctx, sender, coins))
Expand All @@ -110,6 +112,7 @@ func (s *TestSuiteExecutor) SetupSuite() {
s.ctx = ctx
s.keeper = TestOnlySudoKeeper{Keeper: s.nibiru.SudoKeeper}
s.wasmKeeper = wasmkeeper.NewDefaultPermissionKeeper(nibiru.WasmKeeper)
s.nibiru.PerpKeeperV2.Collateral.Set(s.ctx, perpv2types.TestingCollateralDenomNUSD)

s.contractPerp = ContractMap[wasmbin.WasmKeyPerpBinding]
s.contractController = ContractMap[wasmbin.WasmKeyController]
Expand All @@ -126,10 +129,10 @@ func (s *TestSuiteExecutor) OnSetupEnd() {

func (s *TestSuiteExecutor) TestOpenAddRemoveClose() {
pair := asset.MustNewPair(s.happyFields.Pair)
margin := sdk.NewCoin(denoms.NUSD, sdk.NewInt(69))
margin := sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(69))

coins := sdk.NewCoins(
margin.Add(sdk.NewCoin(denoms.NUSD, sdk.NewInt(1_000))),
margin.Add(sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(1_000))),
)
s.NoError(testapp.FundAccount(s.nibiru.BankKeeper, s.ctx, s.contractPerp, coins))

Expand Down Expand Up @@ -466,7 +469,7 @@ func (s *TestSuiteExecutor) TestInsuranceFundWithdraw() {
s.nibiru.BankKeeper,
s.ctx,
perpv2types.PerpEFModuleAccount,
sdk.NewCoins(sdk.NewCoin(denoms.NUSD, sdk.NewInt(420))),
sdk.NewCoins(sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(420))),
)
s.NoError(err)
s.keeper.SetSudoContracts(
Expand Down
2 changes: 1 addition & 1 deletion wasmbinding/querier_perp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (s *TestSuitePerpQuerier) SetupSuite() {
})
coins := sdk.NewCoins(
sdk.NewCoin(denoms.NIBI, sdk.NewInt(10_000_000)),
sdk.NewCoin(denoms.NUSD, sdk.NewInt(1_420_000)),
sdk.NewCoin(perpv2types.TestingCollateralDenomNUSD, sdk.NewInt(1_420_000)),
)
s.NoError(testapp.FundAccount(nibiru.BankKeeper, ctx, sender, coins))

Expand Down
11 changes: 11 additions & 0 deletions x/common/address.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"github.com/NibiruChain/collections"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -20,3 +21,13 @@
}
return addrs
}

// TODO: (realu) Move to collections library
var StringValueEncoder collections.ValueEncoder[string] = stringValueEncoder{}

type stringValueEncoder struct{}

func (a stringValueEncoder) Encode(value string) []byte { return []byte(value) }
func (a stringValueEncoder) Decode(b []byte) string { return string(b) }
func (a stringValueEncoder) Stringify(value string) string { return value }
func (a stringValueEncoder) Name() string { return "string" }

Check warning on line 33 in x/common/address.go

View check run for this annotation

Codecov / codecov/patch

x/common/address.go#L33

Added line #L33 was not covered by tests
23 changes: 23 additions & 0 deletions x/common/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common_test
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/NibiruChain/nibiru/x/common"
Expand All @@ -17,3 +18,25 @@ func TestAddress(t *testing.T) {
require.EqualValues(t, addrs, addrsOut)
})
}

func TestStringValueEncoder(t *testing.T) {
encoder := common.StringValueEncoder
tests := []struct {
given string
}{
{"hello"},
{"12345"},
{""},
{testutil.AccAddress().String()},
}

for _, tc := range tests {
t.Run(tc.given, func(t *testing.T) {
want := tc.given
encoded := encoder.Encode(tc.given)
got := encoder.Decode(encoded)
assert.Equal(t, want, got)
assert.Equal(t, want, encoder.Stringify(got))
})
}
}
8 changes: 8 additions & 0 deletions x/common/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package common

import sdk "github.com/cosmos/cosmos-sdk/types"

const (
TreasuryPoolModuleAccount = "treasury_pool"
// TO_MICRO: multiplier for converting between units and micro-units.
TO_MICRO = int64(1_000_000)

NibiruTeam = "nibi1l8dxzwz9d4peazcqjclnkj2mhvtj7mpnkqx85mg0ndrlhwrnh7gskkzg0v"
)

func NibiruTeamAddr() sdk.AccAddress {
return sdk.MustAccAddressFromBech32(NibiruTeam)

Check warning on line 14 in x/common/constants.go

View check run for this annotation

Codecov / codecov/patch

x/common/constants.go#L13-L14

Added lines #L13 - L14 were not covered by tests
}
7 changes: 7 additions & 0 deletions x/common/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"errors"
"fmt"
"runtime/debug"

grpccodes "google.golang.org/grpc/codes"
grpcstatus "google.golang.org/grpc/status"
)

// TryCatch is an implementation of the try-catch block from languages like C++ and JS.
Expand Down Expand Up @@ -180,3 +183,7 @@
}
return CombineErrors(errs...)
}

func ErrNilMsg() error {
return grpcstatus.Errorf(grpccodes.InvalidArgument, "nil msg")

Check warning on line 188 in x/common/error.go

View check run for this annotation

Codecov / codecov/patch

x/common/error.go#L187-L188

Added lines #L187 - L188 were not covered by tests
}
3 changes: 3 additions & 0 deletions x/common/testutil/cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import (
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"google.golang.org/grpc"

perpV2types "github.com/NibiruChain/nibiru/x/perp/v2/types"

"github.com/NibiruChain/nibiru/x/common/denoms"

"github.com/NibiruChain/nibiru/app"
Expand Down Expand Up @@ -174,6 +176,7 @@ func BuildNetworkConfig(appGenesis app.GenesisState) Config {
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),
StartingTokens: sdk.NewCoins(
sdk.NewCoin(denoms.NUSD, sdk.TokensFromConsensusPower(1e12, sdk.DefaultPowerReduction)),
sdk.NewCoin(perpV2types.TestingCollateralDenomNUSD, sdk.TokensFromConsensusPower(1e12, sdk.DefaultPowerReduction)),
sdk.NewCoin(denoms.NIBI, sdk.TokensFromConsensusPower(1e12, sdk.DefaultPowerReduction)),
sdk.NewCoin(denoms.USDC, sdk.TokensFromConsensusPower(1e12, sdk.DefaultPowerReduction)),
),
Expand Down
1 change: 1 addition & 0 deletions x/common/testutil/genesis/perp_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func AddPerpV2Genesis(gen app.GenesisState) app.GenesisState {
Amms: ammsv2,
Positions: []perpv2types.GenesisPosition{},
ReserveSnapshots: []perpv2types.ReserveSnapshot{},
CollateralDenom: perpv2types.TestingCollateralDenomNUSD,
}

gen[perpv2types.ModuleName] = app.MakeEncodingConfig().Marshaler.
Expand Down
3 changes: 3 additions & 0 deletions x/common/testutil/testapp/testapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/NibiruChain/nibiru/x/common/denoms"
epochstypes "github.com/NibiruChain/nibiru/x/epochs/types"
inflationtypes "github.com/NibiruChain/nibiru/x/inflation/types"
"github.com/NibiruChain/nibiru/x/perp/v2/types"
)

// NewNibiruTestAppAndContext creates an 'app.NibiruApp' instance with an
Expand All @@ -34,6 +35,8 @@ func NewNibiruTestAppAndContext() (*app.NibiruApp, sdk.Context) {
app.OracleKeeper.SetPrice(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), sdk.NewDec(20000))
app.OracleKeeper.SetPrice(ctx, "xxx:yyy", sdk.NewDec(20000))

app.PerpKeeperV2.Collateral.Set(ctx, types.TestingCollateralDenomNUSD)

return app, ctx
}

Expand Down
Loading
Loading