@mentaproject/client 0.0.7 → 0.0.9

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,11 +1,12 @@
1
1
  function isClassInstance(obj) {
2
- if (obj === null || typeof obj !== 'object' || !obj.constructor) {
2
+ if (!obj)
3
+ return false;
4
+ const constructor = obj.constructor;
5
+ const nativeConstructors = [Array, Date, RegExp, Map, Set, Promise, Function, Number, String, Boolean, Error, Object];
6
+ if (nativeConstructors.includes(constructor)) {
3
7
  return false;
4
8
  }
5
- // Les constructeurs de classes ES6 commencent par "class " quand on les convertit en string.
6
- // Attention: cela peut être sensible à la minification ou à des transpilers très spécifiques,
7
- // mais c'est généralement fiable pour les classes définies avec le mot-clé `class`.
8
- return obj.constructor.toString().startsWith('class ');
9
+ return true;
9
10
  }
10
11
  ;
11
12
  function convertBigintsToStrings(obj) {
@@ -26,6 +27,7 @@ function convertBigintsToStrings(obj) {
26
27
  }
27
28
  export async function toJSON({ obj, depth = 0 }) {
28
29
  const data = {};
30
+ const promises = [];
29
31
  for (const key in obj) {
30
32
  // skip class related properties
31
33
  if (key === "toJSON" || key === "constructor" || key === "rpcClient")
@@ -35,34 +37,41 @@ export async function toJSON({ obj, depth = 0 }) {
35
37
  // do not fetch getters if depth is 0
36
38
  if (depth === 0)
37
39
  continue;
38
- // fetch getters
39
- const value = await obj[key]();
40
- // if the value is a class instance, convert it to JSON recursively
41
- if (typeof value === "object" && isClassInstance(value)) {
42
- data[key] = await toJSON({ obj: value, depth: depth - 1 });
43
- continue;
44
- }
45
- ;
46
- data[key] = value;
47
- continue;
40
+ const promise = async () => {
41
+ const value = await obj[key]();
42
+ // if the value is a class instance, convert it to JSON recursively
43
+ if (typeof value === "object" && isClassInstance(value))
44
+ return data[key] = await toJSON({ obj: value, depth: depth - 1 });
45
+ data[key] = value;
46
+ };
47
+ promises.push(promise());
48
48
  }
49
+ ;
49
50
  if (typeof prop === "object" && isClassInstance(prop)) {
50
51
  // If depth is 0, just flatten the object using recursion
51
52
  if (depth === 0) {
52
- data[key] = await toJSON({
53
- obj: prop,
54
- depth: depth - 1
55
- });
53
+ const promise = async () => {
54
+ data[key] = await toJSON({
55
+ obj: prop,
56
+ depth: depth - 1
57
+ });
58
+ };
59
+ promises.push(promise());
56
60
  continue;
57
61
  }
58
62
  ;
59
63
  // If depth is not 0, fetch the object
60
- data[key] = await prop.toJSON({ depth: depth - 1 });
64
+ const promise = async () => {
65
+ data[key] = await prop.toJSON({ depth: depth - 1 });
66
+ };
67
+ promises.push(promise());
61
68
  continue;
62
69
  }
70
+ ;
63
71
  data[key] = prop;
64
72
  }
65
73
  ;
74
+ await Promise.all(promises);
66
75
  return convertBigintsToStrings(data);
67
76
  }
68
77
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentaproject/client",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "High level EVM library used into the Menta App to facilitate Blockchain interactions. ",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/test.ts CHANGED
@@ -9,13 +9,20 @@ const viemAccount = privateKeyToAccount(generatePrivateKey());
9
9
 
10
10
  const client = new MentaClient({
11
11
  account: viemAccount,
12
- transport: http("https://red-nameless-panorama.quiknode.pro/0f30278c4c66afdc9f8679975e13cd5a92c7d937/"),
12
+ transport: http("https://red-nameless-panorama.quiknode.pro/0f30278c4c66afdc9f8679975e13cd5a92c7d937/", {
13
+ batch: {
14
+ batchSize: 15,
15
+ wait: 1000
16
+ },
17
+ onFetchRequest: (r) => console.log(`>>: ${r.url}`),
18
+ // onFetchResponse: (r) => console.log(`<<: ${r.url}`),
19
+ }),
13
20
  chain: mainnet
14
21
  });
15
22
 
16
23
  (async () => {
17
24
  const firstTx = await client.transactions.get({ hash: "0xeae261aeada2db19fee5cc4dfd47d575ff2380a5fb6a4039685d5044fdc8e2bb" });
18
- const json = await firstTx.toJSON();
25
+ const json = await firstTx.toJSON(3);
19
26
 
20
27
  writeFileSync("./test.json", JSON.stringify(json, null, 2));
21
28
  })()