@dydxprotocol/v4-client-js 0.30.0 → 0.31.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.
@@ -8,7 +8,9 @@ import {
8
8
  getAccountBalances,
9
9
  getUserStats,
10
10
  simulateDeposit,
11
+ simulateTransferNativeToken,
11
12
  simulateWithdraw,
13
+ transferNativeToken,
12
14
  withdraw,
13
15
  withdrawToIBC,
14
16
  wrappedError,
@@ -20,13 +22,24 @@ async function test(): Promise<void> {
20
22
  const wallet = await connectWallet(DYDX_TEST_MNEMONIC);
21
23
  console.log(wallet);
22
24
 
23
- const address = await connect(Environment.staging, DYDX_TEST_MNEMONIC);
25
+ const address = await connect(Environment.testnet, DYDX_TEST_MNEMONIC);
24
26
  console.log(address);
25
27
 
26
28
  const payload = `{ "address": "${DYDX_TEST_ADDRESS}" }`;
27
29
  const userStats = await getUserStats(payload);
28
30
  console.log(userStats);
29
31
 
32
+ const sendTokenPayload = {
33
+ subaccountNumber: 0,
34
+ amount: 10, // Dydx Token
35
+ recipient: 'dydx15ndn9c895f8ntck25qughtuck9spv2d9svw5qx',
36
+ };
37
+ const fees = await simulateTransferNativeToken(JSON.stringify(sendTokenPayload));
38
+ console.log(fees);
39
+
40
+ let tx = await transferNativeToken(JSON.stringify(sendTokenPayload));
41
+ console.log(tx);
42
+
30
43
  let balances = await getAccountBalances();
31
44
  console.log(balances);
32
45
 
@@ -41,7 +54,7 @@ async function test(): Promise<void> {
41
54
  subaccountNumber: 0,
42
55
  amount: 20,
43
56
  };
44
- let tx = await withdraw(JSON.stringify(withdrawlPayload));
57
+ tx = await withdraw(JSON.stringify(withdrawlPayload));
45
58
  console.log(tx);
46
59
 
47
60
  balances = await getAccountBalances();
@@ -66,6 +66,7 @@ async function test(): Promise<void> {
66
66
  TEST_RECIPIENT_ADDRESS,
67
67
  DYDX_DENOM,
68
68
  amountAfterFee,
69
+ false,
69
70
  Method.BroadcastTxCommit,
70
71
  );
71
72
  console.log('**Send**');
@@ -57,6 +57,7 @@ async function test(): Promise<void> {
57
57
  0,
58
58
  amountAfterFee,
59
59
  TEST_RECIPIENT_ADDRESS,
60
+ false,
60
61
  Method.BroadcastTxCommit,
61
62
  );
62
63
  console.log('**Withdraw and Send**');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dydxprotocol/v4-client-js",
3
- "version": "0.30.0",
3
+ "version": "0.31.0",
4
4
  "description": "General client library for the new dYdX system (v4 decentralized)",
5
5
  "main": "build/src/index.js",
6
6
  "scripts": {
@@ -0,0 +1,4 @@
1
+ #!/bin/sh
2
+
3
+ npm run webpack
4
+ cp __native__/__ios__/v4-native-client.js ~/native-ios-v4/dydx/dydxPresenters/dydxPresenters/_Features
@@ -12,7 +12,7 @@ if [ $? -eq 0 ]; then
12
12
  git config --global user.name "github_actions"
13
13
 
14
14
  # Get version and tag
15
- git tag v4-client-js ${VERSION}
15
+ git tag v4-client-js@${VERSION}
16
16
  git push --tags
17
17
 
18
18
  npm publish
@@ -6,6 +6,7 @@ import protobuf from 'protobufjs';
6
6
 
7
7
  import { OrderFlags } from '../types';
8
8
  import {
9
+ DYDX_DENOM,
9
10
  GAS_PRICE,
10
11
  Network, OrderExecution, OrderSide, OrderTimeInForce, OrderType,
11
12
  } from './constants';
@@ -541,6 +542,32 @@ export class CompositeClient {
541
542
  );
542
543
  }
543
544
 
545
+ /**
546
+ * @description Create message to send dydx token from subaccount to wallet
547
+ * with human readable input.
548
+ *
549
+ * @param subaccount The subaccount to withdraw from
550
+ * @param amount The amount to withdraw
551
+ * @param recipient The recipient address
552
+ *
553
+ * @throws UnexpectedClientError if a malformed response is returned with no GRPC error
554
+ * at any point.
555
+ * @returns The message
556
+ */
557
+ sendTokenMessage(
558
+ subaccount: Subaccount,
559
+ amount: number,
560
+ recipient: string,
561
+ ): EncodeObject {
562
+ const quantums: Long = Long.fromNumber(amount * (10 ** 6));
563
+ return this.validatorClient.post.composer.composeMsgSendToken(
564
+ subaccount.address,
565
+ recipient,
566
+ DYDX_DENOM,
567
+ quantums,
568
+ );
569
+ }
570
+
544
571
  async signPlaceOrder(
545
572
  subaccount: Subaccount,
546
573
  marketId: string,
@@ -148,6 +148,8 @@ export function calculateOrderFlags(type: OrderType, timeInForce?: OrderTimeInFo
148
148
  export function calculateClientMetadata(orderType: OrderType): number {
149
149
  switch (orderType) {
150
150
  case OrderType.MARKET:
151
+ case OrderType.STOP_MARKET:
152
+ case OrderType.TAKE_PROFIT_MARKET:
151
153
  return 1;
152
154
 
153
155
  default:
@@ -441,6 +441,7 @@ export class Post {
441
441
  assetId: number,
442
442
  quantums: Long,
443
443
  recipient?: string,
444
+ zeroFee: boolean = true,
444
445
  broadcastMode?: BroadcastMode,
445
446
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
446
447
  const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
@@ -453,7 +454,7 @@ export class Post {
453
454
  );
454
455
  resolve([msg]);
455
456
  });
456
- return this.send(subaccount.wallet, () => msgs, true, undefined, undefined, broadcastMode);
457
+ return this.send(subaccount.wallet, () => msgs, zeroFee, undefined, undefined, broadcastMode);
457
458
  }
458
459
 
459
460
  async sendToken(
@@ -461,6 +462,7 @@ export class Post {
461
462
  recipient: string,
462
463
  coinDenom: string,
463
464
  quantums: Long,
465
+ zeroFee: boolean = true,
464
466
  broadcastMode?: BroadcastMode,
465
467
  ): Promise<BroadcastTxAsyncResponse | BroadcastTxSyncResponse | IndexedTx> {
466
468
  const msgs: Promise<EncodeObject[]> = new Promise((resolve) => {
@@ -475,7 +477,7 @@ export class Post {
475
477
  return this.send(
476
478
  subaccount.wallet,
477
479
  () => msgs,
478
- false,
480
+ zeroFee,
479
481
  coinDenom === DYDX_DENOM ? GAS_PRICE_DYDX_DENOM : GAS_PRICE,
480
482
  undefined,
481
483
  broadcastMode,
@@ -8,7 +8,7 @@ import { Order_Side, Order_TimeInForce } from '@dydxprotocol/dydxjs/src/codegen/
8
8
  import * as AuthModule from 'cosmjs-types/cosmos/auth/v1beta1/query';
9
9
  import Long from 'long';
10
10
 
11
- import { BECH32_PREFIX, USDC_DENOM } from '../lib/constants';
11
+ import { BECH32_PREFIX, GAS_PRICE_DYDX_DENOM, USDC_DENOM } from '../lib/constants';
12
12
  import { UserError } from '../lib/errors';
13
13
  import { encodeJson } from '../lib/helpers';
14
14
  import { deriveHDKeyFromEthereumSignature } from '../lib/onboarding';
@@ -360,6 +360,7 @@ export async function withdraw(
360
360
  const tx = await client.withdrawFromSubaccount(
361
361
  subaccount,
362
362
  amount,
363
+ json.recipient,
363
364
  );
364
365
  return encodeJson(tx);
365
366
  } catch (error) {
@@ -444,6 +445,53 @@ export async function withdrawToIBC(
444
445
  }
445
446
  }
446
447
 
448
+ export async function transferNativeToken(
449
+ payload: string,
450
+ ): Promise<string> {
451
+ try {
452
+ const client = globalThis.client;
453
+ if (client === undefined) {
454
+ throw new UserError('client is not connected. Call connectClient() first');
455
+ }
456
+ const wallet = globalThis.wallet;
457
+ if (wallet === undefined) {
458
+ throw new UserError('wallet is not set. Call connectWallet() first');
459
+ }
460
+
461
+ const json = JSON.parse(payload);
462
+ const subaccountNumber = json.subaccountNumber;
463
+ if (subaccountNumber === undefined) {
464
+ throw new UserError('subaccountNumber is not set');
465
+ }
466
+ const amount = json.amount;
467
+ if (amount === undefined) {
468
+ throw new UserError('amount is not set');
469
+ }
470
+
471
+ const subaccount = new Subaccount(wallet, subaccountNumber);
472
+ const msg: EncodeObject = client.sendTokenMessage(
473
+ subaccount,
474
+ amount,
475
+ json.recipient,
476
+ );
477
+ const msgs = [msg];
478
+ const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
479
+
480
+ const tx = await client.send(
481
+ wallet,
482
+ () => {
483
+ return encodeObjects;
484
+ },
485
+ false,
486
+ GAS_PRICE_DYDX_DENOM,
487
+ undefined,
488
+ );
489
+ return encodeJson(tx);
490
+ } catch (error) {
491
+ return wrappedError(error);
492
+ }
493
+ }
494
+
447
495
  export async function getAccountBalance(): Promise<String> {
448
496
  try {
449
497
  const client = globalThis.client;
@@ -567,6 +615,7 @@ export async function simulateWithdraw(
567
615
  const msg: EncodeObject = client.withdrawFromSubaccountMessage(
568
616
  subaccount,
569
617
  amount,
618
+ json.recipient,
570
619
  );
571
620
  const msgs: EncodeObject[] = [msg];
572
621
  const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
@@ -579,6 +628,53 @@ export async function simulateWithdraw(
579
628
  }
580
629
  }
581
630
 
631
+ export async function simulateTransferNativeToken(
632
+ payload: string,
633
+ ): Promise<string> {
634
+ try {
635
+ const client = globalThis.client;
636
+ if (client === undefined) {
637
+ throw new UserError('client is not connected. Call connectClient() first');
638
+ }
639
+ const wallet = globalThis.wallet;
640
+ if (wallet === undefined) {
641
+ throw new UserError('wallet is not set. Call connectWallet() first');
642
+ }
643
+ const json = JSON.parse(payload);
644
+ const subaccountNumber = json.subaccountNumber;
645
+ if (subaccountNumber === undefined) {
646
+ throw new UserError('subaccountNumber is not set');
647
+ }
648
+ const recipient = json.recipient;
649
+ if (recipient === undefined) {
650
+ throw new UserError('recipient is not set');
651
+ }
652
+ const amount = json.amount;
653
+ if (amount === undefined) {
654
+ throw new UserError('amount is not set');
655
+ }
656
+
657
+ const subaccount = new Subaccount(wallet, subaccountNumber);
658
+ const msg: EncodeObject = client.sendTokenMessage(
659
+ subaccount,
660
+ amount,
661
+ json.recipient,
662
+ );
663
+ const msgs: EncodeObject[] = [msg];
664
+ const encodeObjects: Promise<EncodeObject[]> = new Promise((resolve) => resolve(msgs));
665
+ const stdFee = await client.simulate(
666
+ globalThis.wallet,
667
+ () => {
668
+ return encodeObjects;
669
+ },
670
+ GAS_PRICE_DYDX_DENOM,
671
+ );
672
+ return encodeJson(stdFee);
673
+ } catch (error) {
674
+ return wrappedError(error);
675
+ }
676
+ }
677
+
582
678
  export async function signRawPlaceOrder(
583
679
  subaccountNumber: number,
584
680
  clientId: number,