Skip to content

Commit

Permalink
return mintedShares in vault deposit keeper method (#2139)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 authored Aug 22, 2024
1 parent 431c69b commit 14393e4
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 77 deletions.
28 changes: 23 additions & 5 deletions indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SubaccountId, SubaccountIdSDKType } from "../subaccounts/subaccount";
import { QuotingParams, QuotingParamsSDKType, VaultParams, VaultParamsSDKType } from "./params";
import { VaultId, VaultIdSDKType } from "./vault";
import { NumShares, NumSharesSDKType } from "./share";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial } from "../../helpers";
/**
Expand Down Expand Up @@ -29,10 +30,16 @@ export interface MsgDepositToMegavaultSDKType {
}
/** MsgDepositToMegavaultResponse is the Msg/DepositToMegavault response type. */

export interface MsgDepositToMegavaultResponse {}
export interface MsgDepositToMegavaultResponse {
/** The number of shares minted from the deposit. */
mintedShares?: NumShares;
}
/** MsgDepositToMegavaultResponse is the Msg/DepositToMegavault response type. */

export interface MsgDepositToMegavaultResponseSDKType {}
export interface MsgDepositToMegavaultResponseSDKType {
/** The number of shares minted from the deposit. */
minted_shares?: NumSharesSDKType;
}
/**
* MsgUpdateDefaultQuotingParams is the Msg/UpdateDefaultQuotingParams request
* type.
Expand Down Expand Up @@ -152,11 +159,17 @@ export const MsgDepositToMegavault = {
};

function createBaseMsgDepositToMegavaultResponse(): MsgDepositToMegavaultResponse {
return {};
return {
mintedShares: undefined
};
}

export const MsgDepositToMegavaultResponse = {
encode(_: MsgDepositToMegavaultResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
encode(message: MsgDepositToMegavaultResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.mintedShares !== undefined) {
NumShares.encode(message.mintedShares, writer.uint32(10).fork()).ldelim();
}

return writer;
},

Expand All @@ -169,6 +182,10 @@ export const MsgDepositToMegavaultResponse = {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.mintedShares = NumShares.decode(reader, reader.uint32());
break;

default:
reader.skipType(tag & 7);
break;
Expand All @@ -178,8 +195,9 @@ export const MsgDepositToMegavaultResponse = {
return message;
},

fromPartial(_: DeepPartial<MsgDepositToMegavaultResponse>): MsgDepositToMegavaultResponse {
fromPartial(object: DeepPartial<MsgDepositToMegavaultResponse>): MsgDepositToMegavaultResponse {
const message = createBaseMsgDepositToMegavaultResponse();
message.mintedShares = object.mintedShares !== undefined && object.mintedShares !== null ? NumShares.fromPartial(object.mintedShares) : undefined;
return message;
}

Expand Down
6 changes: 5 additions & 1 deletion proto/dydxprotocol/vault/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "cosmos_proto/cosmos.proto";
import "cosmos/msg/v1/msg.proto";
import "dydxprotocol/subaccounts/subaccount.proto";
import "dydxprotocol/vault/params.proto";
import "dydxprotocol/vault/share.proto";
import "dydxprotocol/vault/vault.proto";
import "gogoproto/gogo.proto";

Expand Down Expand Up @@ -44,7 +45,10 @@ message MsgDepositToMegavault {
}

// MsgDepositToMegavaultResponse is the Msg/DepositToMegavault response type.
message MsgDepositToMegavaultResponse {}
message MsgDepositToMegavaultResponse {
// The number of shares minted from the deposit.
NumShares minted_shares = 1 [ (gogoproto.nullable) = false ];
}

// MsgUpdateDefaultQuotingParams is the Msg/UpdateDefaultQuotingParams request
// type.
Expand Down
60 changes: 50 additions & 10 deletions protocol/x/vault/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,60 @@ import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
assettypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
sendingtypes "github.com/dydxprotocol/v4-chain/protocol/x/sending/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

// DepositToMegavault deposits from a subaccount to megavault by
// 1. Minting shares for owner address of `fromSubaccount`.
// 2. Transferring `quoteQuantums` from `fromSubaccount` to megavault subaccount 0.
func (k Keeper) DepositToMegavault(
ctx sdk.Context,
fromSubaccount satypes.SubaccountId,
quoteQuantums *big.Int,
) (mintedShares *big.Int, err error) {
// Mint shares.
mintedShares, err = k.MintShares(
ctx,
fromSubaccount.Owner,
quoteQuantums,
)
if err != nil {
return nil, err
}

// Transfer from sender subaccount to megavault.
// Note: Transfer should take place after minting shares for
// shares calculation to be correct.
err = k.sendingKeeper.ProcessTransfer(
ctx,
&sendingtypes.Transfer{
Sender: fromSubaccount,
Recipient: types.MegavaultMainSubaccount,
AssetId: assettypes.AssetUsdc.Id,
Amount: quoteQuantums.Uint64(),
},
)
if err != nil {
return nil, err
}

return mintedShares, nil
}

// MintShares mints shares for `owner` based on `quantumsToDeposit` by:
// 1. Increasing total shares.
// 2. Increasing owner shares for given `owner`.
func (k Keeper) MintShares(
ctx sdk.Context,
owner string,
quantumsToDeposit *big.Int,
) error {
) (mintedShares *big.Int, err error) {
// Quantums to deposit should be positive.
if quantumsToDeposit.Sign() <= 0 {
return types.ErrInvalidDepositAmount
return nil, types.ErrInvalidDepositAmount
}
// Get existing TotalShares of the vault.
existingTotalShares := k.GetTotalShares(ctx).NumShares.BigInt()
Expand All @@ -30,11 +70,11 @@ func (k Keeper) MintShares(
// Get megavault equity.
equity, err := k.GetMegavaultEquity(ctx)
if err != nil {
return err
return nil, err
}
// Don't mint shares if equity is non-positive.
if equity.Sign() <= 0 {
return types.ErrNonPositiveEquity
return nil, types.ErrNonPositiveEquity
}
// Mint `deposit (in quote quantums) * existing shares / vault equity (in quote quantums)`
// number of shares.
Expand All @@ -48,19 +88,19 @@ func (k Keeper) MintShares(

// Return error if `sharesToMint` is rounded down to 0.
if sharesToMint.Sign() == 0 {
return types.ErrZeroSharesToMint
return nil, types.ErrZeroSharesToMint
}
}

// Increase total shares.
err := k.SetTotalShares(
err = k.SetTotalShares(
ctx,
types.BigIntToNumShares(
existingTotalShares.Add(existingTotalShares, sharesToMint),
),
)
if err != nil {
return err
return nil, err
}

// Increase owner shares.
Expand All @@ -73,7 +113,7 @@ func (k Keeper) MintShares(
types.BigIntToNumShares(sharesToMint),
)
if err != nil {
return err
return nil, err
}
} else {
// Increase existing owner shares by sharesToMint.
Expand All @@ -86,9 +126,9 @@ func (k Keeper) MintShares(
),
)
if err != nil {
return err
return nil, err
}
}

return nil
return sharesToMint, nil
}
9 changes: 8 additions & 1 deletion protocol/x/vault/keeper/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,15 @@ func TestMintShares(t *testing.T) {
}

// Mint shares.
err = tApp.App.VaultKeeper.MintShares(
mintedShares, err := tApp.App.VaultKeeper.MintShares(
ctx,
tc.owner,
tc.quantumsToDeposit,
)
if tc.expectedErr != nil {
// Check that error is as expected.
require.ErrorContains(t, err, tc.expectedErr.Error())
require.Nil(t, mintedShares)
// Check that TotalShares is unchanged.
totalShares := tApp.App.VaultKeeper.GetTotalShares(ctx)
require.Equal(
Expand All @@ -204,6 +205,12 @@ func TestMintShares(t *testing.T) {
require.Equal(t, vaulttypes.BigIntToNumShares(tc.ownerShares), ownerShares)
} else {
require.NoError(t, err)
// Check that minted shares is as expected.
require.Equal(
t,
new(big.Int).Sub(tc.expectedTotalShares, tc.totalShares),
mintedShares,
)
// Check that TotalShares is as expected.
totalShares := tApp.App.VaultKeeper.GetTotalShares(ctx)
require.Equal(
Expand Down
27 changes: 5 additions & 22 deletions protocol/x/vault/keeper/msg_server_deposit_to_megavault.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"

"github.com/dydxprotocol/v4-chain/protocol/lib"
assettypes "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
sendingtypes "github.com/dydxprotocol/v4-chain/protocol/x/sending/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

Expand All @@ -17,31 +15,16 @@ func (k msgServer) DepositToMegavault(
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)
quoteQuantums := msg.QuoteQuantums.BigInt()

// Mint shares.
err := k.MintShares(
mintedShares, err := k.Keeper.DepositToMegavault(
ctx,
msg.SubaccountId.Owner,
*msg.SubaccountId,
quoteQuantums,
)
if err != nil {
return nil, err
}

// Transfer from sender subaccount to megavault.
// Note: Transfer should take place after minting shares for
// shares calculation to be correct.
err = k.sendingKeeper.ProcessTransfer(
ctx,
&sendingtypes.Transfer{
Sender: *msg.SubaccountId,
Recipient: types.MegavaultMainSubaccount,
AssetId: assettypes.AssetUsdc.Id,
Amount: quoteQuantums.Uint64(),
},
)
if err != nil {
return nil, err
}

return &types.MsgDepositToMegavaultResponse{}, nil
return &types.MsgDepositToMegavaultResponse{
MintedShares: types.BigIntToNumShares(mintedShares),
}, nil
}
Loading

0 comments on commit 14393e4

Please sign in to comment.