-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(perp): query position and margin (#477)
* skeleton * reserve assets * proto * reserve asset * remove reserve asset * fix * Fix type casting on perp queryServer * add a test * clean up * clean up * lint * remove trader margin query * Update x/perp/keeper/grpc_query_test.go Co-authored-by: Walter White <[email protected]> * fix * Update x/perp/keeper/grpc_query_test.go Co-authored-by: Walter White <[email protected]> * fixes * proto-gen * fix Co-authored-by: Walter White <[email protected]> Co-authored-by: Mat-Cosmos <[email protected]> Co-authored-by: Walter White <[email protected]>
- Loading branch information
1 parent
ebf5b56
commit 3413950
Showing
11 changed files
with
742 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/NibiruChain/nibiru/x/common" | ||
"github.com/NibiruChain/nibiru/x/perp/types" | ||
) | ||
|
||
type queryServer struct { | ||
Keeper | ||
} | ||
|
||
func NewQuerier(k Keeper) queryServer { | ||
return queryServer{Keeper: k} | ||
} | ||
|
||
var _ types.QueryServer = queryServer{} | ||
|
||
func (q queryServer) TraderPosition( | ||
goCtx context.Context, req *types.QueryTraderPositionRequest, | ||
) (*types.QueryTraderPositionResponse, error) { | ||
if req == nil { | ||
return nil, status.Error(codes.InvalidArgument, "invalid request") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(goCtx) | ||
position, err := q.Keeper.Positions().Get(ctx, common.TokenPair(req.TokenPair), req.Trader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &types.QueryTraderPositionResponse{ | ||
Position: position, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/NibiruChain/nibiru/x/common" | ||
"github.com/NibiruChain/nibiru/x/perp/keeper" | ||
"github.com/NibiruChain/nibiru/x/perp/types" | ||
"github.com/NibiruChain/nibiru/x/testutil" | ||
"github.com/NibiruChain/nibiru/x/testutil/sample" | ||
) | ||
|
||
func TestQueryPosition_Ok(t *testing.T) { | ||
t.Log("initialize keeper") | ||
nibiruApp, ctx := testutil.NewNibiruApp(true) | ||
perpKeeper := &nibiruApp.PerpKeeper | ||
|
||
queryServer := keeper.NewQuerier(*perpKeeper) | ||
|
||
trader := sample.AccAddress() | ||
vpoolPair, err := common.NewTokenPairFromStr("btc:nusd") | ||
require.NoError(t, err) | ||
|
||
oldPosition := &types.Position{ | ||
TraderAddress: trader, | ||
Pair: vpoolPair.String(), | ||
Size_: sdk.NewDec(10), | ||
OpenNotional: sdk.NewDec(10), | ||
Margin: sdk.NewDec(1), | ||
} | ||
|
||
perpKeeper.SetPosition( | ||
ctx, vpoolPair, trader, oldPosition) | ||
|
||
res, err := queryServer.TraderPosition( | ||
sdk.WrapSDKContext(ctx), | ||
&types.QueryTraderPositionRequest{ | ||
Trader: trader, | ||
TokenPair: vpoolPair.String(), | ||
}, | ||
) | ||
fmt.Println("res:", res) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, oldPosition.TraderAddress, res.Position.TraderAddress) | ||
assert.Equal(t, oldPosition.Pair, res.Position.Pair) | ||
assert.Equal(t, oldPosition.Size_, res.Position.Size_) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.