@mentaproject/client 0.1.16 → 0.1.18
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/managers/AccountManager.js +1 -1
- package/dist/managers/PersistenceManager.d.ts +3 -4
- package/dist/managers/PersistenceManager.js +1 -2
- package/dist/structures/Account.d.ts +1 -1
- package/dist/structures/Account.js +2 -2
- package/dist/structures/Block.js +1 -1
- package/dist/structures/Transaction.js +4 -4
- package/dist/types/PersistenceAdapter.d.ts +3 -4
- package/dist/types/Transaction.d.ts +3 -0
- package/dist/utils/toJSON.js +1 -1
- package/package.json +1 -1
- package/test.ts +2 -2
- package/tests/structures/Account.test.ts +7 -4
- package/tests/structures/Block.test.ts +1 -1
- package/tests/structures/Transaction.test.ts +12 -7
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { Address } from '@mentaproject/core/types';
|
|
2
|
-
import { IPersistenceAdapter } from '../types/PersistenceAdapter';
|
|
1
|
+
import { Address, Transaction as RawTransaction } from '@mentaproject/core/types';
|
|
3
2
|
import { Account } from '../structures/Account';
|
|
4
3
|
import { MentaClient } from '../structures';
|
|
5
|
-
import { GetTransactionsParams,
|
|
4
|
+
import { GetTransactionsParams, IPersistenceAdapter } from 'src/types';
|
|
6
5
|
/**
|
|
7
6
|
* Manages the persistence of data, including transactions, using a specified persistence adapter.
|
|
8
7
|
* It handles fetching data from both local storage and remote sources, and synchronizing them.
|
|
@@ -21,7 +20,7 @@ export declare class PersistenceManager {
|
|
|
21
20
|
* @param params - Parameters for retrieving transactions.
|
|
22
21
|
* @returns A promise that resolves to an array of transactions with associated account information.
|
|
23
22
|
*/
|
|
24
|
-
getTransactions(address: Address, params: GetTransactionsParams): Promise<
|
|
23
|
+
getTransactions(address: Address, params: GetTransactionsParams): Promise<RawTransaction[]>;
|
|
25
24
|
/**
|
|
26
25
|
* Synchronizes an account's transactions from the remote source to the local persistence.
|
|
27
26
|
* It fetches new transactions starting from the last synced block and updates the local storage.
|
|
@@ -20,8 +20,7 @@ export class PersistenceManager {
|
|
|
20
20
|
* @returns A promise that resolves to an array of transactions with associated account information.
|
|
21
21
|
*/
|
|
22
22
|
async getTransactions(address, params) {
|
|
23
|
-
|
|
24
|
-
return localTransactions;
|
|
23
|
+
return await this.persistenceAdapter.getTransactions(address, params);
|
|
25
24
|
}
|
|
26
25
|
/**
|
|
27
26
|
* Synchronizes an account's transactions from the remote source to the local persistence.
|
|
@@ -20,7 +20,7 @@ export declare class Account implements AccountData {
|
|
|
20
20
|
* @param address The blockchain address of the account.
|
|
21
21
|
* @param persistenceManager An optional PersistenceManager instance for caching and data synchronization.
|
|
22
22
|
*/
|
|
23
|
-
constructor(client: MentaClient,
|
|
23
|
+
constructor(client: MentaClient, data: AccountData, persistenceManager?: PersistenceManager);
|
|
24
24
|
/**
|
|
25
25
|
* Checks if the account's address belongs to a smart contract.
|
|
26
26
|
* @description This method queries the blockchain for the code associated with the account's address.
|
|
@@ -14,9 +14,9 @@ export class Account {
|
|
|
14
14
|
* @param address The blockchain address of the account.
|
|
15
15
|
* @param persistenceManager An optional PersistenceManager instance for caching and data synchronization.
|
|
16
16
|
*/
|
|
17
|
-
constructor(client,
|
|
17
|
+
constructor(client, data, persistenceManager) {
|
|
18
18
|
this.client = client;
|
|
19
|
-
this.address = address;
|
|
19
|
+
this.address = data.address;
|
|
20
20
|
this.persistenceManager = persistenceManager;
|
|
21
21
|
}
|
|
22
22
|
/**
|
package/dist/structures/Block.js
CHANGED
|
@@ -38,7 +38,7 @@ export class Block {
|
|
|
38
38
|
this.uncles = data.uncles;
|
|
39
39
|
this.withdrawals = data.withdrawals;
|
|
40
40
|
this.withdrawalsRoot = data.withdrawalsRoot;
|
|
41
|
-
this.miner = new Account(this.client, data.miner);
|
|
41
|
+
this.miner = new Account(this.client, { address: data.miner });
|
|
42
42
|
if (!data.transactions || data.transactions.length === 0) {
|
|
43
43
|
const parsed = data.transactions.map((data) => typeof data === "string" ? data : this.client.transactions.parse(data));
|
|
44
44
|
this.transactions = parsed;
|
|
@@ -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 =
|
|
43
|
-
this.to = data.to ?
|
|
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;
|
|
44
44
|
}
|
|
45
45
|
;
|
|
46
46
|
/**
|
|
@@ -54,8 +54,8 @@ export class Transaction {
|
|
|
54
54
|
return undefined;
|
|
55
55
|
const calls = traces.filter(t => t.type === "call" && t.action !== undefined);
|
|
56
56
|
return calls.map(c => ({
|
|
57
|
-
from: new Account(this.client, c.action.from),
|
|
58
|
-
to: new Account(this.client, c.action.to),
|
|
57
|
+
from: new Account(this.client, { address: c.action.from }),
|
|
58
|
+
to: new Account(this.client, { address: c.action.to }),
|
|
59
59
|
value: hexToBigInt(c.action.value),
|
|
60
60
|
input: c.action.input,
|
|
61
61
|
gas: hexToBigInt(c.action.gas),
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Address } from '@mentaproject/core/types';
|
|
1
|
+
import { Address, Transaction as RawTransaction } from '@mentaproject/core/types';
|
|
2
2
|
import { Transaction } from '../structures/Transaction';
|
|
3
3
|
import { GetTransactionsParams } from './Account';
|
|
4
|
-
import { JSONTransaction } from './Transaction';
|
|
5
4
|
/**
|
|
6
5
|
* @interface IPersistenceAdapter
|
|
7
6
|
* @description Defines the contract for persistence layers, ensuring consistent data storage and retrieval operations.
|
|
@@ -18,9 +17,9 @@ export interface IPersistenceAdapter {
|
|
|
18
17
|
* @method getTransactions
|
|
19
18
|
* @description Retrieves a paginated list of transactions for a specific account based on provided filters.
|
|
20
19
|
* @param {GetTransactionsFilters} filters - An object containing filters for retrieving transactions, such as account address, block range, pagination, and sort order.
|
|
21
|
-
* @returns {Promise<
|
|
20
|
+
* @returns {Promise<RawTransaction[]>} A Promise that resolves with transactions data.
|
|
22
21
|
*/
|
|
23
|
-
getTransactions(address: Address, params: GetTransactionsParams): Promise<
|
|
22
|
+
getTransactions(address: Address, params: GetTransactionsParams): Promise<RawTransaction[]>;
|
|
24
23
|
/**
|
|
25
24
|
* @method getLastSyncedBlock
|
|
26
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.
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { Hex, TransactionReceipt, Transaction as RawTransaction } from "@mentaproject/core";
|
|
5
5
|
import { Account } from "../structures";
|
|
6
6
|
import { BlockData } from "./Block";
|
|
7
|
+
import { JSONAccount } from "./Account";
|
|
7
8
|
/**
|
|
8
9
|
* Defines the direction of a transaction relative to an account.
|
|
9
10
|
* "in" for incoming transactions, "out" for outgoing transactions.
|
|
@@ -32,6 +33,8 @@ export type TransactionCall = {
|
|
|
32
33
|
callType: string;
|
|
33
34
|
};
|
|
34
35
|
export type JSONTransaction = RawTransaction & {
|
|
36
|
+
from: JSONAccount;
|
|
37
|
+
to?: JSONAccount;
|
|
35
38
|
block: BlockData;
|
|
36
39
|
receipt: TransactionReceipt;
|
|
37
40
|
calls?: TransactionCall[];
|
package/dist/utils/toJSON.js
CHANGED
|
@@ -52,7 +52,7 @@ export async function toJSON({ obj, depth = 0 }) {
|
|
|
52
52
|
const promises = [];
|
|
53
53
|
for (const key in obj) {
|
|
54
54
|
// skip class related properties
|
|
55
|
-
if (key === "toJSON" || key === "constructor" || key === "client")
|
|
55
|
+
if (key === "toJSON" || key === "constructor" || key === "client" || key === "persistenceManager")
|
|
56
56
|
continue;
|
|
57
57
|
const prop = obj[key];
|
|
58
58
|
if (typeof prop === "function") {
|
package/package.json
CHANGED
package/test.ts
CHANGED
|
@@ -50,7 +50,7 @@ const client = new MentaClient({
|
|
|
50
50
|
transactions.set(checksumed, []);
|
|
51
51
|
};
|
|
52
52
|
|
|
53
|
-
const jsonTransaction = await transaction.toJSON();
|
|
53
|
+
const jsonTransaction = await transaction.toJSON(0);
|
|
54
54
|
transactions.get(checksumed)?.push(jsonTransaction);
|
|
55
55
|
};
|
|
56
56
|
|
|
@@ -80,5 +80,5 @@ const client = new MentaClient({
|
|
|
80
80
|
await account.syncTransactions(20)
|
|
81
81
|
const res = await account.transactions({ limit: 20 });
|
|
82
82
|
|
|
83
|
-
console.log(res
|
|
83
|
+
console.log(res[0]);
|
|
84
84
|
})();
|
|
@@ -45,8 +45,11 @@ describe('Account', () => {
|
|
|
45
45
|
parse: jest.fn().mockImplementation((data) => new Transaction(mockMentaClient, data)),
|
|
46
46
|
send: jest.fn(),
|
|
47
47
|
} as any;
|
|
48
|
+
mockMentaClient.accounts = {
|
|
49
|
+
get: jest.fn().mockImplementation((address) => new Account(mockMentaClient, { address: address })),
|
|
50
|
+
} as any;
|
|
48
51
|
mockPersistenceManager = new MockedPersistenceManager(mockMentaClient, {} as any) as jest.Mocked<PersistenceManager>;
|
|
49
|
-
account = new Account(mockMentaClient, mockAddress, mockPersistenceManager);
|
|
52
|
+
account = new Account(mockMentaClient, { address: mockAddress }, mockPersistenceManager);
|
|
50
53
|
});
|
|
51
54
|
|
|
52
55
|
describe('constructor', () => {
|
|
@@ -151,7 +154,7 @@ describe('Account', () => {
|
|
|
151
154
|
});
|
|
152
155
|
|
|
153
156
|
it('should fetch transactions from remote if persistence is not configured', async () => {
|
|
154
|
-
const localAccount = new Account(mockMentaClient, mockAddress); // No persistenceManager
|
|
157
|
+
const localAccount = new Account(mockMentaClient, { address: mockAddress }); // No persistenceManager
|
|
155
158
|
const mockHashes: Hash[] = ['0x1', '0x2'];
|
|
156
159
|
const mockBaseTx = {
|
|
157
160
|
blockHash: '0xblockhash',
|
|
@@ -195,14 +198,14 @@ describe('Account', () => {
|
|
|
195
198
|
});
|
|
196
199
|
|
|
197
200
|
it('should throw an error if persistence manager is not configured', async () => {
|
|
198
|
-
const localAccount = new Account(mockMentaClient, mockAddress);
|
|
201
|
+
const localAccount = new Account(mockMentaClient, { address: mockAddress });
|
|
199
202
|
await expect(localAccount.syncTransactions()).rejects.toThrow('The persistence module is not configured.');
|
|
200
203
|
});
|
|
201
204
|
});
|
|
202
205
|
|
|
203
206
|
describe('tokenTransactions', () => {
|
|
204
207
|
it('should throw an error if persistence is not configured', async () => {
|
|
205
|
-
const localAccount = new Account(mockMentaClient, mockAddress);
|
|
208
|
+
const localAccount = new Account(mockMentaClient, { address: mockAddress });
|
|
206
209
|
await expect(localAccount.tokenTransactions(mockAddress)).rejects.toThrow('The persistence module is not configured.');
|
|
207
210
|
});
|
|
208
211
|
|
|
@@ -59,7 +59,7 @@ describe('Block', () => {
|
|
|
59
59
|
expect(block).toBeInstanceOf(Block);
|
|
60
60
|
expect(block.number).toBe(123n);
|
|
61
61
|
expect(block.hash).toBe('0xblockhash');
|
|
62
|
-
expect(Account).toHaveBeenCalledWith(mockMentaClient, mockMinerAddress);
|
|
62
|
+
expect(Account).toHaveBeenCalledWith(mockMentaClient, { address: mockMinerAddress });
|
|
63
63
|
expect(block.miner).toBeInstanceOf(Account);
|
|
64
64
|
});
|
|
65
65
|
|
|
@@ -13,15 +13,17 @@ jest.mock('@mentaproject/core/actions', () => ({
|
|
|
13
13
|
getBlock: jest.fn(),
|
|
14
14
|
}));
|
|
15
15
|
|
|
16
|
-
// Mocking MentaClient
|
|
17
|
-
const mockMentaClient = {
|
|
18
|
-
rpc: {},
|
|
19
|
-
} as unknown as MentaClient;
|
|
20
|
-
|
|
21
16
|
// Mocking structures
|
|
22
17
|
jest.mock('../../src/structures/Account');
|
|
23
18
|
jest.mock('../../src/structures/Block');
|
|
24
19
|
|
|
20
|
+
const mockMentaClient = {
|
|
21
|
+
rpc: {},
|
|
22
|
+
accounts: {
|
|
23
|
+
get: jest.fn(),
|
|
24
|
+
},
|
|
25
|
+
} as unknown as MentaClient;
|
|
26
|
+
|
|
25
27
|
describe('Transaction', () => {
|
|
26
28
|
const mockTxHash: Hash = '0xabcdef123456';
|
|
27
29
|
const mockFromAddress: Address = '0xfrom123';
|
|
@@ -50,6 +52,9 @@ describe('Transaction', () => {
|
|
|
50
52
|
|
|
51
53
|
beforeEach(() => {
|
|
52
54
|
jest.clearAllMocks();
|
|
55
|
+
(mockMentaClient.accounts.get as jest.Mock).mockImplementation(
|
|
56
|
+
(address: Address) => new Account(mockMentaClient, { address }),
|
|
57
|
+
);
|
|
53
58
|
transaction = new Transaction(mockMentaClient, mockRawTx);
|
|
54
59
|
});
|
|
55
60
|
|
|
@@ -58,8 +63,8 @@ describe('Transaction', () => {
|
|
|
58
63
|
expect(transaction).toBeInstanceOf(Transaction);
|
|
59
64
|
expect(transaction.hash).toBe(mockTxHash);
|
|
60
65
|
expect(transaction.value).toBe(1000n);
|
|
61
|
-
expect(
|
|
62
|
-
expect(
|
|
66
|
+
expect(mockMentaClient.accounts.get).toHaveBeenCalledWith(mockFromAddress);
|
|
67
|
+
expect(mockMentaClient.accounts.get).toHaveBeenCalledWith(mockToAddress);
|
|
63
68
|
expect(transaction.from).toBeInstanceOf(Account);
|
|
64
69
|
expect(transaction.to).toBeInstanceOf(Account);
|
|
65
70
|
});
|