@secondlayer/sdk 14.1.0 → 14.3.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.
@@ -6,10 +6,13 @@ type FetchLike = (input: string | URL | Request, init?: RequestInit) => Promise<
6
6
  interface SecondLayerOptions {
7
7
  /** Base URL of the instance API (trailing slashes are stripped). */
8
8
  baseUrl: string;
9
- /** Bearer token for authenticated instance requests (`INSTANCE_TOKEN`). */
9
+ /** Bearer token for authenticated requests. Defaults to
10
+ * `SECONDLAYER_API_KEY` on `api.secondlayer.tools`, `INSTANCE_TOKEN`
11
+ * elsewhere. */
10
12
  apiKey?: string;
11
13
  /** Hosted account key (`sk-sl_*`). Env `SECONDLAYER_API_KEY`. Used by
12
- * `sl.archive` quote/fetch/credits, never by instance `/v1` or `/api`. */
14
+ * `sl.archive` quote/fetch/credits; reaches `/v1` and `/api` only through
15
+ * `apiKey` when the base URL is the hosted API. */
13
16
  accountKey?: string;
14
17
  /** Fetch implementation. Tests and edge runtimes can provide their own. */
15
18
  fetchImpl?: FetchLike;
@@ -1840,6 +1843,37 @@ type CanonicalListParams = {
1840
1843
  toHeight?: number;
1841
1844
  };
1842
1845
  type CanonicalWalkParams = Omit<CanonicalListParams, "limit"> & WalkOptions;
1846
+ /** One MARF write a block committed, in the node's write order. Present only
1847
+ * from the height the instance's node subscribed to `state_writes`. */
1848
+ type IndexStateWrite = {
1849
+ /** `<block_height>:<ordinal>`. */
1850
+ cursor: string;
1851
+ block_height: number;
1852
+ /** Position in the block's write order: a separate clock from
1853
+ * `event_index`. */
1854
+ ordinal: number;
1855
+ /** Null for block-level writes. */
1856
+ tx_index: number | null;
1857
+ /** Full MARF key, e.g. `vm::<contract>::0::map::<hex>`. */
1858
+ key: string;
1859
+ value_hex: string;
1860
+ };
1861
+ type StateWritesEnvelope = {
1862
+ state_writes: IndexStateWrite[];
1863
+ next_cursor: string | null;
1864
+ tip: IndexTip;
1865
+ reorgs: IndexReorg[];
1866
+ };
1867
+ type StateWritesListParams = {
1868
+ cursor?: string | null;
1869
+ fromCursor?: string | null;
1870
+ limit?: number;
1871
+ fromHeight?: number;
1872
+ toHeight?: number;
1873
+ /** One block. Excludes `fromHeight`/`toHeight`; a cursor must sit at it. */
1874
+ blockHeight?: number;
1875
+ };
1876
+ type StateWritesWalkParams = Omit<StateWritesListParams, "limit"> & WalkOptions;
1843
1877
  /** A block resource. Metadata is intentionally thin — only chain-linkage and
1844
1878
  * burn-anchor fields are persisted (no miner / tx_count / signer). */
1845
1879
  type IndexBlock = {
@@ -2851,6 +2885,11 @@ declare class Index extends BaseClient {
2851
2885
  list: (params?: CanonicalListParams) => Promise<CanonicalEnvelope>;
2852
2886
  walk: (params?: CanonicalWalkParams) => AsyncIterable<IndexCanonicalBlock>;
2853
2887
  };
2888
+ /** Exact MARF writes per canonical block (`/v1/index/state-writes`). */
2889
+ readonly stateWrites: {
2890
+ list: (params?: StateWritesListParams) => Promise<StateWritesEnvelope>;
2891
+ walk: (params?: StateWritesWalkParams) => AsyncIterable<IndexStateWrite>;
2892
+ };
2854
2893
  /** Canonical blocks: paginated `list`/`walk`, plus `get` by height or hash
2855
2894
  * (resolves to null on 404). */
2856
2895
  readonly blocks: {
@@ -2920,6 +2959,8 @@ declare class Index extends BaseClient {
2920
2959
  private walkContractCalls;
2921
2960
  private listCanonical;
2922
2961
  private walkCanonical;
2962
+ private listStateWrites;
2963
+ private walkStateWrites;
2923
2964
  private listBlocks;
2924
2965
  private getBlock;
2925
2966
  private walkBlocks;
@@ -208,13 +208,20 @@ function resolveBaseUrl(explicit) {
208
208
  }
209
209
  return LOCAL_API_URL;
210
210
  }
211
- function resolveApiKey(apiKey) {
211
+ function isHostedApiUrl(url) {
212
+ try {
213
+ return new URL(url).hostname === "api.secondlayer.tools";
214
+ } catch {
215
+ return false;
216
+ }
217
+ }
218
+ function resolveApiKey(apiKey, baseUrl) {
212
219
  if (apiKey !== undefined)
213
220
  return apiKey;
214
221
  if (typeof process === "undefined")
215
222
  return;
216
- const token = process.env?.INSTANCE_TOKEN || undefined;
217
- return token;
223
+ const name = baseUrl !== undefined && isHostedApiUrl(baseUrl) ? ACCOUNT_KEY_ENV : INSTANCE_TOKEN_ENV;
224
+ return process.env?.[name] || undefined;
218
225
  }
219
226
  function resolveAccountKey(accountKey) {
220
227
  if (accountKey !== undefined)
@@ -272,7 +279,7 @@ class BaseClient {
272
279
  etagCache;
273
280
  constructor(options = {}) {
274
281
  this.baseUrl = resolveBaseUrl(options.baseUrl);
275
- this.apiKey = resolveApiKey(options.apiKey);
282
+ this.apiKey = resolveApiKey(options.apiKey, this.baseUrl);
276
283
  this.origin = options.origin ?? "cli";
277
284
  this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
278
285
  this.fetchImpl = options.fetchImpl ?? ((...args) => fetch(...args));
@@ -1704,6 +1711,10 @@ class Index extends BaseClient {
1704
1711
  list: (params = {}) => this.listCanonical(params),
1705
1712
  walk: (params = {}) => this.walkCanonical(params)
1706
1713
  };
1714
+ stateWrites = {
1715
+ list: (params = {}) => this.listStateWrites(params),
1716
+ walk: (params = {}) => this.walkStateWrites(params)
1717
+ };
1707
1718
  blocks = {
1708
1719
  list: (params = {}) => this.listBlocks(params),
1709
1720
  walk: (params = {}) => this.walkBlocks(params),
@@ -1899,6 +1910,19 @@ class Index extends BaseClient {
1899
1910
  walkCanonical(params = {}) {
1900
1911
  return this.keysetWalk(params, (page) => this.listCanonical({ ...params, ...page }), (e) => e.canonical);
1901
1912
  }
1913
+ async listStateWrites(params = {}) {
1914
+ return this.request("GET", `/v1/index/state-writes${buildQuery({
1915
+ ...indexPageQuery(params),
1916
+ block_height: params.blockHeight
1917
+ })}`, undefined, { signal: params.signal });
1918
+ }
1919
+ walkStateWrites(params = {}) {
1920
+ return this.keysetWalk(params, (page) => this.listStateWrites({
1921
+ ...params,
1922
+ ...page,
1923
+ fromHeight: params.blockHeight === undefined ? page.fromHeight : undefined
1924
+ }), (e) => e.state_writes);
1925
+ }
1902
1926
  async listBlocks(params = {}) {
1903
1927
  return this.request("GET", `/v1/index/blocks${buildQuery(indexPageQuery(params))}`, undefined, { signal: params.signal });
1904
1928
  }
@@ -2303,7 +2327,7 @@ function isLoopbackUrl(baseUrl) {
2303
2327
  }
2304
2328
  function createStreamsClient(options) {
2305
2329
  const baseUrl = normalizeBaseUrl(resolveBaseUrl(options.baseUrl));
2306
- const apiKey = resolveApiKey(options.apiKey);
2330
+ const apiKey = resolveApiKey(options.apiKey, baseUrl);
2307
2331
  const origin = options.origin ?? "cli";
2308
2332
  const authHeaders = () => ({
2309
2333
  "x-sl-origin": origin,
@@ -2799,5 +2823,5 @@ export {
2799
2823
  getSubgraph
2800
2824
  };
2801
2825
 
2802
- //# debugId=553F4BC532FBAFDA64756E2164756E21
2826
+ //# debugId=3252B7843081BB3A64756E2164756E21
2803
2827
  //# sourceMappingURL=index.js.map