@mentaproject/client 0.1.18 → 0.1.20

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/index.d.ts CHANGED
@@ -16,3 +16,4 @@ export * from './managers';
16
16
  * Re-exports the Cache structure for managing cached data.
17
17
  */
18
18
  export * from './structures/Cache';
19
+ export * from "./utils/bigint";
package/dist/index.js CHANGED
@@ -16,3 +16,4 @@ export * from './managers';
16
16
  * Re-exports the Cache structure for managing cached data.
17
17
  */
18
18
  export * from './structures/Cache';
19
+ export * from "./utils/bigint";
@@ -1,6 +1,6 @@
1
- import { Address, Transaction as RawTransaction } from '@mentaproject/core/types';
1
+ import { Address } from '@mentaproject/core/types';
2
2
  import { Account } from '../structures/Account';
3
- import { MentaClient } from '../structures';
3
+ import { MentaClient, Transaction } from '../structures';
4
4
  import { GetTransactionsParams, IPersistenceAdapter } from 'src/types';
5
5
  /**
6
6
  * Manages the persistence of data, including transactions, using a specified persistence adapter.
@@ -20,7 +20,7 @@ export declare class PersistenceManager {
20
20
  * @param params - Parameters for retrieving transactions.
21
21
  * @returns A promise that resolves to an array of transactions with associated account information.
22
22
  */
23
- getTransactions(address: Address, params: GetTransactionsParams): Promise<RawTransaction[]>;
23
+ getTransactions(address: Address, params: GetTransactionsParams): Promise<Transaction[]>;
24
24
  /**
25
25
  * Synchronizes an account's transactions from the remote source to the local persistence.
26
26
  * It fetches new transactions starting from the last synced block and updates the local storage.
@@ -1,9 +1,10 @@
1
1
  /**
2
2
  * @module TransactionManager
3
3
  */
4
- import { GetTransactionParameters, Transaction as RawTransaction, SendTransactionParameters } from "@mentaproject/core/types";
4
+ import { GetTransactionParameters, SendTransactionParameters } from "@mentaproject/core/types";
5
5
  import { Transaction } from "../structures/Transaction";
6
6
  import { MentaClient } from "../structures";
7
+ import { JSONTransaction } from "src/types";
7
8
  /**
8
9
  * Manages blockchain transaction-related operations, providing methods to retrieve and send transactions.
9
10
  * @class
@@ -34,20 +35,12 @@ export declare class TransactionManager {
34
35
  */
35
36
  get(params: GetTransactionParameters): Promise<Transaction>;
36
37
  /**
37
- * Parses a transaction object from a raw transaction data object.
38
- * @description This method takes a raw transaction data object and returns a Transaction instance.
39
- * @param {ITransactionData} params - The raw transaction data object.
40
- * @returns {Transaction} A Transaction instance representing the parsed transaction.
41
- * @example
42
- *
43
- * const transactionData: ITransactionData = {
44
- * // data here
45
- * };
38
+ * Parse a transaction object from a JSON converted transaction data object.
46
39
  *
47
- * const transaction = client.transactions.parse(transactionData);
48
- * console.log(transaction);
40
+ * @param {J} data - The JSON converted transaction data object.
41
+ * @returns {Transaction} A Transaction instance representing the parsed transaction.
49
42
  */
50
- parse(params: RawTransaction): Transaction;
43
+ parse<J extends JSONTransaction<number>>(data: J): Transaction;
51
44
  /**
52
45
  * Sends a transaction to the blockchain network.
53
46
  * @description This method submits a new transaction to the blockchain. After the transaction
@@ -1,5 +1,6 @@
1
1
  import { Transaction } from "../structures/Transaction";
2
2
  import { getTransaction, sendTransaction } from "@mentaproject/core/actions";
3
+ import { parseBingints } from "src/utils/bigint";
3
4
  /**
4
5
  * Manages blockchain transaction-related operations, providing methods to retrieve and send transactions.
5
6
  * @class
@@ -32,25 +33,23 @@ export class TransactionManager {
32
33
  */
33
34
  async get(params) {
34
35
  const data = await getTransaction(this.client.rpc, params);
35
- return this.parse(data);
36
+ return new Transaction(this.client, data);
36
37
  }
37
38
  ;
38
39
  /**
39
- * Parses a transaction object from a raw transaction data object.
40
- * @description This method takes a raw transaction data object and returns a Transaction instance.
41
- * @param {ITransactionData} params - The raw transaction data object.
42
- * @returns {Transaction} A Transaction instance representing the parsed transaction.
43
- * @example
44
- *
45
- * const transactionData: ITransactionData = {
46
- * // data here
47
- * };
40
+ * Parse a transaction object from a JSON converted transaction data object.
48
41
  *
49
- * const transaction = client.transactions.parse(transactionData);
50
- * console.log(transaction);
42
+ * @param {J} data - The JSON converted transaction data object.
43
+ * @returns {Transaction} A Transaction instance representing the parsed transaction.
51
44
  */
52
- parse(params) {
53
- return new Transaction(this.client, params);
45
+ parse(data) {
46
+ const parsed = parseBingints(data);
47
+ const rawData = {
48
+ ...parsed,
49
+ from: parsed.from.address,
50
+ to: parsed.to ? parsed.to.address : null,
51
+ };
52
+ return new Transaction(this.client, rawData);
54
53
  }
55
54
  ;
56
55
  /**
@@ -93,7 +93,7 @@ export declare class Account implements AccountData {
93
93
  * @param {number} [depth=1] The depth to which nested objects should be converted. A depth of 1 means only direct properties are included.
94
94
  * @returns {Promise<object>} A promise that resolves to the JSON representation of the account.
95
95
  */
96
- toJSON(depth?: number): Promise<JSONAccount>;
96
+ toJSON<D extends number = 1>(depth: D): Promise<JSONAccount<D>>;
97
97
  /**
98
98
  * Fetches transactions from the account using a block range exploration with trace_filter calls.
99
99
  *
@@ -59,7 +59,7 @@ export class Account {
59
59
  }]
60
60
  });
61
61
  const data = await getTransaction(this.client.rpc, { hash });
62
- return this.client.transactions.parse(data);
62
+ return new Transaction(this.client, data);
63
63
  }
64
64
  ;
65
65
  /**
@@ -102,8 +102,7 @@ export class Account {
102
102
  const transactions = await Promise.all(hashes.map(hash => this.client.transactions.get({ hash })));
103
103
  return transactions;
104
104
  }
105
- const cachedTransactions = await this.persistenceManager.getTransactions(this.address, params);
106
- return cachedTransactions.map(t => new Transaction(this.client, t));
105
+ return await this.persistenceManager.getTransactions(this.address, params);
107
106
  }
108
107
  /**
109
108
  * Synchronizes the account's transactions with the remote data source using the configured `PersistenceManager`.
@@ -137,7 +136,7 @@ export class Account {
137
136
  * @param {number} [depth=1] The depth to which nested objects should be converted. A depth of 1 means only direct properties are included.
138
137
  * @returns {Promise<object>} A promise that resolves to the JSON representation of the account.
139
138
  */
140
- async toJSON(depth = 1) {
139
+ async toJSON(depth) {
141
140
  return await toJSON({
142
141
  obj: {
143
142
  address: this.address,
@@ -1,13 +1,13 @@
1
- import { Hash, Hex, Withdrawal } from "@mentaproject/core/types";
1
+ import { Block as RawBlock, Hash, Hex, Withdrawal } from "@mentaproject/core";
2
2
  import { Account } from "./Account";
3
3
  import { Transaction } from "./Transaction";
4
- import { BlockData, JSONBlock } from "../types/Block";
4
+ import { JSONBlock } from "../types/Block";
5
5
  import { MentaClient } from "./MentaClient";
6
6
  /**
7
7
  * @description Represents a blockchain block with its properties and methods.
8
8
  * This class provides a structured way to access block-related data and interact with the blockchain client.
9
9
  */
10
- export declare class Block implements Omit<BlockData, "miner" | "transactions"> {
10
+ export declare class Block implements Omit<RawBlock, "miner" | "transactions"> {
11
11
  client: MentaClient;
12
12
  /**
13
13
  * @description The base fee per gas for the block.
@@ -36,15 +36,15 @@ export declare class Block implements Omit<BlockData, "miner" | "transactions">
36
36
  /**
37
37
  * @description The gas used in the block.
38
38
  */
39
- gasUsed?: bigint;
39
+ gasUsed: bigint;
40
40
  /**
41
41
  * @description The hash of the block.
42
42
  */
43
- hash?: Hash | null;
43
+ hash: Hash | null;
44
44
  /**
45
45
  * @description The logs bloom filter for the block.
46
46
  */
47
- logsBloom?: Hex | null;
47
+ logsBloom: Hex | null;
48
48
  /**
49
49
  * @description The mix hash of the block.
50
50
  */
@@ -52,15 +52,15 @@ export declare class Block implements Omit<BlockData, "miner" | "transactions">
52
52
  /**
53
53
  * @description The nonce of the block.
54
54
  */
55
- nonce?: Hex | null;
55
+ nonce: Hex | null;
56
56
  /**
57
57
  * @description The block number.
58
58
  */
59
- number?: bigint | null;
59
+ number: bigint | null;
60
60
  /**
61
61
  * @description The parent beacon block root.
62
62
  */
63
- parentBeaconBlockRoot?: Hex | undefined;
63
+ parentBeaconBlockRoot?: Hex;
64
64
  /**
65
65
  * @description The hash of the parent block.
66
66
  */
@@ -92,7 +92,7 @@ export declare class Block implements Omit<BlockData, "miner" | "transactions">
92
92
  /**
93
93
  * @description The total difficulty of the chain up to this block.
94
94
  */
95
- totalDifficulty?: bigint | null;
95
+ totalDifficulty: bigint | null;
96
96
  /**
97
97
  * @description The root of the transactions trie.
98
98
  */
@@ -108,7 +108,7 @@ export declare class Block implements Omit<BlockData, "miner" | "transactions">
108
108
  /**
109
109
  * @description The root of the withdrawals trie.
110
110
  */
111
- withdrawalsRoot?: Hex | null;
111
+ withdrawalsRoot?: Hex;
112
112
  /**
113
113
  * @description The miner of the block, represented as an Account instance.
114
114
  */
@@ -123,7 +123,7 @@ export declare class Block implements Omit<BlockData, "miner" | "transactions">
123
123
  * @param data The raw block data conforming to IBlockData.
124
124
  * @param includeTransactions Whether to include full transaction objects. Defaults to false.
125
125
  */
126
- constructor(client: MentaClient, data: BlockData);
126
+ constructor(client: MentaClient, data: RawBlock);
127
127
  protected includeTransactions(): this is this & {
128
128
  transactions: Transaction[];
129
129
  };
@@ -134,5 +134,5 @@ export declare class Block implements Omit<BlockData, "miner" | "transactions">
134
134
  * @param depth The depth of the conversion. Defaults to 1.
135
135
  * @returns A promise that resolves to the JSON representation of the block.
136
136
  */
137
- toJSON(depth?: number): Promise<JSONBlock>;
137
+ toJSON<D extends number = 1>(depth: D): Promise<JSONBlock<D>>;
138
138
  }
@@ -1,4 +1,5 @@
1
1
  import { Account } from "./Account";
2
+ import { Transaction } from "./Transaction";
2
3
  import { toJSON } from "../utils/toJSON";
3
4
  /**
4
5
  * @description Represents a blockchain block with its properties and methods.
@@ -40,7 +41,7 @@ export class Block {
40
41
  this.withdrawalsRoot = data.withdrawalsRoot;
41
42
  this.miner = new Account(this.client, { address: data.miner });
42
43
  if (!data.transactions || data.transactions.length === 0) {
43
- const parsed = data.transactions.map((data) => typeof data === "string" ? data : this.client.transactions.parse(data));
44
+ const parsed = data.transactions.map((data) => typeof data === "string" ? data : new Transaction(this.client, data));
44
45
  this.transactions = parsed;
45
46
  }
46
47
  }
@@ -57,7 +58,7 @@ export class Block {
57
58
  * @param depth The depth of the conversion. Defaults to 1.
58
59
  * @returns A promise that resolves to the JSON representation of the block.
59
60
  */
60
- async toJSON(depth = 1) {
61
+ async toJSON(depth) {
61
62
  return await toJSON({
62
63
  obj: {
63
64
  ...this,
@@ -134,5 +134,5 @@ export declare class Transaction implements Omit<RawTransaction, "from" | "to">
134
134
  * @param {number} [depth=1] The depth of the conversion. Controls how deeply nested objects are serialized.
135
135
  * @returns {Promise<object>} A promise that resolves to the JSON representation of the transaction.
136
136
  */
137
- toJSON(depth?: number): Promise<JSONTransaction>;
137
+ toJSON<D extends number = 1>(depth: D): Promise<JSONTransaction<D>>;
138
138
  }
@@ -39,8 +39,8 @@ export class Transaction {
39
39
  this.blockHash = data.blockHash;
40
40
  this.blockNumber = data.blockNumber;
41
41
  this.typeHex = data.typeHex;
42
- this.from = data.from ? this.client.accounts.get(typeof data.from === "string" ? data.from : data.from) : undefined;
43
- this.to = data.to ? this.client.accounts.get(typeof data.to === "string" ? data.to : data.to) : undefined;
42
+ this.from = data.from ? this.client.accounts.get(data.from) : undefined;
43
+ this.to = data.to ? this.client.accounts.get(data.to) : undefined;
44
44
  }
45
45
  ;
46
46
  /**
@@ -107,7 +107,7 @@ export class Transaction {
107
107
  * @param {number} [depth=1] The depth of the conversion. Controls how deeply nested objects are serialized.
108
108
  * @returns {Promise<object>} A promise that resolves to the JSON representation of the transaction.
109
109
  */
110
- async toJSON(depth = 1) {
110
+ async toJSON(depth) {
111
111
  return await toJSON({
112
112
  obj: {
113
113
  ...this,
@@ -30,7 +30,7 @@ export type FetchTransactionParams = {
30
30
  export type AccountData = {
31
31
  address: Address;
32
32
  };
33
- export type JSONAccount = AccountData & {
33
+ export type JSONAccount<depth extends number> = depth extends 0 ? AccountData : AccountData & {
34
34
  isContract: boolean;
35
35
  ETHBalance: string;
36
36
  contractType: string;
@@ -1,67 +1,11 @@
1
- import { Address, Hash, Hex, Transaction as RawTransaction, Withdrawal } from "@mentaproject/core/types";
1
+ import { Hash } from "@mentaproject/core/types";
2
+ import { Block as RawBlock } from "@mentaproject/core";
2
3
  import { JSONAccount } from "./Account";
3
- import { Transaction } from "../structures";
4
- /**
5
- * @interface BlockData
6
- * Represents the raw data structure of a blockchain block.
7
- */
8
- export type BlockData = {
9
- /** Base fee per gas */
10
- baseFeePerGas: bigint | null;
11
- /** Total used blob gas by all transactions in this block */
12
- blobGasUsed: bigint;
13
- /** Difficulty for this block */
14
- difficulty: bigint;
15
- /** Excess blob gas */
16
- excessBlobGas: bigint;
17
- /** "Extra data" field of this block */
18
- extraData: Hex;
19
- /** Maximum gas allowed in this block */
20
- gasLimit: bigint;
21
- /** Total used gas by all transactions in this block */
22
- gasUsed?: bigint;
23
- /** Block hash or `null` if pending */
24
- hash?: Hash | null;
25
- /** Logs bloom filter or `null` if pending */
26
- logsBloom?: Hex | null;
27
- /** Address that received this block’s mining rewards, COINBASE address */
28
- miner: Address;
29
- /** Unique identifier for the block. */
30
- mixHash: Hash;
31
- /** Proof-of-work hash or `null` if pending */
32
- nonce?: Hex | null;
33
- /** Block number or `null` if pending */
34
- number?: bigint | null;
35
- /** Root of the parent beacon chain block. */
36
- parentBeaconBlockRoot?: Hex;
37
- /** Hash of the parent block. */
38
- parentHash: Hash;
39
- /** Root of the receipts trie for this block. */
40
- receiptsRoot: Hex;
41
- /** List of seal fields. */
42
- sealFields: Hex[];
43
- /** SHA3 hash of the uncles data in this block. */
44
- sha3Uncles: Hash;
45
- /** Size of this block in bytes. */
46
- size: bigint;
47
- /** Root of the final state trie for this block. */
48
- stateRoot: Hash;
49
- /** Unix timestamp of when this block was collated. */
50
- timestamp: bigint;
51
- /** Total difficulty of the chain until this block. */
52
- totalDifficulty?: bigint | null;
53
- /** List of transaction objects or hashes included in this block. */
54
- transactions: RawTransaction[] | Hash[];
55
- /** Root of the transaction trie for this block. */
56
- transactionsRoot: Hash;
57
- /** List of uncle hashes. */
58
- uncles: Hash[];
59
- /** List of withdrawal objects included in this block. */
60
- withdrawals?: Withdrawal[];
61
- /** Root of the withdrawals trie for this block. */
62
- withdrawalsRoot?: Hex | null;
63
- };
64
- export type JSONBlock = BlockData & {
65
- miner: JSONAccount;
66
- transactions?: Transaction[] | Hash[];
4
+ import { WithBigintStringified } from "./Utils";
5
+ import { JSONTransaction } from "./Transaction";
6
+ export type JSONBlock<depth extends number> = depth extends 0 ? WithBigintStringified<Omit<RawBlock, "miner" | "transactions">> & {
7
+ transactions?: JSONTransaction<depth>[] | Hash[];
8
+ } : WithBigintStringified<Omit<RawBlock, "miner" | "transactions">> & {
9
+ miner: JSONAccount<depth>;
10
+ transactions?: JSONTransaction<depth>[] | Hash[];
67
11
  };
@@ -1,4 +1,4 @@
1
- import { Address, Transaction as RawTransaction } from '@mentaproject/core/types';
1
+ import { Address } from '@mentaproject/core/types';
2
2
  import { Transaction } from '../structures/Transaction';
3
3
  import { GetTransactionsParams } from './Account';
4
4
  /**
@@ -17,9 +17,9 @@ export interface IPersistenceAdapter {
17
17
  * @method getTransactions
18
18
  * @description Retrieves a paginated list of transactions for a specific account based on provided filters.
19
19
  * @param {GetTransactionsFilters} filters - An object containing filters for retrieving transactions, such as account address, block range, pagination, and sort order.
20
- * @returns {Promise<RawTransaction[]>} A Promise that resolves with transactions data.
20
+ * @returns {Promise<Transaction[]>} A Promise that resolves with transactions objects.
21
21
  */
22
- getTransactions(address: Address, params: GetTransactionsParams): Promise<RawTransaction[]>;
22
+ getTransactions(address: Address, params: GetTransactionsParams): Promise<Transaction[]>;
23
23
  /**
24
24
  * @method getLastSyncedBlock
25
25
  * @description Retrieves the last synced block number for a given account. This is used to track the synchronization progress of an account's transactions.
@@ -3,8 +3,9 @@
3
3
  */
4
4
  import { Hex, TransactionReceipt, Transaction as RawTransaction } from "@mentaproject/core";
5
5
  import { Account } from "../structures";
6
- import { BlockData } from "./Block";
6
+ import { JSONBlock } from "./Block";
7
7
  import { JSONAccount } from "./Account";
8
+ import { WithBigintStringified } from "./Utils";
8
9
  /**
9
10
  * Defines the direction of a transaction relative to an account.
10
11
  * "in" for incoming transactions, "out" for outgoing transactions.
@@ -32,10 +33,13 @@ export type TransactionCall = {
32
33
  gas: bigint;
33
34
  callType: string;
34
35
  };
35
- export type JSONTransaction = RawTransaction & {
36
- from: JSONAccount;
37
- to?: JSONAccount;
38
- block: BlockData;
39
- receipt: TransactionReceipt;
40
- calls?: TransactionCall[];
36
+ export type JSONTransaction<depth extends number> = depth extends 0 ? WithBigintStringified<Omit<RawTransaction, 'from' | 'to'>> & {
37
+ from: JSONAccount<depth>;
38
+ to?: JSONAccount<depth>;
39
+ } : WithBigintStringified<Omit<RawTransaction, 'from' | 'to'>> & {
40
+ from: JSONAccount<depth>;
41
+ to?: JSONAccount<depth>;
42
+ block: JSONBlock<depth>;
43
+ receipt: WithBigintStringified<TransactionReceipt>;
44
+ calls?: WithBigintStringified<TransactionCall>[];
41
45
  };
@@ -0,0 +1,7 @@
1
+ export type BigintStringified = `${number}n`;
2
+ export type WithBigintStringified<T> = T extends bigint ? BigintStringified : T extends (infer E)[] ? Array<WithBigintStringified<E>> : T extends object ? {
3
+ [K in keyof T]: WithBigintStringified<T[K]>;
4
+ } : T;
5
+ export type WithoutBigintStringified<T> = T extends BigintStringified ? bigint : T extends (infer E)[] ? Array<WithoutBigintStringified<E>> : T extends object ? {
6
+ [K in keyof T]: WithoutBigintStringified<T[K]>;
7
+ } : T;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,8 @@
1
+ import { WithoutBigintStringified } from "src/types/Utils";
1
2
  export declare const bigIntMax: (...args: bigint[]) => bigint;
2
3
  export declare const bigIntMin: (...args: bigint[]) => bigint;
4
+ export declare function bigIntReviver(key: string, value: any): any;
5
+ export declare function stringifyBingints(obj: any): any;
6
+ export declare function parseBingints<T extends {
7
+ [key: string]: any;
8
+ }>(obj: T): WithoutBigintStringified<T>;
@@ -1,2 +1,54 @@
1
1
  export const bigIntMax = (...args) => args.reduce((m, e) => e > m ? e : m);
2
2
  export const bigIntMin = (...args) => args.reduce((m, e) => e < m ? e : m);
3
+ export function bigIntReviver(key, value) {
4
+ if (typeof value === 'string') {
5
+ if (value.endsWith('n')) {
6
+ try {
7
+ return BigInt(value.slice(0, -1));
8
+ }
9
+ catch (e) {
10
+ return value;
11
+ }
12
+ }
13
+ ;
14
+ }
15
+ ;
16
+ return value;
17
+ }
18
+ ;
19
+ export function stringifyBingints(obj) {
20
+ if (typeof obj === 'bigint') {
21
+ return obj.toString() + "n";
22
+ }
23
+ if (typeof obj === 'object') {
24
+ for (const key in obj) {
25
+ obj[key] = stringifyBingints(obj[key]);
26
+ }
27
+ }
28
+ if (Array.isArray(obj)) {
29
+ for (let i = 0; i < obj.length; i++) {
30
+ obj[i] = stringifyBingints(obj[i]);
31
+ }
32
+ }
33
+ return obj;
34
+ }
35
+ ;
36
+ export function parseBingints(obj) {
37
+ if (typeof obj !== 'object' || obj === null) {
38
+ return obj;
39
+ }
40
+ if (Array.isArray(obj)) {
41
+ return obj.map((item, index) => bigIntReviver(index.toString(), parseBingints(item)));
42
+ }
43
+ const newObj = {};
44
+ for (const key in obj) {
45
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
46
+ const value = obj[key];
47
+ const processedValue = parseBingints(value);
48
+ newObj[key] = bigIntReviver(key, processedValue);
49
+ }
50
+ }
51
+ ;
52
+ return newObj;
53
+ }
54
+ ;
@@ -24,7 +24,7 @@ function isClassInstance(obj) {
24
24
  */
25
25
  function convertBigintsToStrings(obj) {
26
26
  if (typeof obj === 'bigint') {
27
- return obj.toString();
27
+ return obj.toString() + "n";
28
28
  }
29
29
  if (typeof obj === 'object') {
30
30
  for (const key in obj) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentaproject/client",
3
- "version": "0.1.18",
3
+ "version": "0.1.20",
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
@@ -3,15 +3,13 @@ import { MentaClient, Transaction } from "./src";
3
3
  import { privateKeyToAccount, generatePrivateKey } from "viem/accounts"
4
4
  import { mainnet } from "@mentaproject/core/chains";
5
5
  import { MemoryCache } from "./src/structures/Cache";
6
- import { watchBlockNumber, watchBlocks } from "@mentaproject/core/actions";
7
6
  import { writeFileSync } from "fs";
8
- import { JSONTransaction } from "./src/types";
9
7
 
10
8
  const viemAccount = privateKeyToAccount(generatePrivateKey());
11
9
 
12
10
  let counter = 0;
13
11
  let lastSyncedBlock = new Map<Address, bigint>();
14
- let transactions = new Map<Address, JSONTransaction[]>();
12
+ let transactions = new Map<Address, Transaction[]>();
15
13
 
16
14
  const client = new MentaClient({
17
15
  account: viemAccount,
@@ -34,7 +32,7 @@ const client = new MentaClient({
34
32
  getLastSyncedBlock: async (accountAddress: Address) => {
35
33
  return lastSyncedBlock.get(accountAddress) ?? null;
36
34
  },
37
- getTransactions: async (address: Address, params) => {
35
+ getTransactions: async (address: Address, params): Promise<Transaction[]> => {
38
36
  return transactions.get(address) ?? [];
39
37
  },
40
38
  upsertTransactions: async (txs: Transaction[]) => {
@@ -51,7 +49,9 @@ const client = new MentaClient({
51
49
  };
52
50
 
53
51
  const jsonTransaction = await transaction.toJSON(0);
54
- transactions.get(checksumed)?.push(jsonTransaction);
52
+ const parsed = client.transactions.parse(jsonTransaction);
53
+
54
+ transactions.get(checksumed)?.push(parsed);
55
55
  };
56
56
 
57
57
  if (toAddress) {
@@ -61,8 +61,10 @@ const client = new MentaClient({
61
61
  transactions.set(checksumed, []);
62
62
  };
63
63
 
64
- const jsonTransaction = await transaction.toJSON();
65
- transactions.get(checksumed)?.push(jsonTransaction);
64
+ const jsonTransaction = await transaction.toJSON(0);
65
+ const parsed = client.transactions.parse(jsonTransaction);
66
+
67
+ transactions.get(checksumed)?.push(parsed);
66
68
  }
67
69
  }
68
70
  },
@@ -77,8 +79,11 @@ const client = new MentaClient({
77
79
  console.log("Watching for new blocks and block numbers...");
78
80
 
79
81
  const account = client.accounts.get("0x53b9B72DC6f96Eb4B54143B211B22e2548e4cf5c");
80
- await account.syncTransactions(20)
81
- const res = await account.transactions({ limit: 20 });
82
+ await account.syncTransactions(50)
83
+ const res = await account.transactions({ limit: 50 });
84
+ console.log(res.map(r => r.blockNumber));
85
+ const json = await Promise.all(res.map(async t => await t.toJSON(0)));
82
86
 
83
- console.log(res[0]);
87
+ writeFileSync("transactions.json", JSON.stringify(json, null, 2));
88
+ console.log("Done");
84
89
  })();
@@ -221,12 +221,12 @@ describe('Account', () => {
221
221
  (coreActions.getBalance as jest.Mock).mockResolvedValue(100n);
222
222
  (coreActions.getTransactionCount as jest.Mock).mockResolvedValue(5);
223
223
 
224
- const json = await account.toJSON();
224
+ const json = await account.toJSON(1);
225
225
 
226
226
  expect(json.address).toBe(mockAddress);
227
227
  expect(json.isContract).toBe(true);
228
228
  expect(json.contractType).toBe('ERC20');
229
- expect(json.ETHBalance).toBe('100');
229
+ expect(json.ETHBalance).toBe('100n');
230
230
  expect(json.transactionCount).toBe(5);
231
231
  });
232
232
  });
@@ -3,7 +3,7 @@ import { MentaClient } from '../../src/structures/MentaClient';
3
3
  import { Account } from '../../src/structures/Account';
4
4
  import { Transaction } from '../../src/structures/Transaction';
5
5
  import { Address, Hash } from '@mentaproject/core/types';
6
- import { BlockData } from '../../src/types';
6
+ import { Block as RawBloc } from '@mentaproject/core';
7
7
 
8
8
  // Mocking MentaClient
9
9
  const mockMentaClient = {
@@ -21,7 +21,7 @@ describe('Block', () => {
21
21
  const mockMinerAddress: Address = '0xminer123';
22
22
  const mockTxHash: Hash = '0xhash123';
23
23
 
24
- const mockBlockData: BlockData = {
24
+ const mockBlockData: RawBloc = {
25
25
  number: 123n,
26
26
  hash: '0xblockhash',
27
27
  parentHash: '0xparenthash',
@@ -88,9 +88,9 @@ describe('Block', () => {
88
88
  describe('toJSON', () => {
89
89
  it('should return a JSON representation of the block', async () => {
90
90
  const block = new Block(mockMentaClient, mockBlockData);
91
- const json = await block.toJSON();
91
+ const json = await block.toJSON(1);
92
92
 
93
- expect(json.number).toBe('123');
93
+ expect(json.number).toBe('123n');
94
94
  expect(json.hash).toBe('0xblockhash');
95
95
  expect(json.miner).toBeUndefined();
96
96
  });
@@ -140,7 +140,7 @@ describe('Transaction', () => {
140
140
  jest.spyOn(transaction, 'receipt').mockResolvedValue({ toJSON: () => ({ receipt: 'data' }) } as any);
141
141
  jest.spyOn(transaction, 'calls').mockResolvedValue([{ toJSON: () => ({ call: 'data' }) } as any]);
142
142
 
143
- const json = await transaction.toJSON();
143
+ const json = await transaction.toJSON(1);
144
144
 
145
145
  expect(json.hash).toBe(mockTxHash);
146
146
  expect(json.block).toBeDefined();