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: reduce gas on openposition #1237

Merged
merged 15 commits into from
Mar 29, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#1223](https://github.com/NibiruChain/nibiru/pull/1223) - chore(deps): bump github.com/golang/protobuf from 1.5.2 to 1.5.3
* [#1205](https://github.com/NibiruChain/nibiru/pull/1205) - test: first testing framework skeleton and example
* [#1228](https://github.com/NibiruChain/nibiru/pull/1228) - feat: update github.com/CosmWasm/wasmd 0.29.2
* [#1237](https://github.com/NibiruChain/nibiru/pull/1237) - feat: reduce gas on openposition

### Bug Fixes

Expand Down
40 changes: 21 additions & 19 deletions x/common/testutil/mock/perp_interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions x/perp/keeper/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"fmt"

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

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

"github.com/NibiruChain/nibiru/x/common/asset"
Expand Down Expand Up @@ -86,19 +88,16 @@ position without making it go underwater.
- err: error
*/
func (k Keeper) calcFreeCollateral(
ctx sdk.Context, pos types.Position,
ctx sdk.Context, vpool vpooltypes.Vpool, pos types.Position,
) (freeCollateral sdk.Dec, err error) {
if err = pos.Pair.Validate(); err != nil {
return
}

if err = k.requireVpool(ctx, pos.Pair); err != nil {
return
}

positionNotional, unrealizedPnL, err := k.
GetPreferencePositionNotionalAndUnrealizedPnL(
ctx,
vpool,
pos,
types.PnLPreferenceOption_MIN,
)
Expand Down
68 changes: 3 additions & 65 deletions x/perp/keeper/calc_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,6 @@ import (
vpooltypes "github.com/NibiruChain/nibiru/x/vpool/types"
)

func TestCalcFreeCollateralErrors(t *testing.T) {
testCases := []struct {
name string
test func()
}{
{
name: "invalid token pair - error",
test: func() {
k, _, ctx := getKeeper(t)
alice := testutil.AccAddress()
pos := types.ZeroPosition(ctx, asset.Pair("foobar"), alice)
_, err := k.calcFreeCollateral(ctx, pos)

require.Error(t, err)
require.ErrorIs(t, err, asset.ErrInvalidTokenPair)
},
},
{
name: "token pair not found - error",
test: func() {
k, mocks, ctx := getKeeper(t)

mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD)).Return(false)

pos := types.ZeroPosition(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), testutil.AccAddress())

_, err := k.calcFreeCollateral(ctx, pos)

require.Error(t, err)
require.ErrorIs(t, err, types.ErrPairNotFound)
},
},
{
name: "zero position",
test: func() {
k, mocks, ctx := getKeeper(t)

mocks.mockVpoolKeeper.EXPECT().
ExistsPool(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD)).Return(true)
mocks.mockVpoolKeeper.EXPECT().
GetMaintenanceMarginRatio(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD)).
Return(sdk.MustNewDecFromStr("0.0625"), nil)

pos := types.ZeroPosition(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), testutil.AccAddress())

freeCollateral, err := k.calcFreeCollateral(ctx, pos)

require.NoError(t, err)
assert.EqualValues(t, sdk.ZeroDec(), freeCollateral)
},
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
tc.test()
})
}
}

func TestCalcFreeCollateralSuccess(t *testing.T) {
testCases := []struct {
name string
Expand Down Expand Up @@ -148,6 +87,7 @@ func TestCalcFreeCollateralSuccess(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
k, mocks, ctx := getKeeper(t)

vpool := vpooltypes.Vpool{Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD)}
pos := types.Position{
TraderAddress: testutil.AccAddress().String(),
Pair: asset.Registry.Pair(denoms.BTC, denoms.NUSD),
Expand All @@ -159,13 +99,11 @@ func TestCalcFreeCollateralSuccess(t *testing.T) {
}

t.Log("mock vpool keeper")
mocks.mockVpoolKeeper.EXPECT().ExistsPool(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD)).Return(true)
mocks.mockVpoolKeeper.EXPECT().
GetMaintenanceMarginRatio(ctx, asset.Registry.Pair(denoms.BTC, denoms.NUSD)).
Return(sdk.MustNewDecFromStr("0.0625"), nil)
mocks.mockVpoolKeeper.EXPECT().GetBaseAssetPrice(
ctx,
asset.Registry.Pair(denoms.BTC, denoms.NUSD),
vpool,
tc.vpoolDirection,
sdk.OneDec(),
).Return(tc.positionNotional, nil)
Expand All @@ -177,7 +115,7 @@ func TestCalcFreeCollateralSuccess(t *testing.T) {
15*time.Minute,
).Return(tc.positionNotional, nil)

freeCollateral, err := k.calcFreeCollateral(ctx, pos)
freeCollateral, err := k.calcFreeCollateral(ctx, vpool, pos)

require.NoError(t, err)
assert.EqualValues(t, tc.expectedFreeCollateral, freeCollateral)
Expand Down
Loading