@t402/ton 2.0.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.
- package/dist/cjs/exact/client/index.d.ts +53 -0
- package/dist/cjs/exact/client/index.js +181 -0
- package/dist/cjs/exact/client/index.js.map +1 -0
- package/dist/cjs/exact/facilitator/index.d.ts +79 -0
- package/dist/cjs/exact/facilitator/index.js +331 -0
- package/dist/cjs/exact/facilitator/index.js.map +1 -0
- package/dist/cjs/exact/server/index.d.ts +135 -0
- package/dist/cjs/exact/server/index.js +318 -0
- package/dist/cjs/exact/server/index.js.map +1 -0
- package/dist/cjs/index.d.ts +293 -0
- package/dist/cjs/index.js +442 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/signer-CFiw2DST.d.ts +277 -0
- package/dist/esm/chunk-6LOUEHJT.mjs +192 -0
- package/dist/esm/chunk-6LOUEHJT.mjs.map +1 -0
- package/dist/esm/chunk-RST5PY7E.mjs +85 -0
- package/dist/esm/chunk-RST5PY7E.mjs.map +1 -0
- package/dist/esm/chunk-ZCMWKFVA.mjs +101 -0
- package/dist/esm/chunk-ZCMWKFVA.mjs.map +1 -0
- package/dist/esm/exact/client/index.d.mts +53 -0
- package/dist/esm/exact/client/index.mjs +8 -0
- package/dist/esm/exact/client/index.mjs.map +1 -0
- package/dist/esm/exact/facilitator/index.d.mts +79 -0
- package/dist/esm/exact/facilitator/index.mjs +258 -0
- package/dist/esm/exact/facilitator/index.mjs.map +1 -0
- package/dist/esm/exact/server/index.d.mts +135 -0
- package/dist/esm/exact/server/index.mjs +212 -0
- package/dist/esm/exact/server/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +293 -0
- package/dist/esm/index.mjs +108 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/signer-CFiw2DST.d.mts +277 -0
- package/package.json +99 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { Address, Cell, SendMode } from '@ton/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TON Payment Payload Types
|
|
5
|
+
*
|
|
6
|
+
* Defines the payload structure for TON Jetton payments in the t402 protocol.
|
|
7
|
+
* Uses BOC (Bag of Cells) format for message serialization.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* TON payment payload for the exact scheme (V2)
|
|
11
|
+
* Contains a pre-signed external message for Jetton transfer
|
|
12
|
+
*/
|
|
13
|
+
type ExactTonPayloadV2 = {
|
|
14
|
+
/**
|
|
15
|
+
* Base64 encoded signed external message (BOC format)
|
|
16
|
+
* Contains the complete Jetton transfer message ready for broadcast
|
|
17
|
+
* The message is signed by the client's wallet private key
|
|
18
|
+
*/
|
|
19
|
+
signedBoc: string;
|
|
20
|
+
/**
|
|
21
|
+
* Transfer authorization metadata
|
|
22
|
+
* Provides human-readable and verifiable parameters
|
|
23
|
+
*/
|
|
24
|
+
authorization: {
|
|
25
|
+
/**
|
|
26
|
+
* Sender wallet address (friendly format, bounceable)
|
|
27
|
+
* This is the TON wallet address that will send the Jetton transfer
|
|
28
|
+
*/
|
|
29
|
+
from: string;
|
|
30
|
+
/**
|
|
31
|
+
* Recipient wallet address (friendly format)
|
|
32
|
+
* Final destination for the Jetton tokens
|
|
33
|
+
*/
|
|
34
|
+
to: string;
|
|
35
|
+
/**
|
|
36
|
+
* Jetton master contract address
|
|
37
|
+
* Identifies which Jetton token is being transferred
|
|
38
|
+
*/
|
|
39
|
+
jettonMaster: string;
|
|
40
|
+
/**
|
|
41
|
+
* Jetton amount in smallest units (e.g., 1000000 for 1 USDT with 6 decimals)
|
|
42
|
+
*/
|
|
43
|
+
jettonAmount: string;
|
|
44
|
+
/**
|
|
45
|
+
* TON amount attached for gas (in nanoTON)
|
|
46
|
+
* Required to pay for the internal message execution
|
|
47
|
+
*/
|
|
48
|
+
tonAmount: string;
|
|
49
|
+
/**
|
|
50
|
+
* Unix timestamp (seconds) after which the authorization expires
|
|
51
|
+
* Message will be rejected by the network after this time
|
|
52
|
+
*/
|
|
53
|
+
validUntil: number;
|
|
54
|
+
/**
|
|
55
|
+
* Wallet sequence number at time of signing
|
|
56
|
+
* Prevents replay attacks - each seqno can only be used once
|
|
57
|
+
*/
|
|
58
|
+
seqno: number;
|
|
59
|
+
/**
|
|
60
|
+
* Query ID for the Jetton transfer
|
|
61
|
+
* Used for message correlation and deduplication
|
|
62
|
+
*/
|
|
63
|
+
queryId: string;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Alias for the current payload version
|
|
68
|
+
*/
|
|
69
|
+
type ExactTonPayload = ExactTonPayloadV2;
|
|
70
|
+
/**
|
|
71
|
+
* Result of message verification
|
|
72
|
+
*/
|
|
73
|
+
type VerifyMessageResult = {
|
|
74
|
+
/** Whether the message is valid */
|
|
75
|
+
valid: boolean;
|
|
76
|
+
/** Reason for invalidity (if applicable) */
|
|
77
|
+
reason?: string;
|
|
78
|
+
/** Extracted transfer parameters */
|
|
79
|
+
transfer?: {
|
|
80
|
+
from: string;
|
|
81
|
+
to: string;
|
|
82
|
+
jettonAmount: bigint;
|
|
83
|
+
queryId: bigint;
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Transaction confirmation result
|
|
88
|
+
*/
|
|
89
|
+
type TransactionConfirmation = {
|
|
90
|
+
/** Whether the transaction was confirmed */
|
|
91
|
+
success: boolean;
|
|
92
|
+
/** Logical time of the transaction */
|
|
93
|
+
lt?: bigint;
|
|
94
|
+
/** Transaction hash */
|
|
95
|
+
hash?: string;
|
|
96
|
+
/** Error message if failed */
|
|
97
|
+
error?: string;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* TON Signer Interfaces
|
|
102
|
+
*
|
|
103
|
+
* Defines the signer interfaces for t402 client and facilitator operations.
|
|
104
|
+
* These interfaces abstract away the specific wallet implementation,
|
|
105
|
+
* allowing integration with various TON wallets and signing mechanisms.
|
|
106
|
+
*/
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Parameters for signing a TON internal message
|
|
110
|
+
*/
|
|
111
|
+
type SignMessageParams = {
|
|
112
|
+
/** Destination address (typically Jetton wallet contract) */
|
|
113
|
+
to: Address;
|
|
114
|
+
/** Amount of TON to attach (for gas) in nanoTON */
|
|
115
|
+
value: bigint;
|
|
116
|
+
/** Message body (Jetton transfer cell) */
|
|
117
|
+
body: Cell;
|
|
118
|
+
/** Send mode flags (optional, defaults to PAY_GAS_SEPARATELY) */
|
|
119
|
+
sendMode?: SendMode;
|
|
120
|
+
/** Bounce flag (optional, defaults to true) */
|
|
121
|
+
bounce?: boolean;
|
|
122
|
+
/** Message validity timeout in seconds (optional) */
|
|
123
|
+
timeout?: number;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* ClientTonSigner - Used by t402 clients to sign Jetton transfer messages
|
|
127
|
+
*
|
|
128
|
+
* This interface represents a TON wallet that can:
|
|
129
|
+
* - Sign internal messages for Jetton transfers
|
|
130
|
+
* - Query its own seqno (sequence number)
|
|
131
|
+
*
|
|
132
|
+
* Implementations may include:
|
|
133
|
+
* - WalletContractV4 with private key
|
|
134
|
+
* - TonConnect wallet adapter
|
|
135
|
+
* - Hardware wallet integration
|
|
136
|
+
*/
|
|
137
|
+
type ClientTonSigner = {
|
|
138
|
+
/** The wallet address */
|
|
139
|
+
readonly address: Address;
|
|
140
|
+
/**
|
|
141
|
+
* Sign an internal message for Jetton transfer
|
|
142
|
+
* Returns the complete signed external message ready for broadcast
|
|
143
|
+
*
|
|
144
|
+
* @param params - Message parameters
|
|
145
|
+
* @returns Signed external message as Cell (BOC)
|
|
146
|
+
*/
|
|
147
|
+
signMessage(params: SignMessageParams): Promise<Cell>;
|
|
148
|
+
/**
|
|
149
|
+
* Get current seqno for the wallet
|
|
150
|
+
* Used for replay protection
|
|
151
|
+
*
|
|
152
|
+
* @returns Current sequence number
|
|
153
|
+
*/
|
|
154
|
+
getSeqno(): Promise<number>;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Parameters for verifying a signed message
|
|
158
|
+
*/
|
|
159
|
+
type VerifyMessageParams = {
|
|
160
|
+
/** The signed BOC from client (base64) */
|
|
161
|
+
signedBoc: string;
|
|
162
|
+
/** Expected sender address */
|
|
163
|
+
expectedFrom: string;
|
|
164
|
+
/** Expected Jetton transfer details */
|
|
165
|
+
expectedTransfer: {
|
|
166
|
+
/** Expected Jetton amount */
|
|
167
|
+
jettonAmount: bigint;
|
|
168
|
+
/** Expected destination address */
|
|
169
|
+
destination: string;
|
|
170
|
+
/** Jetton master address */
|
|
171
|
+
jettonMaster: string;
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Parameters for waiting on transaction confirmation
|
|
176
|
+
*/
|
|
177
|
+
type WaitForTransactionParams = {
|
|
178
|
+
/** Address to monitor */
|
|
179
|
+
address: string;
|
|
180
|
+
/** Expected seqno after transaction */
|
|
181
|
+
seqno: number;
|
|
182
|
+
/** Timeout in milliseconds (optional) */
|
|
183
|
+
timeout?: number;
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* FacilitatorTonSigner - Used by t402 facilitators to verify and settle payments
|
|
187
|
+
*
|
|
188
|
+
* This interface combines RPC capabilities with signing abilities:
|
|
189
|
+
* - Query Jetton balances and wallet addresses
|
|
190
|
+
* - Verify signed messages
|
|
191
|
+
* - Broadcast transactions
|
|
192
|
+
* - Wait for confirmations
|
|
193
|
+
*/
|
|
194
|
+
type FacilitatorTonSigner = {
|
|
195
|
+
/**
|
|
196
|
+
* Get all addresses this facilitator can use for signing
|
|
197
|
+
* Enables dynamic address selection for load balancing
|
|
198
|
+
*/
|
|
199
|
+
getAddresses(): readonly string[];
|
|
200
|
+
/**
|
|
201
|
+
* Query Jetton balance for an owner
|
|
202
|
+
*
|
|
203
|
+
* @param params - Owner and Jetton master addresses
|
|
204
|
+
* @returns Balance in smallest units
|
|
205
|
+
*/
|
|
206
|
+
getJettonBalance(params: {
|
|
207
|
+
ownerAddress: string;
|
|
208
|
+
jettonMasterAddress: string;
|
|
209
|
+
}): Promise<bigint>;
|
|
210
|
+
/**
|
|
211
|
+
* Get Jetton wallet address for an owner
|
|
212
|
+
* Derives the associated Jetton wallet contract address
|
|
213
|
+
*
|
|
214
|
+
* @param params - Owner and Jetton master addresses
|
|
215
|
+
* @returns Jetton wallet address
|
|
216
|
+
*/
|
|
217
|
+
getJettonWalletAddress(params: {
|
|
218
|
+
ownerAddress: string;
|
|
219
|
+
jettonMasterAddress: string;
|
|
220
|
+
}): Promise<string>;
|
|
221
|
+
/**
|
|
222
|
+
* Verify a signed message matches expected parameters
|
|
223
|
+
* Validates the BOC structure and transfer details
|
|
224
|
+
*
|
|
225
|
+
* @param params - Verification parameters
|
|
226
|
+
* @returns Verification result
|
|
227
|
+
*/
|
|
228
|
+
verifyMessage(params: VerifyMessageParams): Promise<VerifyMessageResult>;
|
|
229
|
+
/**
|
|
230
|
+
* Send a pre-signed external message to the network
|
|
231
|
+
*
|
|
232
|
+
* @param signedBoc - Base64 encoded signed BOC
|
|
233
|
+
* @returns Transaction hash or identifier
|
|
234
|
+
*/
|
|
235
|
+
sendExternalMessage(signedBoc: string): Promise<string>;
|
|
236
|
+
/**
|
|
237
|
+
* Wait for transaction confirmation
|
|
238
|
+
*
|
|
239
|
+
* @param params - Transaction monitoring parameters
|
|
240
|
+
* @returns Confirmation result
|
|
241
|
+
*/
|
|
242
|
+
waitForTransaction(params: WaitForTransactionParams): Promise<TransactionConfirmation>;
|
|
243
|
+
/**
|
|
244
|
+
* Get current seqno for an address
|
|
245
|
+
*
|
|
246
|
+
* @param address - Wallet address to query
|
|
247
|
+
* @returns Current seqno
|
|
248
|
+
*/
|
|
249
|
+
getSeqno(address: string): Promise<number>;
|
|
250
|
+
/**
|
|
251
|
+
* Check if a wallet is deployed (active)
|
|
252
|
+
*
|
|
253
|
+
* @param address - Address to check
|
|
254
|
+
* @returns true if deployed
|
|
255
|
+
*/
|
|
256
|
+
isDeployed(address: string): Promise<boolean>;
|
|
257
|
+
};
|
|
258
|
+
/**
|
|
259
|
+
* Converts a TON wallet to a ClientTonSigner
|
|
260
|
+
* Identity function for type compatibility
|
|
261
|
+
*
|
|
262
|
+
* @param signer - The signer to convert
|
|
263
|
+
* @returns The same signer with ClientTonSigner type
|
|
264
|
+
*/
|
|
265
|
+
declare function toClientTonSigner(signer: ClientTonSigner): ClientTonSigner;
|
|
266
|
+
/**
|
|
267
|
+
* Creates a FacilitatorTonSigner from a single-address facilitator
|
|
268
|
+
* Wraps the single address in a getAddresses() function for compatibility
|
|
269
|
+
*
|
|
270
|
+
* @param client - Facilitator client with single address
|
|
271
|
+
* @returns FacilitatorTonSigner with getAddresses() support
|
|
272
|
+
*/
|
|
273
|
+
declare function toFacilitatorTonSigner(client: Omit<FacilitatorTonSigner, "getAddresses"> & {
|
|
274
|
+
address: string;
|
|
275
|
+
}): FacilitatorTonSigner;
|
|
276
|
+
|
|
277
|
+
export { type ClientTonSigner as C, type ExactTonPayloadV2 as E, type FacilitatorTonSigner as F, type SignMessageParams as S, type TransactionConfirmation as T, type VerifyMessageParams as V, type WaitForTransactionParams as W, toFacilitatorTonSigner as a, type ExactTonPayload as b, type VerifyMessageResult as c, toClientTonSigner as t };
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var TON_MAINNET_CAIP2 = "ton:mainnet";
|
|
3
|
+
var TON_TESTNET_CAIP2 = "ton:testnet";
|
|
4
|
+
var TON_NETWORKS = [TON_MAINNET_CAIP2, TON_TESTNET_CAIP2];
|
|
5
|
+
var TON_MAINNET_ENDPOINT = "https://toncenter.com/api/v2/jsonRPC";
|
|
6
|
+
var TON_TESTNET_ENDPOINT = "https://testnet.toncenter.com/api/v2/jsonRPC";
|
|
7
|
+
var TON_MAINNET_V4_ENDPOINT = "https://mainnet-v4.tonhubapi.com";
|
|
8
|
+
var TON_TESTNET_V4_ENDPOINT = "https://testnet-v4.tonhubapi.com";
|
|
9
|
+
var NETWORK_ENDPOINTS = {
|
|
10
|
+
[TON_MAINNET_CAIP2]: TON_MAINNET_ENDPOINT,
|
|
11
|
+
[TON_TESTNET_CAIP2]: TON_TESTNET_ENDPOINT
|
|
12
|
+
};
|
|
13
|
+
var NETWORK_V4_ENDPOINTS = {
|
|
14
|
+
[TON_MAINNET_CAIP2]: TON_MAINNET_V4_ENDPOINT,
|
|
15
|
+
[TON_TESTNET_CAIP2]: TON_TESTNET_V4_ENDPOINT
|
|
16
|
+
};
|
|
17
|
+
var JETTON_TRANSFER_OP = 260734629;
|
|
18
|
+
var JETTON_INTERNAL_TRANSFER_OP = 395134233;
|
|
19
|
+
var JETTON_TRANSFER_NOTIFICATION_OP = 1935855772;
|
|
20
|
+
var JETTON_BURN_OP = 1499400124;
|
|
21
|
+
var DEFAULT_JETTON_TRANSFER_TON = 100000000n;
|
|
22
|
+
var DEFAULT_FORWARD_TON = 1n;
|
|
23
|
+
var MIN_JETTON_TRANSFER_TON = 50000000n;
|
|
24
|
+
var MAX_JETTON_TRANSFER_TON = 500000000n;
|
|
25
|
+
var SCHEME_EXACT = "exact";
|
|
26
|
+
var DEFAULT_VALIDITY_DURATION = 3600;
|
|
27
|
+
|
|
28
|
+
// src/utils.ts
|
|
29
|
+
import { Address, beginCell } from "@ton/core";
|
|
30
|
+
function normalizeNetwork(network) {
|
|
31
|
+
if (network.startsWith("ton:")) {
|
|
32
|
+
if (!TON_NETWORKS.includes(network)) {
|
|
33
|
+
throw new Error(`Unsupported TON network: ${network}`);
|
|
34
|
+
}
|
|
35
|
+
return network;
|
|
36
|
+
}
|
|
37
|
+
const mapping = {
|
|
38
|
+
ton: TON_MAINNET_CAIP2,
|
|
39
|
+
"ton-mainnet": TON_MAINNET_CAIP2,
|
|
40
|
+
mainnet: TON_MAINNET_CAIP2,
|
|
41
|
+
"ton-testnet": TON_TESTNET_CAIP2,
|
|
42
|
+
testnet: TON_TESTNET_CAIP2
|
|
43
|
+
};
|
|
44
|
+
const caip2 = mapping[network.toLowerCase()];
|
|
45
|
+
if (!caip2) {
|
|
46
|
+
throw new Error(`Unsupported TON network: ${network}`);
|
|
47
|
+
}
|
|
48
|
+
return caip2;
|
|
49
|
+
}
|
|
50
|
+
function getEndpoint(network) {
|
|
51
|
+
const caip2 = normalizeNetwork(network);
|
|
52
|
+
const endpoint = NETWORK_ENDPOINTS[caip2];
|
|
53
|
+
if (!endpoint) {
|
|
54
|
+
throw new Error(`No endpoint configured for network: ${network}`);
|
|
55
|
+
}
|
|
56
|
+
return endpoint;
|
|
57
|
+
}
|
|
58
|
+
function isTonNetwork(network) {
|
|
59
|
+
try {
|
|
60
|
+
normalizeNetwork(network);
|
|
61
|
+
return true;
|
|
62
|
+
} catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function validateTonAddress(address) {
|
|
67
|
+
try {
|
|
68
|
+
Address.parse(address);
|
|
69
|
+
return true;
|
|
70
|
+
} catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function parseTonAddress(address) {
|
|
75
|
+
return Address.parse(address);
|
|
76
|
+
}
|
|
77
|
+
function addressesEqual(addr1, addr2) {
|
|
78
|
+
try {
|
|
79
|
+
const a1 = Address.parse(addr1);
|
|
80
|
+
const a2 = Address.parse(addr2);
|
|
81
|
+
return a1.equals(a2);
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function formatAddress(address, options) {
|
|
87
|
+
const addr = typeof address === "string" ? Address.parse(address) : address;
|
|
88
|
+
return addr.toString({
|
|
89
|
+
bounceable: options?.bounceable ?? true,
|
|
90
|
+
testOnly: options?.testOnly ?? false
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function convertToJettonAmount(decimalAmount, decimals) {
|
|
94
|
+
const amount = parseFloat(decimalAmount);
|
|
95
|
+
if (isNaN(amount)) {
|
|
96
|
+
throw new Error(`Invalid amount: ${decimalAmount}`);
|
|
97
|
+
}
|
|
98
|
+
const jettonAmount = Math.floor(amount * Math.pow(10, decimals));
|
|
99
|
+
return jettonAmount.toString();
|
|
100
|
+
}
|
|
101
|
+
function convertFromJettonAmount(jettonAmount, decimals) {
|
|
102
|
+
const amount = typeof jettonAmount === "string" ? BigInt(jettonAmount) : jettonAmount;
|
|
103
|
+
const divisor = BigInt(Math.pow(10, decimals));
|
|
104
|
+
const wholePart = amount / divisor;
|
|
105
|
+
const fractionalPart = amount % divisor;
|
|
106
|
+
if (fractionalPart === 0n) {
|
|
107
|
+
return wholePart.toString();
|
|
108
|
+
}
|
|
109
|
+
const fractionalStr = fractionalPart.toString().padStart(decimals, "0");
|
|
110
|
+
return `${wholePart}.${fractionalStr}`.replace(/\.?0+$/, "");
|
|
111
|
+
}
|
|
112
|
+
function generateQueryId() {
|
|
113
|
+
const timestamp = BigInt(Date.now());
|
|
114
|
+
const random = BigInt(Math.floor(Math.random() * 1e6));
|
|
115
|
+
return timestamp * 1000000n + random;
|
|
116
|
+
}
|
|
117
|
+
function buildJettonTransferBody(params) {
|
|
118
|
+
const builder = beginCell().storeUint(JETTON_TRANSFER_OP, 32).storeUint(params.queryId, 64).storeCoins(params.amount).storeAddress(params.destination).storeAddress(params.responseDestination).storeBit(false);
|
|
119
|
+
builder.storeCoins(params.forwardAmount ?? 1n);
|
|
120
|
+
if (params.forwardPayload) {
|
|
121
|
+
builder.storeBit(true).storeRef(params.forwardPayload);
|
|
122
|
+
} else {
|
|
123
|
+
builder.storeBit(false);
|
|
124
|
+
}
|
|
125
|
+
return builder.endCell();
|
|
126
|
+
}
|
|
127
|
+
function parseJettonTransferBody(body) {
|
|
128
|
+
const slice = body.beginParse();
|
|
129
|
+
const op = slice.loadUint(32);
|
|
130
|
+
if (op !== JETTON_TRANSFER_OP) {
|
|
131
|
+
throw new Error(`Not a Jetton transfer message. Expected op ${JETTON_TRANSFER_OP}, got ${op}`);
|
|
132
|
+
}
|
|
133
|
+
const queryId = slice.loadUintBig(64);
|
|
134
|
+
const amount = slice.loadCoins();
|
|
135
|
+
const destination = slice.loadAddress();
|
|
136
|
+
const responseDestination = slice.loadAddress();
|
|
137
|
+
const hasCustomPayload = slice.loadBit();
|
|
138
|
+
if (hasCustomPayload) {
|
|
139
|
+
slice.loadRef();
|
|
140
|
+
}
|
|
141
|
+
const forwardAmount = slice.loadCoins();
|
|
142
|
+
const hasForwardPayload = slice.loadBit();
|
|
143
|
+
const forwardPayload = hasForwardPayload ? slice.loadRef() : void 0;
|
|
144
|
+
return {
|
|
145
|
+
op,
|
|
146
|
+
queryId,
|
|
147
|
+
amount,
|
|
148
|
+
destination,
|
|
149
|
+
responseDestination,
|
|
150
|
+
forwardAmount,
|
|
151
|
+
forwardPayload
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function estimateJettonTransferGas(_params) {
|
|
155
|
+
return 100000000n;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export {
|
|
159
|
+
TON_MAINNET_CAIP2,
|
|
160
|
+
TON_TESTNET_CAIP2,
|
|
161
|
+
TON_NETWORKS,
|
|
162
|
+
TON_MAINNET_ENDPOINT,
|
|
163
|
+
TON_TESTNET_ENDPOINT,
|
|
164
|
+
TON_MAINNET_V4_ENDPOINT,
|
|
165
|
+
TON_TESTNET_V4_ENDPOINT,
|
|
166
|
+
NETWORK_ENDPOINTS,
|
|
167
|
+
NETWORK_V4_ENDPOINTS,
|
|
168
|
+
JETTON_TRANSFER_OP,
|
|
169
|
+
JETTON_INTERNAL_TRANSFER_OP,
|
|
170
|
+
JETTON_TRANSFER_NOTIFICATION_OP,
|
|
171
|
+
JETTON_BURN_OP,
|
|
172
|
+
DEFAULT_JETTON_TRANSFER_TON,
|
|
173
|
+
DEFAULT_FORWARD_TON,
|
|
174
|
+
MIN_JETTON_TRANSFER_TON,
|
|
175
|
+
MAX_JETTON_TRANSFER_TON,
|
|
176
|
+
SCHEME_EXACT,
|
|
177
|
+
DEFAULT_VALIDITY_DURATION,
|
|
178
|
+
normalizeNetwork,
|
|
179
|
+
getEndpoint,
|
|
180
|
+
isTonNetwork,
|
|
181
|
+
validateTonAddress,
|
|
182
|
+
parseTonAddress,
|
|
183
|
+
addressesEqual,
|
|
184
|
+
formatAddress,
|
|
185
|
+
convertToJettonAmount,
|
|
186
|
+
convertFromJettonAmount,
|
|
187
|
+
generateQueryId,
|
|
188
|
+
buildJettonTransferBody,
|
|
189
|
+
parseJettonTransferBody,
|
|
190
|
+
estimateJettonTransferGas
|
|
191
|
+
};
|
|
192
|
+
//# sourceMappingURL=chunk-6LOUEHJT.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/constants.ts","../../src/utils.ts"],"sourcesContent":["/**\n * TON Network Constants\n *\n * This module provides constants for TON blockchain integration including:\n * - CAIP-2 network identifiers\n * - RPC endpoints\n * - Jetton transfer operation codes\n * - Default gas amounts\n */\n\n/**\n * CAIP-2 Network Identifiers for TON\n * Using simple identifiers for mainnet/testnet\n */\nexport const TON_MAINNET_CAIP2 = \"ton:mainnet\";\nexport const TON_TESTNET_CAIP2 = \"ton:testnet\";\n\n/**\n * Supported TON networks\n */\nexport const TON_NETWORKS = [TON_MAINNET_CAIP2, TON_TESTNET_CAIP2] as const;\n\nexport type TonNetwork = (typeof TON_NETWORKS)[number];\n\n/**\n * Default RPC endpoints (TonCenter API v2)\n */\nexport const TON_MAINNET_ENDPOINT = \"https://toncenter.com/api/v2/jsonRPC\";\nexport const TON_TESTNET_ENDPOINT = \"https://testnet.toncenter.com/api/v2/jsonRPC\";\n\n/**\n * TON API v4 endpoints (for @ton/ton TonClient4)\n */\nexport const TON_MAINNET_V4_ENDPOINT = \"https://mainnet-v4.tonhubapi.com\";\nexport const TON_TESTNET_V4_ENDPOINT = \"https://testnet-v4.tonhubapi.com\";\n\n/**\n * Network endpoint mapping\n */\nexport const NETWORK_ENDPOINTS: Record<string, string> = {\n [TON_MAINNET_CAIP2]: TON_MAINNET_ENDPOINT,\n [TON_TESTNET_CAIP2]: TON_TESTNET_ENDPOINT,\n};\n\nexport const NETWORK_V4_ENDPOINTS: Record<string, string> = {\n [TON_MAINNET_CAIP2]: TON_MAINNET_V4_ENDPOINT,\n [TON_TESTNET_CAIP2]: TON_TESTNET_V4_ENDPOINT,\n};\n\n/**\n * Jetton Transfer Operation Codes (TEP-74)\n * @see https://github.com/ton-blockchain/TEPs/blob/master/text/0074-jettons-standard.md\n */\nexport const JETTON_TRANSFER_OP = 0x0f8a7ea5; // transfer\nexport const JETTON_INTERNAL_TRANSFER_OP = 0x178d4519; // internal_transfer\nexport const JETTON_TRANSFER_NOTIFICATION_OP = 0x7362d09c; // transfer_notification\nexport const JETTON_BURN_OP = 0x595f07bc; // burn\n\n/**\n * Default gas amounts for Jetton transfers\n * TON requires attaching TON for gas to internal messages\n */\nexport const DEFAULT_JETTON_TRANSFER_TON = 100_000_000n; // 0.1 TON for gas\nexport const DEFAULT_FORWARD_TON = 1n; // Minimal forward amount (notification)\nexport const MIN_JETTON_TRANSFER_TON = 50_000_000n; // 0.05 TON minimum\n\n/**\n * Maximum gas amounts to prevent excessive fees\n */\nexport const MAX_JETTON_TRANSFER_TON = 500_000_000n; // 0.5 TON maximum\n\n/**\n * Scheme identifier for exact payments\n */\nexport const SCHEME_EXACT = \"exact\";\n\n/**\n * Default timeout for payment validity (in seconds)\n */\nexport const DEFAULT_VALIDITY_DURATION = 3600; // 1 hour\n\n/**\n * Address format constants\n */\nexport const TON_ADDRESS_LENGTH = 48; // Friendly format length (base64url)\nexport const TON_RAW_ADDRESS_LENGTH = 66; // Raw format: workchain:hash (0:64hex)\n","/**\n * TON Utility Functions\n *\n * Helper functions for TON address handling, message building,\n * and network operations.\n */\n\nimport { Address, beginCell, Cell } from \"@ton/core\";\nimport type { Network } from \"@t402/core/types\";\nimport {\n TON_MAINNET_CAIP2,\n TON_TESTNET_CAIP2,\n TON_NETWORKS,\n NETWORK_ENDPOINTS,\n JETTON_TRANSFER_OP,\n} from \"./constants.js\";\n\n/**\n * Normalize network identifier to CAIP-2 format\n *\n * @param network - Network identifier (may be legacy format)\n * @returns Normalized CAIP-2 network identifier\n * @throws Error if network is not supported\n */\nexport function normalizeNetwork(network: Network): Network {\n // Already in CAIP-2 format\n if (network.startsWith(\"ton:\")) {\n if (!TON_NETWORKS.includes(network as (typeof TON_NETWORKS)[number])) {\n throw new Error(`Unsupported TON network: ${network}`);\n }\n return network as Network;\n }\n\n // Handle legacy format conversions\n const mapping: Record<string, Network> = {\n ton: TON_MAINNET_CAIP2 as Network,\n \"ton-mainnet\": TON_MAINNET_CAIP2 as Network,\n mainnet: TON_MAINNET_CAIP2 as Network,\n \"ton-testnet\": TON_TESTNET_CAIP2 as Network,\n testnet: TON_TESTNET_CAIP2 as Network,\n };\n\n const caip2 = mapping[network.toLowerCase()];\n if (!caip2) {\n throw new Error(`Unsupported TON network: ${network}`);\n }\n return caip2;\n}\n\n/**\n * Get RPC endpoint for a network\n *\n * @param network - Network identifier\n * @returns RPC endpoint URL\n */\nexport function getEndpoint(network: Network): string {\n const caip2 = normalizeNetwork(network);\n const endpoint = NETWORK_ENDPOINTS[caip2];\n if (!endpoint) {\n throw new Error(`No endpoint configured for network: ${network}`);\n }\n return endpoint;\n}\n\n/**\n * Check if a network identifier is a supported TON network\n *\n * @param network - Network identifier to check\n * @returns true if supported\n */\nexport function isTonNetwork(network: string): boolean {\n try {\n normalizeNetwork(network as Network);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Validate TON address format\n *\n * @param address - Address to validate\n * @returns true if valid TON address\n */\nexport function validateTonAddress(address: string): boolean {\n try {\n Address.parse(address);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Parse TON address from string\n *\n * @param address - Address string (friendly or raw format)\n * @returns Parsed Address object\n * @throws Error if invalid format\n */\nexport function parseTonAddress(address: string): Address {\n return Address.parse(address);\n}\n\n/**\n * Compare two TON addresses for equality\n * Handles different address formats (friendly, raw, bounceable/non-bounceable)\n *\n * @param addr1 - First address\n * @param addr2 - Second address\n * @returns true if addresses are equal\n */\nexport function addressesEqual(addr1: string, addr2: string): boolean {\n try {\n const a1 = Address.parse(addr1);\n const a2 = Address.parse(addr2);\n return a1.equals(a2);\n } catch {\n return false;\n }\n}\n\n/**\n * Format address to friendly format\n *\n * @param address - Address to format\n * @param options - Formatting options\n * @returns Friendly format address string\n */\nexport function formatAddress(\n address: string | Address,\n options?: { bounceable?: boolean; testOnly?: boolean },\n): string {\n const addr = typeof address === \"string\" ? Address.parse(address) : address;\n return addr.toString({\n bounceable: options?.bounceable ?? true,\n testOnly: options?.testOnly ?? false,\n });\n}\n\n/**\n * Convert decimal amount to smallest units (e.g., nano-Jettons)\n *\n * @param decimalAmount - Amount in decimal format (e.g., \"1.50\")\n * @param decimals - Number of decimal places\n * @returns Amount in smallest units as string\n */\nexport function convertToJettonAmount(decimalAmount: string, decimals: number): string {\n const amount = parseFloat(decimalAmount);\n if (isNaN(amount)) {\n throw new Error(`Invalid amount: ${decimalAmount}`);\n }\n const jettonAmount = Math.floor(amount * Math.pow(10, decimals));\n return jettonAmount.toString();\n}\n\n/**\n * Convert smallest units to decimal amount\n *\n * @param jettonAmount - Amount in smallest units\n * @param decimals - Number of decimal places\n * @returns Amount in decimal format as string\n */\nexport function convertFromJettonAmount(jettonAmount: string | bigint, decimals: number): string {\n const amount = typeof jettonAmount === \"string\" ? BigInt(jettonAmount) : jettonAmount;\n const divisor = BigInt(Math.pow(10, decimals));\n const wholePart = amount / divisor;\n const fractionalPart = amount % divisor;\n\n if (fractionalPart === 0n) {\n return wholePart.toString();\n }\n\n const fractionalStr = fractionalPart.toString().padStart(decimals, \"0\");\n return `${wholePart}.${fractionalStr}`.replace(/\\.?0+$/, \"\");\n}\n\n/**\n * Generate a unique query ID for Jetton transfer\n * Uses timestamp + random component for uniqueness\n *\n * @returns BigInt query ID\n */\nexport function generateQueryId(): bigint {\n const timestamp = BigInt(Date.now());\n const random = BigInt(Math.floor(Math.random() * 1000000));\n return timestamp * 1000000n + random;\n}\n\n/**\n * Build Jetton transfer message body (TEP-74)\n *\n * @param params - Transfer parameters\n * @returns Cell containing the transfer message\n */\nexport function buildJettonTransferBody(params: {\n queryId: bigint;\n amount: bigint;\n destination: Address;\n responseDestination: Address;\n forwardAmount?: bigint;\n forwardPayload?: Cell;\n}): Cell {\n const builder = beginCell()\n .storeUint(JETTON_TRANSFER_OP, 32) // op: transfer\n .storeUint(params.queryId, 64) // query_id\n .storeCoins(params.amount) // amount\n .storeAddress(params.destination) // destination\n .storeAddress(params.responseDestination) // response_destination\n .storeBit(false); // no custom payload\n\n // Forward amount (for notification)\n builder.storeCoins(params.forwardAmount ?? 1n);\n\n // Forward payload (optional)\n if (params.forwardPayload) {\n builder.storeBit(true).storeRef(params.forwardPayload);\n } else {\n builder.storeBit(false);\n }\n\n return builder.endCell();\n}\n\n/**\n * Parse Jetton transfer message from Cell\n *\n * @param body - Cell containing the message\n * @returns Parsed transfer parameters\n * @throws Error if not a valid Jetton transfer message\n */\nexport function parseJettonTransferBody(body: Cell): {\n op: number;\n queryId: bigint;\n amount: bigint;\n destination: Address;\n responseDestination: Address;\n forwardAmount: bigint;\n forwardPayload?: Cell;\n} {\n const slice = body.beginParse();\n\n const op = slice.loadUint(32);\n if (op !== JETTON_TRANSFER_OP) {\n throw new Error(`Not a Jetton transfer message. Expected op ${JETTON_TRANSFER_OP}, got ${op}`);\n }\n\n const queryId = slice.loadUintBig(64);\n const amount = slice.loadCoins();\n const destination = slice.loadAddress();\n const responseDestination = slice.loadAddress();\n\n // Skip custom_payload bit\n const hasCustomPayload = slice.loadBit();\n if (hasCustomPayload) {\n slice.loadRef(); // Skip custom payload\n }\n\n const forwardAmount = slice.loadCoins();\n\n // Forward payload\n const hasForwardPayload = slice.loadBit();\n const forwardPayload = hasForwardPayload ? slice.loadRef() : undefined;\n\n return {\n op,\n queryId,\n amount,\n destination,\n responseDestination,\n forwardAmount,\n forwardPayload,\n };\n}\n\n/**\n * Calculate estimated gas for Jetton transfer\n * Based on typical TON network fees\n *\n * @param params - Optional parameters for estimation\n * @returns Estimated gas in nanoTON\n */\nexport function estimateJettonTransferGas(_params?: {\n hasForwardPayload?: boolean;\n}): bigint {\n // Base cost for Jetton transfer (typical)\n // Includes: external message, wallet internal message, Jetton wallet message\n return 100_000_000n; // 0.1 TON (conservative estimate)\n}\n"],"mappings":";AAcO,IAAM,oBAAoB;AAC1B,IAAM,oBAAoB;AAK1B,IAAM,eAAe,CAAC,mBAAmB,iBAAiB;AAO1D,IAAM,uBAAuB;AAC7B,IAAM,uBAAuB;AAK7B,IAAM,0BAA0B;AAChC,IAAM,0BAA0B;AAKhC,IAAM,oBAA4C;AAAA,EACvD,CAAC,iBAAiB,GAAG;AAAA,EACrB,CAAC,iBAAiB,GAAG;AACvB;AAEO,IAAM,uBAA+C;AAAA,EAC1D,CAAC,iBAAiB,GAAG;AAAA,EACrB,CAAC,iBAAiB,GAAG;AACvB;AAMO,IAAM,qBAAqB;AAC3B,IAAM,8BAA8B;AACpC,IAAM,kCAAkC;AACxC,IAAM,iBAAiB;AAMvB,IAAM,8BAA8B;AACpC,IAAM,sBAAsB;AAC5B,IAAM,0BAA0B;AAKhC,IAAM,0BAA0B;AAKhC,IAAM,eAAe;AAKrB,IAAM,4BAA4B;;;ACxEzC,SAAS,SAAS,iBAAuB;AAiBlC,SAAS,iBAAiB,SAA2B;AAE1D,MAAI,QAAQ,WAAW,MAAM,GAAG;AAC9B,QAAI,CAAC,aAAa,SAAS,OAAwC,GAAG;AACpE,YAAM,IAAI,MAAM,4BAA4B,OAAO,EAAE;AAAA,IACvD;AACA,WAAO;AAAA,EACT;AAGA,QAAM,UAAmC;AAAA,IACvC,KAAK;AAAA,IACL,eAAe;AAAA,IACf,SAAS;AAAA,IACT,eAAe;AAAA,IACf,SAAS;AAAA,EACX;AAEA,QAAM,QAAQ,QAAQ,QAAQ,YAAY,CAAC;AAC3C,MAAI,CAAC,OAAO;AACV,UA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AAY;AAG9C,QAAM,mBAAmB,MAAM,QAAQ;AACvC,MAAI,kBAAkB;AACpB,UAAM,QAAQ;AAAA,EAChB;AAEA,QAAM,gBAAgB,MAAM,UAAU;AAGtC,QAAM,oBAAoB,MAAM,QAAQ;AACxC,QAAM,iBAAiB,oBAAoB,MAAM,QAAQ,IAAI;AAE7D,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AASO,SAAS,0BAA0B,SAE/B;AAGT,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {
|
|
2
|
+
TON_MAINNET_CAIP2,
|
|
3
|
+
TON_TESTNET_CAIP2
|
|
4
|
+
} from "./chunk-6LOUEHJT.mjs";
|
|
5
|
+
|
|
6
|
+
// src/tokens.ts
|
|
7
|
+
var USDT_ADDRESSES = {
|
|
8
|
+
// TON Mainnet - Official Tether USDT
|
|
9
|
+
[TON_MAINNET_CAIP2]: "EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs",
|
|
10
|
+
// TON Testnet - Test USDT (may vary)
|
|
11
|
+
[TON_TESTNET_CAIP2]: "kQBqSpvo4S87mX9tTc4FX3Sfqf4uSp3Tx-Fz4RBUfTRWBx"
|
|
12
|
+
};
|
|
13
|
+
var JETTON_REGISTRY = {
|
|
14
|
+
// TON Mainnet
|
|
15
|
+
[TON_MAINNET_CAIP2]: {
|
|
16
|
+
USDT: {
|
|
17
|
+
masterAddress: USDT_ADDRESSES[TON_MAINNET_CAIP2],
|
|
18
|
+
symbol: "USDT",
|
|
19
|
+
name: "Tether USD",
|
|
20
|
+
decimals: 6,
|
|
21
|
+
priority: 1
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
// TON Testnet
|
|
25
|
+
[TON_TESTNET_CAIP2]: {
|
|
26
|
+
USDT: {
|
|
27
|
+
masterAddress: USDT_ADDRESSES[TON_TESTNET_CAIP2],
|
|
28
|
+
symbol: "USDT",
|
|
29
|
+
name: "Tether USD (Testnet)",
|
|
30
|
+
decimals: 6,
|
|
31
|
+
priority: 1
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
function getJettonConfig(network, symbol) {
|
|
36
|
+
return JETTON_REGISTRY[network]?.[symbol.toUpperCase()];
|
|
37
|
+
}
|
|
38
|
+
function getNetworkJettons(network) {
|
|
39
|
+
const jettons = JETTON_REGISTRY[network];
|
|
40
|
+
if (!jettons) return [];
|
|
41
|
+
return Object.values(jettons).sort((a, b) => a.priority - b.priority);
|
|
42
|
+
}
|
|
43
|
+
function getDefaultJetton(network) {
|
|
44
|
+
const jettons = getNetworkJettons(network);
|
|
45
|
+
return jettons[0];
|
|
46
|
+
}
|
|
47
|
+
function getJettonByAddress(network, address) {
|
|
48
|
+
const jettons = JETTON_REGISTRY[network];
|
|
49
|
+
if (!jettons) return void 0;
|
|
50
|
+
return Object.values(jettons).find(
|
|
51
|
+
(j) => j.masterAddress.toLowerCase() === address.toLowerCase()
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
function getNetworksForJetton(symbol) {
|
|
55
|
+
const networks = [];
|
|
56
|
+
for (const [network, jettons] of Object.entries(JETTON_REGISTRY)) {
|
|
57
|
+
if (jettons[symbol.toUpperCase()]) {
|
|
58
|
+
networks.push(network);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return networks;
|
|
62
|
+
}
|
|
63
|
+
function getUsdtNetworks() {
|
|
64
|
+
return getNetworksForJetton("USDT");
|
|
65
|
+
}
|
|
66
|
+
function isNetworkSupported(network) {
|
|
67
|
+
return network in JETTON_REGISTRY;
|
|
68
|
+
}
|
|
69
|
+
function getSupportedNetworks() {
|
|
70
|
+
return Object.keys(JETTON_REGISTRY);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export {
|
|
74
|
+
USDT_ADDRESSES,
|
|
75
|
+
JETTON_REGISTRY,
|
|
76
|
+
getJettonConfig,
|
|
77
|
+
getNetworkJettons,
|
|
78
|
+
getDefaultJetton,
|
|
79
|
+
getJettonByAddress,
|
|
80
|
+
getNetworksForJetton,
|
|
81
|
+
getUsdtNetworks,
|
|
82
|
+
isNetworkSupported,
|
|
83
|
+
getSupportedNetworks
|
|
84
|
+
};
|
|
85
|
+
//# sourceMappingURL=chunk-RST5PY7E.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/tokens.ts"],"sourcesContent":["/**\n * TON Jetton Token Configuration\n *\n * This module provides comprehensive Jetton token definitions including:\n * - USDT (Tether USD on TON)\n * - Network-specific configurations\n * - Helper functions for token lookups\n */\n\nimport { TON_MAINNET_CAIP2, TON_TESTNET_CAIP2 } from \"./constants.js\";\n\n/**\n * Jetton token configuration\n */\nexport interface JettonConfig {\n /** Jetton master contract address (friendly format) */\n masterAddress: string;\n /** Token symbol */\n symbol: string;\n /** Token name */\n name: string;\n /** Number of decimal places */\n decimals: number;\n /** Payment priority (lower = higher priority) */\n priority: number;\n}\n\n/**\n * Network token registry mapping network -> symbol -> config\n */\nexport type NetworkJettonRegistry = Record<string, Record<string, JettonConfig>>;\n\n/**\n * USDT Jetton Master Contract Addresses by Network\n *\n * USDT on TON follows the TEP-74 Jetton standard.\n * @see https://docs.tether.to/tether-on-ton\n */\nexport const USDT_ADDRESSES: Record<string, string> = {\n // TON Mainnet - Official Tether USDT\n [TON_MAINNET_CAIP2]: \"EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDs\",\n // TON Testnet - Test USDT (may vary)\n [TON_TESTNET_CAIP2]: \"kQBqSpvo4S87mX9tTc4FX3Sfqf4uSp3Tx-Fz4RBUfTRWBx\",\n};\n\n/**\n * Complete Jetton registry with all supported tokens per network\n */\nexport const JETTON_REGISTRY: NetworkJettonRegistry = {\n // TON Mainnet\n [TON_MAINNET_CAIP2]: {\n USDT: {\n masterAddress: USDT_ADDRESSES[TON_MAINNET_CAIP2],\n symbol: \"USDT\",\n name: \"Tether USD\",\n decimals: 6,\n priority: 1,\n },\n },\n\n // TON Testnet\n [TON_TESTNET_CAIP2]: {\n USDT: {\n masterAddress: USDT_ADDRESSES[TON_TESTNET_CAIP2],\n symbol: \"USDT\",\n name: \"Tether USD (Testnet)\",\n decimals: 6,\n priority: 1,\n },\n },\n};\n\n/**\n * Get Jetton configuration for a specific token on a network\n *\n * @param network - Network identifier (CAIP-2 format)\n * @param symbol - Token symbol (e.g., \"USDT\")\n * @returns Jetton configuration or undefined\n */\nexport function getJettonConfig(network: string, symbol: string): JettonConfig | undefined {\n return JETTON_REGISTRY[network]?.[symbol.toUpperCase()];\n}\n\n/**\n * Get all Jettons available on a network\n *\n * @param network - Network identifier\n * @returns Array of Jetton configurations sorted by priority\n */\nexport function getNetworkJettons(network: string): JettonConfig[] {\n const jettons = JETTON_REGISTRY[network];\n if (!jettons) return [];\n return Object.values(jettons).sort((a, b) => a.priority - b.priority);\n}\n\n/**\n * Get the default/preferred Jetton for a network\n * Prefers USDT based on priority\n *\n * @param network - Network identifier\n * @returns Default Jetton configuration or undefined\n */\nexport function getDefaultJetton(network: string): JettonConfig | undefined {\n const jettons = getNetworkJettons(network);\n return jettons[0]; // Already sorted by priority\n}\n\n/**\n * Get Jetton by master contract address on a network\n *\n * @param network - Network identifier\n * @param address - Jetton master contract address\n * @returns Jetton configuration or undefined\n */\nexport function getJettonByAddress(network: string, address: string): JettonConfig | undefined {\n const jettons = JETTON_REGISTRY[network];\n if (!jettons) return undefined;\n\n // Normalize address comparison (case-insensitive for base64)\n return Object.values(jettons).find(\n (j) => j.masterAddress.toLowerCase() === address.toLowerCase(),\n );\n}\n\n/**\n * Get all networks that support a specific Jetton\n *\n * @param symbol - Token symbol\n * @returns Array of network identifiers\n */\nexport function getNetworksForJetton(symbol: string): string[] {\n const networks: string[] = [];\n for (const [network, jettons] of Object.entries(JETTON_REGISTRY)) {\n if (jettons[symbol.toUpperCase()]) {\n networks.push(network);\n }\n }\n return networks;\n}\n\n/**\n * Get USDT networks on TON\n *\n * @returns Array of networks supporting USDT\n */\nexport function getUsdtNetworks(): string[] {\n return getNetworksForJetton(\"USDT\");\n}\n\n/**\n * Check if a network is supported\n *\n * @param network - Network identifier to check\n * @returns true if network has configured Jettons\n */\nexport function isNetworkSupported(network: string): boolean {\n return network in JETTON_REGISTRY;\n}\n\n/**\n * Get all supported networks\n *\n * @returns Array of all supported network identifiers\n */\nexport function getSupportedNetworks(): string[] {\n return Object.keys(JETTON_REGISTRY);\n}\n"],"mappings":";;;;;;AAsCO,IAAM,iBAAyC;AAAA;AAAA,EAEpD,CAAC,iBAAiB,GAAG;AAAA;AAAA,EAErB,CAAC,iBAAiB,GAAG;AACvB;AAKO,IAAM,kBAAyC;AAAA;AAAA,EAEpD,CAAC,iBAAiB,GAAG;AAAA,IACnB,MAAM;AAAA,MACJ,eAAe,eAAe,iBAAiB;AAAA,MAC/C,QAAQ;AAAA,MACR,MAAM;AAAA,MACN,UAAU;AAAA,MACV,UAAU;AAAA,IACZ;AAAA,EACF;AAAA;AAAA,EAGA,CAAC,iBAAiB,GAAG;AAAA,IACnB,MAAM;AAAA,MACJ,eAAe,eAAe,iBAAiB;AAAA,MAC/C,QAAQ;AAAA,MACR,MAAM;AAAA,MACN,UAAU;AAAA,MACV,UAAU;AAAA,IACZ;AAAA,EACF;AACF;AASO,SAAS,gBAAgB,SAAiB,QAA0C;AACzF,SAAO,gBAAgB,OAAO,IAAI,OAAO,YAAY,CAAC;AACxD;AAQO,SAAS,kBAAkB,SAAiC;AACjE,QAAM,UAAU,gBAAgB,OAAO;AACvC,MAAI,CAAC,QAAS,QAAO,CAAC;AACtB,SAAO,OAAO,OAAO,OAAO,EAAE,KAAK,CAAC,GAAG,MAAM,EAAE,WAAW,EAAE,QAAQ;AACtE;AASO,SAAS,iBAAiB,SAA2C;AAC1E,QAAM,UAAU,kBAAkB,OAAO;AACzC,SAAO,QAAQ,CAAC;AAClB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