@dydxprotocol/v4-client-js 3.0.3 → 3.0.5
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.
- package/CHANGELOG.md +2 -2
- package/LICENSE +7 -796
- package/build/cjs/examples/permissioned_keys_example.js +35 -49
- package/build/cjs/src/clients/composite-client.js +3 -3
- package/build/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/build/esm/examples/permissioned_keys_example.js +37 -51
- package/build/esm/src/clients/composite-client.d.ts +1 -1
- package/build/esm/src/clients/composite-client.d.ts.map +1 -1
- package/build/esm/src/clients/composite-client.js +3 -3
- package/build/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/examples/permissioned_keys_example.ts +66 -70
- package/package.json +1 -1
- package/src/clients/composite-client.ts +2 -1
|
@@ -1,19 +1,29 @@
|
|
|
1
|
-
import
|
|
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 {
|
|
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 {
|
|
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.
|
|
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
|
-
|
|
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,
|
|
80
|
+
await placeOrder(client, apiTradingWallet1, subaccount1.address, newAuthenticatorID);
|
|
59
81
|
|
|
60
|
-
// Placing order using
|
|
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,
|
|
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,
|
|
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:
|
|
101
|
+
id: string,
|
|
80
102
|
): Promise<void> {
|
|
81
|
-
await client.removeAuthenticator(subaccount, id
|
|
103
|
+
await client.removeAuthenticator(subaccount, id);
|
|
82
104
|
}
|
|
83
105
|
|
|
84
|
-
async function
|
|
106
|
+
async function createApiTradingWallet(
|
|
85
107
|
client: CompositeClient,
|
|
86
108
|
subaccount: SubaccountInfo,
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
-
|
|
124
|
-
|
|
125
|
-
authenticatorId:
|
|
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('
|
|
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.
|
|
139
|
-
SubaccountInfo.forPermissionedWallet(
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
151
|
-
|
|
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
|
-
|
|
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
|
@@ -421,6 +421,7 @@ export class CompositeClient {
|
|
|
421
421
|
currentHeight?: number,
|
|
422
422
|
goodTilBlock?: number,
|
|
423
423
|
memo?: string,
|
|
424
|
+
broadcastMode?: BroadcastMode,
|
|
424
425
|
): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
|
|
425
426
|
const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
|
|
426
427
|
const msg = this.placeOrderMessage(
|
|
@@ -458,7 +459,7 @@ export class CompositeClient {
|
|
|
458
459
|
true,
|
|
459
460
|
undefined,
|
|
460
461
|
memo,
|
|
461
|
-
|
|
462
|
+
broadcastMode,
|
|
462
463
|
() => account,
|
|
463
464
|
);
|
|
464
465
|
}
|