teleton 0.3.0 → 0.4.0

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.
@@ -0,0 +1,157 @@
1
+ // src/ton/format-transactions.ts
2
+ import { fromNano } from "@ton/ton";
3
+ var OP_CODES = {
4
+ COMMENT: 0,
5
+ JETTON_TRANSFER: 260734629,
6
+ JETTON_TRANSFER_NOTIFICATION: 1935855772,
7
+ JETTON_INTERNAL_TRANSFER: 395134233,
8
+ JETTON_BURN: 1499400124,
9
+ NFT_TRANSFER: 1607220500,
10
+ NFT_OWNERSHIP_ASSIGNED: 85167505,
11
+ EXCESSES: 3576854235,
12
+ BOUNCE: 4294967295
13
+ };
14
+ function parseMessageBody(body) {
15
+ if (!body) return null;
16
+ try {
17
+ const slice = body.beginParse();
18
+ if (slice.remainingBits < 32) return null;
19
+ const op = slice.loadUint(32);
20
+ if (op === OP_CODES.COMMENT && slice.remainingBits > 0) {
21
+ return { op, comment: slice.loadStringTail() };
22
+ }
23
+ if (op === OP_CODES.JETTON_TRANSFER_NOTIFICATION) {
24
+ const _queryId = slice.loadUint(64);
25
+ const amount = slice.loadCoins();
26
+ const _sender = slice.loadAddress();
27
+ return { op, jettonAmount: amount.toString() };
28
+ }
29
+ if (op === OP_CODES.JETTON_TRANSFER) {
30
+ const _queryId = slice.loadUint(64);
31
+ const amount = slice.loadCoins();
32
+ const _destination = slice.loadAddress();
33
+ return { op, jettonAmount: amount.toString() };
34
+ }
35
+ if (op === OP_CODES.NFT_OWNERSHIP_ASSIGNED) {
36
+ const _queryId = slice.loadUint(64);
37
+ const _prevOwner = slice.loadAddress();
38
+ return { op };
39
+ }
40
+ if (op === OP_CODES.NFT_TRANSFER) {
41
+ const _queryId = slice.loadUint(64);
42
+ const newOwner = slice.loadAddress();
43
+ return { op, nftAddress: newOwner?.toString() };
44
+ }
45
+ return { op };
46
+ } catch {
47
+ return null;
48
+ }
49
+ }
50
+ function formatTransactions(transactions) {
51
+ return transactions.map((tx) => {
52
+ const inMsg = tx.inMessage;
53
+ const outMsgArray = [...tx.outMessages.values()];
54
+ const hash = tx.hash().toString("hex");
55
+ const explorer = `https://tonviewer.com/transaction/${hash}`;
56
+ const txTimeMs = tx.now * 1e3;
57
+ const date = new Date(txTimeMs).toISOString();
58
+ const secondsAgo = Math.max(0, Math.floor((Date.now() - txTimeMs) / 1e3));
59
+ if (inMsg?.info.type === "internal") {
60
+ const tonAmount = fromNano(inMsg.info.value.coins);
61
+ const from = inMsg.info.src?.toString() || "unknown";
62
+ const parsed = parseMessageBody(inMsg.body);
63
+ if (parsed?.op === OP_CODES.EXCESSES) {
64
+ return {
65
+ type: "gas_refund",
66
+ hash,
67
+ amount: `${tonAmount} TON`,
68
+ from,
69
+ date,
70
+ secondsAgo,
71
+ explorer
72
+ };
73
+ }
74
+ if (parsed?.op === OP_CODES.JETTON_TRANSFER_NOTIFICATION) {
75
+ return {
76
+ type: "jetton_received",
77
+ hash,
78
+ jettonAmount: parsed.jettonAmount,
79
+ jettonWallet: from,
80
+ date,
81
+ secondsAgo,
82
+ explorer
83
+ };
84
+ }
85
+ if (parsed?.op === OP_CODES.NFT_OWNERSHIP_ASSIGNED) {
86
+ return { type: "nft_received", hash, nftAddress: from, date, secondsAgo, explorer };
87
+ }
88
+ if (inMsg.info.bounced || parsed?.op === OP_CODES.BOUNCE) {
89
+ return {
90
+ type: "bounce",
91
+ hash,
92
+ amount: `${tonAmount} TON`,
93
+ from,
94
+ date,
95
+ secondsAgo,
96
+ explorer
97
+ };
98
+ }
99
+ return {
100
+ type: "ton_received",
101
+ hash,
102
+ amount: `${tonAmount} TON`,
103
+ from,
104
+ comment: parsed?.comment || null,
105
+ date,
106
+ secondsAgo,
107
+ explorer
108
+ };
109
+ }
110
+ if (outMsgArray.length > 0) {
111
+ const results = [];
112
+ for (const outMsg of outMsgArray) {
113
+ if (outMsg.info.type !== "internal") continue;
114
+ const info = outMsg.info;
115
+ const to = info.dest?.toString() || "unknown";
116
+ const tonAmount = fromNano(info.value.coins);
117
+ const parsed = parseMessageBody(outMsg.body);
118
+ if (parsed?.op === OP_CODES.JETTON_TRANSFER) {
119
+ results.push({
120
+ type: "jetton_sent",
121
+ hash,
122
+ jettonAmount: parsed.jettonAmount,
123
+ jettonWallet: to,
124
+ date,
125
+ secondsAgo,
126
+ explorer
127
+ });
128
+ continue;
129
+ }
130
+ if (parsed?.op === OP_CODES.NFT_TRANSFER) {
131
+ results.push({ type: "nft_sent", hash, nftAddress: to, date, secondsAgo, explorer });
132
+ continue;
133
+ }
134
+ results.push({
135
+ type: "ton_sent",
136
+ hash,
137
+ amount: `${tonAmount} TON`,
138
+ to,
139
+ comment: parsed?.comment || null,
140
+ date,
141
+ secondsAgo,
142
+ explorer
143
+ });
144
+ }
145
+ if (results.length === 1) return results[0];
146
+ if (results.length > 1) {
147
+ return { type: "multi_send", hash, transfers: results, date, secondsAgo, explorer };
148
+ }
149
+ }
150
+ return { type: "contract_call", hash, date, secondsAgo, explorer };
151
+ });
152
+ }
153
+
154
+ export {
155
+ parseMessageBody,
156
+ formatTransactions
157
+ };
@@ -8,7 +8,7 @@ var MAX_JSON_FIELD_CHARS = 8e3;
8
8
  var MAX_TOTAL_PROMPT_CHARS = 32e3;
9
9
  var VOYAGE_BATCH_SIZE = 128;
10
10
  var SQLITE_CACHE_SIZE_KB = 64e3;
11
- var SQLITE_MMAP_SIZE = 3e10;
11
+ var SQLITE_MMAP_SIZE = 256e6;
12
12
  var SECONDS_PER_DAY = 86400;
13
13
  var SECONDS_PER_HOUR = 3600;
14
14
  var COMPACTION_MAX_MESSAGES = 200;
@@ -0,0 +1,16 @@
1
+ // src/ton/endpoint.ts
2
+ import { getHttpEndpoint } from "@orbs-network/ton-access";
3
+ var ENDPOINT_CACHE_TTL_MS = 6e4;
4
+ var _cache = null;
5
+ async function getCachedHttpEndpoint() {
6
+ if (_cache && Date.now() - _cache.ts < ENDPOINT_CACHE_TTL_MS) {
7
+ return _cache.url;
8
+ }
9
+ const url = await getHttpEndpoint({ network: "mainnet" });
10
+ _cache = { url, ts: Date.now() };
11
+ return url;
12
+ }
13
+
14
+ export {
15
+ getCachedHttpEndpoint
16
+ };