@nadohq/client 0.1.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. package/README.md +3 -0
  2. package/dist/apis/base.cjs +66 -0
  3. package/dist/apis/base.cjs.map +1 -0
  4. package/dist/apis/base.d.cts +23 -0
  5. package/dist/apis/base.d.ts +23 -0
  6. package/dist/apis/base.js +41 -0
  7. package/dist/apis/base.js.map +1 -0
  8. package/dist/apis/market/MarketExecuteAPI.cjs +169 -0
  9. package/dist/apis/market/MarketExecuteAPI.cjs.map +1 -0
  10. package/dist/apis/market/MarketExecuteAPI.d.cts +57 -0
  11. package/dist/apis/market/MarketExecuteAPI.d.ts +57 -0
  12. package/dist/apis/market/MarketExecuteAPI.js +144 -0
  13. package/dist/apis/market/MarketExecuteAPI.js.map +1 -0
  14. package/dist/apis/market/MarketQueryAPI.cjs +191 -0
  15. package/dist/apis/market/MarketQueryAPI.cjs.map +1 -0
  16. package/dist/apis/market/MarketQueryAPI.d.cts +127 -0
  17. package/dist/apis/market/MarketQueryAPI.d.ts +127 -0
  18. package/dist/apis/market/MarketQueryAPI.js +166 -0
  19. package/dist/apis/market/MarketQueryAPI.js.map +1 -0
  20. package/dist/apis/market/index.cjs +38 -0
  21. package/dist/apis/market/index.cjs.map +1 -0
  22. package/dist/apis/market/index.d.cts +18 -0
  23. package/dist/apis/market/index.d.ts +18 -0
  24. package/dist/apis/market/index.js +11 -0
  25. package/dist/apis/market/index.js.map +1 -0
  26. package/dist/apis/market/types.cjs +19 -0
  27. package/dist/apis/market/types.cjs.map +1 -0
  28. package/dist/apis/market/types.d.cts +26 -0
  29. package/dist/apis/market/types.d.ts +26 -0
  30. package/dist/apis/market/types.js +1 -0
  31. package/dist/apis/market/types.js.map +1 -0
  32. package/dist/apis/perp/PerpExecuteAPI.cjs +36 -0
  33. package/dist/apis/perp/PerpExecuteAPI.cjs.map +1 -0
  34. package/dist/apis/perp/PerpExecuteAPI.d.cts +13 -0
  35. package/dist/apis/perp/PerpExecuteAPI.d.ts +13 -0
  36. package/dist/apis/perp/PerpExecuteAPI.js +11 -0
  37. package/dist/apis/perp/PerpExecuteAPI.js.map +1 -0
  38. package/dist/apis/perp/PerpQueryAPI.cjs +47 -0
  39. package/dist/apis/perp/PerpQueryAPI.cjs.map +1 -0
  40. package/dist/apis/perp/PerpQueryAPI.d.cts +23 -0
  41. package/dist/apis/perp/PerpQueryAPI.d.ts +23 -0
  42. package/dist/apis/perp/PerpQueryAPI.js +22 -0
  43. package/dist/apis/perp/PerpQueryAPI.js.map +1 -0
  44. package/dist/apis/perp/index.cjs +35 -0
  45. package/dist/apis/perp/index.cjs.map +1 -0
  46. package/dist/apis/perp/index.d.cts +16 -0
  47. package/dist/apis/perp/index.d.ts +16 -0
  48. package/dist/apis/perp/index.js +10 -0
  49. package/dist/apis/perp/index.js.map +1 -0
  50. package/dist/apis/spot/BaseSpotAPI.cjs +55 -0
  51. package/dist/apis/spot/BaseSpotAPI.cjs.map +1 -0
  52. package/dist/apis/spot/BaseSpotAPI.d.cts +19 -0
  53. package/dist/apis/spot/BaseSpotAPI.d.ts +19 -0
  54. package/dist/apis/spot/BaseSpotAPI.js +30 -0
  55. package/dist/apis/spot/BaseSpotAPI.js.map +1 -0
  56. package/dist/apis/spot/SpotExecuteAPI.cjs +108 -0
  57. package/dist/apis/spot/SpotExecuteAPI.cjs.map +1 -0
  58. package/dist/apis/spot/SpotExecuteAPI.d.cts +28 -0
  59. package/dist/apis/spot/SpotExecuteAPI.d.ts +28 -0
  60. package/dist/apis/spot/SpotExecuteAPI.js +88 -0
  61. package/dist/apis/spot/SpotExecuteAPI.js.map +1 -0
  62. package/dist/apis/spot/SpotQueryAPI.cjs +74 -0
  63. package/dist/apis/spot/SpotQueryAPI.cjs.map +1 -0
  64. package/dist/apis/spot/SpotQueryAPI.d.cts +35 -0
  65. package/dist/apis/spot/SpotQueryAPI.d.ts +35 -0
  66. package/dist/apis/spot/SpotQueryAPI.js +49 -0
  67. package/dist/apis/spot/SpotQueryAPI.js.map +1 -0
  68. package/dist/apis/spot/index.cjs +38 -0
  69. package/dist/apis/spot/index.cjs.map +1 -0
  70. package/dist/apis/spot/index.d.cts +23 -0
  71. package/dist/apis/spot/index.d.ts +23 -0
  72. package/dist/apis/spot/index.js +11 -0
  73. package/dist/apis/spot/index.js.map +1 -0
  74. package/dist/apis/spot/types.cjs +19 -0
  75. package/dist/apis/spot/types.cjs.map +1 -0
  76. package/dist/apis/spot/types.d.cts +23 -0
  77. package/dist/apis/spot/types.d.ts +23 -0
  78. package/dist/apis/spot/types.js +1 -0
  79. package/dist/apis/spot/types.js.map +1 -0
  80. package/dist/apis/subaccount/SubaccountExecuteAPI.cjs +59 -0
  81. package/dist/apis/subaccount/SubaccountExecuteAPI.cjs.map +1 -0
  82. package/dist/apis/subaccount/SubaccountExecuteAPI.d.cts +26 -0
  83. package/dist/apis/subaccount/SubaccountExecuteAPI.d.ts +26 -0
  84. package/dist/apis/subaccount/SubaccountExecuteAPI.js +34 -0
  85. package/dist/apis/subaccount/SubaccountExecuteAPI.js.map +1 -0
  86. package/dist/apis/subaccount/SubaccountQueryAPI.cjs +84 -0
  87. package/dist/apis/subaccount/SubaccountQueryAPI.cjs.map +1 -0
  88. package/dist/apis/subaccount/SubaccountQueryAPI.d.cts +51 -0
  89. package/dist/apis/subaccount/SubaccountQueryAPI.d.ts +51 -0
  90. package/dist/apis/subaccount/SubaccountQueryAPI.js +61 -0
  91. package/dist/apis/subaccount/SubaccountQueryAPI.js.map +1 -0
  92. package/dist/apis/subaccount/index.cjs +68 -0
  93. package/dist/apis/subaccount/index.cjs.map +1 -0
  94. package/dist/apis/subaccount/index.d.cts +27 -0
  95. package/dist/apis/subaccount/index.d.ts +27 -0
  96. package/dist/apis/subaccount/index.js +41 -0
  97. package/dist/apis/subaccount/index.js.map +1 -0
  98. package/dist/apis/subaccount/types.cjs +19 -0
  99. package/dist/apis/subaccount/types.cjs.map +1 -0
  100. package/dist/apis/subaccount/types.d.cts +9 -0
  101. package/dist/apis/subaccount/types.d.ts +9 -0
  102. package/dist/apis/subaccount/types.js +1 -0
  103. package/dist/apis/subaccount/types.js.map +1 -0
  104. package/dist/apis/types.cjs +19 -0
  105. package/dist/apis/types.cjs.map +1 -0
  106. package/dist/apis/types.d.cts +9 -0
  107. package/dist/apis/types.d.ts +9 -0
  108. package/dist/apis/types.js +1 -0
  109. package/dist/apis/types.js.map +1 -0
  110. package/dist/apis/ws/WebSocketExecuteAPI.cjs +73 -0
  111. package/dist/apis/ws/WebSocketExecuteAPI.cjs.map +1 -0
  112. package/dist/apis/ws/WebSocketExecuteAPI.d.cts +48 -0
  113. package/dist/apis/ws/WebSocketExecuteAPI.d.ts +48 -0
  114. package/dist/apis/ws/WebSocketExecuteAPI.js +48 -0
  115. package/dist/apis/ws/WebSocketExecuteAPI.js.map +1 -0
  116. package/dist/apis/ws/WebSocketQueryAPI.cjs +42 -0
  117. package/dist/apis/ws/WebSocketQueryAPI.cjs.map +1 -0
  118. package/dist/apis/ws/WebSocketQueryAPI.d.cts +23 -0
  119. package/dist/apis/ws/WebSocketQueryAPI.d.ts +23 -0
  120. package/dist/apis/ws/WebSocketQueryAPI.js +17 -0
  121. package/dist/apis/ws/WebSocketQueryAPI.js.map +1 -0
  122. package/dist/apis/ws/WebSocketSubscriptionAPI.cjs +61 -0
  123. package/dist/apis/ws/WebSocketSubscriptionAPI.cjs.map +1 -0
  124. package/dist/apis/ws/WebSocketSubscriptionAPI.d.cts +37 -0
  125. package/dist/apis/ws/WebSocketSubscriptionAPI.d.ts +37 -0
  126. package/dist/apis/ws/WebSocketSubscriptionAPI.js +36 -0
  127. package/dist/apis/ws/WebSocketSubscriptionAPI.js.map +1 -0
  128. package/dist/apis/ws/index.cjs +42 -0
  129. package/dist/apis/ws/index.cjs.map +1 -0
  130. package/dist/apis/ws/index.d.cts +22 -0
  131. package/dist/apis/ws/index.d.ts +22 -0
  132. package/dist/apis/ws/index.js +17 -0
  133. package/dist/apis/ws/index.js.map +1 -0
  134. package/dist/client.cjs +79 -0
  135. package/dist/client.cjs.map +1 -0
  136. package/dist/client.d.cts +57 -0
  137. package/dist/client.d.ts +57 -0
  138. package/dist/client.js +54 -0
  139. package/dist/client.js.map +1 -0
  140. package/dist/context.cjs +125 -0
  141. package/dist/context.cjs.map +1 -0
  142. package/dist/context.d.cts +42 -0
  143. package/dist/context.d.ts +42 -0
  144. package/dist/context.js +109 -0
  145. package/dist/context.js.map +1 -0
  146. package/dist/createNadoClient.cjs +35 -0
  147. package/dist/createNadoClient.cjs.map +1 -0
  148. package/dist/createNadoClient.d.cts +42 -0
  149. package/dist/createNadoClient.d.ts +42 -0
  150. package/dist/createNadoClient.js +12 -0
  151. package/dist/createNadoClient.js.map +1 -0
  152. package/dist/index.cjs +47 -0
  153. package/dist/index.cjs.map +1 -0
  154. package/dist/index.d.cts +32 -0
  155. package/dist/index.d.ts +32 -0
  156. package/dist/index.js +14 -0
  157. package/dist/index.js.map +1 -0
  158. package/package.json +54 -0
  159. package/src/apis/base.ts +52 -0
  160. package/src/apis/market/MarketExecuteAPI.ts +169 -0
  161. package/src/apis/market/MarketQueryAPI.ts +214 -0
  162. package/src/apis/market/index.ts +7 -0
  163. package/src/apis/market/types.ts +58 -0
  164. package/src/apis/perp/PerpExecuteAPI.ts +8 -0
  165. package/src/apis/perp/PerpQueryAPI.ts +25 -0
  166. package/src/apis/perp/index.ts +5 -0
  167. package/src/apis/spot/BaseSpotAPI.ts +30 -0
  168. package/src/apis/spot/SpotExecuteAPI.ts +102 -0
  169. package/src/apis/spot/SpotQueryAPI.ts +53 -0
  170. package/src/apis/spot/index.ts +10 -0
  171. package/src/apis/spot/types.ts +44 -0
  172. package/src/apis/subaccount/SubaccountExecuteAPI.ts +32 -0
  173. package/src/apis/subaccount/SubaccountQueryAPI.ts +78 -0
  174. package/src/apis/subaccount/index.ts +44 -0
  175. package/src/apis/subaccount/types.ts +16 -0
  176. package/src/apis/types.ts +11 -0
  177. package/src/apis/ws/WebSocketExecuteAPI.ts +70 -0
  178. package/src/apis/ws/WebSocketQueryAPI.ts +25 -0
  179. package/src/apis/ws/WebSocketSubscriptionAPI.ts +62 -0
  180. package/src/apis/ws/index.ts +21 -0
  181. package/src/client.ts +67 -0
  182. package/src/context.ts +165 -0
  183. package/src/createNadoClient.ts +20 -0
  184. package/src/index.ts +15 -0
@@ -0,0 +1,44 @@
1
+ import {
2
+ EngineBurnVlpParams,
3
+ EngineMintVlpParams,
4
+ EngineTransferQuoteParams,
5
+ EngineWithdrawCollateralParams,
6
+ } from '@nadohq/engine-client';
7
+ import { BigDecimalish } from '@nadohq/utils';
8
+ import { OptionalSignatureParams, OptionalSubaccountOwner } from '../types';
9
+
10
+ export type ProductIdOrTokenAddress =
11
+ | {
12
+ productId: number;
13
+ }
14
+ | {
15
+ tokenAddress: string;
16
+ };
17
+
18
+ type TokenQueryParams = {
19
+ address: string;
20
+ } & ProductIdOrTokenAddress;
21
+
22
+ export type ApproveAllowanceParams = ProductIdOrTokenAddress & {
23
+ amount: BigDecimalish;
24
+ };
25
+
26
+ export type GetTokenWalletBalanceParams = TokenQueryParams;
27
+
28
+ export type GetTokenAllowanceParams = TokenQueryParams;
29
+
30
+ export type WithdrawCollateralParams = OptionalSignatureParams<
31
+ OptionalSubaccountOwner<EngineWithdrawCollateralParams>
32
+ >;
33
+
34
+ export type TransferQuoteParams = OptionalSignatureParams<
35
+ OptionalSubaccountOwner<EngineTransferQuoteParams>
36
+ >;
37
+
38
+ export type MintVlpParams = OptionalSignatureParams<
39
+ OptionalSubaccountOwner<EngineMintVlpParams>
40
+ >;
41
+
42
+ export type BurnVlpParams = OptionalSignatureParams<
43
+ OptionalSubaccountOwner<EngineBurnVlpParams>
44
+ >;
@@ -0,0 +1,32 @@
1
+ import { BaseNadoAPI } from '../base';
2
+ import { LinkSignerParams, LiquidateSubaccountParams } from './types';
3
+
4
+ export class SubaccountExecuteAPI extends BaseNadoAPI {
5
+ /**
6
+ * Liquidates a subaccount
7
+ *
8
+ * @param params
9
+ */
10
+ async liquidateSubaccount(params: LiquidateSubaccountParams) {
11
+ return this.context.engineClient.liquidateSubaccount({
12
+ ...params,
13
+ subaccountOwner: this.getSubaccountOwnerIfNeeded(params),
14
+ verifyingAddr: params.verifyingAddr ?? this.getEndpointAddress(),
15
+ chainId: this.getWalletClientChainIdIfNeeded(params),
16
+ });
17
+ }
18
+
19
+ /**
20
+ * Links a signer to a subaccount to allow them to sign transactions on behalf of the subaccount
21
+ *
22
+ * @param params
23
+ */
24
+ async linkSigner(params: LinkSignerParams) {
25
+ return this.context.engineClient.linkSigner({
26
+ ...params,
27
+ subaccountOwner: this.getSubaccountOwnerIfNeeded(params),
28
+ verifyingAddr: params.verifyingAddr ?? this.getEndpointAddress(),
29
+ chainId: this.getWalletClientChainIdIfNeeded(params),
30
+ });
31
+ }
32
+ }
@@ -0,0 +1,78 @@
1
+ import {
2
+ getSubaccountSummary,
3
+ GetSubaccountSummaryParams,
4
+ } from '@nadohq/contracts';
5
+ import {
6
+ GetEngineEstimatedSubaccountSummaryParams,
7
+ GetEngineIsolatedPositionsParams,
8
+ GetEngineSubaccountFeeRatesParams,
9
+ GetEngineSubaccountSummaryParams,
10
+ } from '@nadohq/engine-client';
11
+ import {
12
+ GetIndexerLinkedSignerParams,
13
+ GetIndexerReferralCodeParams,
14
+ } from '@nadohq/indexer-client';
15
+ import { BaseNadoAPI } from '../base';
16
+
17
+ export class SubaccountQueryAPI extends BaseNadoAPI {
18
+ /**
19
+ * Returns a comprehensive summary for a subaaccount, including its balances and health
20
+ *
21
+ * @see {@link getSubaccountSummary:CONTRACTS | getSubaccountSummary}
22
+ */
23
+ async getSubaccountSummary(params: GetSubaccountSummaryParams) {
24
+ return getSubaccountSummary(this.paramsWithContracts(params));
25
+ }
26
+
27
+ /**
28
+ * Gets the subaccount state according to the offchain engine
29
+ * @param params
30
+ */
31
+ async getEngineSubaccountSummary(params: GetEngineSubaccountSummaryParams) {
32
+ return this.context.engineClient.getSubaccountSummary(params);
33
+ }
34
+
35
+ /**
36
+ * Gets the estimated subaccount state from offchain engine after a series of proposed txs
37
+ * @param params
38
+ */
39
+ async getEngineEstimatedSubaccountSummary(
40
+ params: GetEngineEstimatedSubaccountSummaryParams,
41
+ ) {
42
+ return this.context.engineClient.getEstimatedSubaccountSummary(params);
43
+ }
44
+
45
+ /**
46
+ * Gets the isolated positions for a subaccount
47
+ * @param params
48
+ */
49
+ async getIsolatedPositions(params: GetEngineIsolatedPositionsParams) {
50
+ return this.context.engineClient.getIsolatedPositions(params);
51
+ }
52
+
53
+ /**
54
+ * Queries engine to get subaccount fee rates
55
+ * @param params
56
+ */
57
+ async getSubaccountFeeRates(params: GetEngineSubaccountFeeRatesParams) {
58
+ return this.context.engineClient.getSubaccountFeeRates(params);
59
+ }
60
+
61
+ /**
62
+ * Retrieves the current signer and link signer rate limit from the indexer
63
+ * @param params
64
+ */
65
+ async getSubaccountLinkedSignerWithRateLimit(
66
+ params: GetIndexerLinkedSignerParams,
67
+ ) {
68
+ return this.context.indexerClient.getLinkedSignerWithRateLimit(params);
69
+ }
70
+
71
+ /**
72
+ * Retrieves referral code for an address
73
+ * @param params
74
+ */
75
+ async getReferralCode(params: GetIndexerReferralCodeParams) {
76
+ return this.context.indexerClient.getReferralCode(params);
77
+ }
78
+ }
@@ -0,0 +1,44 @@
1
+ import { createDeterministicLinkedSignerPrivateKey } from '@nadohq/contracts';
2
+ import { WalletNotProvidedError } from '@nadohq/utils';
3
+ import { Mixin } from 'ts-mixer';
4
+ import { privateKeyToAccount } from 'viem/accounts';
5
+ import { SubaccountExecuteAPI } from './SubaccountExecuteAPI';
6
+ import { SubaccountQueryAPI } from './SubaccountQueryAPI';
7
+ import { CreateStandardLinkedSignerResult } from './types';
8
+
9
+ export * from './types';
10
+
11
+ export class SubaccountAPI extends Mixin(
12
+ SubaccountExecuteAPI,
13
+ SubaccountQueryAPI,
14
+ ) {
15
+ /**
16
+ * Given the current context, create a "standard" link signer Account for the subaccount
17
+ * The "standard" linked signer private key is deterministically generated so that the frontend running on
18
+ * different clients can have a consistent private key for the subaccount linked signer.
19
+ *
20
+ * @param subaccountName
21
+ */
22
+ async createStandardLinkedSigner(
23
+ subaccountName: string,
24
+ ): Promise<CreateStandardLinkedSignerResult> {
25
+ const walletClient = this.context.walletClient;
26
+
27
+ if (!walletClient) {
28
+ throw new WalletNotProvidedError();
29
+ }
30
+
31
+ const privateKey = await createDeterministicLinkedSignerPrivateKey({
32
+ chainId: walletClient.chain.id,
33
+ endpointAddress: this.getEndpointAddress(),
34
+ walletClient,
35
+ subaccountName,
36
+ subaccountOwner: walletClient.account.address,
37
+ });
38
+
39
+ return {
40
+ privateKey,
41
+ account: privateKeyToAccount(privateKey),
42
+ };
43
+ }
44
+ }
@@ -0,0 +1,16 @@
1
+ import { AccountWithPrivateKey } from '@nadohq/contracts';
2
+ import {
3
+ EngineLinkSignerParams,
4
+ EngineLiquidateSubaccountParams,
5
+ } from '@nadohq/engine-client';
6
+ import { OptionalSignatureParams, OptionalSubaccountOwner } from '../types';
7
+
8
+ export type LinkSignerParams = OptionalSignatureParams<
9
+ OptionalSubaccountOwner<EngineLinkSignerParams>
10
+ >;
11
+
12
+ export type LiquidateSubaccountParams = OptionalSignatureParams<
13
+ OptionalSubaccountOwner<EngineLiquidateSubaccountParams>
14
+ >;
15
+
16
+ export type CreateStandardLinkedSignerResult = AccountWithPrivateKey;
@@ -0,0 +1,11 @@
1
+ export type OptionalSubaccountOwner<T> = Omit<T, 'subaccountOwner'> & {
2
+ subaccountOwner?: string;
3
+ };
4
+
5
+ export type OptionalSignatureParams<T> = Omit<
6
+ T,
7
+ 'verifyingAddr' | 'chainId'
8
+ > & {
9
+ verifyingAddr?: string;
10
+ chainId?: number;
11
+ };
@@ -0,0 +1,70 @@
1
+ import { EIP712OrderParams } from '@nadohq/contracts';
2
+ import {
3
+ EngineExecuteRequestParamsByType,
4
+ WithSignature,
5
+ } from '@nadohq/engine-client';
6
+ import { BaseNadoAPI } from '../base';
7
+
8
+ /**
9
+ * Builds execute messages as expected by the server to send over Websocket.
10
+ * @example nadoClient.ws.execute.buildPlaceOrderMessage(...)
11
+ */
12
+ export class WebSocketExecuteAPI extends BaseNadoAPI {
13
+ /**
14
+ * Builds ws message for the `liquidate_subaccount` execute action.
15
+ * @param params LiquidateSubaccount params.
16
+ * @returns `liquidate_subaccount` execute message
17
+ */
18
+ async buildLiquidateSubaccountMessage(
19
+ params: EngineExecuteRequestParamsByType['liquidate_subaccount'],
20
+ ) {
21
+ return this.context.engineClient.payloadBuilder.buildLiquidateSubaccountPayload(
22
+ params,
23
+ );
24
+ }
25
+
26
+ /**
27
+ * Builds ws message for the `withdraw_collateral` execute action.
28
+ * @param params WithdrawCollateral params.
29
+ * @returns `liquidate_subaccount` execute message
30
+ */
31
+ async buildWithdrawCollateralMessage(
32
+ params: EngineExecuteRequestParamsByType['withdraw_collateral'],
33
+ ) {
34
+ return this.context.engineClient.payloadBuilder.buildWithdrawCollateralPayload(
35
+ params,
36
+ );
37
+ }
38
+
39
+ /**
40
+ * Builds ws message for the `place_order` execute action.
41
+ * @param params PlaceOrder params.
42
+ * @returns `place_order` execute message
43
+ */
44
+ buildPlaceOrderMessage(
45
+ params: WithSignature<
46
+ EngineExecuteRequestParamsByType['place_order'] & {
47
+ order: EIP712OrderParams;
48
+ }
49
+ >,
50
+ ) {
51
+ return this.context.engineClient.payloadBuilder.buildPlaceOrderPayloadSync(
52
+ params,
53
+ );
54
+ }
55
+
56
+ /**
57
+ * Builds ws message for the `cancel_orders` execute action.
58
+ * @param params PlaceOrder params.
59
+ * @returns `cancel_orders` execute message
60
+ */
61
+ buildCancelOrdersMessage(
62
+ params: WithSignature<
63
+ EngineExecuteRequestParamsByType['cancel_orders'] & { nonce: string }
64
+ >,
65
+ ) {
66
+ return this.context.engineClient.payloadBuilder.buildCancelOrdersPayloadSync(
67
+ params,
68
+ );
69
+ }
70
+ }
@@ -0,0 +1,25 @@
1
+ import {
2
+ EngineServerQueryRequest,
3
+ EngineServerQueryRequestByType,
4
+ EngineServerQueryRequestType,
5
+ } from '@nadohq/engine-client';
6
+ import { BaseNadoAPI } from '../base';
7
+
8
+ /**
9
+ * Builds query messages as expected by the server to send over Websocket.
10
+ * @example nadoClient.ws.query.buildQueryMessage('all_products', {})
11
+ */
12
+ export class WebSocketQueryAPI extends BaseNadoAPI {
13
+ /**
14
+ * Builds a query request message as expected by the server via Websocket.
15
+ * @param requestType
16
+ * @param params
17
+ * @returns query request message.
18
+ */
19
+ public buildQueryMessage<TRequestType extends EngineServerQueryRequestType>(
20
+ requestType: TRequestType,
21
+ params: EngineServerQueryRequestByType[TRequestType],
22
+ ): EngineServerQueryRequest<TRequestType> {
23
+ return this.context.engineClient.getQueryRequest(requestType, params);
24
+ }
25
+ }
@@ -0,0 +1,62 @@
1
+ import {
2
+ EngineServerSubscriptionRequest,
3
+ EngineServerSubscriptionRequestByType,
4
+ EngineServerSubscriptionRequestType,
5
+ EngineServerSubscriptionStream,
6
+ EngineServerSubscriptionStreamParamsByType,
7
+ EngineServerSubscriptionStreamParamsType,
8
+ } from '@nadohq/engine-client';
9
+ import { BaseNadoAPI } from '../base';
10
+
11
+ /**
12
+ * Builds subscription messages as expected by the server to send over Websocket.
13
+ *
14
+ * @example
15
+ * const tradeSubscriptionParams = nadoClient.ws.subscription.buildSubscriptionParams('trade', ...);
16
+ * const tradeSubscriptionMessage = nadoClient.ws.subscription.buildSubscriptionMessage(
17
+ * 'subscribe', tradeSubscriptionParams);
18
+ */
19
+ export class WebSocketSubscriptionAPI extends BaseNadoAPI {
20
+ /**
21
+ * Builds a subscription request message as expected by the server via Websocket.
22
+ * @param requestType name of request to build message for.
23
+ * @param id identifier to associate messages with responses.
24
+ * @param params request message params.
25
+ * @returns subscription request message.
26
+ */
27
+ public buildSubscriptionMessage<
28
+ TRequestType extends EngineServerSubscriptionRequestType,
29
+ >(
30
+ id: number,
31
+ requestType: TRequestType,
32
+ params: EngineServerSubscriptionRequestByType[TRequestType],
33
+ ): EngineServerSubscriptionRequest<TRequestType> {
34
+ return {
35
+ id,
36
+ method: requestType,
37
+ ...params,
38
+ };
39
+ }
40
+
41
+ /**
42
+ * Builds a subscription stream param as expected by the server via Websocket.
43
+ * @param streamType name of stream to build params for.
44
+ * @param params
45
+ * @returns subscription stream params.
46
+ */
47
+ public buildSubscriptionParams<
48
+ TStreamType extends EngineServerSubscriptionStreamParamsType,
49
+ >(
50
+ streamType: TStreamType,
51
+ params: EngineServerSubscriptionStreamParamsByType[TStreamType],
52
+ ): {
53
+ stream: EngineServerSubscriptionStream<TStreamType>;
54
+ } {
55
+ return {
56
+ stream: {
57
+ type: streamType,
58
+ ...params,
59
+ },
60
+ };
61
+ }
62
+ }
@@ -0,0 +1,21 @@
1
+ import { NadoClientContext } from '../../context';
2
+ import { BaseNadoAPI } from '../base';
3
+ import { WebSocketExecuteAPI } from './WebSocketExecuteAPI';
4
+ import { WebSocketQueryAPI } from './WebSocketQueryAPI';
5
+ import { WebSocketSubscriptionAPI } from './WebSocketSubscriptionAPI';
6
+
7
+ /**
8
+ * Builds ws messages as expected by the server to send over Websocket.
9
+ */
10
+ export class WebsocketAPI extends BaseNadoAPI {
11
+ readonly query: WebSocketQueryAPI;
12
+ readonly execute: WebSocketExecuteAPI;
13
+ readonly subscription: WebSocketSubscriptionAPI;
14
+
15
+ constructor(context: NadoClientContext) {
16
+ super(context);
17
+ this.query = new WebSocketQueryAPI(context);
18
+ this.execute = new WebSocketExecuteAPI(context);
19
+ this.subscription = new WebSocketSubscriptionAPI(context);
20
+ }
21
+ }
package/src/client.ts ADDED
@@ -0,0 +1,67 @@
1
+ import { WalletClientWithAccount } from '@nadohq/contracts';
2
+ import { MarketAPI } from './apis/market';
3
+ import { PerpAPI } from './apis/perp';
4
+ import { SpotAPI } from './apis/spot';
5
+ import { SubaccountAPI } from './apis/subaccount';
6
+ import { WebsocketAPI } from './apis/ws';
7
+ import { createClientContext, NadoClientContext } from './context';
8
+
9
+ /**
10
+ * Client for querying and executing against Nado Clearinghouse.
11
+ * Usually not instantiated directly. Instead, use {@link createNadoClient:CLIENT}.
12
+ */
13
+ export class NadoClient {
14
+ context!: NadoClientContext;
15
+ market!: MarketAPI;
16
+ subaccount!: SubaccountAPI;
17
+ spot!: SpotAPI;
18
+ perp!: PerpAPI;
19
+ ws!: WebsocketAPI;
20
+
21
+ constructor(context: NadoClientContext) {
22
+ this.setupFromContext(context);
23
+ }
24
+
25
+ /**
26
+ * Sets the linked signer for the client. Set to null to revert to the chain signer.
27
+ * @param linkedSignerWalletClient
28
+ */
29
+ setLinkedSigner(linkedSignerWalletClient: WalletClientWithAccount | null) {
30
+ // This is a bit ugly, but works for now
31
+ this.context.linkedSignerWalletClient =
32
+ linkedSignerWalletClient ?? undefined;
33
+ this.context.engineClient.setLinkedSigner(linkedSignerWalletClient);
34
+ this.context.triggerClient.setLinkedSigner(linkedSignerWalletClient);
35
+ }
36
+
37
+ /**
38
+ * Sets the WalletClient for the client. Will cause a full reload of the current context.
39
+ * @param walletClient
40
+ */
41
+ setWalletClient(walletClient: NadoClientContext['walletClient']) {
42
+ const newContext = createClientContext(
43
+ {
44
+ contractAddresses: this.context.contractAddresses,
45
+ engineEndpoint: this.context.engineClient.opts.url,
46
+ indexerEndpoint: this.context.indexerClient.opts.url,
47
+ triggerEndpoint: this.context.triggerClient.opts.url,
48
+ },
49
+ {
50
+ walletClient,
51
+ // No need to call setLinkedSigner as this property doesn't change
52
+ linkedSignerWalletClient: this.context.linkedSignerWalletClient,
53
+ publicClient: this.context.publicClient,
54
+ },
55
+ );
56
+ this.setupFromContext(newContext);
57
+ }
58
+
59
+ private setupFromContext(context: NadoClientContext) {
60
+ this.context = context;
61
+ this.market = new MarketAPI(context);
62
+ this.subaccount = new SubaccountAPI(context);
63
+ this.spot = new SpotAPI(context);
64
+ this.perp = new PerpAPI(context);
65
+ this.ws = new WebsocketAPI(context);
66
+ }
67
+ }
package/src/context.ts ADDED
@@ -0,0 +1,165 @@
1
+ import {
2
+ ChainEnv,
3
+ NADO_ABIS,
4
+ NADO_DEPLOYMENTS,
5
+ NadoContractName,
6
+ NadoContracts,
7
+ NadoDeploymentAddresses,
8
+ WalletClientWithAccount,
9
+ } from '@nadohq/contracts';
10
+ import { ENGINE_CLIENT_ENDPOINTS, EngineClient } from '@nadohq/engine-client';
11
+ import {
12
+ INDEXER_CLIENT_ENDPOINTS,
13
+ IndexerClient,
14
+ } from '@nadohq/indexer-client';
15
+ import {
16
+ TRIGGER_CLIENT_ENDPOINTS,
17
+ TriggerClient,
18
+ } from '@nadohq/trigger-client';
19
+ import { getContract, PublicClient } from 'viem';
20
+
21
+ /**
22
+ * Context required to use the Nado client.
23
+ */
24
+ export interface NadoClientContext {
25
+ publicClient: PublicClient;
26
+ walletClient?: WalletClientWithAccount;
27
+ // If provided, this is used to sign engine transactions instead of the account associated with walletClient
28
+ linkedSignerWalletClient?: WalletClientWithAccount;
29
+ contracts: NadoContracts;
30
+ contractAddresses: NadoDeploymentAddresses;
31
+ engineClient: EngineClient;
32
+ indexerClient: IndexerClient;
33
+ triggerClient: TriggerClient;
34
+ }
35
+
36
+ /**
37
+ * Args for creating a context
38
+ */
39
+ interface NadoClientContextOpts {
40
+ contractAddresses: NadoDeploymentAddresses;
41
+ engineEndpoint: string;
42
+ indexerEndpoint: string;
43
+ triggerEndpoint: string;
44
+ }
45
+
46
+ /**
47
+ * Args for signing configuration for creating a context
48
+ */
49
+ export type CreateNadoClientContextAccountOpts = Pick<
50
+ NadoClientContext,
51
+ 'walletClient' | 'linkedSignerWalletClient' | 'publicClient'
52
+ >;
53
+
54
+ export type CreateNadoClientContextOpts = NadoClientContextOpts | ChainEnv;
55
+
56
+ /**
57
+ * Utility function to create client context from options
58
+ *
59
+ * @param opts
60
+ * @param accountOpts
61
+ */
62
+ export function createClientContext(
63
+ opts: CreateNadoClientContextOpts,
64
+ accountOpts: CreateNadoClientContextAccountOpts,
65
+ ): NadoClientContext {
66
+ const {
67
+ contractAddresses,
68
+ engineEndpoint,
69
+ indexerEndpoint,
70
+ triggerEndpoint,
71
+ } = ((): NadoClientContextOpts => {
72
+ // Custom options
73
+ if (typeof opts === 'object') {
74
+ return opts;
75
+ }
76
+
77
+ const chainEnv = opts;
78
+ return {
79
+ contractAddresses: NADO_DEPLOYMENTS[chainEnv],
80
+ engineEndpoint: ENGINE_CLIENT_ENDPOINTS[chainEnv],
81
+ indexerEndpoint: INDEXER_CLIENT_ENDPOINTS[chainEnv],
82
+ triggerEndpoint: TRIGGER_CLIENT_ENDPOINTS[chainEnv],
83
+ };
84
+ })();
85
+ const { publicClient, walletClient, linkedSignerWalletClient } = accountOpts;
86
+
87
+ return {
88
+ walletClient,
89
+ linkedSignerWalletClient,
90
+ publicClient,
91
+ contracts: {
92
+ querier: getNadoContract({
93
+ contractAddresses,
94
+ contractName: 'querier',
95
+ walletClient,
96
+ publicClient,
97
+ }),
98
+ clearinghouse: getNadoContract({
99
+ contractAddresses,
100
+ contractName: 'clearinghouse',
101
+ walletClient,
102
+ publicClient,
103
+ }),
104
+ endpoint: getNadoContract({
105
+ contractAddresses,
106
+ contractName: 'endpoint',
107
+ walletClient,
108
+ publicClient,
109
+ }),
110
+ spotEngine: getNadoContract({
111
+ contractAddresses,
112
+ contractName: 'spotEngine',
113
+ walletClient,
114
+ publicClient,
115
+ }),
116
+ perpEngine: getNadoContract({
117
+ contractAddresses,
118
+ contractName: 'perpEngine',
119
+ walletClient,
120
+ publicClient,
121
+ }),
122
+ withdrawPool: getNadoContract({
123
+ contractAddresses,
124
+ contractName: 'withdrawPool',
125
+ walletClient,
126
+ publicClient,
127
+ }),
128
+ },
129
+ contractAddresses,
130
+ engineClient: new EngineClient({
131
+ url: engineEndpoint,
132
+ walletClient,
133
+ linkedSignerWalletClient,
134
+ }),
135
+ indexerClient: new IndexerClient({
136
+ url: indexerEndpoint,
137
+ walletClient,
138
+ }),
139
+ triggerClient: new TriggerClient({
140
+ url: triggerEndpoint,
141
+ walletClient,
142
+ linkedSignerWalletClient,
143
+ }),
144
+ };
145
+ }
146
+
147
+ interface GetNadoContractParams<T extends NadoContractName> {
148
+ contractAddresses: NadoDeploymentAddresses;
149
+ contractName: T;
150
+ walletClient?: WalletClientWithAccount;
151
+ publicClient: PublicClient;
152
+ }
153
+
154
+ function getNadoContract<T extends NadoContractName>({
155
+ contractAddresses,
156
+ contractName,
157
+ walletClient,
158
+ publicClient,
159
+ }: GetNadoContractParams<T>): NadoContracts[T] {
160
+ return getContract({
161
+ address: contractAddresses[contractName],
162
+ abi: NADO_ABIS[contractName],
163
+ client: walletClient ?? publicClient,
164
+ }) as NadoContracts[T];
165
+ }
@@ -0,0 +1,20 @@
1
+ import { NadoClient } from './client';
2
+ import {
3
+ createClientContext,
4
+ CreateNadoClientContextAccountOpts,
5
+ CreateNadoClientContextOpts,
6
+ } from './context';
7
+
8
+ /**
9
+ * Creates a Nado client from given options.
10
+ * {@label CLIENT}
11
+ *
12
+ * @param opts
13
+ * @param accountOpts
14
+ */
15
+ export function createNadoClient(
16
+ opts: CreateNadoClientContextOpts,
17
+ accountOpts: CreateNadoClientContextAccountOpts,
18
+ ): NadoClient {
19
+ return new NadoClient(createClientContext(opts, accountOpts));
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ export * from './createNadoClient';
2
+ export * from './client';
3
+ export * from './context';
4
+
5
+ export * from './apis/market';
6
+ export * from './apis/perp';
7
+ export * from './apis/spot';
8
+ export * from './apis/subaccount';
9
+
10
+ // Subpackage exports
11
+ export * from '@nadohq/utils';
12
+ export * from '@nadohq/contracts';
13
+ export * from '@nadohq/engine-client';
14
+ export * from '@nadohq/indexer-client';
15
+ export * from '@nadohq/trigger-client';