@mentaproject/client 0.1.22 → 0.1.24
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/PersistenceManager.d.ts +3 -3
- package/dist/managers/TransactionManager.d.ts +2 -2
- package/dist/managers/TransactionManager.js +1 -1
- package/dist/structures/Account.js +2 -1
- package/dist/types/PersistenceAdapter.d.ts +3 -2
- package/package.json +2 -2
- package/src/index.ts +21 -0
- package/src/managers/AccountManager.ts +51 -0
- package/src/managers/BlockManager.ts +51 -0
- package/src/managers/ContractManager.ts +60 -0
- package/src/managers/PersistenceManager.ts +59 -0
- package/src/managers/TransactionManager.ts +92 -0
- package/src/managers/index.ts +4 -0
- package/src/structures/Account.ts +206 -0
- package/src/structures/Block.ts +185 -0
- package/src/structures/Cache.ts +126 -0
- package/src/structures/MentaClient.ts +127 -0
- package/src/structures/Transaction.ts +220 -0
- package/src/structures/index.ts +5 -0
- package/src/types/Account.ts +44 -0
- package/src/types/Block.ts +14 -0
- package/src/types/Cache.ts +55 -0
- package/src/types/JsonRPC.ts +126 -0
- package/src/types/MentaClient.ts +67 -0
- package/src/types/PersistenceAdapter.ts +44 -0
- package/src/types/Transaction.ts +52 -0
- package/src/types/Utils.ts +30 -0
- package/src/types/index.ts +8 -0
- package/src/utils/bigint.ts +59 -0
- package/src/utils/toJSON.ts +123 -0
- package/src/utils/withCache.ts +50 -0
- package/test.ts +0 -89
package/test.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { Address, checksumAddress, http } from "@mentaproject/core";
|
|
2
|
-
import { MentaClient, Transaction } from "./src";
|
|
3
|
-
import { privateKeyToAccount, generatePrivateKey } from "viem/accounts"
|
|
4
|
-
import { mainnet } from "@mentaproject/core/chains";
|
|
5
|
-
import { MemoryCache } from "./src/structures/Cache";
|
|
6
|
-
import { writeFileSync } from "fs";
|
|
7
|
-
|
|
8
|
-
const viemAccount = privateKeyToAccount(generatePrivateKey());
|
|
9
|
-
|
|
10
|
-
let counter = 0;
|
|
11
|
-
let lastSyncedBlock = new Map<Address, bigint>();
|
|
12
|
-
let transactions = new Map<Address, Transaction[]>();
|
|
13
|
-
|
|
14
|
-
const client = new MentaClient({
|
|
15
|
-
account: viemAccount,
|
|
16
|
-
transport: http("https://ethereum-rpc.publicnode.com", {
|
|
17
|
-
batch: {
|
|
18
|
-
batchSize: 50,
|
|
19
|
-
wait: 20,
|
|
20
|
-
},
|
|
21
|
-
onFetchRequest: async (r) => {
|
|
22
|
-
// const data = await r.json();
|
|
23
|
-
// console.log(data.map(j => `${j.method}(${j.params?.map(p => JSON.stringify(p)).join(', ')})`).join('\n'));
|
|
24
|
-
// console.log(`>> ${counter} requests`);
|
|
25
|
-
|
|
26
|
-
// console.log(`>> ${r.url}\n>> ${req.map(j => j.method).join(', ')}\n>> ${delay / 1000}s\n`)
|
|
27
|
-
},
|
|
28
|
-
// onFetchResponse: (r) => console.log(`<<: ${r.url}`),
|
|
29
|
-
}),
|
|
30
|
-
cache: new MemoryCache(0n, { maxSize: 1000, defaultTtl: 1 }),
|
|
31
|
-
persistenceAdapter: {
|
|
32
|
-
getLastSyncedBlock: async (accountAddress: Address) => {
|
|
33
|
-
return lastSyncedBlock.get(accountAddress) ?? null;
|
|
34
|
-
},
|
|
35
|
-
getTransactions: async (address: Address, params): Promise<Transaction[]> => {
|
|
36
|
-
return transactions.get(address) ?? [];
|
|
37
|
-
},
|
|
38
|
-
upsertTransactions: async (txs: Transaction[]) => {
|
|
39
|
-
console.log("upsertTransactions", txs.length);
|
|
40
|
-
for (const transaction of txs) {
|
|
41
|
-
const fromAddress = transaction.from?.address;
|
|
42
|
-
const toAddress = transaction.to?.address;
|
|
43
|
-
|
|
44
|
-
if (fromAddress) {
|
|
45
|
-
const checksumed = checksumAddress(fromAddress);
|
|
46
|
-
|
|
47
|
-
if (!transactions.has(checksumed)) {
|
|
48
|
-
transactions.set(checksumed, []);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const jsonTransaction = await transaction.toJSON(0);
|
|
52
|
-
const parsed = client.transactions.parse(jsonTransaction);
|
|
53
|
-
|
|
54
|
-
transactions.get(checksumed)?.push(parsed);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
if (toAddress) {
|
|
58
|
-
const checksumed = checksumAddress(toAddress);
|
|
59
|
-
|
|
60
|
-
if (!transactions.has(checksumed)) {
|
|
61
|
-
transactions.set(checksumed, []);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const jsonTransaction = await transaction.toJSON(0);
|
|
65
|
-
const parsed = client.transactions.parse(jsonTransaction);
|
|
66
|
-
|
|
67
|
-
transactions.get(checksumed)?.push(parsed);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
setLastSyncedBlock: async (accountAddress: Address, blockNumber: bigint) => {
|
|
72
|
-
lastSyncedBlock.set(accountAddress, blockNumber);
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
chain: mainnet
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
(async () => {
|
|
79
|
-
console.log("Watching for new blocks and block numbers...");
|
|
80
|
-
|
|
81
|
-
const account = client.accounts.get("0x53b9B72DC6f96Eb4B54143B211B22e2548e4cf5c");
|
|
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)));
|
|
86
|
-
|
|
87
|
-
writeFileSync("transactions.json", JSON.stringify(json, null, 2));
|
|
88
|
-
console.log("Done");
|
|
89
|
-
})();
|