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

add ability to allocate to vaults #2277

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rpc } from "../../helpers";
import * as _m0 from "protobufjs/minimal";
import { MsgDepositToMegavault, MsgDepositToMegavaultResponse, MsgUpdateDefaultQuotingParams, MsgUpdateDefaultQuotingParamsResponse, MsgUpdateOperatorParams, MsgUpdateOperatorParamsResponse, MsgSetVaultParams, MsgSetVaultParamsResponse, MsgUnlockShares, MsgUnlockSharesResponse } from "./tx";
import { MsgDepositToMegavault, MsgDepositToMegavaultResponse, MsgUpdateDefaultQuotingParams, MsgUpdateDefaultQuotingParamsResponse, MsgUpdateOperatorParams, MsgUpdateOperatorParamsResponse, MsgSetVaultParams, MsgSetVaultParamsResponse, MsgUnlockShares, MsgUnlockSharesResponse, MsgAllocateToVault, MsgAllocateToVaultResponse } from "./tx";
/** Msg defines the Msg service. */

export interface Msg {
Expand All @@ -21,6 +21,9 @@ export interface Msg {
*/

unlockShares(request: MsgUnlockShares): Promise<MsgUnlockSharesResponse>;
/** AllocateToVault allocates funds from main vault to a vault. */

allocateToVault(request: MsgAllocateToVault): Promise<MsgAllocateToVaultResponse>;
}
export class MsgClientImpl implements Msg {
private readonly rpc: Rpc;
Expand All @@ -32,6 +35,7 @@ export class MsgClientImpl implements Msg {
this.updateOperatorParams = this.updateOperatorParams.bind(this);
this.setVaultParams = this.setVaultParams.bind(this);
this.unlockShares = this.unlockShares.bind(this);
this.allocateToVault = this.allocateToVault.bind(this);
}

depositToMegavault(request: MsgDepositToMegavault): Promise<MsgDepositToMegavaultResponse> {
Expand Down Expand Up @@ -64,4 +68,10 @@ export class MsgClientImpl implements Msg {
return promise.then(data => MsgUnlockSharesResponse.decode(new _m0.Reader(data)));
}

allocateToVault(request: MsgAllocateToVault): Promise<MsgAllocateToVaultResponse> {
const data = MsgAllocateToVault.encode(request).finish();
const promise = this.rpc.request("dydxprotocol.vault.Msg", "AllocateToVault", data);
return promise.then(data => MsgAllocateToVaultResponse.decode(new _m0.Reader(data)));
}

}
127 changes: 127 additions & 0 deletions indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,34 @@ export interface MsgUpdateOperatorParamsResponse {}
/** MsgUpdateVaultParamsResponse is the Msg/UpdateOperatorParams response type. */

export interface MsgUpdateOperatorParamsResponseSDKType {}
/** MsgAllocateToVault is the Msg/AllocateToVault request type. */

export interface MsgAllocateToVault {
authority: string;
/** The vault to allocate to. */

vaultId?: VaultId;
/** Number of quote quantums to allocate. */

quoteQuantums: Uint8Array;
}
/** MsgAllocateToVault is the Msg/AllocateToVault request type. */

export interface MsgAllocateToVaultSDKType {
authority: string;
/** The vault to allocate to. */

vault_id?: VaultIdSDKType;
/** Number of quote quantums to allocate. */

quote_quantums: Uint8Array;
}
/** MsgAllocateToVaultResponse is the Msg/AllocateToVault response type. */

export interface MsgAllocateToVaultResponse {}
Comment on lines +203 to +205
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a type alias instead of an empty interface.

An empty interface is equivalent to {} and can be confusing. Consider using a type alias for clarity:

-export interface MsgAllocateToVaultResponse {}
+export type MsgAllocateToVaultResponse = Record<string, never>;

This change will also resolve the lint error.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/** MsgAllocateToVaultResponse is the Msg/AllocateToVault response type. */
export interface MsgAllocateToVaultResponse {}
/** MsgAllocateToVaultResponse is the Msg/AllocateToVault response type. */
export type MsgAllocateToVaultResponse = Record<string, never>;
Tools
Biome

[error] 205-205: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)

/** MsgAllocateToVaultResponse is the Msg/AllocateToVault response type. */

export interface MsgAllocateToVaultResponseSDKType {}
Comment on lines +206 to +208
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a type alias instead of an empty interface.

Similar to MsgAllocateToVaultResponse, consider using a type alias instead of an empty interface for clarity:

-export interface MsgAllocateToVaultResponseSDKType {}
+export type MsgAllocateToVaultResponseSDKType = Record<string, never>;

This change will also resolve the lint error.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/** MsgAllocateToVaultResponse is the Msg/AllocateToVault response type. */
export interface MsgAllocateToVaultResponseSDKType {}
/** MsgAllocateToVaultResponse is the Msg/AllocateToVault response type. */
export type MsgAllocateToVaultResponseSDKType = Record<string, never>;
Tools
Biome

[error] 208-208: An empty interface is equivalent to {}.

Safe fix: Use a type alias instead.

(lint/suspicious/noEmptyInterface)


function createBaseMsgDepositToMegavault(): MsgDepositToMegavault {
return {
Expand Down Expand Up @@ -709,4 +737,103 @@ export const MsgUpdateOperatorParamsResponse = {
return message;
}

};

function createBaseMsgAllocateToVault(): MsgAllocateToVault {
return {
authority: "",
vaultId: undefined,
quoteQuantums: new Uint8Array()
};
}

export const MsgAllocateToVault = {
encode(message: MsgAllocateToVault, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.authority !== "") {
writer.uint32(10).string(message.authority);
}

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

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

return writer;
},

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

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

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

case 2:
message.vaultId = VaultId.decode(reader, reader.uint32());
break;

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

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

return message;
},

fromPartial(object: DeepPartial<MsgAllocateToVault>): MsgAllocateToVault {
const message = createBaseMsgAllocateToVault();
message.authority = object.authority ?? "";
message.vaultId = object.vaultId !== undefined && object.vaultId !== null ? VaultId.fromPartial(object.vaultId) : undefined;
message.quoteQuantums = object.quoteQuantums ?? new Uint8Array();
return message;
}

};

function createBaseMsgAllocateToVaultResponse(): MsgAllocateToVaultResponse {
return {};
}

export const MsgAllocateToVaultResponse = {
encode(_: MsgAllocateToVaultResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
return writer;
},

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

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

switch (tag >>> 3) {
default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(_: DeepPartial<MsgAllocateToVaultResponse>): MsgAllocateToVaultResponse {
const message = createBaseMsgAllocateToVaultResponse();
return message;
}

};
23 changes: 23 additions & 0 deletions proto/dydxprotocol/vault/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ service Msg {
// UnlockShares unlocks an owner's shares that are due to unlock by the block
// height that this transaction is included in.
rpc UnlockShares(MsgUnlockShares) returns (MsgUnlockSharesResponse);

// AllocateToVault allocates funds from main vault to a vault.
rpc AllocateToVault(MsgAllocateToVault) returns (MsgAllocateToVaultResponse);
}

// MsgDepositToMegavault deposits the specified asset from the subaccount to
Expand Down Expand Up @@ -129,3 +132,23 @@ message MsgUpdateOperatorParams {

// MsgUpdateVaultParamsResponse is the Msg/UpdateOperatorParams response type.
message MsgUpdateOperatorParamsResponse {}

// MsgAllocateToVault is the Msg/AllocateToVault request type.
message MsgAllocateToVault {
// The address that has the authority to allocate to a vault.
option (cosmos.msg.v1.signer) = "authority";
string authority = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

// The vault to allocate to.
VaultId vault_id = 2 [ (gogoproto.nullable) = false ];

// Number of quote quantums to allocate.
bytes quote_quantums = 3 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];
}

// MsgAllocateToVaultResponse is the Msg/AllocateToVault response type.
message MsgAllocateToVaultResponse {}
2 changes: 2 additions & 0 deletions protocol/app/msgs/all_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ var (
"/dydxprotocol.stats.MsgUpdateParamsResponse": {},

// vault
"/dydxprotocol.vault.MsgAllocateToVault": {},
"/dydxprotocol.vault.MsgAllocateToVaultResponse": {},
"/dydxprotocol.vault.MsgDepositToMegavault": {},
"/dydxprotocol.vault.MsgDepositToMegavaultResponse": {},
"/dydxprotocol.vault.MsgSetVaultParams": {},
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/msgs/normal_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ 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{},
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 @@ -147,6 +147,8 @@ func TestNormalMsgs_Key(t *testing.T) {
"/dydxprotocol.sending.MsgWithdrawFromSubaccountResponse",

// vault
"/dydxprotocol.vault.MsgAllocateToVault",
"/dydxprotocol.vault.MsgAllocateToVaultResponse",
"/dydxprotocol.vault.MsgDepositToMegavault",
"/dydxprotocol.vault.MsgDepositToMegavaultResponse",
"/dydxprotocol.vault.MsgSetVaultParams",
Expand Down
17 changes: 16 additions & 1 deletion protocol/scripts/genesis/sample_pregenesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -3922,7 +3922,22 @@
}
},
"subaccounts": {
"subaccounts": []
"subaccounts": [
{
"asset_positions": [
{
"asset_id": 0,
"index": 0,
"quantums": "1000000000000"
}
],
"id": {
"number": 0,
"owner": "dydx18tkxrnrkqc2t0lr3zxr5g6a4hdvqksylxqje4r"
},
"margin_enabled": true
}
]
},
"transfer": {
"denom_traces": [],
Expand Down
6 changes: 6 additions & 0 deletions protocol/testing/genesis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ NATIVE_TOKEN="adv4tnt" # public testnet token
DEFAULT_SUBACCOUNT_QUOTE_BALANCE=100000000000000000
DEFAULT_SUBACCOUNT_QUOTE_BALANCE_FAUCET=900000000000000000
DEFAULT_SUBACCOUNT_QUOTE_BALANCE_VAULT=1000000000
MEGAVAULT_MAIN_VAULT_ACCOUNT_ADDR="dydx18tkxrnrkqc2t0lr3zxr5g6a4hdvqksylxqje4r"
DEFAULT_MEGAVAULT_MAIN_VAULT_QUOTE_BALANCE=1000000000000 # 1 million USDC
NATIVE_TOKEN_WHOLE_COIN="dv4tnt"
COIN_NAME="dYdX V4 Testnet Token"
# Each testnet validator has 1 million whole coins of native token.
Expand Down Expand Up @@ -1824,6 +1826,10 @@ function edit_genesis() {
total_accounts_quote_balance=$(($total_accounts_quote_balance + $DEFAULT_SUBACCOUNT_QUOTE_BALANCE_VAULT))
acct_idx=$(($acct_idx + 1))
done
# Update subaccounts module for megavault main vault account.
add_subaccount "$GENESIS" "$acct_idx" "$MEGAVAULT_MAIN_VAULT_ACCOUNT_ADDR" "$DEFAULT_MEGAVAULT_MAIN_VAULT_QUOTE_BALANCE"
total_accounts_quote_balance=$(($total_accounts_quote_balance + $DEFAULT_MEGAVAULT_MAIN_VAULT_QUOTE_BALANCE))
acct_idx=$(($acct_idx + 1))

next_bank_idx=0
if (( total_accounts_quote_balance > 0 )); then
Expand Down
51 changes: 51 additions & 0 deletions protocol/x/vault/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func GetTxCmd() *cobra.Command {

cmd.AddCommand(CmdDepositToMegavault())
cmd.AddCommand(CmdSetVaultParams())
cmd.AddCommand(CmdAllocateToVault())

return cmd
}
Expand Down Expand Up @@ -141,3 +142,53 @@ func CmdSetVaultParams() *cobra.Command {

return cmd
}

func CmdAllocateToVault() *cobra.Command {
cmd := &cobra.Command{
Use: "allocate-to-vault [authority] [vault_type] [vault_number] [quote_quantums]",
Short: "Broadcast message AllocateToVault",
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Parse vault type.
vaultType, err := GetVaultTypeFromString(args[1])
if err != nil {
return err
}

// Parse vault number.
vaultNumber, err := strconv.ParseUint(args[2], 10, 32)
if err != nil {
return err
}

// Parse quantums.
quantums, err := cast.ToUint64E(args[3])
if err != nil {
return err
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

// Create MsgAllocateToVault.
msg := &types.MsgAllocateToVault{
Authority: args[0],
VaultId: types.VaultId{
Type: vaultType,
Number: uint32(vaultNumber),
},
QuoteQuantums: dtypes.NewIntFromUint64(quantums),
}

// Broadcast or generate the transaction.
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

// Add the necessary flags.
flags.AddTxFlagsToCmd(cmd)

return cmd
}
Loading
Loading