@depthhq/sdk 0.1.1 → 0.1.2

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.
@@ -0,0 +1,9 @@
1
+ import * as anchor from "@anchor-lang/core";
2
+ import { Connection, PublicKey } from "@solana/web3.js";
3
+ export type AccountState = {
4
+ id: string;
5
+ status: "uninitialized" | "opening" | "ready" | "crediting" | "withdrawing" | "moving" | "claiming";
6
+ version: bigint;
7
+ slot: number;
8
+ };
9
+ export declare function balanceState(program: anchor.Program, connection: Connection, owner: PublicKey, address: PublicKey): Promise<AccountState>;
@@ -18,3 +18,4 @@ export type ResolvedConnection = {
18
18
  environment?: DepthEnvironment;
19
19
  };
20
20
  export declare function resolveConnection(options: ConnectionOptions): Promise<ResolvedConnection>;
21
+ export declare function prepare_depth(options: ConnectionOptions): Promise<ResolvedConnection>;
@@ -32,5 +32,5 @@ export type DeploymentPolicy = {
32
32
  };
33
33
  export declare function manifestBytes(value: unknown): Buffer;
34
34
  export declare function authenticateManifest(signed: SignedManifest, trusted: PublicKey): Promise<void>;
35
- export declare function captureDeployment(provider: anchor.AnchorProvider, version: number, expiresAt: number): Promise<DeploymentManifest>;
36
- export declare function verifyDeployment(provider: anchor.AnchorProvider, policy: DeploymentPolicy): Promise<void>;
35
+ export declare function captureDeployment(provider: Pick<anchor.Provider, "connection">, version: number, expiresAt: number): Promise<DeploymentManifest>;
36
+ export declare function verifyDeployment(provider: Pick<anchor.Provider, "connection">, policy: DeploymentPolicy): Promise<void>;
@@ -51,6 +51,7 @@ export declare class DepthClient {
51
51
  private withdrawRequest;
52
52
  private accountActions;
53
53
  readonly account: (() => {
54
+ state: () => Promise<import("../account/state.js").AccountState>;
54
55
  detail: (item: Parameters<FundingDiscovery["detail"]>[0]) => Promise<import("../account/discovery.js").ReceiptDetail>;
55
56
  transaction: (item: Parameters<FundingDiscovery["transaction"]>[0]) => Promise<import("../account/discovery.js").ReceiptDetail>;
56
57
  history: (options?: Parameters<FundingDiscovery["history"]>[0]) => Promise<import("../account/discovery.js").PendingPage>;
@@ -78,6 +79,7 @@ export declare class DepthClient {
78
79
  pollMs?: number;
79
80
  }) => Promise<BalanceSnapshot>;
80
81
  }) & {
82
+ state: () => Promise<import("../account/state.js").AccountState>;
81
83
  detail: (item: Parameters<FundingDiscovery["detail"]>[0]) => Promise<import("../account/discovery.js").ReceiptDetail>;
82
84
  transaction: (item: Parameters<FundingDiscovery["transaction"]>[0]) => Promise<import("../account/discovery.js").ReceiptDetail>;
83
85
  history: (options?: Parameters<FundingDiscovery["history"]>[0]) => Promise<import("../account/discovery.js").PendingPage>;
package/dist/index.d.ts CHANGED
@@ -26,7 +26,8 @@ export { resolveAmount } from "./trade/amount.js";
26
26
  export type { AmountSelector } from "./trade/amount.js";
27
27
  export type { BalanceSnapshot, FillSnapshot, PositionSnapshot, QuoteBalanceSnapshot, } from "./view/view.js";
28
28
  export type { DepthOptions, DepthWallet, TradeInput, ReductionInput, CloseInput, ReverseInput, TradeIntent, } from "./client/types.js";
29
- export { resolveConnection } from "./chain/environment.js";
29
+ export { resolveConnection, prepare_depth } from "./chain/environment.js";
30
+ export type { AccountState } from "./account/state.js";
30
31
  export type { ConnectionOptions, ResolvedConnection, DepthEnvironment, } from "./chain/environment.js";
31
32
  export { DepthError } from "./operation/error.js";
32
33
  export { DepthProvider } from "./chain/provider.js";
package/dist/index.js CHANGED
@@ -44491,7 +44491,7 @@ async function captureDeployment(provider, version, expiresAt) {
44491
44491
  const protocol = coder.decode("protocol", protocolInfo.data);
44492
44492
  if (!market.settlementMint.equals(protocol.settlementMint))
44493
44493
  throw new Error("Deployment settlement mints disagree");
44494
- const arcium2 = getArciumProgram(provider);
44494
+ const arcium2 = new anchor2.Program(ARCIUM_IDL, provider);
44495
44495
  const mxeAddress = getMXEAccAddress(DEPTH_PROGRAM_ID);
44496
44496
  const mxe = await arcium2.account.mxeAccount.fetch(mxeAddress);
44497
44497
  if (!mxe.mxeProgramId.equals(DEPTH_PROGRAM_ID))
@@ -44521,6 +44521,7 @@ async function captureDeployment(provider, version, expiresAt) {
44521
44521
  })
44522
44522
  };
44523
44523
  }
44524
+ var verified = new WeakMap;
44524
44525
  async function verifyDeployment(provider, policy) {
44525
44526
  if ("localnet" in policy) {
44526
44527
  const url = new URL(provider.connection.rpcEndpoint);
@@ -44530,9 +44531,28 @@ async function verifyDeployment(provider, policy) {
44530
44531
  }
44531
44532
  await authenticateManifest(policy.manifest, policy.trustedSigner);
44532
44533
  const expected = policy.manifest.payload;
44533
- const actual = await captureDeployment(provider, expected.version, expected.expiresAt);
44534
- if (!manifestBytes(actual).equals(manifestBytes(expected)))
44535
- throw new Error("Signed deployment does not match the connected chain");
44534
+ const key = manifestBytes(policy.manifest).toString("base64");
44535
+ const previous = verified.get(provider.connection);
44536
+ if (previous?.key === key && previous.expiresAt > Date.now())
44537
+ return previous.task;
44538
+ const task = (async () => {
44539
+ const actual = await captureDeployment(provider, expected.version, expected.expiresAt);
44540
+ if (!manifestBytes(actual).equals(manifestBytes(expected)))
44541
+ throw new Error("Signed deployment does not match the connected chain");
44542
+ })();
44543
+ const entry = {
44544
+ key,
44545
+ expiresAt: Math.min(Date.now() + 30000, expected.expiresAt * 1000),
44546
+ task
44547
+ };
44548
+ verified.set(provider.connection, entry);
44549
+ try {
44550
+ await task;
44551
+ } catch (error) {
44552
+ if (verified.get(provider.connection) === entry)
44553
+ verified.delete(provider.connection);
44554
+ throw error;
44555
+ }
44536
44556
  }
44537
44557
 
44538
44558
  // src/chain/network.ts
@@ -44687,6 +44707,11 @@ async function resolveConnection(options) {
44687
44707
  throw unavailable("Depth deployment could not be verified", error);
44688
44708
  }
44689
44709
  }
44710
+ async function prepare_depth(options) {
44711
+ const resolved = await resolveConnection(options);
44712
+ await verifyDeployment({ connection: resolved.connection }, resolved.deployment);
44713
+ return resolved;
44714
+ }
44690
44715
 
44691
44716
  // src/chain/arcium.ts
44692
44717
  function arciumAccount(provider) {
@@ -45827,6 +45852,34 @@ class WithdrawalHandle {
45827
45852
  }
45828
45853
  }
45829
45854
 
45855
+ // src/account/state.ts
45856
+ async function balanceState(program, connection, owner, address2) {
45857
+ const { context, value: value2 } = await connection.getAccountInfoAndContext(address2, "confirmed");
45858
+ const base = { id: address2.toBase58(), slot: context.slot };
45859
+ if (!value2)
45860
+ return { ...base, status: "uninitialized", version: 0n };
45861
+ if (!value2.owner.equals(program.programId))
45862
+ throw new Error("Balance account is not owned by Depth");
45863
+ const state = program.coder.accounts.decode("balanceAccount", value2.data);
45864
+ if (!state.owner.equals(owner))
45865
+ throw new Error("Balance account does not belong to this wallet");
45866
+ const status3 = Object.keys(state.status)[0];
45867
+ if (![
45868
+ "opening",
45869
+ "ready",
45870
+ "crediting",
45871
+ "withdrawing",
45872
+ "moving",
45873
+ "claiming"
45874
+ ].includes(status3))
45875
+ throw new Error("Balance account has an invalid status");
45876
+ return {
45877
+ ...base,
45878
+ status: status3,
45879
+ version: BigInt(state.version.toString())
45880
+ };
45881
+ }
45882
+
45830
45883
  // src/account/token.ts
45831
45884
  init_buffer();
45832
45885
  import {
@@ -47051,6 +47104,7 @@ class DepthClient {
47051
47104
  }
47052
47105
  accountActions() {
47053
47106
  const actions = {
47107
+ state: () => balanceState(this.program, this.options.connection, this.options.wallet.publicKey, this.funding.balance),
47054
47108
  detail: (item) => new FundingDiscovery(this.program, this.options.connection, this.options.wallet.publicKey).detail(item),
47055
47109
  transaction: (item) => new FundingDiscovery(this.program, this.options.connection, this.options.wallet.publicKey).transaction(item),
47056
47110
  history: (options) => new FundingDiscovery(this.program, this.options.connection, this.options.wallet.publicKey).history(options),
@@ -47067,6 +47121,9 @@ class DepthClient {
47067
47121
  summary: async (options = {
47068
47122
  timeoutMs: 180000
47069
47123
  }) => {
47124
+ const state = await actions.state();
47125
+ if (state.status === "uninitialized")
47126
+ return { ...state, status: "uninitialized", amount: 0n };
47070
47127
  const view = await this.requestBalanceView();
47071
47128
  const result = await view.wait(options);
47072
47129
  if (result.status !== "pending")
@@ -47506,6 +47563,7 @@ export {
47506
47563
  verifyDeployment,
47507
47564
  resolveConnection,
47508
47565
  resolveAmount,
47566
+ prepare_depth,
47509
47567
  positionPnl,
47510
47568
  manifestBytes,
47511
47569
  create_depth_data,
@@ -1,7 +1,7 @@
1
1
  import * as anchor from "@anchor-lang/core";
2
2
  import { Connection, PublicKey } from "@solana/web3.js";
3
3
  export type BalanceSnapshot = {
4
- status: "pending" | "ready" | "stale";
4
+ status: "uninitialized" | "pending" | "ready" | "stale";
5
5
  id: string;
6
6
  version: bigint;
7
7
  slot: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@depthhq/sdk",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -44,8 +44,8 @@
44
44
  "@noble/hashes": "1.8.0",
45
45
  "@solana/web3.js": "1.99.0",
46
46
  "buffer": "6.0.3",
47
- "@depthhq/schema": "0.1.1",
48
- "@depthhq/bindings": "0.1.1"
47
+ "@depthhq/bindings": "0.1.1",
48
+ "@depthhq/schema": "0.1.1"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "node ../../scripts/build-package.mjs sdk",