@mentaproject/client 0.0.8 → 0.0.10

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.
@@ -36,7 +36,9 @@ export class Block {
36
36
  ;
37
37
  async toJSON(depth = 1) {
38
38
  return await toJSON({
39
- obj: this,
39
+ obj: {
40
+ ...this,
41
+ },
40
42
  depth
41
43
  });
42
44
  }
@@ -27,6 +27,7 @@ function convertBigintsToStrings(obj) {
27
27
  }
28
28
  export async function toJSON({ obj, depth = 0 }) {
29
29
  const data = {};
30
+ const promises = [];
30
31
  for (const key in obj) {
31
32
  // skip class related properties
32
33
  if (key === "toJSON" || key === "constructor" || key === "rpcClient")
@@ -36,34 +37,41 @@ export async function toJSON({ obj, depth = 0 }) {
36
37
  // do not fetch getters if depth is 0
37
38
  if (depth === 0)
38
39
  continue;
39
- // fetch getters
40
- const value = await obj[key]();
41
- // if the value is a class instance, convert it to JSON recursively
42
- if (typeof value === "object" && isClassInstance(value)) {
43
- data[key] = await toJSON({ obj: value, depth: depth - 1 });
44
- continue;
45
- }
46
- ;
47
- data[key] = value;
48
- 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());
49
48
  }
49
+ ;
50
50
  if (typeof prop === "object" && isClassInstance(prop)) {
51
51
  // If depth is 0, just flatten the object using recursion
52
52
  if (depth === 0) {
53
- data[key] = await toJSON({
54
- obj: prop,
55
- depth: depth - 1
56
- });
53
+ const promise = async () => {
54
+ data[key] = await toJSON({
55
+ obj: prop,
56
+ depth: depth - 1
57
+ });
58
+ };
59
+ promises.push(promise());
57
60
  continue;
58
61
  }
59
62
  ;
60
63
  // If depth is not 0, fetch the object
61
- 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());
62
68
  continue;
63
69
  }
70
+ ;
64
71
  data[key] = prop;
65
72
  }
66
73
  ;
74
+ await Promise.all(promises);
67
75
  return convertBigintsToStrings(data);
68
76
  }
69
77
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentaproject/client",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
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",
@@ -29,7 +29,7 @@
29
29
  "author": "@mentaproject",
30
30
  "license": "ISC",
31
31
  "dependencies": {
32
- "@mentaproject/contracts": "^0.0.8",
32
+ "@mentaproject/contracts": "^0.0.9",
33
33
  "@mentaproject/core": "^0.0.4",
34
34
  "@shazow/whatsabi": "^0.21.1"
35
35
  },
package/test.ts CHANGED
@@ -7,15 +7,30 @@ import { writeFileSync } from "fs";
7
7
 
8
8
  const viemAccount = privateKeyToAccount(generatePrivateKey());
9
9
 
10
+ let time = Date.now();
11
+
10
12
  const client = new MentaClient({
11
13
  account: viemAccount,
12
- transport: http("https://red-nameless-panorama.quiknode.pro/0f30278c4c66afdc9f8679975e13cd5a92c7d937/"),
14
+ transport: http("https://red-nameless-panorama.quiknode.pro/0f30278c4c66afdc9f8679975e13cd5a92c7d937/", {
15
+ batch: {
16
+ batchSize: 6,
17
+ wait: 500,
18
+ waitAsRateLimit: true
19
+ },
20
+ onFetchRequest: async (r) => {
21
+ const delay = Date.now() - time;
22
+ time = Date.now();
23
+
24
+ console.log(`>> ${r.url}\n>> ${(await r.json()).map(j => j.method).join(', ')}\n>> ${delay / 1000}s\n`)
25
+ },
26
+ // onFetchResponse: (r) => console.log(`<<: ${r.url}`),
27
+ }),
13
28
  chain: mainnet
14
29
  });
15
30
 
16
31
  (async () => {
17
- const firstTx = await client.transactions.get({ hash: "0xeae261aeada2db19fee5cc4dfd47d575ff2380a5fb6a4039685d5044fdc8e2bb" });
18
- const json = await firstTx.toJSON(3);
32
+ const firstTx = await client.transactions.get({ hash: "0x48e6f527c659c6e2c5a504864fc269599047e151dd35fc509387599fe4b82370" });
33
+ const json = await firstTx.toJSON(1);
19
34
 
20
35
  writeFileSync("./test.json", JSON.stringify(json, null, 2));
21
36
  })()