@mentaproject/client 0.0.4 → 0.0.5

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.
@@ -18,7 +18,7 @@ export declare class Account {
18
18
  * @returns
19
19
  */
20
20
  isContract(): Promise<boolean>;
21
- getContractType(): Promise<any>;
21
+ contractType(): Promise<any>;
22
22
  /**
23
23
  * Sends native cryptocurrency (ETH) to the account.
24
24
  * @param amount The amount of ETH to send (in wei).
@@ -29,12 +29,12 @@ export declare class Account {
29
29
  * Gets the native cryptocurrency (ETH) balance of the account.
30
30
  * @returns A Promise that resolves to the ETH balance (in wei).
31
31
  */
32
- getETHBalance(): Promise<bigint>;
32
+ ETHBalance(): Promise<bigint>;
33
33
  /**
34
34
  * Gets the transaction count (nonce) for the account.
35
35
  * @returns A Promise that resolves to the transaction count.
36
36
  **/
37
- getTransactionCount(): Promise<number>;
37
+ transactionCount(): Promise<number>;
38
38
  /**
39
39
  * Get all transactions involving this account in the given block range.
40
40
  * NOTE: This method relies on the non-standard 'trace_filter' method of the RPC. It may not be supported by all nodes.
@@ -43,5 +43,6 @@ export declare class Account {
43
43
  *
44
44
  * @returns A paginated result containing the transaction hashes.
45
45
  */
46
- getTransactions(toRetreive: number): Promise<GetTransactionsReturnType>;
46
+ transactions(toRetreive?: number): Promise<GetTransactionsReturnType>;
47
+ toJSON(depth?: number): Promise<any>;
47
48
  }
@@ -2,6 +2,7 @@ import { createBlockRangePager, getBalance, getBlockNumber, getCode, getTransact
2
2
  import { Transaction } from "./Transaction";
3
3
  import { toHex } from "@mentaproject/core/utils";
4
4
  import { getContractType } from "@mentaproject/contracts";
5
+ import { toJSON } from "src/utils/toJSON";
5
6
  /**
6
7
  * Represents an account with methods for interacting with it.
7
8
  */
@@ -26,7 +27,10 @@ export class Account {
26
27
  return true;
27
28
  }
28
29
  ;
29
- async getContractType() {
30
+ async contractType() {
31
+ const isContract = await this.isContract();
32
+ if (!isContract)
33
+ return undefined;
30
34
  return await getContractType(this.rpcClient, this.address);
31
35
  }
32
36
  ;
@@ -50,7 +54,7 @@ export class Account {
50
54
  * Gets the native cryptocurrency (ETH) balance of the account.
51
55
  * @returns A Promise that resolves to the ETH balance (in wei).
52
56
  */
53
- async getETHBalance() {
57
+ async ETHBalance() {
54
58
  // Assurez-vous que rpcClient est bien un PublicClient ou WalletClient
55
59
  return await getBalance(this.rpcClient, {
56
60
  address: this.address,
@@ -61,7 +65,7 @@ export class Account {
61
65
  * Gets the transaction count (nonce) for the account.
62
66
  * @returns A Promise that resolves to the transaction count.
63
67
  **/
64
- async getTransactionCount() {
68
+ async transactionCount() {
65
69
  return await getTransactionCount(this.rpcClient, { address: this.address });
66
70
  }
67
71
  /**
@@ -72,7 +76,7 @@ export class Account {
72
76
  *
73
77
  * @returns A paginated result containing the transaction hashes.
74
78
  */
75
- async getTransactions(toRetreive) {
79
+ async transactions(toRetreive = 10) {
76
80
  const currentBlock = await getBlockNumber(this.rpcClient);
77
81
  const onBlockRange = async ({ fromBlock, toBlock }) => {
78
82
  const outgoing = await traceFilter(this.rpcClient, {
@@ -99,5 +103,18 @@ export class Account {
99
103
  itemsPerPage: toRetreive
100
104
  }, onBlockRange);
101
105
  }
106
+ ;
107
+ async toJSON(depth = 1) {
108
+ return await toJSON({
109
+ obj: {
110
+ ...this,
111
+ isContract: this.isContract,
112
+ contractType: this.contractType,
113
+ ETHBalance: this.ETHBalance,
114
+ transactionCount: this.transactionCount,
115
+ },
116
+ depth
117
+ });
118
+ }
102
119
  }
103
120
  ;
@@ -33,4 +33,5 @@ export declare class Block implements Omit<IBlockData, "miner" | "transactions">
33
33
  miner: Account;
34
34
  transactions?: Transaction[];
35
35
  constructor(rpcClient: CoreClient, data: IBlockData, includeTransactions?: boolean);
36
+ toJSON(depth?: number): Promise<any>;
36
37
  }
@@ -1,5 +1,6 @@
1
1
  import { Account } from "./Account";
2
2
  import { Transaction } from "./Transaction";
3
+ import { toJSON } from "src/utils/toJSON";
3
4
  export class Block {
4
5
  constructor(rpcClient, data, includeTransactions = false) {
5
6
  this.rpcClient = rpcClient;
@@ -33,5 +34,11 @@ export class Block {
33
34
  this.transactions = data.transactions.map((txData) => new Transaction(this.rpcClient, txData));
34
35
  }
35
36
  ;
37
+ async toJSON(depth = 1) {
38
+ return await toJSON({
39
+ obj: this,
40
+ depth
41
+ });
42
+ }
36
43
  }
37
44
  ;
@@ -24,7 +24,7 @@ export declare class Transaction implements Omit<ITransactionData, "from" | "to"
24
24
  from?: Account;
25
25
  to?: Account;
26
26
  constructor(rpcClient: CoreClient, data: ITransactionData);
27
- getCalls(): Promise<{
27
+ calls(): Promise<{
28
28
  from: Account;
29
29
  to: Account;
30
30
  value: bigint;
@@ -35,4 +35,5 @@ export declare class Transaction implements Omit<ITransactionData, "from" | "to"
35
35
  waitForReceipt(confirmations?: number): Promise<WaitForTransactionReceiptReturnType>;
36
36
  receipt(): Promise<WaitForTransactionReceiptReturnType>;
37
37
  block(): Promise<Block>;
38
+ toJSON(depth?: number): Promise<any>;
38
39
  }
@@ -2,6 +2,7 @@ import { Account } from './Account';
2
2
  import { Block } from './Block';
3
3
  import { getBlock, getTransactionReceipt, traceTransaction, waitForTransactionReceipt } from '@mentaproject/core/actions';
4
4
  import { hexToBigInt } from '@mentaproject/core/utils';
5
+ import { toJSON } from 'src/utils/toJSON';
5
6
  export class Transaction {
6
7
  constructor(rpcClient, data) {
7
8
  this.rpcClient = rpcClient;
@@ -26,7 +27,7 @@ export class Transaction {
26
27
  this.to = data.to ? new Account(this.rpcClient, data.to) : undefined;
27
28
  }
28
29
  ;
29
- async getCalls() {
30
+ async calls() {
30
31
  const traces = await traceTransaction(this.rpcClient, this.hash);
31
32
  const calls = traces.filter(t => t.type === "call" && t.action !== undefined);
32
33
  return calls.map(c => ({
@@ -54,5 +55,19 @@ export class Transaction {
54
55
  return new Block(this.rpcClient, data);
55
56
  }
56
57
  ;
58
+ async toJSON(depth = 1) {
59
+ return await toJSON({
60
+ obj: {
61
+ ...this,
62
+ block: this.block,
63
+ receipt: this.receipt,
64
+ calls: async () => {
65
+ const calls = await this.calls();
66
+ return Promise.all(calls.map(async (c) => await toJSON({ obj: c, depth: depth })));
67
+ }
68
+ },
69
+ depth
70
+ });
71
+ }
57
72
  }
58
73
  ;
@@ -0,0 +1,9 @@
1
+ export interface IToJSONParams<T extends {
2
+ [key: string]: any;
3
+ } = {
4
+ [key: string]: any;
5
+ }> {
6
+ obj: T;
7
+ depth?: number;
8
+ }
9
+ export declare function toJSON({ obj, depth }: IToJSONParams): Promise<any>;
@@ -0,0 +1,71 @@
1
+ function isClassInstance(obj) {
2
+ if (obj === null || typeof obj !== 'object' || !obj.constructor) {
3
+ return false;
4
+ }
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
+ }
10
+ ;
11
+ function convertBigintsToStrings(obj) {
12
+ if (typeof obj === 'bigint') {
13
+ return obj.toString();
14
+ }
15
+ if (typeof obj === 'object') {
16
+ for (const key in obj) {
17
+ obj[key] = convertBigintsToStrings(obj[key]);
18
+ }
19
+ }
20
+ if (Array.isArray(obj)) {
21
+ for (let i = 0; i < obj.length; i++) {
22
+ obj[i] = convertBigintsToStrings(obj[i]);
23
+ }
24
+ }
25
+ return obj;
26
+ }
27
+ export async function toJSON({ obj, depth = 0 }) {
28
+ // copy the properties of the object
29
+ const data = Object.assign({}, obj);
30
+ for (const key in obj) {
31
+ // skip class related properties
32
+ if (key === "toJSON" || key === "constructor" || key === "rpcClient") {
33
+ delete data[key];
34
+ continue;
35
+ }
36
+ ;
37
+ const prop = obj[key];
38
+ if (typeof prop === "function") {
39
+ // do not fetch getters if depth is 0
40
+ if (depth === 0) {
41
+ delete data[key];
42
+ continue;
43
+ }
44
+ // fetch getters
45
+ const value = await obj[key]();
46
+ // if the value is a class instance, convert it to JSON recursively
47
+ if (typeof value === "object" && isClassInstance(value)) {
48
+ data[key] = await toJSON({ obj: value, depth: depth - 1 });
49
+ continue;
50
+ }
51
+ data[key] = value;
52
+ continue;
53
+ }
54
+ if (typeof prop === "object" && isClassInstance(prop)) {
55
+ // If depth is 0, just flatten the object using recursion
56
+ if (depth === 0) {
57
+ data[key] = await toJSON({
58
+ obj: prop,
59
+ depth: depth - 1
60
+ });
61
+ continue;
62
+ }
63
+ ;
64
+ // If depth is not 0, fetch the object
65
+ data[key] = await prop.toJSON({ depth: depth - 1 });
66
+ }
67
+ }
68
+ ;
69
+ return convertBigintsToStrings(data);
70
+ }
71
+ ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentaproject/client",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
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",
@@ -37,6 +37,7 @@
37
37
  "@rollup/plugin-commonjs": "^28.0.3",
38
38
  "@rollup/plugin-node-resolve": "^16.0.1",
39
39
  "@rollup/plugin-typescript": "^12.1.2",
40
+ "@types/node": "^22.15.29",
40
41
  "rollup": "^4.40.1",
41
42
  "tslib": "^2.8.1",
42
43
  "tsx": "^4.19.4",
package/test.ts CHANGED
@@ -3,6 +3,8 @@ import { MentaClient } from "./src";
3
3
  import { privateKeyToAccount, generatePrivateKey } from "viem/accounts"
4
4
  import { mainnet } from "@mentaproject/core/chains";
5
5
 
6
+ import { writeFileSync } from "fs";
7
+
6
8
  const viemAccount = privateKeyToAccount(generatePrivateKey());
7
9
 
8
10
  const client = new MentaClient({
@@ -13,16 +15,7 @@ const client = new MentaClient({
13
15
 
14
16
  (async () => {
15
17
  const firstTx = await client.transactions.get({ hash: "0xeae261aeada2db19fee5cc4dfd47d575ff2380a5fb6a4039685d5044fdc8e2bb" });
16
- const calls = await firstTx.getCalls();
18
+ const json = await firstTx.toJSON(2);
17
19
 
18
- for (const call of calls) {
19
- const contract = call.to;
20
- console.log(`Calling ${contract.address}`);
21
- const isContract = await contract.isContract();
22
- console.log(isContract);
23
- if (!isContract) continue;
24
- const contractType = await contract.getContractType();
25
- console.log(contractType);
26
- console.log(`Contract ${contract.address} detected as ${Object.keys(contractType).join(", ")}`);
27
- }
20
+ writeFileSync("./test.json", JSON.stringify(json, null, 2));
28
21
  })()