@dydxprotocol/v4-client-js 3.0.3 → 3.0.4

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.
@@ -1,19 +1,29 @@
1
- import { toBase64 } from '@cosmjs/encoding';
2
- import { Order_TimeInForce } from '@dydxprotocol/v4-proto/src/codegen/dydxprotocol/clob/order';
1
+ import Long from 'long';
3
2
 
4
3
  import { BECH32_PREFIX } from '../src';
5
4
  import { CompositeClient } from '../src/clients/composite-client';
6
- import { AuthenticatorType, Network, OrderSide, SelectedGasDenom } from '../src/clients/constants';
5
+ import {
6
+ Network,
7
+ OrderExecution,
8
+ OrderSide,
9
+ OrderTimeInForce,
10
+ OrderType,
11
+ SelectedGasDenom,
12
+ } from '../src/clients/constants';
7
13
  import LocalWallet from '../src/clients/modules/local-wallet';
8
14
  import { SubaccountInfo } from '../src/clients/subaccount';
9
- import { DYDX_TEST_MNEMONIC, DYDX_TEST_MNEMONIC_2, DYDX_TEST_MNEMONIC_3 } from './constants';
15
+ import {
16
+ createNewRandomDydxWallet,
17
+ getAuthorizeNewTradingKeyArguments,
18
+ getAuthorizedTradingKeysMetadata,
19
+ } from '../src/lib/trading-key-utils';
20
+ import { DYDX_TEST_MNEMONIC, DYDX_TEST_MNEMONIC_2 } from './constants';
10
21
 
11
22
  async function test(): Promise<void> {
12
23
  const wallet1 = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX);
13
24
  const wallet2 = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC_2, BECH32_PREFIX);
14
- const wallet3 = await LocalWallet.fromMnemonic(DYDX_TEST_MNEMONIC_3, BECH32_PREFIX);
15
25
 
16
- const network = Network.staging();
26
+ const network = Network.testnet();
17
27
  const client = await CompositeClient.connect(network);
18
28
  client.setSelectedGasDenom(SelectedGasDenom.NATIVE);
19
29
 
@@ -22,7 +32,6 @@ async function test(): Promise<void> {
22
32
 
23
33
  const subaccount1 = SubaccountInfo.forLocalWallet(wallet1, 0);
24
34
  const subaccount2 = SubaccountInfo.forLocalWallet(wallet2, 0);
25
- const subaccount3 = SubaccountInfo.forLocalWallet(wallet3, 0);
26
35
 
27
36
  // Change second wallet pubkey
28
37
  // Add an authenticator to allow wallet2 to place orders
@@ -31,7 +40,12 @@ async function test(): Promise<void> {
31
40
  const authsBefore = await client.getAuthenticators(wallet1.address!);
32
41
  const beforeCount = authsBefore.accountAuthenticators.length;
33
42
  console.log(`Authenticators before: ${beforeCount}`);
34
- await addTestAuthenticator(client, subaccount1, wallet2.pubKey!.value);
43
+
44
+ const apiTradingWalletInfo1 = await createApiTradingWallet(client, subaccount1);
45
+ const apiTradingWallet1 = await LocalWallet.fromPrivateKey(
46
+ apiTradingWalletInfo1.privteKeyHex,
47
+ BECH32_PREFIX,
48
+ );
35
49
 
36
50
  console.log('** Waiting 3 seconds for txn to be confirmed **');
37
51
  await new Promise((resolve) => setTimeout(resolve, 3000));
@@ -45,21 +59,29 @@ async function test(): Promise<void> {
45
59
  } else {
46
60
  console.log('Authenticator count incremented by 1 as expected.');
47
61
  }
48
- // Last element in authenticators array is the most recently created
49
- const lastElement = authsAfter.accountAuthenticators.length - 1;
50
- const newAuthenticatorID = authsAfter.accountAuthenticators[lastElement].id;
51
62
 
63
+ const parsedAuths = getAuthorizedTradingKeysMetadata(authsAfter.accountAuthenticators);
64
+ const newAuthenticatorID = parsedAuths.find(
65
+ (a) => apiTradingWallet1.address != null && a.address === apiTradingWallet1.address,
66
+ )?.id;
67
+ if (newAuthenticatorID == null) {
68
+ console.error(
69
+ 'Unable to locate the created authenticator. Address: ',
70
+ apiTradingWallet1.address,
71
+ );
72
+ throw new Error('Unable to locate the new authenticator');
73
+ }
52
74
  console.log(`New authenticator ID: ${newAuthenticatorID}`);
53
75
 
54
76
  // Placing order using subaccount2 for subaccount1 succeeds
55
77
  console.log(
56
78
  '** Placing order for subaccount1 with subaccount2 + authenticator, should succeed **',
57
79
  );
58
- await placeOrder(client, subaccount2, subaccount1, newAuthenticatorID);
80
+ await placeOrder(client, apiTradingWallet1, subaccount1.address, newAuthenticatorID);
59
81
 
60
- // Placing order using subaccount3 for subaccount1 should fail
82
+ // Placing order using subaccount2 for subaccount1 should fail
61
83
  console.log('** Placing order for subaccount1 with subaccount3 + authenticator, should fail **');
62
- await placeOrder(client, subaccount3, subaccount1, newAuthenticatorID);
84
+ await placeOrder(client, subaccount2.signingWallet, subaccount1.address, newAuthenticatorID);
63
85
 
64
86
  // Remove authenticator
65
87
  console.log('** Removing authenticator **');
@@ -70,91 +92,65 @@ async function test(): Promise<void> {
70
92
 
71
93
  // Placing an order using subaccount2 will now fail
72
94
  console.log('** Placing order with removed authenticator should fail **');
73
- await placeOrder(client, subaccount2, subaccount1, newAuthenticatorID);
95
+ await placeOrder(client, apiTradingWallet1, subaccount1.address, newAuthenticatorID);
74
96
  }
75
97
 
76
98
  async function removeAuthenticator(
77
99
  client: CompositeClient,
78
100
  subaccount: SubaccountInfo,
79
- id: Long,
101
+ id: string,
80
102
  ): Promise<void> {
81
- await client.removeAuthenticator(subaccount, id.toString());
103
+ await client.removeAuthenticator(subaccount, id);
82
104
  }
83
105
 
84
- async function addTestAuthenticator(
106
+ async function createApiTradingWallet(
85
107
  client: CompositeClient,
86
108
  subaccount: SubaccountInfo,
87
- authedPubKey: string,
88
- ): Promise<void> {
89
- const msgType = (s: string): string => toBase64(new TextEncoder().encode(s));
90
- const anyOfSubAuth = [
91
- {
92
- type: AuthenticatorType.MESSAGE_FILTER,
93
- config: msgType('/dydxprotocol.clob.MsgPlaceOrder'),
94
- },
95
- {
96
- type: AuthenticatorType.MESSAGE_FILTER,
97
- config: msgType('/dydxprotocol.sending.MsgCreateTransfer'),
98
- },
99
- ];
100
-
101
- // Nested AnyOf config must be base64(JSON([...])) as on-chain expects []byte
102
- const anyOfConfigB64 = toBase64(new TextEncoder().encode(JSON.stringify(anyOfSubAuth)));
103
-
104
- const subAuth = [
105
- {
106
- type: AuthenticatorType.SIGNATURE_VERIFICATION,
107
- config: authedPubKey,
108
- },
109
- {
110
- type: AuthenticatorType.ANY_OF,
111
- config: anyOfConfigB64,
112
- },
113
- ];
114
-
115
- const jsonString = JSON.stringify(subAuth);
116
- const encodedData = new TextEncoder().encode(jsonString);
117
-
118
- await client.addAuthenticator(subaccount, AuthenticatorType.ALL_OF, encodedData);
109
+ ): Promise<{ privteKeyHex: string; forDydxAddress: string }> {
110
+ const wallet = await createNewRandomDydxWallet();
111
+ if (wallet == null) {
112
+ throw new Error("Couldn't create wallet");
113
+ }
114
+ const { data, type } = getAuthorizeNewTradingKeyArguments({
115
+ generatedWalletPubKey: wallet?.publicKey,
116
+ });
117
+ console.log('adding', wallet.address);
118
+ await client.addAuthenticator(subaccount, type, data);
119
+ return { privteKeyHex: wallet.privateKeyHex, forDydxAddress: subaccount.address };
119
120
  }
120
121
 
121
122
  async function placeOrder(
122
123
  client: CompositeClient,
123
- fromAccount: SubaccountInfo,
124
- forAccount: SubaccountInfo,
125
- authenticatorId: Long,
124
+ apiTradingWallet: LocalWallet,
125
+ forAccountAddress: string,
126
+ authenticatorId: string,
126
127
  ): Promise<void> {
127
128
  try {
128
129
  const side = OrderSide.BUY;
129
- const price = Number('1000');
130
- const currentBlock = await client.validatorClient.get.latestBlockHeight();
131
- const nextValidBlockHeight = currentBlock + 5;
132
- const goodTilBlock = nextValidBlockHeight + 10;
133
-
134
- const timeInForce = Order_TimeInForce.TIME_IN_FORCE_UNSPECIFIED;
130
+ const price = Number('10000');
135
131
 
136
132
  const clientId = Math.floor(Math.random() * 10000);
137
133
 
138
- const tx = await client.placeShortTermOrder(
139
- SubaccountInfo.forPermissionedWallet(
140
- fromAccount.signingWallet,
141
- forAccount.address,
142
- forAccount.subaccountNumber,
143
- [authenticatorId],
144
- ),
134
+ const tx = await client.placeOrder(
135
+ SubaccountInfo.forPermissionedWallet(apiTradingWallet, forAccountAddress, 0, [
136
+ Long.fromString(authenticatorId),
137
+ ]),
145
138
  'ETH-USD',
139
+ OrderType.MARKET,
146
140
  side,
147
141
  price,
148
142
  0.01,
149
143
  clientId,
150
- goodTilBlock,
151
- timeInForce,
144
+ OrderTimeInForce.IOC,
145
+ 100,
146
+ OrderExecution.IOC,
147
+ false,
152
148
  false,
153
- undefined,
154
149
  );
155
150
  console.log('**Order Tx**');
156
151
  console.log(Buffer.from(tx.hash).toString('hex'));
157
- } catch (error) {
152
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
153
+ } catch (error: any) {
158
154
  console.log(error.message);
159
155
  }
160
156
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dydxprotocol/v4-client-js",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "description": "General client library for the new dYdX system (v4 decentralized)",
5
5
  "main": "build/cjs/src/index.js",
6
6
  "module": "build/esm/src/index.js",