@argent/x-shared 1.43.1 → 1.44.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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("async-retry"),c=require("url-join"),o=require("./types/backend.model.cjs"),d=require("../../utils/schemas.cjs");class u{constructor(s,e){this.apiBase=s,this.httpService=e,this.TOKENS_INFO_URL="/tokens/info"}async fetchTokensInfoFromBackend(s){const e=c(this.apiBase,this.TOKENS_INFO_URL,s??"","?chain=starknet"),t=await this.httpService.get(e),n=o.apiTokensInfoResponseSchema.safeParse(t);if(n.success)return n.data.tokens;const r=o.apiTokenInfoResponseSchema.safeParse(t);if(r.success)return[r.data]}async fetchAddressTokenBalancesFromBackend(s,e,t){const n=c(this.apiBase,"activity","starknet",e,"account",s,"balance");return(await h(async a=>{let p;try{p=await this.httpService.get(n)}catch{return a(new Error("Error fetching")),[]}const i=o.apiAccountTokenBalancesSchema.safeParse(p);if(!i.success)return a(new Error("Error parsing response")),[];if(i.data.status!=="initialised")throw new Error("Not initialised yet");return i.data.balances},{retries:5,minTimeout:5e3,...t})).map(a=>({address:a.tokenAddress,balance:a.tokenBalance,networkId:e}))}async fetchDefiDecomposition(s,e){const t=c(this.apiBase,"tokens/defi",s,"investments","?chain=starknet",`&application=${e}`),n=await this.httpService.get(t);return d.safeParseAndWarn(o.apiDefiPositionsSchema,n).success||console.error("Backend schema has changed. Make the changes to the models to prevent unknown/unhandled errors."),n.dapps}}exports.TokenServiceWeb=u;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("url-join"),r=require("../../utils/retryUntilInitialised.cjs"),i=require("./types/backend.model.cjs"),h=require("../../utils/schemas.cjs");class d{constructor(n,e){this.apiBase=n,this.httpService=e,this.TOKENS_INFO_URL="/tokens/info"}async fetchTokensInfoFromBackend(n){const e=o(this.apiBase,this.TOKENS_INFO_URL,n??"","?chain=starknet"),a=await this.httpService.get(e),t=i.apiTokensInfoResponseSchema.safeParse(a);if(t.success)return t.data.tokens;const s=i.apiTokenInfoResponseSchema.safeParse(a);if(s.success)return[s.data]}async fetchAddressTokenBalancesFromBackend(n,e,a){const t=o(this.apiBase,"activity","starknet",e,"account",n,"balance"),s=await r.retryUntilInitialised(()=>this.httpService.get(t),i.apiAccountTokenBalancesSchema,a);return((s==null?void 0:s.status)==="initialised"?s.balances:[]).map(c=>({address:c.tokenAddress,balance:c.tokenBalance,networkId:e}))}async fetchDefiDecomposition(n,e){const a=o(this.apiBase,"tokens/defi",n,"investments","?chain=starknet",`&application=${e}`),t=await this.httpService.get(a);return h.safeParseAndWarn(i.apiDefiPositionsSchema,t).success||console.error("Backend schema has changed. Make the changes to the models to prevent unknown/unhandled errors."),t.dapps}}exports.TokenServiceWeb=d;
@@ -1,64 +1,42 @@
1
- import p from "async-retry";
2
- import i from "url-join";
3
- import { apiTokensInfoResponseSchema as h, apiTokenInfoResponseSchema as d, apiAccountTokenBalancesSchema as u, apiDefiPositionsSchema as f } from "./types/backend.model.js";
1
+ import o from "url-join";
2
+ import { retryUntilInitialised as c } from "../../utils/retryUntilInitialised.js";
3
+ import { apiTokensInfoResponseSchema as r, apiTokenInfoResponseSchema as p, apiDefiPositionsSchema as h, apiAccountTokenBalancesSchema as d } from "./types/backend.model.js";
4
4
  import { safeParseAndWarn as k } from "../../utils/schemas.js";
5
- class R {
6
- constructor(s, e) {
7
- this.apiBase = s, this.httpService = e, this.TOKENS_INFO_URL = "/tokens/info";
5
+ class v {
6
+ constructor(t, e) {
7
+ this.apiBase = t, this.httpService = e, this.TOKENS_INFO_URL = "/tokens/info";
8
8
  }
9
9
  /**
10
10
  * Fetch tokens info from backend
11
11
  * @param {Address} tokenAddress - The token address.
12
12
  * @returns {Promise<ApiTokenInfo[]>} - The fetched tokens or undefined if there was an error or not default network
13
13
  */
14
- async fetchTokensInfoFromBackend(s) {
15
- const e = i(this.apiBase, this.TOKENS_INFO_URL, s ?? "", "?chain=starknet"), n = await this.httpService.get(e), t = h.safeParse(n);
16
- if (t.success)
17
- return t.data.tokens;
18
- const r = d.safeParse(n);
19
- if (r.success)
20
- return [r.data];
14
+ async fetchTokensInfoFromBackend(t) {
15
+ const e = o(this.apiBase, this.TOKENS_INFO_URL, t ?? "", "?chain=starknet"), a = await this.httpService.get(e), n = r.safeParse(a);
16
+ if (n.success)
17
+ return n.data.tokens;
18
+ const s = p.safeParse(a);
19
+ if (s.success)
20
+ return [s.data];
21
21
  }
22
22
  /**
23
23
  * Fetch list of tokens and balances for given account from backend
24
24
  * @param {Address} address - The account address.
25
25
  * @returns {Promise<WebBaseTokenWithBalance[]>} - The fetched balances for the account address.
26
26
  */
27
- async fetchAddressTokenBalancesFromBackend(s, e, n) {
28
- const t = i(this.apiBase, "activity", "starknet", e, "account", s, "balance");
29
- return (await p(
30
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
- async (a) => {
32
- let c;
33
- try {
34
- c = await this.httpService.get(t);
35
- } catch {
36
- return a(new Error("Error fetching")), [];
37
- }
38
- const o = u.safeParse(c);
39
- if (!o.success)
40
- return a(new Error("Error parsing response")), [];
41
- if (o.data.status !== "initialised")
42
- throw new Error("Not initialised yet");
43
- return o.data.balances;
44
- },
45
- {
46
- /** seems to take 5-10 sec for initialised state */
47
- retries: 5,
48
- minTimeout: 5e3,
49
- ...n
50
- }
51
- )).map((a) => ({
52
- address: a.tokenAddress,
53
- balance: a.tokenBalance,
27
+ async fetchAddressTokenBalancesFromBackend(t, e, a) {
28
+ const n = o(this.apiBase, "activity", "starknet", e, "account", t, "balance"), s = await c(() => this.httpService.get(n), d, a);
29
+ return ((s == null ? void 0 : s.status) === "initialised" ? s.balances : []).map((i) => ({
30
+ address: i.tokenAddress,
31
+ balance: i.tokenBalance,
54
32
  networkId: e
55
33
  }));
56
34
  }
57
- async fetchDefiDecomposition(s, e) {
58
- const n = i(this.apiBase, "tokens/defi", s, "investments", "?chain=starknet", `&application=${e}`), t = await this.httpService.get(n);
59
- return k(f, t).success || console.error("Backend schema has changed. Make the changes to the models to prevent unknown/unhandled errors."), t.dapps;
35
+ async fetchDefiDecomposition(t, e) {
36
+ const a = o(this.apiBase, "tokens/defi", t, "investments", "?chain=starknet", `&application=${e}`), n = await this.httpService.get(a);
37
+ return k(h, n).success || console.error("Backend schema has changed. Make the changes to the models to prevent unknown/unhandled errors."), n.dapps;
60
38
  }
61
39
  }
62
40
  export {
63
- R as TokenServiceWeb
41
+ v as TokenServiceWeb
64
42
  };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("async-retry"),o=require("./schemas.cjs");async function u(n,i,s={retries:5,minTimeout:5e3}){return a(async e=>{let t=null;try{t=await n()}catch{return e(new Error("Error fetching")),null}const r=o.safeParseAndWarn(i,t);if(!r.success)return e(new Error("Error parsing response")),null;if(r.data.status!=="initialised")throw new Error("Not initialised yet");return r.data},s)}exports.retryUntilInitialised=u;
@@ -0,0 +1,16 @@
1
+ import { default as retry } from 'async-retry';
2
+ import { z } from 'zod';
3
+ type InitialisationStatus = {
4
+ status: "initialising";
5
+ } | {
6
+ status: "initialised";
7
+ };
8
+ /**
9
+ * Retries an async function until the "initialised" status is achieved or the maximum number of retries is reached.
10
+ * @param fn The async function to retry
11
+ * @param validationSchema A Zod schema to validate the response from the async function
12
+ * @param opts Retry options
13
+ * @returns The result of the successful function call, or null if the maximum number of retries is reached or an error occurs
14
+ */
15
+ export declare function retryUntilInitialised<T extends InitialisationStatus>(fn: () => Promise<T>, validationSchema: z.ZodType, opts?: retry.Options): Promise<T | null>;
16
+ export {};
@@ -0,0 +1,21 @@
1
+ import a from "async-retry";
2
+ import { safeParseAndWarn as o } from "./schemas.js";
3
+ async function c(n, i, s = { retries: 5, minTimeout: 5e3 }) {
4
+ return a(async (t) => {
5
+ let e = null;
6
+ try {
7
+ e = await n();
8
+ } catch {
9
+ return t(new Error("Error fetching")), null;
10
+ }
11
+ const r = o(i, e);
12
+ if (!r.success)
13
+ return t(new Error("Error parsing response")), null;
14
+ if (r.data.status !== "initialised")
15
+ throw new Error("Not initialised yet");
16
+ return r.data;
17
+ }, s);
18
+ }
19
+ export {
20
+ c as retryUntilInitialised
21
+ };
@@ -4,8 +4,12 @@ export declare const stringToBooleanSchema: z.ZodEffects<z.ZodString, boolean, s
4
4
  export declare function createSchemaWithWarnings<T extends z.ZodRawShape>(shape: T): z.ZodEffects<z.ZodObject<T, "passthrough", z.ZodTypeAny, z.objectOutputType<T, z.ZodTypeAny, "passthrough">, z.objectInputType<T, z.ZodTypeAny, "passthrough">>, z.objectOutputType<T, z.ZodTypeAny, "passthrough">, z.objectInputType<T, z.ZodTypeAny, "passthrough">> & {
5
5
  extend: <E extends z.ZodRawShape>(extension: E) => z.ZodEffects<z.ZodObject<T & E, "passthrough", z.ZodTypeAny, z.objectOutputType<T & E, z.ZodTypeAny, "passthrough">, z.objectInputType<T & E, z.ZodTypeAny, "passthrough">>, z.objectOutputType<T & E, z.ZodTypeAny, "passthrough">, z.objectInputType<T & E, z.ZodTypeAny, "passthrough">>;
6
6
  };
7
- export declare const safeParseAndWarn: <T extends z.ZodType>(schema: T, data: unknown) => {
8
- success: boolean;
9
- data?: z.infer<T>;
10
- error?: z.ZodError;
7
+ type ISafeParseAndWarn<T extends z.ZodType> = {
8
+ success: true;
9
+ data: z.infer<T>;
10
+ } | {
11
+ success: false;
12
+ error: z.ZodError;
11
13
  };
14
+ export declare const safeParseAndWarn: <T extends z.ZodType>(schema: T, data: unknown) => ISafeParseAndWarn<T>;
15
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@argent/x-shared",
3
- "version": "1.43.1",
3
+ "version": "1.44.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/argentlabs/x-shared.git"