@dydxprotocol/v4-client-js 1.7.0 → 1.9.0

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.
@@ -15,6 +15,9 @@ import {
15
15
  withdraw,
16
16
  withdrawToIBC,
17
17
  wrappedError,
18
+ getMegavaultOwnerShares,
19
+ depositToMegavault,
20
+ withdrawFromMegavault,
18
21
  } from '../src/clients/native';
19
22
  import { DYDX_TEST_ADDRESS, DYDX_TEST_MNEMONIC } from './constants';
20
23
 
@@ -47,6 +50,19 @@ async function test(): Promise<void> {
47
50
  const userStats = await getUserStats(payload);
48
51
  console.log(userStats);
49
52
 
53
+ const balances1 = await getAccountBalances();
54
+ console.log(balances1);
55
+
56
+ const vaultOwnerShares = await getMegavaultOwnerShares(payload);
57
+ console.log(vaultOwnerShares);
58
+
59
+ const depositResult = await depositToMegavault(0, 2);
60
+ console.log(depositResult);
61
+
62
+ const withdrawResult = await withdrawFromMegavault(0, 1, 0);
63
+ console.log(withdrawResult);
64
+
65
+
50
66
  const sendTokenPayload = {
51
67
  subaccountNumber: 0,
52
68
  amount: '10', // Dydx Token
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dydxprotocol/v4-client-js",
3
- "version": "1.7.0",
3
+ "version": "1.9.0",
4
4
  "description": "General client library for the new dYdX system (v4 decentralized)",
5
5
  "main": "build/src/index.js",
6
6
  "scripts": {
@@ -23,6 +23,7 @@ export const MAINNET_CHAIN_ID = 'dydx-mainnet-1';
23
23
  // ------------ API URLs ------------
24
24
  export enum IndexerApiHost {
25
25
  TESTNET = 'https://indexer.v4testnet.dydx.exchange/',
26
+ STAGING = 'https://indexer.v4staging.dydx.exchange/',
26
27
  LOCAL = 'http://localhost:3002',
27
28
  // For the deployment by DYDX token holders
28
29
  MAINNET = 'https://indexer.dydx.trade',
@@ -30,6 +31,7 @@ export enum IndexerApiHost {
30
31
 
31
32
  export enum IndexerWSHost {
32
33
  TESTNET = 'wss://dydx-testnet.imperator.co/v4/ws',
34
+ STAGING = 'wss://indexer.v4staging.dydx.exchange/v4/ws',
33
35
  LOCAL = 'ws://localhost:3003',
34
36
  // For the deployment by DYDX token holders
35
37
  MAINNET = 'wss://indexer.dydx.trade/v4/ws',
@@ -41,6 +43,7 @@ export enum FaucetApiHost {
41
43
 
42
44
  export enum ValidatorApiHost {
43
45
  TESTNET = 'https://test-dydx.kingnodes.com',
46
+ STAGING = 'https://validator.v4staging.dydx.exchange',
44
47
  LOCAL = 'http://localhost:26657',
45
48
  // For the deployment by DYDX token holders
46
49
  MAINNET = 'https://dydx-ops-rpc.kingnodes.com:443',
@@ -265,6 +268,24 @@ export class Network {
265
268
  return new Network('testnet', indexerConfig, validatorConfig);
266
269
  }
267
270
 
271
+ static staging(): Network {
272
+ const indexerConfig = new IndexerConfig(IndexerApiHost.STAGING, IndexerWSHost.STAGING);
273
+ const validatorConfig = new ValidatorConfig(
274
+ ValidatorApiHost.STAGING,
275
+ TESTNET_CHAIN_ID,
276
+ {
277
+ CHAINTOKEN_DENOM: 'adv4tnt',
278
+ USDC_DENOM: 'ibc/8E27BA2D5493AF5636760E354E46004562C46AB7EC0CC4C1CA14E9E20E2545B5',
279
+ USDC_GAS_DENOM: 'uusdc',
280
+ USDC_DECIMALS: 6,
281
+ CHAINTOKEN_DECIMALS: 18,
282
+ },
283
+ undefined,
284
+ 'Client Example',
285
+ );
286
+ return new Network('staging', indexerConfig, validatorConfig);
287
+ }
288
+
268
289
  static local(): Network {
269
290
  const indexerConfig = new IndexerConfig(IndexerApiHost.LOCAL, IndexerWSHost.LOCAL);
270
291
  const validatorConfig = new ValidatorConfig(
@@ -1,3 +1,6 @@
1
+ import Long from "long";
2
+
3
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
4
  export function generateQueryPath(url: string, params: {}): string {
2
5
  const definedEntries = Object.entries(params).filter(
3
6
  ([_key, value]: [string, unknown]) => value !== undefined,
@@ -12,3 +15,57 @@ export function generateQueryPath(url: string, params: {}): string {
12
15
  .join('&');
13
16
  return `${url}?${paramsString}`;
14
17
  }
18
+
19
+ export function parseToPrimitives<T>(x: T): T {
20
+ if (typeof x === 'number' || typeof x === 'string' || typeof x === 'boolean' || x === null) {
21
+ return x;
22
+ }
23
+
24
+ if (Array.isArray(x)) {
25
+ return x.map((item) => parseToPrimitives(item)) as T;
26
+ }
27
+
28
+ if (Long.isLong(x)) {
29
+ return x.toString() as T;
30
+ }
31
+
32
+ if (x instanceof Uint8Array) {
33
+ return bytesToBigInt(x).toString() as T;
34
+ }
35
+
36
+ if (x instanceof Date) {
37
+ return x.toString() as T;
38
+ }
39
+
40
+ if (typeof x === 'object') {
41
+ const parsedObj: { [key: string]: any } = {};
42
+ // eslint-disable-next-line no-restricted-syntax
43
+ for (const key in x) {
44
+ if (Object.prototype.hasOwnProperty.call(x, key)) {
45
+ parsedObj[key] = parseToPrimitives((x as any)[key]);
46
+ }
47
+ }
48
+ return parsedObj as T;
49
+ }
50
+
51
+ if (typeof x === 'bigint') {
52
+ return x.toString() as T;
53
+ }
54
+
55
+ throw new Error(`Unsupported data type: ${typeof x}`);
56
+ }
57
+
58
+ /**
59
+ * Converts a byte array (representing an arbitrary-size signed integer) into a bigint.
60
+ * @param u Array of bytes represented as a Uint8Array.
61
+ */
62
+ function bytesToBigInt(u: Uint8Array): bigint {
63
+ if (u.length <= 1) {
64
+ return BigInt(0);
65
+ }
66
+ // eslint-disable-next-line no-bitwise
67
+ const negated: boolean = (u[0] & 1) === 1;
68
+ const hex: string = Buffer.from(u.slice(1)).toString('hex');
69
+ const abs: bigint = BigInt(`0x${hex}`);
70
+ return negated ? -abs : abs;
71
+ }
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
  /*
2
3
  Native app can call JS functions with primitives.
3
4
  */
@@ -30,6 +31,7 @@ import {
30
31
  SelectedGasDenom,
31
32
  } from './constants';
32
33
  import { FaucetClient } from './faucet-client';
34
+ import { parseToPrimitives } from './helpers/request-helpers';
33
35
  import { Response } from './lib/axios';
34
36
  import LocalWallet from './modules/local-wallet';
35
37
  import { NobleClient } from './noble-client';
@@ -1352,3 +1354,90 @@ export async function setSelectedGasDenom(gasDenom: string): Promise<string> {
1352
1354
  return wrappedError(error);
1353
1355
  }
1354
1356
  }
1357
+
1358
+ export async function getMegavaultOwnerShares(payload: string): Promise<string> {
1359
+ try {
1360
+ const client = globalThis.client;
1361
+ if (client === undefined) {
1362
+ throw new UserError('client is not connected. Call connectClient() first');
1363
+ }
1364
+ const json = JSON.parse(payload);
1365
+ const address = json.address;
1366
+ if (address === undefined) {
1367
+ throw new UserError('address is not set');
1368
+ }
1369
+ const response =
1370
+ await globalThis.client?.validatorClient.get.getMegavaultOwnerShares(address);
1371
+ return encodeJson(parseToPrimitives(response));
1372
+ } catch (e) {
1373
+ return wrappedError(e);
1374
+ }
1375
+ }
1376
+
1377
+ export async function getMegavaultWithdrawalInfo(
1378
+ sharesToWithdraw: bigint
1379
+ ): Promise<string> {
1380
+ try {
1381
+ const client = globalThis.client;
1382
+ if (client === undefined) {
1383
+ throw new UserError('client is not connected. Call connectClient() first');
1384
+ }
1385
+ const response =
1386
+ await globalThis.client?.validatorClient.get.getMegavaultWithdrawalInfo(sharesToWithdraw);
1387
+ return encodeJson(parseToPrimitives(response));
1388
+ } catch (e) {
1389
+ return wrappedError(e);
1390
+ }
1391
+ }
1392
+
1393
+ export async function depositToMegavault(
1394
+ subaccountNumber: number,
1395
+ amountUsdc: number
1396
+ ): Promise<string> {
1397
+ try {
1398
+ const client = globalThis.client;
1399
+ if (client === undefined) {
1400
+ throw new UserError('client is not connected. Call connectNetwork() first');
1401
+ }
1402
+ const wallet = globalThis.wallet;
1403
+ if (wallet === undefined) {
1404
+ throw new UserError('wallet is not set. Call connectWallet() first');
1405
+ }
1406
+ const subaccount = new SubaccountInfo(wallet, subaccountNumber);
1407
+ const tx = await client.depositToMegavault(
1408
+ subaccount,
1409
+ amountUsdc,
1410
+ Method.BroadcastTxCommit,
1411
+ );
1412
+ return encodeJson(parseToPrimitives(tx));
1413
+ } catch (error) {
1414
+ return wrappedError(error);
1415
+ }
1416
+ }
1417
+
1418
+ export async function withdrawFromMegavault(
1419
+ subaccountNumber: number,
1420
+ shares: number,
1421
+ minAmount: number,
1422
+ ): Promise<string> {
1423
+ try {
1424
+ const client = globalThis.client;
1425
+ if (client === undefined) {
1426
+ throw new UserError('client is not connected. Call connectNetwork() first');
1427
+ }
1428
+ const wallet = globalThis.wallet;
1429
+ if (wallet === undefined) {
1430
+ throw new UserError('wallet is not set. Call connectWallet() first');
1431
+ }
1432
+ const subaccount = new SubaccountInfo(wallet, subaccountNumber);
1433
+ const tx = await client.withdrawFromMegavault(
1434
+ subaccount,
1435
+ shares,
1436
+ minAmount,
1437
+ Method.BroadcastTxCommit,
1438
+ );
1439
+ return encodeJson(parseToPrimitives(tx));
1440
+ } catch (error) {
1441
+ return wrappedError(error);
1442
+ }
1443
+ }