@tonappchain/sdk 0.7.2-alpha-10 → 0.7.2-alpha-13

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.
Files changed (40) hide show
  1. package/dist/artifacts/dev/ton/internal/build/CrossChainLayer.compiled.json +1 -1
  2. package/dist/src/adapters/BaseContractOpener.d.ts +71 -0
  3. package/dist/src/adapters/BaseContractOpener.js +290 -0
  4. package/dist/src/adapters/LiteClientOpener.d.ts +33 -0
  5. package/dist/src/adapters/LiteClientOpener.js +117 -0
  6. package/dist/src/adapters/OpenerUtils.d.ts +3 -0
  7. package/dist/src/adapters/OpenerUtils.js +39 -0
  8. package/dist/src/adapters/RetryableContractOpener.d.ts +35 -0
  9. package/dist/src/adapters/{retryableContractOpener.js → RetryableContractOpener.js} +60 -12
  10. package/dist/src/adapters/SandboxOpener.d.ts +15 -0
  11. package/dist/src/adapters/SandboxOpener.js +35 -0
  12. package/dist/src/adapters/TonClient4Opener.d.ts +21 -0
  13. package/dist/src/adapters/TonClient4Opener.js +86 -0
  14. package/dist/src/adapters/TonClientOpener.d.ts +16 -0
  15. package/dist/src/adapters/TonClientOpener.js +70 -0
  16. package/dist/src/adapters/index.d.ts +7 -2
  17. package/dist/src/adapters/index.js +7 -2
  18. package/dist/src/index.d.ts +1 -1
  19. package/dist/src/index.js +3 -2
  20. package/dist/src/interfaces/ContractOpener.d.ts +69 -2
  21. package/dist/src/interfaces/ITacSDK.d.ts +5 -0
  22. package/dist/src/sdk/Configuration.js +1 -1
  23. package/dist/src/sdk/Consts.d.ts +5 -2
  24. package/dist/src/sdk/Consts.js +8 -4
  25. package/dist/src/sdk/StartTracking.d.ts +3 -4
  26. package/dist/src/sdk/StartTracking.js +7 -7
  27. package/dist/src/sdk/TONTransactionManager.d.ts +1 -3
  28. package/dist/src/sdk/TONTransactionManager.js +2 -3
  29. package/dist/src/sdk/TacSdk.d.ts +3 -1
  30. package/dist/src/sdk/TacSdk.js +5 -3
  31. package/dist/src/sdk/TxFinalizer.d.ts +1 -8
  32. package/dist/src/sdk/TxFinalizer.js +10 -122
  33. package/dist/src/sdk/Utils.d.ts +5 -0
  34. package/dist/src/sdk/Utils.js +24 -0
  35. package/dist/src/structs/InternalStruct.d.ts +1 -16
  36. package/dist/src/structs/Struct.d.ts +90 -5
  37. package/package.json +1 -1
  38. package/dist/src/adapters/contractOpener.d.ts +0 -24
  39. package/dist/src/adapters/contractOpener.js +0 -310
  40. package/dist/src/adapters/retryableContractOpener.d.ts +0 -29
@@ -1,310 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getAdjacentTransactionsHelper = getAdjacentTransactionsHelper;
4
- exports.liteClientOpener = liteClientOpener;
5
- exports.sandboxOpener = sandboxOpener;
6
- exports.orbsOpener = orbsOpener;
7
- exports.orbsOpener4 = orbsOpener4;
8
- exports.tonClientOpener = tonClientOpener;
9
- const ton_access_1 = require("@orbs-network/ton-access");
10
- const ton_1 = require("@ton/ton");
11
- const ton_lite_client_1 = require("@tonappchain/ton-lite-client");
12
- const artifacts_1 = require("../../artifacts");
13
- const Consts_1 = require("../sdk/Consts");
14
- const Utils_1 = require("../sdk/Utils");
15
- const Struct_1 = require("../structs/Struct");
16
- async function getHttpEndpointWithRetry(network, maxRetries = 5, delay = 1000) {
17
- let lastError;
18
- for (let attempt = 0; attempt < maxRetries; attempt++) {
19
- try {
20
- const tonNetwork = network === Struct_1.Network.MAINNET ? 'mainnet' : 'testnet';
21
- return await (0, ton_access_1.getHttpEndpoint)({ network: tonNetwork });
22
- }
23
- catch (error) {
24
- lastError = error;
25
- if (attempt <= maxRetries) {
26
- await (0, Utils_1.sleep)(delay);
27
- }
28
- }
29
- }
30
- throw lastError || new Error('Failed to get HTTP endpoint after retries');
31
- }
32
- async function getHttpV4EndpointWithRetry(network, maxRetries = 5, delay = 1000) {
33
- let lastError;
34
- for (let attempt = 0; attempt < maxRetries; attempt++) {
35
- try {
36
- const tonNetwork = network === Struct_1.Network.MAINNET ? 'mainnet' : 'testnet';
37
- return await (0, ton_access_1.getHttpV4Endpoint)({ network: tonNetwork });
38
- }
39
- catch (error) {
40
- lastError = error;
41
- if (attempt <= maxRetries) {
42
- await (0, Utils_1.sleep)(delay);
43
- }
44
- }
45
- }
46
- throw lastError || new Error('Failed to get HTTP V4 endpoint after retries');
47
- }
48
- async function findTransactionByHash(addr, targetHashB64, getTransactions, opts) {
49
- const timeoutMs = opts?.timeoutMs ?? Consts_1.DEFAULT_FIND_TX_TIMEOUT_MS; // 60 seconds default
50
- const retryDelayMs = opts?.retryDelayMs ?? Consts_1.DEFAULT_FIND_TX_RETRY_DELAY_MS; // 2 seconds between retries
51
- const deadline = Date.now() + timeoutMs;
52
- const limit = opts?.limit ?? Consts_1.DEFAULT_FIND_TX_LIMIT;
53
- while (Date.now() < deadline) {
54
- const batch = await getTransactions(addr, {
55
- limit,
56
- archival: opts?.archival ?? Consts_1.DEFAULT_FIND_TX_ARCHIVAL,
57
- });
58
- // Check each transaction in the current batch
59
- for (const tx of batch) {
60
- // 1. check tx itself
61
- if (tx.hash().toString('base64') === targetHashB64) {
62
- return tx;
63
- }
64
- // 2. check incoming message(external-in)
65
- if (tx.inMessage && tx.inMessage.info.type === 'external-in') {
66
- const hash = (0, Utils_1.getNormalizedExtMessageHash)(tx.inMessage);
67
- if (hash === targetHashB64) {
68
- return tx;
69
- }
70
- }
71
- // 3. check incoming message(internal)
72
- if (tx.inMessage && tx.inMessage.info.type === 'internal') {
73
- const messageCell = (0, ton_1.beginCell)().store((0, ton_1.storeMessage)(tx.inMessage)).endCell();
74
- const hash = messageCell.hash();
75
- if (hash.toString('base64') === targetHashB64) {
76
- return tx;
77
- }
78
- }
79
- }
80
- if (Date.now() < deadline) {
81
- await (0, Utils_1.sleep)(retryDelayMs);
82
- }
83
- }
84
- return null;
85
- }
86
- async function getAdjacentTransactionsHelper(addr, hashB64, getTransactions, opts) {
87
- // 1. Find the root transaction
88
- const rootTx = await findTransactionByHash(addr, hashB64, getTransactions, opts);
89
- if (!rootTx)
90
- return [];
91
- const adjacent = [];
92
- // 2. Follow every outgoing message
93
- for (const msg of rootTx.outMessages.values()) {
94
- const dst = msg.info.dest;
95
- if (!dst || dst instanceof ton_1.ExternalAddress)
96
- continue;
97
- const msgHashB64 = (0, ton_1.beginCell)().store((0, ton_1.storeMessage)(msg)).endCell().hash().toString('base64');
98
- const tx = await findTransactionByHash(dst, msgHashB64, getTransactions, opts);
99
- if (tx)
100
- adjacent.push(tx);
101
- }
102
- // 3. Optional: follow the incoming message (if it exists and is internal)
103
- if (rootTx.inMessage?.info.type === 'internal') {
104
- // The incoming message belongs to the sender's out-message list,
105
- const msgHashB64 = (0, ton_1.beginCell)().store((0, ton_1.storeMessage)(rootTx.inMessage)).endCell().hash().toString('base64');
106
- const tx = await findTransactionByHash(rootTx.inMessage.info.src, msgHashB64, getTransactions, opts);
107
- if (tx)
108
- adjacent.push(tx);
109
- }
110
- return adjacent;
111
- }
112
- function intToIP(int) {
113
- const part1 = int & 255;
114
- const part2 = (int >> 8) & 255;
115
- const part3 = (int >> 16) & 255;
116
- const part4 = (int >> 24) & 255;
117
- return part4 + '.' + part3 + '.' + part2 + '.' + part1;
118
- }
119
- async function getDefaultLiteServers(network) {
120
- const url = network === Struct_1.Network.TESTNET || network === Struct_1.Network.DEV
121
- ? artifacts_1.testnet.DEFAULT_LITESERVERS
122
- : artifacts_1.mainnet.DEFAULT_LITESERVERS;
123
- const resp = await fetch(url);
124
- const liteClients = await resp.json();
125
- return liteClients.liteservers;
126
- }
127
- async function liteClientOpener(options) {
128
- const liteservers = 'liteservers' in options ? options.liteservers : await getDefaultLiteServers(options.network);
129
- const engines = [];
130
- for (const server of liteservers) {
131
- const engine = await ton_lite_client_1.LiteSingleEngine.create({
132
- host: `tcp://${intToIP(server.ip)}:${server.port}`,
133
- publicKey: Buffer.from(server.id.key, 'base64'),
134
- });
135
- engines.push(engine);
136
- }
137
- const engine = new ton_lite_client_1.LiteRoundRobinEngine(engines);
138
- const client = new ton_lite_client_1.LiteClient({ engine });
139
- const closeConnections = () => {
140
- engine.close();
141
- };
142
- const getTransactions = async (address, opts) => {
143
- const txsBuffered = await client
144
- .getAccountTransactions(address, opts.lt ?? '', opts.hash ? Buffer.from(opts.hash, 'base64') : Buffer.alloc(0), opts.limit)
145
- .then((r) => r.transactions);
146
- const cell = ton_1.Cell.fromBoc(txsBuffered);
147
- return cell.map((c) => (0, ton_1.loadTransaction)(c.beginParse()));
148
- };
149
- return {
150
- getContractState: async (addr) => {
151
- const block = await client.getMasterchainInfo();
152
- const state = await client.getAccountState(addr, block.last);
153
- const accountState = state.state?.storage?.state;
154
- const code = accountState?.type === 'active' ? accountState?.state?.code?.toBoc() : null;
155
- return {
156
- balance: state.balance.coins,
157
- state: state.state.storage.state.type === 'uninit' ? 'uninitialized' : state.state.storage.state.type,
158
- code: code ?? null,
159
- };
160
- },
161
- open: (contract) => client.open(contract),
162
- closeConnections,
163
- getTransactionByHash: async (addr, hash, opts) => {
164
- return await findTransactionByHash(addr, hash, getTransactions, opts);
165
- },
166
- getAdjacentTransactions: async (addr, hash, opts) => getAdjacentTransactionsHelper(addr, hash, getTransactions, opts),
167
- getAddressInformation: async (addr) => {
168
- const block = await client.getMasterchainInfo();
169
- const state = await client.getAccountState(addr, block.last);
170
- return {
171
- lastTransaction: {
172
- lt: state.lastTx?.lt.toString() ?? '',
173
- hash: Buffer.from(state.lastTx?.hash.toString(16) ?? '', 'hex').toString('base64'),
174
- },
175
- };
176
- },
177
- getConfig: async () => {
178
- const block = await client.getMasterchainInfo();
179
- const { config } = await client.getConfig(block.last);
180
- return (0, ton_1.beginCell)().storeDictDirect(config).endCell().toBoc().toString('base64');
181
- },
182
- };
183
- }
184
- function sandboxOpener(blockchain) {
185
- return {
186
- open: (contract) => blockchain.openContract(contract),
187
- getContractState: async (address) => {
188
- const state = await blockchain.provider(address).getState();
189
- return {
190
- balance: state.balance,
191
- code: 'code' in state.state ? (state.state.code ?? null) : null,
192
- state: state.state.type === 'uninit' ? 'uninitialized' : state.state.type,
193
- };
194
- },
195
- getTransactionByHash: () => {
196
- throw new Error('Not implemented.');
197
- },
198
- getAdjacentTransactions() {
199
- throw new Error('Not implemented.');
200
- },
201
- getAddressInformation: async () => {
202
- throw new Error('Not implemented.');
203
- },
204
- getConfig: async () => {
205
- return blockchain.config.toBoc().toString('base64');
206
- },
207
- };
208
- }
209
- async function orbsOpener(network) {
210
- const endpoint = await getHttpEndpointWithRetry(network);
211
- const client = new ton_1.TonClient({ endpoint });
212
- return {
213
- open: client.open,
214
- getContractState: client.getContractState,
215
- getTransactionByHash: async (addr, hash, opts) => {
216
- return await findTransactionByHash(addr, hash, client.getTransactions, opts);
217
- },
218
- getAdjacentTransactions: async (addr, hash, opts) => getAdjacentTransactionsHelper(addr, hash, client.getTransactions, opts),
219
- getAddressInformation: async (addr) => {
220
- const state = await client.getContractState(addr);
221
- return {
222
- lastTransaction: {
223
- lt: state.lastTransaction?.lt ?? '',
224
- hash: state.lastTransaction?.hash ?? '',
225
- },
226
- };
227
- },
228
- getConfig: async () => {
229
- const info = await client.getMasterchainInfo();
230
- // TonClient class does not have methods to get config
231
- const url = new URL('getConfigAll', endpoint);
232
- url.searchParams.append('seqno', info.latestSeqno.toString());
233
- const result = await fetch(url);
234
- const body = await result.json();
235
- return body.result.config.bytes;
236
- },
237
- };
238
- }
239
- async function orbsOpener4(network, timeout = 10000) {
240
- const endpoint = await getHttpV4EndpointWithRetry(network);
241
- const client4 = new ton_1.TonClient4({ endpoint, timeout });
242
- const getTransactions = async (address, opts) => {
243
- return client4
244
- .getAccountTransactions(address, opts.lt ? BigInt(opts.lt) : 0n, opts.hash ? Buffer.from(opts.hash, 'base64') : Buffer.alloc(0))
245
- .then((res) => res.map((t) => t.tx));
246
- };
247
- return {
248
- open: (contract) => client4.open(contract),
249
- getContractState: async (address) => {
250
- const latestBlock = await client4.getLastBlock();
251
- const latestBlockNumber = latestBlock.last.seqno;
252
- const state = await client4.getAccount(latestBlockNumber, address);
253
- return {
254
- balance: BigInt(state.account.balance.coins),
255
- code: 'code' in state.account.state && state.account.state.code !== null
256
- ? Buffer.from(state.account.state.code, 'base64')
257
- : null,
258
- state: state.account.state.type === 'uninit' ? 'uninitialized' : state.account.state.type,
259
- };
260
- },
261
- getTransactionByHash: async (addr, hash, opts) => {
262
- return await findTransactionByHash(addr, hash, getTransactions, opts);
263
- },
264
- getAdjacentTransactions: async (addr, hash, opts) => getAdjacentTransactionsHelper(addr, hash, getTransactions, opts),
265
- getAddressInformation: async (addr) => {
266
- const latestBlock = await client4.getLastBlock();
267
- const latestBlockNumber = latestBlock.last.seqno;
268
- const state = await client4.getAccount(latestBlockNumber, addr);
269
- return {
270
- lastTransaction: {
271
- lt: state.account.last?.lt ?? '',
272
- hash: state.account.last?.hash ?? '',
273
- },
274
- };
275
- },
276
- getConfig: async () => {
277
- const block = await client4.getLastBlock();
278
- const { config } = await client4.getConfig(block.last.seqno);
279
- return config.cell;
280
- },
281
- };
282
- }
283
- function tonClientOpener(client) {
284
- return {
285
- open: client.open.bind(client),
286
- getContractState: client.getContractState.bind(client),
287
- getTransactionByHash: async (addr, hash, opts) => {
288
- return await findTransactionByHash(addr, hash, client.getTransactions.bind(client), opts);
289
- },
290
- getAdjacentTransactions: async (addr, hash, opts) => getAdjacentTransactionsHelper(addr, hash, client.getTransactions.bind(client), opts),
291
- getAddressInformation: async (addr) => {
292
- const state = await client.getContractState(addr);
293
- return {
294
- lastTransaction: {
295
- lt: state.lastTransaction?.lt ?? '',
296
- hash: state.lastTransaction?.hash ?? '',
297
- },
298
- };
299
- },
300
- getConfig: async () => {
301
- const info = await client.getMasterchainInfo();
302
- // TonClient class does not have methods to get config
303
- const url = new URL('getConfigAll', client.parameters.endpoint);
304
- url.searchParams.append('seqno', info.latestSeqno.toString());
305
- const result = await fetch(url);
306
- const body = await result.json();
307
- return body.result.config.bytes;
308
- },
309
- };
310
- }
@@ -1,29 +0,0 @@
1
- import { SandboxContract } from '@ton/sandbox';
2
- import { Address, Contract, OpenedContract, Transaction } from '@ton/ton';
3
- import { ContractOpener } from '../interfaces';
4
- import { AddressInformation, GetTransactionsOptions } from '../structs/InternalStruct';
5
- import { ContractState, Network } from '../structs/Struct';
6
- export interface OpenerConfig {
7
- /** Underlying opener implementation to use for this slot. */
8
- opener: ContractOpener;
9
- /** Number of retry attempts before falling back to the next opener. */
10
- retries: number;
11
- /** Delay in milliseconds between retries for this opener. */
12
- retryDelay: number;
13
- }
14
- export declare class RetryableContractOpener implements ContractOpener {
15
- private readonly openerConfigs;
16
- constructor(openerConfigs: OpenerConfig[]);
17
- getTransactionByHash(address: Address, hash: string, opts: GetTransactionsOptions): Promise<Transaction | null>;
18
- getAdjacentTransactions(address: Address, hash: string): Promise<Transaction[]>;
19
- open<T extends Contract>(src: T): OpenedContract<T> | SandboxContract<T>;
20
- getContractState(address: Address): Promise<ContractState>;
21
- getAddressInformation(address: Address): Promise<AddressInformation>;
22
- getConfig(): Promise<string>;
23
- closeConnections(): void;
24
- private executeWithFallback;
25
- private tryWithRetries;
26
- private createRetryableContract;
27
- private callMethodAcrossOpeners;
28
- }
29
- export declare function createDefaultRetryableOpener(tonRpcEndpoint: string, networkType: Network, maxRetries?: number, retryDelay?: number): Promise<ContractOpener>;