Skip to content

Commit 72c8d02

Browse files
committed
feat: dynamic leverage
1 parent 879e3ae commit 72c8d02

File tree

5 files changed

+93
-14
lines changed

5 files changed

+93
-14
lines changed

v4-client-js/src/clients/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export const TYPE_URL_MSG_CANCEL_ORDER = '/dydxprotocol.clob.MsgCancelOrder';
8282
export const TYPE_URL_BATCH_CANCEL = '/dydxprotocol.clob.MsgBatchCancel';
8383
export const TYPE_URL_MSG_CREATE_CLOB_PAIR = '/dydxprotocol.clob.MsgCreateClobPair';
8484
export const TYPE_URL_MSG_UPDATE_CLOB_PAIR = '/dydxprotocol.clob.MsgUpdateClobPair';
85+
export const TYPE_URL_MSG_UPDATE_LEVERAGE = '/dydxprotocol.clob.MsgUpdateLeverage';
8586

8687
// x/delaymsg
8788
export const TYPE_URL_MSG_DELAY_MESSAGE = '/dydxprotocol.delaymsg.MsgDelayMessage';

v4-client-js/src/clients/lib/registry.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { GeneratedType, Registry } from '@cosmjs/proto-signing';
22
import { defaultRegistryTypes } from '@cosmjs/stargate';
3-
import { MsgAddAuthenticator, MsgRemoveAuthenticator } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/accountplus/tx';
3+
import {
4+
MsgAddAuthenticator,
5+
MsgRemoveAuthenticator,
6+
} from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/accountplus/tx';
47
import { MsgRegisterAffiliate } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/affiliates/tx';
58
import {
69
MsgPlaceOrder,
710
MsgCancelOrder,
811
MsgCreateClobPair,
912
MsgUpdateClobPair,
1013
MsgBatchCancel,
14+
MsgUpdateLeverage,
1115
} from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/tx';
1216
import { MsgDelayMessage } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/delaymsg/tx';
1317
import { MsgCreateMarketPermissionless } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/listing/tx';
@@ -41,6 +45,7 @@ import {
4145
TYPE_URL_MSG_CREATE_MARKET_PERMISSIONLESS,
4246
TYPE_URL_MSG_ADD_AUTHENTICATOR,
4347
TYPE_URL_MSG_REMOVE_AUTHENTICATOR,
48+
TYPE_URL_MSG_UPDATE_LEVERAGE,
4449
} from '../constants';
4550

4651
export const registry: ReadonlyArray<[string, GeneratedType]> = [];
@@ -52,6 +57,7 @@ export function generateRegistry(): Registry {
5257
[TYPE_URL_BATCH_CANCEL, MsgBatchCancel as GeneratedType],
5358
[TYPE_URL_MSG_CREATE_CLOB_PAIR, MsgCreateClobPair as GeneratedType],
5459
[TYPE_URL_MSG_UPDATE_CLOB_PAIR, MsgUpdateClobPair as GeneratedType],
60+
[TYPE_URL_MSG_UPDATE_LEVERAGE, MsgUpdateLeverage as GeneratedType],
5561

5662
// delaymsg
5763
[TYPE_URL_MSG_DELAY_MESSAGE, MsgDelayMessage as GeneratedType],
@@ -77,7 +83,6 @@ export function generateRegistry(): Registry {
7783
// affiliates
7884
[TYPE_URL_MSG_REGISTER_AFFILIATE, MsgRegisterAffiliate as GeneratedType],
7985

80-
8186
// authentication
8287
[TYPE_URL_MSG_ADD_AUTHENTICATOR, MsgAddAuthenticator as GeneratedType],
8388
[TYPE_URL_MSG_REMOVE_AUTHENTICATOR, MsgRemoveAuthenticator as GeneratedType],

v4-client-js/src/clients/modules/composer.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ import {
77
MsgDelegate,
88
MsgUndelegate,
99
} from '@dydxprotocol/v4-proto/src/codegen/cosmos/staking/v1beta1/tx';
10-
import { MsgAddAuthenticator, MsgRemoveAuthenticator } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/accountplus/tx';
10+
import {
11+
MsgAddAuthenticator,
12+
MsgRemoveAuthenticator,
13+
} from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/accountplus/tx';
1114
import { MsgRegisterAffiliate } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/affiliates/tx';
1215
import { ClobPair_Status } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/clob_pair';
1316
import {
17+
LeverageEntry,
1418
MsgBatchCancel,
1519
MsgCreateClobPair,
1620
MsgUpdateClobPair,
21+
MsgUpdateLeverage,
1722
OrderBatch,
1823
} from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/tx';
1924
import { MsgDelayMessage } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/delaymsg/tx';
@@ -56,6 +61,7 @@ import {
5661
TYPE_URL_MSG_ADD_AUTHENTICATOR,
5762
TYPE_URL_MSG_REMOVE_AUTHENTICATOR,
5863
AuthenticatorType,
64+
TYPE_URL_MSG_UPDATE_LEVERAGE,
5965
} from '../constants';
6066
import { DenomConfig, ITwapParameters, IBuilderCodeParameters } from '../types';
6167
import {
@@ -97,7 +103,7 @@ export class Composer {
97103
conditionalOrderTriggerSubticks: Long = Long.fromInt(0),
98104
twapParameters?: ITwapParameters,
99105
builderCodeParameters?: IBuilderCodeParameters,
100-
orderRouterAddress: string = "",
106+
orderRouterAddress: string = '',
101107
): EncodeObject {
102108
this.validateGoodTilBlockAndTime(orderFlags, goodTilBlock, goodTilBlockTime);
103109

@@ -252,6 +258,27 @@ export class Composer {
252258
};
253259
}
254260

261+
public composeMsgUpdateLeverage(
262+
address: string,
263+
subaccountNumber: number,
264+
entries: LeverageEntry[],
265+
): EncodeObject {
266+
const subaccountId: SubaccountId = {
267+
owner: address,
268+
number: subaccountNumber,
269+
};
270+
271+
const msg: MsgUpdateLeverage = {
272+
subaccountId,
273+
clobPairLeverage: entries,
274+
};
275+
276+
return {
277+
typeUrl: TYPE_URL_MSG_UPDATE_LEVERAGE,
278+
value: msg,
279+
};
280+
}
281+
255282
// ------------ x/sending ------------
256283
public composeMsgTransfer(
257284
address: string,
@@ -577,13 +604,13 @@ export class Composer {
577604

578605
const msg: MsgCreateMarketPermissionless = {
579606
ticker,
580-
subaccountId
607+
subaccountId,
581608
};
582609

583610
return {
584611
typeUrl: TYPE_URL_MSG_CREATE_MARKET_PERMISSIONLESS,
585612
value: msg,
586-
}
613+
};
587614
}
588615

589616
// ----------- x/accountplus --------
@@ -597,27 +624,24 @@ export class Composer {
597624
sender: address,
598625
authenticatorType,
599626
data,
600-
}
627+
};
601628

602629
return {
603630
typeUrl: TYPE_URL_MSG_ADD_AUTHENTICATOR,
604631
value: msg,
605-
}
632+
};
606633
}
607634

608-
public composeMsgRemoveAuthenticator(
609-
address: string,
610-
id: Long,
611-
): EncodeObject {
635+
public composeMsgRemoveAuthenticator(address: string, id: Long): EncodeObject {
612636
const msg: MsgRemoveAuthenticator = {
613637
sender: address,
614638
id,
615-
}
639+
};
616640

617641
return {
618642
typeUrl: TYPE_URL_MSG_REMOVE_AUTHENTICATOR,
619643
value: msg,
620-
}
644+
};
621645
}
622646

623647
// ------------ util ------------

v4-client-js/src/clients/modules/get.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,22 @@ export class Get {
364364
return ClobModule.QueryEquityTierLimitConfigurationResponse.decode(data);
365365
}
366366

367+
/**
368+
* @description Get user leverage settings for a given subaccount
369+
* @returns User set leverage for all clob pairs
370+
*/
371+
async getPerpetualMarketsLeverage(
372+
address: string,
373+
subaccountNumber: number,
374+
): Promise<ClobModule.QueryLeverageResponse> {
375+
const requestData: Uint8Array = Uint8Array.from(
376+
ClobModule.QueryLeverageRequest.encode({ owner: address, number: subaccountNumber }).finish(),
377+
);
378+
379+
const data: Uint8Array = await this.sendQuery('/dydxprotocol.clob.Query/Leverage', requestData);
380+
return ClobModule.QueryLeverageResponse.decode(data);
381+
}
382+
367383
/**
368384
*
369385
* @description Get all delegations from a delegator.

v4-client-js/src/clients/modules/post.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
MsgCancelOrder,
3535
Order_ConditionType,
3636
OrderBatch,
37+
LeverageEntry,
3738
} from './proto-includes';
3839

3940
// Required for encoding and decoding queries that are of type Long.
@@ -628,6 +629,38 @@ export class Post {
628629
});
629630
}
630631

632+
async updatePerpetualMarketsLeverage(
633+
subaccount: SubaccountInfo,
634+
address: string,
635+
entries: LeverageEntry[],
636+
broadcastMode?: BroadcastMode,
637+
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
638+
const msg = await this.updatePerpetualMarketsLeverageMsg(subaccount, address, entries);
639+
return this.send(
640+
subaccount,
641+
() => Promise.resolve([msg]),
642+
false,
643+
undefined,
644+
undefined,
645+
broadcastMode,
646+
);
647+
}
648+
649+
async updatePerpetualMarketsLeverageMsg(
650+
subaccount: SubaccountInfo,
651+
address: string,
652+
entries: LeverageEntry[],
653+
): Promise<EncodeObject> {
654+
return new Promise((resolve) => {
655+
const msg = this.composer.composeMsgUpdateLeverage(
656+
address,
657+
subaccount.subaccountNumber,
658+
entries,
659+
);
660+
resolve(msg);
661+
});
662+
}
663+
631664
async transfer(
632665
subaccount: SubaccountInfo,
633666
recipientAddress: string,

0 commit comments

Comments
 (0)