@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.
- package/dist/src/tokens/service/implementation.cjs +1 -1
- package/dist/src/tokens/service/implementation.js +22 -44
- package/dist/src/utils/retryUntilInitialised.cjs +1 -0
- package/dist/src/utils/retryUntilInitialised.d.ts +16 -0
- package/dist/src/utils/retryUntilInitialised.js +21 -0
- package/dist/src/utils/schemas.d.ts +8 -4
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
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
|
|
2
|
-
import
|
|
3
|
-
import { apiTokensInfoResponseSchema as
|
|
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
|
|
6
|
-
constructor(
|
|
7
|
-
this.apiBase =
|
|
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(
|
|
15
|
-
const e =
|
|
16
|
-
if (
|
|
17
|
-
return
|
|
18
|
-
const
|
|
19
|
-
if (
|
|
20
|
-
return [
|
|
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(
|
|
28
|
-
const
|
|
29
|
-
return (
|
|
30
|
-
|
|
31
|
-
|
|
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(
|
|
58
|
-
const
|
|
59
|
-
return k(
|
|
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
|
-
|
|
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
|
-
|
|
8
|
-
success:
|
|
9
|
-
data
|
|
10
|
-
|
|
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 {};
|