Skip to content

Commit

Permalink
add megavault withdrawal msg
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 committed Sep 18, 2024
1 parent a0b2a88 commit c741676
Show file tree
Hide file tree
Showing 16 changed files with 1,682 additions and 319 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Rpc } from "../../helpers";
import * as _m0 from "protobufjs/minimal";
import { MsgDepositToMegavault, MsgDepositToMegavaultResponse, MsgUpdateDefaultQuotingParams, MsgUpdateDefaultQuotingParamsResponse, MsgUpdateOperatorParams, MsgUpdateOperatorParamsResponse, MsgSetVaultParams, MsgSetVaultParamsResponse, MsgUnlockShares, MsgUnlockSharesResponse, MsgAllocateToVault, MsgAllocateToVaultResponse } from "./tx";
import { MsgDepositToMegavault, MsgDepositToMegavaultResponse, MsgWithdrawFromMegavault, MsgWithdrawFromMegavaultResponse, MsgUpdateDefaultQuotingParams, MsgUpdateDefaultQuotingParamsResponse, MsgUpdateOperatorParams, MsgUpdateOperatorParamsResponse, MsgSetVaultParams, MsgSetVaultParamsResponse, MsgUnlockShares, MsgUnlockSharesResponse, MsgAllocateToVault, MsgAllocateToVaultResponse } from "./tx";
/** Msg defines the Msg service. */

export interface Msg {
/** DepositToMegavault deposits funds into megavault. */
depositToMegavault(request: MsgDepositToMegavault): Promise<MsgDepositToMegavaultResponse>;
/** WithdrawFromMegavault withdraws shares from megavault. */

withdrawFromMegavault(request: MsgWithdrawFromMegavault): Promise<MsgWithdrawFromMegavaultResponse>;
/** UpdateDefaultQuotingParams updates the default quoting params in state. */

updateDefaultQuotingParams(request: MsgUpdateDefaultQuotingParams): Promise<MsgUpdateDefaultQuotingParamsResponse>;
Expand All @@ -31,6 +34,7 @@ export class MsgClientImpl implements Msg {
constructor(rpc: Rpc) {
this.rpc = rpc;
this.depositToMegavault = this.depositToMegavault.bind(this);
this.withdrawFromMegavault = this.withdrawFromMegavault.bind(this);
this.updateDefaultQuotingParams = this.updateDefaultQuotingParams.bind(this);
this.updateOperatorParams = this.updateOperatorParams.bind(this);
this.setVaultParams = this.setVaultParams.bind(this);
Expand All @@ -44,6 +48,12 @@ export class MsgClientImpl implements Msg {
return promise.then(data => MsgDepositToMegavaultResponse.decode(new _m0.Reader(data)));
}

withdrawFromMegavault(request: MsgWithdrawFromMegavault): Promise<MsgWithdrawFromMegavaultResponse> {
const data = MsgWithdrawFromMegavault.encode(request).finish();
const promise = this.rpc.request("dydxprotocol.vault.Msg", "WithdrawFromMegavault", data);
return promise.then(data => MsgWithdrawFromMegavaultResponse.decode(new _m0.Reader(data)));
}

updateDefaultQuotingParams(request: MsgUpdateDefaultQuotingParams): Promise<MsgUpdateDefaultQuotingParamsResponse> {
const data = MsgUpdateDefaultQuotingParams.encode(request).finish();
const promise = this.rpc.request("dydxprotocol.vault.Msg", "UpdateDefaultQuotingParams", data);
Expand Down
166 changes: 165 additions & 1 deletion indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SubaccountId, SubaccountIdSDKType } from "../subaccounts/subaccount";
import { NumShares, NumSharesSDKType } from "./share";
import { QuotingParams, QuotingParamsSDKType, OperatorParams, OperatorParamsSDKType, VaultParams, VaultParamsSDKType, Params, ParamsSDKType } 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 @@ -40,6 +40,60 @@ export interface MsgDepositToMegavaultResponseSDKType {
/** The number of shares minted from the deposit. */
minted_shares?: NumSharesSDKType;
}
/**
* MsgWithdrawFromMegavault withdraws the specified shares from megavault to
* a subaccount.
*/

export interface MsgWithdrawFromMegavault {
/** The subaccount to withdraw to. */
subaccountId?: SubaccountId;
/** Number of shares to withdraw. */

shares?: NumShares;
/**
* The minimum number of quote quantums above shares should redeem, i.e.
* transaction fails if above shares redeem less than min_quote_quantums.
*/

minQuoteQuantums: Uint8Array;
}
/**
* MsgWithdrawFromMegavault withdraws the specified shares from megavault to
* a subaccount.
*/

export interface MsgWithdrawFromMegavaultSDKType {
/** The subaccount to withdraw to. */
subaccount_id?: SubaccountIdSDKType;
/** Number of shares to withdraw. */

shares?: NumSharesSDKType;
/**
* The minimum number of quote quantums above shares should redeem, i.e.
* transaction fails if above shares redeem less than min_quote_quantums.
*/

min_quote_quantums: Uint8Array;
}
/**
* MsgWithdrawFromMegavaultResponse is the Msg/WithdrawFromMegavault response
* type.
*/

export interface MsgWithdrawFromMegavaultResponse {
/** The number of quote quantums redeemed from the withdrawal. */
quoteQuantums: Uint8Array;
}
/**
* MsgWithdrawFromMegavaultResponse is the Msg/WithdrawFromMegavault response
* type.
*/

export interface MsgWithdrawFromMegavaultResponseSDKType {
/** The number of quote quantums redeemed from the withdrawal. */
quote_quantums: Uint8Array;
}
/**
* MsgUpdateDefaultQuotingParams is the Msg/UpdateDefaultQuotingParams request
* type.
Expand Down Expand Up @@ -307,6 +361,116 @@ export const MsgDepositToMegavaultResponse = {

};

function createBaseMsgWithdrawFromMegavault(): MsgWithdrawFromMegavault {
return {
subaccountId: undefined,
shares: undefined,
minQuoteQuantums: new Uint8Array()
};
}

export const MsgWithdrawFromMegavault = {
encode(message: MsgWithdrawFromMegavault, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.subaccountId !== undefined) {
SubaccountId.encode(message.subaccountId, writer.uint32(10).fork()).ldelim();
}

if (message.shares !== undefined) {
NumShares.encode(message.shares, writer.uint32(18).fork()).ldelim();
}

if (message.minQuoteQuantums.length !== 0) {
writer.uint32(26).bytes(message.minQuoteQuantums);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): MsgWithdrawFromMegavault {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseMsgWithdrawFromMegavault();

while (reader.pos < end) {
const tag = reader.uint32();

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

case 2:
message.shares = NumShares.decode(reader, reader.uint32());
break;

case 3:
message.minQuoteQuantums = reader.bytes();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<MsgWithdrawFromMegavault>): MsgWithdrawFromMegavault {
const message = createBaseMsgWithdrawFromMegavault();
message.subaccountId = object.subaccountId !== undefined && object.subaccountId !== null ? SubaccountId.fromPartial(object.subaccountId) : undefined;
message.shares = object.shares !== undefined && object.shares !== null ? NumShares.fromPartial(object.shares) : undefined;
message.minQuoteQuantums = object.minQuoteQuantums ?? new Uint8Array();
return message;
}

};

function createBaseMsgWithdrawFromMegavaultResponse(): MsgWithdrawFromMegavaultResponse {
return {
quoteQuantums: new Uint8Array()
};
}

export const MsgWithdrawFromMegavaultResponse = {
encode(message: MsgWithdrawFromMegavaultResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.quoteQuantums.length !== 0) {
writer.uint32(10).bytes(message.quoteQuantums);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): MsgWithdrawFromMegavaultResponse {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseMsgWithdrawFromMegavaultResponse();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.quoteQuantums = reader.bytes();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<MsgWithdrawFromMegavaultResponse>): MsgWithdrawFromMegavaultResponse {
const message = createBaseMsgWithdrawFromMegavaultResponse();
message.quoteQuantums = object.quoteQuantums ?? new Uint8Array();
return message;
}

};

function createBaseMsgUpdateDefaultQuotingParams(): MsgUpdateDefaultQuotingParams {
return {
authority: "",
Expand Down
38 changes: 38 additions & 0 deletions proto/dydxprotocol/vault/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ service Msg {
rpc DepositToMegavault(MsgDepositToMegavault)
returns (MsgDepositToMegavaultResponse);

// WithdrawFromMegavault withdraws shares from megavault.
rpc WithdrawFromMegavault(MsgWithdrawFromMegavault)
returns (MsgWithdrawFromMegavaultResponse);

// UpdateDefaultQuotingParams updates the default quoting params in state.
rpc UpdateDefaultQuotingParams(MsgUpdateDefaultQuotingParams)
returns (MsgUpdateDefaultQuotingParamsResponse);
Expand Down Expand Up @@ -61,6 +65,40 @@ message MsgDepositToMegavaultResponse {
NumShares minted_shares = 1 [ (gogoproto.nullable) = false ];
}

// MsgWithdrawFromMegavault withdraws the specified shares from megavault to
// a subaccount.
message MsgWithdrawFromMegavault {
// This annotation enforces that the tx signer is the owner specified in
// `subaccount_id`.
option (cosmos.msg.v1.signer) = "subaccount_id";

// The subaccount to withdraw to.
dydxprotocol.subaccounts.SubaccountId subaccount_id = 1
[ (gogoproto.nullable) = false ];

// Number of shares to withdraw.
NumShares shares = 2 [ (gogoproto.nullable) = false ];

// The minimum number of quote quantums above shares should redeem, i.e.
// transaction fails if above shares redeem less than min_quote_quantums.
bytes min_quote_quantums = 3 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];
}

// MsgWithdrawFromMegavaultResponse is the Msg/WithdrawFromMegavault response
// type.
message MsgWithdrawFromMegavaultResponse {
// The number of quote quantums redeemed from the withdrawal.
bytes quote_quantums = 1 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];
}

// MsgUpdateDefaultQuotingParams is the Msg/UpdateDefaultQuotingParams request
// type.
message MsgUpdateDefaultQuotingParams {
Expand Down
3 changes: 3 additions & 0 deletions protocol/app/module/interface_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func NewInterfaceRegistry(addrPrefix string, valAddrPrefix string) (types.Interf
"dydxprotocol.vault.MsgDepositToMegavault": getLegacyMsgSignerFn(
[]string{"subaccount_id", "owner"},
),
"dydxprotocol.vault.MsgWithdrawFromMegavault": getLegacyMsgSignerFn(
[]string{"subaccount_id", "owner"},
),
"dydxprotocol.listing.MsgCreateMarketPermissionless": getLegacyMsgSignerFn(
[]string{"subaccount_id", "owner"},
),
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/msgs/all_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ var (
"/dydxprotocol.vault.MsgUpdateOperatorParams": {},
"/dydxprotocol.vault.MsgUpdateOperatorParamsResponse": {},
"/dydxprotocol.vault.MsgUpdateParams": {},
"/dydxprotocol.vault.MsgWithdrawFromMegavault": {},
"/dydxprotocol.vault.MsgWithdrawFromMegavaultResponse": {},

// vest
"/dydxprotocol.vest.MsgSetVestEntry": {},
Expand Down
14 changes: 8 additions & 6 deletions protocol/app/msgs/normal_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,14 @@ var (
"/dydxprotocol.sending.MsgWithdrawFromSubaccountResponse": nil,

// vault
"/dydxprotocol.vault.MsgAllocateToVault": &vault.MsgAllocateToVault{},
"/dydxprotocol.vault.MsgAllocateToVaultResponse": nil,
"/dydxprotocol.vault.MsgDepositToMegavault": &vault.MsgDepositToMegavault{},
"/dydxprotocol.vault.MsgDepositToMegavaultResponse": nil,
"/dydxprotocol.vault.MsgSetVaultParams": &vault.MsgSetVaultParams{},
"/dydxprotocol.vault.MsgSetVaultParamsResponse": nil,
"/dydxprotocol.vault.MsgAllocateToVault": &vault.MsgAllocateToVault{},
"/dydxprotocol.vault.MsgAllocateToVaultResponse": nil,
"/dydxprotocol.vault.MsgDepositToMegavault": &vault.MsgDepositToMegavault{},
"/dydxprotocol.vault.MsgDepositToMegavaultResponse": nil,
"/dydxprotocol.vault.MsgSetVaultParams": &vault.MsgSetVaultParams{},
"/dydxprotocol.vault.MsgSetVaultParamsResponse": nil,
"/dydxprotocol.vault.MsgWithdrawFromMegavault": &vault.MsgWithdrawFromMegavault{},
"/dydxprotocol.vault.MsgWithdrawFromMegavaultResponse": nil,
}

NormalMsgsSlinky = map[string]sdk.Msg{
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/msgs/normal_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func TestNormalMsgs_Key(t *testing.T) {
"/dydxprotocol.vault.MsgDepositToMegavaultResponse",
"/dydxprotocol.vault.MsgSetVaultParams",
"/dydxprotocol.vault.MsgSetVaultParamsResponse",
"/dydxprotocol.vault.MsgWithdrawFromMegavault",
"/dydxprotocol.vault.MsgWithdrawFromMegavaultResponse",

// ibc application module: ICA
"/ibc.applications.interchain_accounts.v1.InterchainAccount",
Expand Down
1 change: 1 addition & 0 deletions protocol/testutil/encoding/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func GetTestEncodingCfg() testutil.TestEncodingConfig {

// Vault.
&vaulttypes.MsgDepositToMegavault{},
&vaulttypes.MsgWithdrawFromMegavault{},
}

for _, msg := range msgInterfacesToRegister {
Expand Down
31 changes: 31 additions & 0 deletions protocol/x/vault/keeper/msg_server_withdraw_from_megavault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package keeper

import (
"context"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

// WithdrawFromMegavault withdraws from megavault to a subaccount.
func (k msgServer) WithdrawFromMegavault(
goCtx context.Context,
msg *types.MsgWithdrawFromMegavault,
) (*types.MsgWithdrawFromMegavaultResponse, error) {
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

redeemedQuoteQuantums, err := k.Keeper.WithdrawFromMegavault(
ctx,
msg.SubaccountId,
msg.Shares.NumShares.BigInt(),
msg.MinQuoteQuantums.BigInt(),
)
if err != nil {
return nil, err
}

return &types.MsgWithdrawFromMegavaultResponse{
QuoteQuantums: dtypes.NewIntFromBigInt(redeemedQuoteQuantums),
}, nil
}
Loading

0 comments on commit c741676

Please sign in to comment.