@depthhq/sdk 0.1.4 → 0.1.6

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.
@@ -17,6 +17,9 @@ function randomBytes(length) {
17
17
  }
18
18
 
19
19
  // src/operation/error.ts
20
+ import { AnchorError, ProgramError } from "@anchor-lang/core";
21
+ import { SendTransactionError } from "@solana/web3.js";
22
+
20
23
  class DepthError extends Error {
21
24
  code;
22
25
  stage;
@@ -34,6 +37,17 @@ class DepthError extends Error {
34
37
  }
35
38
  }
36
39
  function submissionError(operationId, cause) {
40
+ if (cause instanceof DepthError)
41
+ throw cause;
42
+ const message = cause instanceof Error ? cause.message : String(cause);
43
+ const code = cause && typeof cause === "object" && "code" in cause ? cause.code : undefined;
44
+ if (code === 4001 && !(cause instanceof ProgramError) || /user rejected|user denied|approval rejected|request rejected by the user/i.test(message)) {
45
+ throw new DepthError("WALLET_REJECTED", "submission", "Wallet approval declined", true, "Check the existing flow before retrying with the same client nonce", operationId, { cause });
46
+ }
47
+ const rejection = cause instanceof AnchorError ? cause.error.errorMessage : cause instanceof ProgramError ? cause.msg : cause instanceof SendTransactionError ? cause.transactionError.message : undefined;
48
+ if (rejection) {
49
+ throw new DepthError("SUBMISSION_REJECTED", "submission", `Transaction rejected: ${rejection}`, true, "Check the existing flow and resolve the rejection before retrying", operationId, { cause });
50
+ }
37
51
  throw new DepthError("SUBMISSION_UNCONFIRMED", "submission", "Submission was not confirmed; reconcile the saved operation before retrying", true, "Read the operation using the saved client nonce; do not create a new nonce", operationId, { cause });
38
52
  }
39
53
 
package/dist/index.js CHANGED
@@ -44598,6 +44598,9 @@ import { DEPTH_PROGRAM_ID as DEPTH_PROGRAM_ID2 } from "@depthhq/bindings";
44598
44598
  import { PublicKey as PublicKey3 } from "@solana/web3.js";
44599
44599
 
44600
44600
  // src/operation/error.ts
44601
+ import { AnchorError, ProgramError } from "@anchor-lang/core";
44602
+ import { SendTransactionError } from "@solana/web3.js";
44603
+
44601
44604
  class DepthError extends Error {
44602
44605
  code;
44603
44606
  stage;
@@ -44615,6 +44618,17 @@ class DepthError extends Error {
44615
44618
  }
44616
44619
  }
44617
44620
  function submissionError(operationId, cause) {
44621
+ if (cause instanceof DepthError)
44622
+ throw cause;
44623
+ const message = cause instanceof Error ? cause.message : String(cause);
44624
+ const code2 = cause && typeof cause === "object" && "code" in cause ? cause.code : undefined;
44625
+ if (code2 === 4001 && !(cause instanceof ProgramError) || /user rejected|user denied|approval rejected|request rejected by the user/i.test(message)) {
44626
+ throw new DepthError("WALLET_REJECTED", "submission", "Wallet approval declined", true, "Check the existing flow before retrying with the same client nonce", operationId, { cause });
44627
+ }
44628
+ const rejection = cause instanceof AnchorError ? cause.error.errorMessage : cause instanceof ProgramError ? cause.msg : cause instanceof SendTransactionError ? cause.transactionError.message : undefined;
44629
+ if (rejection) {
44630
+ throw new DepthError("SUBMISSION_REJECTED", "submission", `Transaction rejected: ${rejection}`, true, "Check the existing flow and resolve the rejection before retrying", operationId, { cause });
44631
+ }
44618
44632
  throw new DepthError("SUBMISSION_UNCONFIRMED", "submission", "Submission was not confirmed; reconcile the saved operation before retrying", true, "Read the operation using the saved client nonce; do not create a new nonce", operationId, { cause });
44619
44633
  }
44620
44634
 
@@ -44737,7 +44751,7 @@ import * as anchor3 from "@anchor-lang/core";
44737
44751
  import { DEPTH_IDL as DEPTH_IDL2, DEPTH_PROGRAM_ID as DEPTH_PROGRAM_ID3 } from "@depthhq/bindings";
44738
44752
  import {
44739
44753
  ComputeBudgetProgram,
44740
- SendTransactionError,
44754
+ SendTransactionError as SendTransactionError2,
44741
44755
  PublicKey as PublicKey4,
44742
44756
  Transaction
44743
44757
  } from "@solana/web3.js";
@@ -44841,7 +44855,7 @@ class DepthProvider extends anchor3.AnchorProvider {
44841
44855
  });
44842
44856
  if (!failed?.meta?.err)
44843
44857
  throw error;
44844
- throw new SendTransactionError({
44858
+ throw new SendTransactionError2({
44845
44859
  action: "send",
44846
44860
  signature,
44847
44861
  transactionMessage: JSON.stringify(failed.meta.err),
@@ -47378,7 +47392,20 @@ class DepthClient {
47378
47392
  return new DepthOperation(id, undefined, this.program, this.options.connection, (order) => this.requestFillView(order));
47379
47393
  }
47380
47394
  async requestQuoteView(quote) {
47381
- return this.views.request("quote", quote);
47395
+ const record = await this.read("QuoteRecord", quote);
47396
+ if (!record.maker.equals(this.options.wallet.publicKey) || !record.market.equals(this.marketAddress) || !["active", "closing"].includes(enumName2(record.status)))
47397
+ throw new Error("Active quote does not belong to this wallet and market");
47398
+ const viewNonce = nonce();
47399
+ const view = pda([
47400
+ Buffer2.from("quote-view"),
47401
+ quote.toBuffer(),
47402
+ this.options.wallet.publicKey.toBuffer(),
47403
+ viewNonce.toArrayLike(Buffer2, "le", 8)
47404
+ ]);
47405
+ return this.requestView("quote", view, viewNonce, {
47406
+ market: this.marketAddress,
47407
+ quote
47408
+ });
47382
47409
  }
47383
47410
  async requestBalanceView() {
47384
47411
  const owner = this.options.wallet.publicKey;
@@ -47427,7 +47454,7 @@ class DepthClient {
47427
47454
  return this.requestView("fill", view, viewNonce, { order });
47428
47455
  }
47429
47456
  async requestView(kind, view, viewNonce, accounts2) {
47430
- const source = accounts2.balance ?? accounts2.marketAccount ?? accounts2.order;
47457
+ const source = accounts2.balance ?? accounts2.marketAccount ?? accounts2.order ?? accounts2.quote;
47431
47458
  if (this.options.viewAuthorization === "session" || this.options.viewAuthorization !== "wallet" && this.options.privateViews === "device" && this.options.serviceUrl)
47432
47459
  return this.views.request(kind, source);
47433
47460
  const mxePublicKey = await getMXEPublicKey(this.provider, DEPTH_PROGRAM_ID8);
@@ -47436,8 +47463,8 @@ class DepthClient {
47436
47463
  const secret = x25519.utils.randomSecretKey();
47437
47464
  const publicKey = x25519.getPublicKey(secret);
47438
47465
  const offset2 = nonce();
47439
- const queue = await this.queue(kind === "balance" ? "view_balance" : kind === "market" ? "view_market" : "view_fill", offset2);
47440
- const builder = kind === "balance" ? this.program.methods.requestBalanceView : kind === "market" ? this.program.methods.requestMarketView : this.program.methods.requestFillView;
47466
+ const queue = await this.queue(kind === "balance" ? "view_balance" : kind === "market" ? "view_market" : kind === "fill" ? "view_fill" : "view_quote", offset2);
47467
+ const builder = kind === "balance" ? this.program.methods.requestBalanceView : kind === "market" ? this.program.methods.requestMarketView : kind === "fill" ? this.program.methods.requestFillView : this.program.methods.requestQuoteView;
47441
47468
  const signature = await builder(offset2, viewNonce, Array.from(publicKey)).accountsPartial({
47442
47469
  ...queue,
47443
47470
  payer: this.options.wallet.publicKey,
@@ -47662,6 +47689,12 @@ class DepthClient {
47662
47689
  }
47663
47690
  async deferViewClose(kind, id) {
47664
47691
  const view = new PublicKey11(id);
47692
+ const info = await this.options.connection.getAccountInfo(view, "confirmed");
47693
+ if (!info?.owner.equals(DEPTH_PROGRAM_ID8))
47694
+ return;
47695
+ const receipt = this.program.coder.accounts.decode(kind === "balance" ? "balanceView" : "marketView", info.data);
47696
+ if (!receipt.owner.equals(this.options.wallet.publicKey) || "pending" in receipt.status)
47697
+ return;
47665
47698
  const method = kind === "balance" ? this.program.methods.closeBalanceView : this.program.methods.closeMarketView;
47666
47699
  this.provider.deferClose(view, await method().accountsPartial({
47667
47700
  owner: this.options.wallet.publicKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@depthhq/sdk",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },