@stableflow/wallet-ton 3.0.0-beta-02a59f37a0-20260515
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/README.md +30 -0
- package/dist/index.d.mts +55 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.js +1072 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1059 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +47 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1072 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var ton = require('@ton/ton');
|
|
6
|
+
var bigintBuffer = require('bigint-buffer');
|
|
7
|
+
var buffer = require('buffer');
|
|
8
|
+
var core = require('@stableflow/core');
|
|
9
|
+
var lzTonSdkV2 = require('@layerzerolabs/lz-ton-sdk-v2');
|
|
10
|
+
var UlnArtifact = require('@layerzerolabs/lz-ton-sdk-v2/artifacts/Uln.compiled.json');
|
|
11
|
+
var EndpointArtifact = require('@layerzerolabs/lz-ton-sdk-v2/artifacts/Endpoint.compiled.json');
|
|
12
|
+
var UlnConnectionArtifact = require('@layerzerolabs/lz-ton-sdk-v2/artifacts/UlnConnection.compiled.json');
|
|
13
|
+
var ChannelArtifact = require('@layerzerolabs/lz-ton-sdk-v2/artifacts/Channel.compiled.json');
|
|
14
|
+
var Big = require('big.js');
|
|
15
|
+
var bridges = require('@stableflow/bridges');
|
|
16
|
+
var utilsSolana = require('@stableflow/utils-solana');
|
|
17
|
+
|
|
18
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
|
+
|
|
20
|
+
var UlnArtifact__default = /*#__PURE__*/_interopDefault(UlnArtifact);
|
|
21
|
+
var EndpointArtifact__default = /*#__PURE__*/_interopDefault(EndpointArtifact);
|
|
22
|
+
var UlnConnectionArtifact__default = /*#__PURE__*/_interopDefault(UlnConnectionArtifact);
|
|
23
|
+
var ChannelArtifact__default = /*#__PURE__*/_interopDefault(ChannelArtifact);
|
|
24
|
+
var Big__default = /*#__PURE__*/_interopDefault(Big);
|
|
25
|
+
|
|
26
|
+
// src/ton.ts
|
|
27
|
+
var TONCENTER_API = "https://toncenter.com/api/v3";
|
|
28
|
+
async function pollTransactionByBoc(boc, options = {}) {
|
|
29
|
+
const { maxPollCount = 60, pollInterval = 3e3 } = options;
|
|
30
|
+
const cs = new core.Csl(core.OpenAPI.DEBUG);
|
|
31
|
+
const csl = cs.log;
|
|
32
|
+
const bocCell = ton.Cell.fromBoc(buffer.Buffer.from(boc, "base64"))[0];
|
|
33
|
+
const messageHash = bocCell.hash().toString("hex");
|
|
34
|
+
const url = `${TONCENTER_API}/transactionsByMessage?msg_hash=${messageHash}&direction=in&limit=1`;
|
|
35
|
+
for (let i = 0; i < maxPollCount; i++) {
|
|
36
|
+
csl("TON pollTransactionByBoc", "rose-600", "polling transaction status (%s), %d times", messageHash, i + 1);
|
|
37
|
+
const response = await fetch(url);
|
|
38
|
+
const data = await response.json();
|
|
39
|
+
csl("TON pollTransactionByBoc", "rose-600", "polling transaction response: %o", data);
|
|
40
|
+
if (data.transactions && data.transactions.length > 0) {
|
|
41
|
+
const txHash = data.transactions[0].hash;
|
|
42
|
+
const hexHash = buffer.Buffer.from(txHash, "base64").toString("hex");
|
|
43
|
+
return {
|
|
44
|
+
hexHash,
|
|
45
|
+
txHash,
|
|
46
|
+
transaction: data.transactions[0]
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
50
|
+
}
|
|
51
|
+
throw new Error(
|
|
52
|
+
"Query timeout, transaction hash not found. Please check if the wallet transaction was successful."
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
var buildJettonWalletTransferBody = (params) => {
|
|
56
|
+
const {
|
|
57
|
+
memo,
|
|
58
|
+
amount,
|
|
59
|
+
recipient,
|
|
60
|
+
refundTo,
|
|
61
|
+
forwardTonAmount,
|
|
62
|
+
forwardPayload
|
|
63
|
+
} = params;
|
|
64
|
+
let _forwardPayload = forwardPayload;
|
|
65
|
+
if (!_forwardPayload) {
|
|
66
|
+
_forwardPayload = ton.beginCell().endCell();
|
|
67
|
+
if (memo) {
|
|
68
|
+
_forwardPayload = ton.beginCell().storeUint(0, 32).storeStringTail(memo).endCell();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return ton.beginCell().storeUint(260734629, 32).storeUint(0, 64).storeCoins(BigInt(amount)).storeAddress(ton.Address.parse(recipient)).storeAddress(ton.Address.parse(refundTo)).storeBit(0).storeCoins(forwardTonAmount ?? 0n).storeBit(1).storeRef(_forwardPayload).endCell();
|
|
72
|
+
};
|
|
73
|
+
var tonObjects = {
|
|
74
|
+
OFTSend: {
|
|
75
|
+
name: "OFTSend",
|
|
76
|
+
0: {
|
|
77
|
+
fieldName: "OFTSend::dstEid",
|
|
78
|
+
fieldType: "cl::t::uint32"
|
|
79
|
+
},
|
|
80
|
+
1: {
|
|
81
|
+
fieldName: "OFTSend::to",
|
|
82
|
+
fieldType: "cl::t::address"
|
|
83
|
+
},
|
|
84
|
+
2: {
|
|
85
|
+
fieldName: "OFTSend::minAmount",
|
|
86
|
+
fieldType: "cl::t::coins"
|
|
87
|
+
},
|
|
88
|
+
3: {
|
|
89
|
+
fieldName: "OFTSend::nativeFee",
|
|
90
|
+
fieldType: "cl::t::coins"
|
|
91
|
+
},
|
|
92
|
+
4: {
|
|
93
|
+
fieldName: "OFTSend::zroFee",
|
|
94
|
+
fieldType: "cl::t::coins"
|
|
95
|
+
},
|
|
96
|
+
5: {
|
|
97
|
+
fieldName: "OFTSend::extraOptions",
|
|
98
|
+
fieldType: "cl::t::objRef"
|
|
99
|
+
},
|
|
100
|
+
6: {
|
|
101
|
+
fieldName: "OFTSend::composeMessage",
|
|
102
|
+
fieldType: "cl::t::cellRef"
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
UsdtOFT: {
|
|
106
|
+
name: "usdtOFT",
|
|
107
|
+
0: {
|
|
108
|
+
fieldName: "UsdtOFT::oAppStorage",
|
|
109
|
+
fieldType: "cl::t::objRef"
|
|
110
|
+
},
|
|
111
|
+
1: {
|
|
112
|
+
fieldName: "UsdtOFT::credits",
|
|
113
|
+
fieldType: "cl::t::objRef"
|
|
114
|
+
},
|
|
115
|
+
2: {
|
|
116
|
+
fieldName: "UsdtOFT::contractBalance",
|
|
117
|
+
fieldType: "cl::t::coins"
|
|
118
|
+
},
|
|
119
|
+
3: {
|
|
120
|
+
fieldName: "UsdtOFT::feeBalance",
|
|
121
|
+
fieldType: "cl::t::coins"
|
|
122
|
+
},
|
|
123
|
+
4: {
|
|
124
|
+
fieldName: "UsdtOFT::feeBps",
|
|
125
|
+
fieldType: "cl::t::uint16"
|
|
126
|
+
},
|
|
127
|
+
5: {
|
|
128
|
+
fieldName: "UsdtOFT::contractWalletAddress",
|
|
129
|
+
fieldType: "cl::t::address"
|
|
130
|
+
},
|
|
131
|
+
6: {
|
|
132
|
+
fieldName: "UsdtOFT::plannerAddress",
|
|
133
|
+
fieldType: "cl::t::address"
|
|
134
|
+
},
|
|
135
|
+
7: {
|
|
136
|
+
fieldName: "UsdtOFT::gasAsserts",
|
|
137
|
+
fieldType: "cl::t::objRef"
|
|
138
|
+
},
|
|
139
|
+
8: {
|
|
140
|
+
fieldName: "UsdtOFT::costAsserts",
|
|
141
|
+
fieldType: "cl::t::objRef"
|
|
142
|
+
},
|
|
143
|
+
9: {
|
|
144
|
+
fieldName: "UsdtOFT::recoverRequest",
|
|
145
|
+
fieldType: "cl::t::objRef"
|
|
146
|
+
},
|
|
147
|
+
10: {
|
|
148
|
+
fieldName: "UsdtOFT::lpAdminAddress",
|
|
149
|
+
fieldType: "cl::t::address"
|
|
150
|
+
},
|
|
151
|
+
11: {
|
|
152
|
+
fieldName: "UsdtOFT::maxComposeMessageBytes",
|
|
153
|
+
fieldType: "cl::t::uint32"
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
GasAsserts: {
|
|
157
|
+
name: "GasAssert",
|
|
158
|
+
0: {
|
|
159
|
+
fieldName: "GasAsserts::sendOFTGas",
|
|
160
|
+
fieldType: "cl::t::uint32"
|
|
161
|
+
},
|
|
162
|
+
1: {
|
|
163
|
+
fieldName: "GasAsserts::sendOFTGasReceiveGas",
|
|
164
|
+
fieldType: "cl::t::uint32"
|
|
165
|
+
},
|
|
166
|
+
2: {
|
|
167
|
+
fieldName: "GasAsserts::sendCreditsGas",
|
|
168
|
+
fieldType: "cl::t::uint32"
|
|
169
|
+
},
|
|
170
|
+
3: {
|
|
171
|
+
fieldName: "GasAsserts::sendCreditsGasReceiveGas",
|
|
172
|
+
fieldType: "cl::t::uint32"
|
|
173
|
+
},
|
|
174
|
+
4: {
|
|
175
|
+
fieldName: "GasAsserts::lzReceiveExecuteCallbackGas",
|
|
176
|
+
fieldType: "cl::t::uint32"
|
|
177
|
+
},
|
|
178
|
+
5: {
|
|
179
|
+
fieldName: "GasAsserts::sendOFTComposeGas",
|
|
180
|
+
fieldType: "cl::t::uint32"
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
var ulnConfigs = {
|
|
185
|
+
Arbitrum: {
|
|
186
|
+
"confirmations": "2",
|
|
187
|
+
"confirmationsNull": false,
|
|
188
|
+
"executor": "0x0",
|
|
189
|
+
"executorNull": true,
|
|
190
|
+
"maxMessageBytes": "522",
|
|
191
|
+
"optionalDVNs": [],
|
|
192
|
+
"optionalDVNsNull": false,
|
|
193
|
+
"requiredDVNs": [
|
|
194
|
+
"0xd122dec4ec8bd66c68344faf0dd471d727a7d57a21b62051705bbe2e4c272a7",
|
|
195
|
+
"0x4b9f9836004d7f53e52e01602d9c07c14cdf38b8a946b6e813cfea8de10927d"
|
|
196
|
+
],
|
|
197
|
+
"requiredDVNsNull": false,
|
|
198
|
+
"workerQuoteGasLimit": "120000"
|
|
199
|
+
},
|
|
200
|
+
Celo: {
|
|
201
|
+
"confirmations": "2",
|
|
202
|
+
"confirmationsNull": false,
|
|
203
|
+
"executor": "0x0",
|
|
204
|
+
"executorNull": true,
|
|
205
|
+
"maxMessageBytes": "522",
|
|
206
|
+
"optionalDVNs": [],
|
|
207
|
+
"optionalDVNsNull": false,
|
|
208
|
+
"requiredDVNs": [
|
|
209
|
+
"0xd122dec4ec8bd66c68344faf0dd471d727a7d57a21b62051705bbe2e4c272a7",
|
|
210
|
+
"0x4b9f9836004d7f53e52e01602d9c07c14cdf38b8a946b6e813cfea8de10927d"
|
|
211
|
+
],
|
|
212
|
+
"requiredDVNsNull": false,
|
|
213
|
+
"workerQuoteGasLimit": "120000"
|
|
214
|
+
},
|
|
215
|
+
Ethereum: {
|
|
216
|
+
"confirmations": "2",
|
|
217
|
+
"confirmationsNull": false,
|
|
218
|
+
"executor": "0x0",
|
|
219
|
+
"executorNull": true,
|
|
220
|
+
"maxMessageBytes": "522",
|
|
221
|
+
"optionalDVNs": [],
|
|
222
|
+
"optionalDVNsNull": false,
|
|
223
|
+
"requiredDVNs": [
|
|
224
|
+
"0xd122dec4ec8bd66c68344faf0dd471d727a7d57a21b62051705bbe2e4c272a7",
|
|
225
|
+
"0x4b9f9836004d7f53e52e01602d9c07c14cdf38b8a946b6e813cfea8de10927d"
|
|
226
|
+
],
|
|
227
|
+
"requiredDVNsNull": false,
|
|
228
|
+
"workerQuoteGasLimit": "120000"
|
|
229
|
+
},
|
|
230
|
+
Solana: {
|
|
231
|
+
"confirmations": "2",
|
|
232
|
+
"confirmationsNull": false,
|
|
233
|
+
"executor": "0xf9a60ea29c5c9e4643601e8881e850498ee680a413e8ba01d5e55ce1c221024",
|
|
234
|
+
"executorNull": false,
|
|
235
|
+
"maxMessageBytes": "1024",
|
|
236
|
+
"optionalDVNs": [],
|
|
237
|
+
"optionalDVNsNull": true,
|
|
238
|
+
"requiredDVNs": [
|
|
239
|
+
"0xd122dec4ec8bd66c68344faf0dd471d727a7d57a21b62051705bbe2e4c272a7",
|
|
240
|
+
"0x4b9f9836004d7f53e52e01602d9c07c14cdf38b8a946b6e813cfea8de10927d"
|
|
241
|
+
],
|
|
242
|
+
"requiredDVNsNull": false,
|
|
243
|
+
"workerQuoteGasLimit": "0"
|
|
244
|
+
},
|
|
245
|
+
Tron: {
|
|
246
|
+
"confirmations": "2",
|
|
247
|
+
"confirmationsNull": false,
|
|
248
|
+
"executor": "0x0",
|
|
249
|
+
"executorNull": true,
|
|
250
|
+
"maxMessageBytes": "522",
|
|
251
|
+
"optionalDVNs": [],
|
|
252
|
+
"optionalDVNsNull": false,
|
|
253
|
+
"requiredDVNs": [
|
|
254
|
+
"0xd122dec4ec8bd66c68344faf0dd471d727a7d57a21b62051705bbe2e4c272a7",
|
|
255
|
+
"0x4b9f9836004d7f53e52e01602d9c07c14cdf38b8a946b6e813cfea8de10927d"
|
|
256
|
+
],
|
|
257
|
+
"requiredDVNsNull": false,
|
|
258
|
+
"workerQuoteGasLimit": "120000"
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
function to32ByteBuffer(value) {
|
|
262
|
+
if (typeof value === "string") {
|
|
263
|
+
if (!/^(0x)?[0-9A-Fa-f]*$/.test(value)) {
|
|
264
|
+
throw new Error("only hex string is supported");
|
|
265
|
+
}
|
|
266
|
+
let hex = value.replace(/^0x/, "");
|
|
267
|
+
if (hex.length % 2 !== 0) {
|
|
268
|
+
hex = "0" + hex;
|
|
269
|
+
}
|
|
270
|
+
value = bigintBuffer.toBigIntBE(buffer.Buffer.from(hex, "hex"));
|
|
271
|
+
}
|
|
272
|
+
if (value instanceof Uint8Array) {
|
|
273
|
+
value = bigintBuffer.toBigIntBE(buffer.Buffer.from(value));
|
|
274
|
+
}
|
|
275
|
+
const bf = bigintBuffer.toBufferBE(BigInt(value), 66);
|
|
276
|
+
return bf.subarray(-32);
|
|
277
|
+
}
|
|
278
|
+
function bigintToAddress(value) {
|
|
279
|
+
const buf = to32ByteBuffer(value);
|
|
280
|
+
return ton.Address.parse(`0:${buf.toString("hex")}`);
|
|
281
|
+
}
|
|
282
|
+
function parseTonAddress(address) {
|
|
283
|
+
if (address instanceof ton.Address) return address;
|
|
284
|
+
if (typeof address === "bigint" || typeof address === "number") {
|
|
285
|
+
return bigintToAddress(BigInt(address));
|
|
286
|
+
}
|
|
287
|
+
if (address.startsWith("0x")) {
|
|
288
|
+
return bigintToAddress(BigInt(address));
|
|
289
|
+
}
|
|
290
|
+
try {
|
|
291
|
+
return ton.Address.parse(address);
|
|
292
|
+
} catch {
|
|
293
|
+
return bigintToAddress(BigInt(`0x${address}`));
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function bigIntToAddress(address) {
|
|
297
|
+
return parseTonAddress("0x" + address.toString(16));
|
|
298
|
+
}
|
|
299
|
+
function objectBuild(tonObjects2) {
|
|
300
|
+
const generated = lzTonSdkV2.generateBuildClass(tonObjects2);
|
|
301
|
+
return Object.fromEntries(
|
|
302
|
+
Object.keys(tonObjects2).map((key) => [
|
|
303
|
+
key,
|
|
304
|
+
(fields) => generated(key, fields)
|
|
305
|
+
])
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
function objectDecode(tonObjects2) {
|
|
309
|
+
const generated = lzTonSdkV2.generateDecodeClass(tonObjects2);
|
|
310
|
+
return Object.fromEntries(
|
|
311
|
+
Object.keys(tonObjects2).map((key) => [
|
|
312
|
+
key,
|
|
313
|
+
(cell) => generated(key, cell)
|
|
314
|
+
])
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
function buildTonTransferCell(opts) {
|
|
318
|
+
const builder = ton.beginCell().storeUint(260734629, 32).storeUint(opts.queryId ?? 69, 64).storeCoins(opts.jettonAmount).storeAddress(opts.toAddress).storeAddress(opts.fromAddress).storeUint(0, 1).storeCoins(opts.fwdAmount);
|
|
319
|
+
if (opts.forwardPayload instanceof ton.Cell) {
|
|
320
|
+
builder.storeBit(1);
|
|
321
|
+
builder.storeRef(opts.forwardPayload);
|
|
322
|
+
} else {
|
|
323
|
+
builder.storeBit(0);
|
|
324
|
+
}
|
|
325
|
+
return builder.endCell();
|
|
326
|
+
}
|
|
327
|
+
var TON_EID = 30343;
|
|
328
|
+
var contractCode = {
|
|
329
|
+
uln: ton.Cell.fromBoc(buffer.Buffer.from(UlnArtifact__default.default.hex, "hex"))[0],
|
|
330
|
+
endpoint: ton.Cell.fromBoc(buffer.Buffer.from(EndpointArtifact__default.default.hex, "hex"))[0],
|
|
331
|
+
ulnConnection: ton.Cell.fromBoc(
|
|
332
|
+
buffer.Buffer.from(UlnConnectionArtifact__default.default.hex, "hex")
|
|
333
|
+
)[0],
|
|
334
|
+
channel: ton.Cell.fromBoc(buffer.Buffer.from(ChannelArtifact__default.default.hex, "hex"))[0]
|
|
335
|
+
};
|
|
336
|
+
function computeContractAddress(code, storage) {
|
|
337
|
+
return bigintBuffer.toBigIntBE(
|
|
338
|
+
ton.beginCell().storeUint(6, 5).storeRef(code).storeRef(storage).endCell().hash()
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
function initBaseStorage(owner) {
|
|
342
|
+
return lzTonSdkV2.baseBuildClass("BaseStorage", {
|
|
343
|
+
owner,
|
|
344
|
+
authenticated: false,
|
|
345
|
+
initialized: false,
|
|
346
|
+
initialStorage: lzTonSdkV2.emptyCell()
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
function getUlnReceiveConfigDefault() {
|
|
350
|
+
return lzTonSdkV2.buildClass("UlnReceiveConfig", {
|
|
351
|
+
minCommitPacketGasNull: true,
|
|
352
|
+
minCommitPacketGas: 0,
|
|
353
|
+
confirmationsNull: true,
|
|
354
|
+
confirmations: 0,
|
|
355
|
+
requiredDVNsNull: true,
|
|
356
|
+
requiredDVNs: lzTonSdkV2.emptyCell(),
|
|
357
|
+
optionalDVNsNull: true,
|
|
358
|
+
optionalDVNs: lzTonSdkV2.emptyCell(),
|
|
359
|
+
optionalDVNThreshold: 0
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
function getUlnSendConfigDefault() {
|
|
363
|
+
return lzTonSdkV2.buildClass("UlnSendConfig", {
|
|
364
|
+
workerQuoteGasLimit: 0,
|
|
365
|
+
maxMessageBytes: 0,
|
|
366
|
+
executorNull: true,
|
|
367
|
+
executor: 0n,
|
|
368
|
+
requiredDVNsNull: true,
|
|
369
|
+
requiredDVNs: lzTonSdkV2.emptyCell(),
|
|
370
|
+
optionalDVNsNull: true,
|
|
371
|
+
optionalDVNs: lzTonSdkV2.emptyCell(),
|
|
372
|
+
confirmationsNull: true,
|
|
373
|
+
confirmations: 0
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
function computeTonUlnAddress(_kind, owner, dstEid) {
|
|
377
|
+
return computeContractAddress(
|
|
378
|
+
contractCode.uln,
|
|
379
|
+
lzTonSdkV2.buildClass("Uln", {
|
|
380
|
+
baseStorage: initBaseStorage(owner),
|
|
381
|
+
eid: TON_EID,
|
|
382
|
+
dstEid,
|
|
383
|
+
defaultUlnReceiveConfig: getUlnReceiveConfigDefault(),
|
|
384
|
+
defaultUlnSendConfig: getUlnSendConfigDefault(),
|
|
385
|
+
connectionCode: lzTonSdkV2.emptyCell(),
|
|
386
|
+
workerFeelibInfos: lzTonSdkV2.emptyMap(),
|
|
387
|
+
treasuryFeeBps: 0,
|
|
388
|
+
remainingWorkerSlots: 0,
|
|
389
|
+
remainingAdminWorkerSlots: 0
|
|
390
|
+
})
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
function computeTonEndpointAddress(_kind, owner, dstEid) {
|
|
394
|
+
return computeContractAddress(
|
|
395
|
+
contractCode.endpoint,
|
|
396
|
+
lzTonSdkV2.buildClass("Endpoint", {
|
|
397
|
+
baseStorage: initBaseStorage(owner),
|
|
398
|
+
eid: TON_EID,
|
|
399
|
+
dstEid,
|
|
400
|
+
msglibs: lzTonSdkV2.emptyMap(),
|
|
401
|
+
numMsglibs: 0,
|
|
402
|
+
channelCode: lzTonSdkV2.emptyCell(),
|
|
403
|
+
channelStorageInit: lzTonSdkV2.nullObject(),
|
|
404
|
+
defaultSendLibInfo: lzTonSdkV2.nullObject(),
|
|
405
|
+
defaultReceiveLibInfo: lzTonSdkV2.nullObject(),
|
|
406
|
+
defaultTimeoutReceiveLibInfo: lzTonSdkV2.nullObject(),
|
|
407
|
+
defaultSendMsglibManager: 0n,
|
|
408
|
+
defaultExpiry: 0
|
|
409
|
+
})
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
function computeTonUlnConnectionAddress(_kind, owner, dstEid, dstOApp, ulnManagerAddress, ulnAddress) {
|
|
413
|
+
return computeContractAddress(
|
|
414
|
+
contractCode.ulnConnection,
|
|
415
|
+
buildUlnConnnection(owner, dstEid, dstOApp, ulnManagerAddress, ulnAddress)
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
function buildUlnConnnection(owner, dstEid, dstOApp, ulnManagerAddress, ulnAddress) {
|
|
419
|
+
return lzTonSdkV2.buildClass("UlnConnection", {
|
|
420
|
+
baseStorage: initBaseStorage(ulnManagerAddress),
|
|
421
|
+
path: {
|
|
422
|
+
srcEid: TON_EID,
|
|
423
|
+
dstEid,
|
|
424
|
+
srcOApp: owner,
|
|
425
|
+
dstOApp
|
|
426
|
+
},
|
|
427
|
+
endpointAddress: 0n,
|
|
428
|
+
channelAddress: 0n,
|
|
429
|
+
ulnAddress,
|
|
430
|
+
UlnSendConfigOApp: getUlnSendConfigDefault(),
|
|
431
|
+
UlnReceiveConfigOApp: getUlnReceiveConfigDefault(),
|
|
432
|
+
hashLookups: lzTonSdkV2.emptyMap(),
|
|
433
|
+
firstUnexecutedNonce: 1,
|
|
434
|
+
commitPOOO: lzTonSdkV2.emptyCell()
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
function computeTonChannelAddress(_kind, owner, dstEid, dstOApp, controllerAddress, endpointAddress) {
|
|
438
|
+
return computeContractAddress(
|
|
439
|
+
contractCode.channel,
|
|
440
|
+
lzTonSdkV2.buildClass("Channel", {
|
|
441
|
+
baseStorage: initBaseStorage(controllerAddress),
|
|
442
|
+
path: {
|
|
443
|
+
srcEid: TON_EID,
|
|
444
|
+
dstEid,
|
|
445
|
+
srcOApp: owner,
|
|
446
|
+
dstOApp
|
|
447
|
+
},
|
|
448
|
+
endpointAddress,
|
|
449
|
+
epConfigOApp: {
|
|
450
|
+
isNull: true,
|
|
451
|
+
sendMsglib: 0n,
|
|
452
|
+
sendMsglibConnection: 0n,
|
|
453
|
+
sendMsglibManager: 0n,
|
|
454
|
+
receiveMsglib: 0n,
|
|
455
|
+
receiveMsglibConnection: 0n,
|
|
456
|
+
timeoutReceiveMsglib: 0n,
|
|
457
|
+
timeoutReceiveMsglibConnection: 0n,
|
|
458
|
+
timeoutReceiveMsglibExpiry: 0
|
|
459
|
+
},
|
|
460
|
+
outboundNonce: 0,
|
|
461
|
+
sendRequestQueue: lzTonSdkV2.emptyCell(),
|
|
462
|
+
lastSendRequestId: 0,
|
|
463
|
+
commitPOOO: lzTonSdkV2.emptyPOOO(),
|
|
464
|
+
executePOOO: lzTonSdkV2.emptyPOOO(),
|
|
465
|
+
executionQueue: lzTonSdkV2.emptyCell(),
|
|
466
|
+
zroBalance: 0
|
|
467
|
+
})
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
var TonWallet = class {
|
|
471
|
+
constructor(options) {
|
|
472
|
+
this.tonConnectUI = options.tonConnectUI;
|
|
473
|
+
this.account = options.account;
|
|
474
|
+
this.tonClient = new ton.TonClient({
|
|
475
|
+
endpoint: core.getChainRpcUrl("Ton").rpcUrl
|
|
476
|
+
});
|
|
477
|
+
const cs = new core.Csl(core.OpenAPI.DEBUG);
|
|
478
|
+
this.csl = cs.log;
|
|
479
|
+
}
|
|
480
|
+
// Check if the token is native TON
|
|
481
|
+
isNativeToken(originAsset) {
|
|
482
|
+
const lowerAsset = originAsset.toLowerCase();
|
|
483
|
+
return lowerAsset === "ton" || lowerAsset === "native";
|
|
484
|
+
}
|
|
485
|
+
async getSenderJettonWallet(masterAddress, account) {
|
|
486
|
+
const _account = account || this.account;
|
|
487
|
+
try {
|
|
488
|
+
const jettonMasterAddress = ton.Address.parse(masterAddress);
|
|
489
|
+
const owner = ton.Address.parse(_account);
|
|
490
|
+
const ownerCell = ton.beginCell().storeAddress(owner).endCell();
|
|
491
|
+
const stack = [{ type: "slice", cell: ownerCell }];
|
|
492
|
+
const response = await this.tonClient.runMethod(jettonMasterAddress, "get_wallet_address", stack);
|
|
493
|
+
const jettonWalletCell = response.stack.readCell();
|
|
494
|
+
const jettonWalletAddress = jettonWalletCell.beginParse().loadAddress();
|
|
495
|
+
return jettonWalletAddress;
|
|
496
|
+
} catch (error) {
|
|
497
|
+
console.error("get %s jetton wallet failed: %o", _account, error);
|
|
498
|
+
throw error;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
async transfer(data) {
|
|
502
|
+
const { originAsset, depositAddress, amount, memo } = data;
|
|
503
|
+
if (!this.tonConnectUI) {
|
|
504
|
+
throw new Error("TON Connect UI not initialized");
|
|
505
|
+
}
|
|
506
|
+
try {
|
|
507
|
+
if (this.isNativeToken(originAsset)) {
|
|
508
|
+
const transaction2 = {
|
|
509
|
+
messages: [
|
|
510
|
+
{
|
|
511
|
+
address: depositAddress,
|
|
512
|
+
amount: ton.toNano(amount).toString()
|
|
513
|
+
}
|
|
514
|
+
],
|
|
515
|
+
validUntil: Math.floor(Date.now() / 1e3) + 600
|
|
516
|
+
// 10 minutes
|
|
517
|
+
};
|
|
518
|
+
const result2 = await this.tonConnectUI.sendTransaction(transaction2);
|
|
519
|
+
return result2.boc;
|
|
520
|
+
}
|
|
521
|
+
const senderJettonWallet = await this.getSenderJettonWallet(originAsset);
|
|
522
|
+
const body = buildJettonWalletTransferBody({
|
|
523
|
+
memo,
|
|
524
|
+
amount,
|
|
525
|
+
recipient: depositAddress,
|
|
526
|
+
refundTo: this.account
|
|
527
|
+
});
|
|
528
|
+
const transaction = {
|
|
529
|
+
validUntil: Math.floor(Date.now() / 1e3) + 360,
|
|
530
|
+
messages: [
|
|
531
|
+
{
|
|
532
|
+
address: senderJettonWallet.toString(),
|
|
533
|
+
// sender jetton wallet
|
|
534
|
+
amount: ton.toNano("0.05").toString(),
|
|
535
|
+
// for commission fees, excess will be returned
|
|
536
|
+
payload: body.toBoc().toString("base64")
|
|
537
|
+
// payload with jetton transfer body
|
|
538
|
+
}
|
|
539
|
+
]
|
|
540
|
+
};
|
|
541
|
+
const result = await this.tonConnectUI.sendTransaction(transaction);
|
|
542
|
+
return result.boc;
|
|
543
|
+
} catch (error) {
|
|
544
|
+
console.log("TON transfer error:", error);
|
|
545
|
+
throw error;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
async getBalance(token, account, options) {
|
|
549
|
+
const { isCatchError = false } = options || {};
|
|
550
|
+
try {
|
|
551
|
+
if (this.isNativeToken(token.symbol)) {
|
|
552
|
+
const parsedAddress = ton.Address.parse(account);
|
|
553
|
+
const accountState = await this.tonClient.getBalance(parsedAddress);
|
|
554
|
+
return accountState.toString();
|
|
555
|
+
}
|
|
556
|
+
const tokenJettonWallet = await this.getSenderJettonWallet(token.contractAddress, account);
|
|
557
|
+
const response = await this.tonClient.runMethod(tokenJettonWallet, "get_wallet_data", []);
|
|
558
|
+
const balance = response.stack.readBigNumber();
|
|
559
|
+
return balance.toString();
|
|
560
|
+
} catch (error) {
|
|
561
|
+
this.csl("Ton getBalance", "red-500", "Get balance failed: %o", error);
|
|
562
|
+
if (isCatchError) {
|
|
563
|
+
throw error;
|
|
564
|
+
}
|
|
565
|
+
return "0";
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
async balanceOf(token, account, options) {
|
|
569
|
+
return await this.getBalance(token, account, options);
|
|
570
|
+
}
|
|
571
|
+
async estimateTransferGas(data) {
|
|
572
|
+
const { fromToken, amount, depositAddress, account } = data;
|
|
573
|
+
let estimateGas;
|
|
574
|
+
if (this.isNativeToken(fromToken.symbol)) {
|
|
575
|
+
estimateGas = ton.toNano("0.01");
|
|
576
|
+
} else {
|
|
577
|
+
const senderJettonWallet = await this.getSenderJettonWallet(fromToken.contractAddress, account);
|
|
578
|
+
const body = buildJettonWalletTransferBody({
|
|
579
|
+
amount,
|
|
580
|
+
recipient: depositAddress,
|
|
581
|
+
refundTo: account
|
|
582
|
+
});
|
|
583
|
+
try {
|
|
584
|
+
const estimation = await this.tonClient.estimateExternalMessageFee(senderJettonWallet, {
|
|
585
|
+
body,
|
|
586
|
+
initCode: null,
|
|
587
|
+
initData: null,
|
|
588
|
+
ignoreSignature: true
|
|
589
|
+
});
|
|
590
|
+
const { in_fwd_fee, storage_fee, gas_fee, fwd_fee } = estimation.source_fees;
|
|
591
|
+
estimateGas = BigInt(in_fwd_fee) + BigInt(storage_fee) + BigInt(gas_fee) + BigInt(fwd_fee);
|
|
592
|
+
this.csl("TON estimateTransferGas", "blue-300", "estimateGas: %o", estimateGas);
|
|
593
|
+
estimateGas = estimateGas + ton.toNano("0.1");
|
|
594
|
+
} catch {
|
|
595
|
+
estimateGas = ton.toNano("0.12");
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
return {
|
|
599
|
+
gasLimit: estimateGas,
|
|
600
|
+
gasPrice: 1n,
|
|
601
|
+
// TON has no gas price concept, use 1 for compatibility
|
|
602
|
+
estimateGas
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
async getEstimateGas(params) {
|
|
606
|
+
const { gasLimit, price, nativeToken } = params;
|
|
607
|
+
const estimateGasAmount = Big__default.default(gasLimit.toString()).div(10 ** nativeToken.decimals);
|
|
608
|
+
const estimateGasUsd = Big__default.default(estimateGasAmount).times(price || 1);
|
|
609
|
+
return {
|
|
610
|
+
gasPrice: 1n,
|
|
611
|
+
usd: core.numberRemoveEndZero(Big__default.default(estimateGasUsd).toFixed(20)),
|
|
612
|
+
wei: gasLimit,
|
|
613
|
+
amount: core.numberRemoveEndZero(Big__default.default(estimateGasAmount).toFixed(nativeToken.decimals))
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
async estimateTransaction(params) {
|
|
617
|
+
const {
|
|
618
|
+
dry,
|
|
619
|
+
body,
|
|
620
|
+
estimateGas,
|
|
621
|
+
fromToken,
|
|
622
|
+
prices
|
|
623
|
+
} = params;
|
|
624
|
+
const nativeTokenPrice = core.getPrice(prices, fromToken.nativeToken.symbol);
|
|
625
|
+
let finalEstimateGas = estimateGas || ton.toNano("0.12");
|
|
626
|
+
if (body) {
|
|
627
|
+
try {
|
|
628
|
+
const account = this.account || core.DefaultAddresses["ton"];
|
|
629
|
+
const senderJettonWallet = await this.getSenderJettonWallet(fromToken.contractAddress, account);
|
|
630
|
+
const estimation = await this.tonClient.estimateExternalMessageFee(senderJettonWallet, {
|
|
631
|
+
body,
|
|
632
|
+
initCode: null,
|
|
633
|
+
initData: null,
|
|
634
|
+
ignoreSignature: true
|
|
635
|
+
});
|
|
636
|
+
this.csl("TON estimateTransaction", "blue-300", "estimation: %o", estimation);
|
|
637
|
+
const { in_fwd_fee, storage_fee, gas_fee, fwd_fee } = estimation.source_fees;
|
|
638
|
+
finalEstimateGas = BigInt(in_fwd_fee) + BigInt(storage_fee) + BigInt(gas_fee) + BigInt(fwd_fee);
|
|
639
|
+
finalEstimateGas = finalEstimateGas + ton.toNano("0.1");
|
|
640
|
+
} catch (error) {
|
|
641
|
+
this.csl("TON estimateTransaction", "red-500", "estimateExternalMessageFee failed: %o", error);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
const { usd, wei } = await this.getEstimateGas({
|
|
645
|
+
gasLimit: finalEstimateGas,
|
|
646
|
+
price: nativeTokenPrice,
|
|
647
|
+
nativeToken: fromToken.nativeToken
|
|
648
|
+
});
|
|
649
|
+
const result = {
|
|
650
|
+
estimateSourceGasLimit: finalEstimateGas,
|
|
651
|
+
estimateSourceGas: wei,
|
|
652
|
+
estimateSourceGasUsd: usd
|
|
653
|
+
};
|
|
654
|
+
return result;
|
|
655
|
+
}
|
|
656
|
+
async quote(type, params) {
|
|
657
|
+
switch (type) {
|
|
658
|
+
case core.Service.Usdt0:
|
|
659
|
+
return await this.quoteOFT(params);
|
|
660
|
+
case core.Service.OneClick:
|
|
661
|
+
return await this.quoteOneClickProxy(params);
|
|
662
|
+
default:
|
|
663
|
+
throw new Error(`Unsupported quote type: ${type}`);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
async send(type, params) {
|
|
667
|
+
switch (type) {
|
|
668
|
+
case core.SendType.SEND:
|
|
669
|
+
return await this.sendTransaction(params);
|
|
670
|
+
case core.SendType.TRANSFER:
|
|
671
|
+
return await this.transfer(params);
|
|
672
|
+
default:
|
|
673
|
+
throw new Error(`Unsupported send type: ${type}`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
async sendTransaction(params) {
|
|
677
|
+
const {
|
|
678
|
+
transaction
|
|
679
|
+
} = params;
|
|
680
|
+
const result = await this.tonConnectUI.sendTransaction(transaction);
|
|
681
|
+
const { hexHash } = await pollTransactionByBoc(result.boc, { maxPollCount: 60, pollInterval: 3e3 });
|
|
682
|
+
return hexHash;
|
|
683
|
+
}
|
|
684
|
+
async quoteOFT(params) {
|
|
685
|
+
const { Options } = await import('@layerzerolabs/lz-v2-utilities');
|
|
686
|
+
const {
|
|
687
|
+
dry,
|
|
688
|
+
originLayerzeroAddress,
|
|
689
|
+
destinationLayerzeroAddress,
|
|
690
|
+
fromToken,
|
|
691
|
+
toToken,
|
|
692
|
+
dstEid,
|
|
693
|
+
refundTo,
|
|
694
|
+
recipient,
|
|
695
|
+
amountWei,
|
|
696
|
+
payInLzToken,
|
|
697
|
+
slippageTolerance,
|
|
698
|
+
multiHopComposer,
|
|
699
|
+
isMultiHopComposer,
|
|
700
|
+
isOriginLegacy,
|
|
701
|
+
isDestinationLegacy,
|
|
702
|
+
prices,
|
|
703
|
+
excludeFees,
|
|
704
|
+
originLayerzero,
|
|
705
|
+
destinationLayerzero
|
|
706
|
+
} = params;
|
|
707
|
+
const _quoteType = `Usdt0 TON ${fromToken.chainName}->${toToken.chainName}`;
|
|
708
|
+
const execTime = new core.ExecTime({ type: _quoteType, logStyle: "indigo-100", isDebug: core.OpenAPI.DEBUG });
|
|
709
|
+
const result = {
|
|
710
|
+
needApprove: false,
|
|
711
|
+
sendParam: void 0,
|
|
712
|
+
fees: {},
|
|
713
|
+
estimateSourceGas: void 0,
|
|
714
|
+
totalEstimateSourceGas: 0n,
|
|
715
|
+
estimateSourceGasUsd: void 0,
|
|
716
|
+
outputAmount: core.numberRemoveEndZero(Big__default.default(amountWei || 0).div(10 ** fromToken.decimals).toFixed(fromToken.decimals, 0)),
|
|
717
|
+
quoteParam: {
|
|
718
|
+
...params
|
|
719
|
+
},
|
|
720
|
+
totalFeesUsd: void 0,
|
|
721
|
+
estimateTime: 0
|
|
722
|
+
};
|
|
723
|
+
const TON_ULN_MANAGER = "0x06b52b11abaf65bf1ff47c57e890ba4ad6a75a68859bbe5a51c1fc451954c54c";
|
|
724
|
+
const TON_CONTROLLER = "0x1eb2bbea3d8c0d42ff7fd60f0264c866c934bbff727526ca759e7374cae0c166";
|
|
725
|
+
const srcAddress = lzTonSdkV2.parseTonAddress("0:933ef20686fe0b3121443d6813e9457bca975336fcb481446f3c7d131517e7f8");
|
|
726
|
+
this.csl("TON quoteOFT", "yellow-600", "srcAddress: %o", srcAddress.toString());
|
|
727
|
+
const tonUlnManagerBigInt = BigInt(TON_ULN_MANAGER);
|
|
728
|
+
const tonControllerBigInt = BigInt(TON_CONTROLLER);
|
|
729
|
+
const proxyAddress = ton.Address.parse(originLayerzeroAddress);
|
|
730
|
+
const JETTON_TRANSFER_GAS = 0.07;
|
|
731
|
+
const GAS_ASSERT_MULTIPLIER = 440n;
|
|
732
|
+
const oftBuild = objectBuild(tonObjects);
|
|
733
|
+
const oftDecode = objectDecode(tonObjects);
|
|
734
|
+
const amountLd = BigInt(amountWei);
|
|
735
|
+
const slippage = slippageTolerance;
|
|
736
|
+
const minAmountLd = BigInt(Big__default.default(amountWei).times(Big__default.default(1).minus(Big__default.default(slippage).div(100))).toFixed(0));
|
|
737
|
+
const lzReceiveOptionGas = isDestinationLegacy ? destinationLayerzero.lzReceiveOptionGasLegacy : destinationLayerzero.lzReceiveOptionGas;
|
|
738
|
+
let lzReceiveOptionValue = 0;
|
|
739
|
+
execTime.breakpoint();
|
|
740
|
+
const destATA = await utilsSolana.getDestinationAssociatedTokenAddress({
|
|
741
|
+
recipient,
|
|
742
|
+
toToken
|
|
743
|
+
});
|
|
744
|
+
execTime.log("getDestinationAssociatedTokenAddress");
|
|
745
|
+
if (destATA.needCreateTokenAccount) {
|
|
746
|
+
lzReceiveOptionValue = bridges.LZ_RECEIVE_VALUE[toToken.chainName] || 0;
|
|
747
|
+
}
|
|
748
|
+
let _dstEid = dstEid;
|
|
749
|
+
const finalDstAddress = core.addressToBytes32(toToken.chainType, recipient);
|
|
750
|
+
let to = finalDstAddress;
|
|
751
|
+
this.csl("TON quoteOFT", "blue-300", "_dstEid: %o", _dstEid);
|
|
752
|
+
this.csl("TON quoteOFT", "blue-300", "to: %o", to);
|
|
753
|
+
let dstProxyAddress = core.addressToBytes32(toToken.chainType, destinationLayerzeroAddress);
|
|
754
|
+
if (isMultiHopComposer) {
|
|
755
|
+
_dstEid = multiHopComposer.eid;
|
|
756
|
+
to = core.addressToBytes32("evm", multiHopComposer.oftMultiHopComposer);
|
|
757
|
+
dstProxyAddress = core.addressToBytes32("evm", multiHopComposer.oftLegacy);
|
|
758
|
+
this.csl("TON quoteOFT", "blue-300", "MultiHop _dstEid: %o", _dstEid);
|
|
759
|
+
this.csl("TON quoteOFT", "blue-300", "MultiHop to: %o", to);
|
|
760
|
+
}
|
|
761
|
+
const dstProxyAddressBigInt = BigInt(dstProxyAddress);
|
|
762
|
+
execTime.breakpoint();
|
|
763
|
+
const jettonWalletAddress = await this.getSenderJettonWallet(fromToken.contractAddress, refundTo);
|
|
764
|
+
execTime.log("getSenderJettonWallet");
|
|
765
|
+
this.csl("TON quoteOFT", "blue-300", "jettonWalletAddress: %o", jettonWalletAddress.toString());
|
|
766
|
+
this.csl("TON quoteOFT", "blue-300", "refundTo: %o", refundTo);
|
|
767
|
+
this.csl("TON quoteOFT", "blue-300", "Address.parse(refundTo): %o", ton.Address.parse(refundTo));
|
|
768
|
+
const oftProxyBigInt = lzTonSdkV2.addressToBigInt(proxyAddress);
|
|
769
|
+
this.csl("TON quoteOFT", "blue-300", "oftProxyBigInt: %o", oftProxyBigInt);
|
|
770
|
+
const buildForwardPayload = async (dstAddress, nativeFee2, minAmountOut) => {
|
|
771
|
+
let _composeMessage = ton.beginCell().endCell();
|
|
772
|
+
const optionsV2Params = {
|
|
773
|
+
lzReceiveGas: 0n,
|
|
774
|
+
lzReceiveValue: 0n,
|
|
775
|
+
nativeDropAddress: BigInt(dstAddress),
|
|
776
|
+
nativeDropAmount: 0n,
|
|
777
|
+
lzComposeGas: 0n,
|
|
778
|
+
lzComposeValue: 0n
|
|
779
|
+
};
|
|
780
|
+
if (isMultiHopComposer) {
|
|
781
|
+
let multiHopExtraOptions = Options.newOptions().toHex();
|
|
782
|
+
if (lzReceiveOptionValue) {
|
|
783
|
+
multiHopExtraOptions = Options.newOptions().addExecutorLzReceiveOption(lzReceiveOptionGas, lzReceiveOptionValue).toHex();
|
|
784
|
+
}
|
|
785
|
+
const composeMsgSendParam = {
|
|
786
|
+
dstEid,
|
|
787
|
+
to: core.addressToBytes32(toToken.chainType, recipient),
|
|
788
|
+
amountLD: amountLd,
|
|
789
|
+
minAmountLD: minAmountLd,
|
|
790
|
+
extraOptions: multiHopExtraOptions,
|
|
791
|
+
composeMsg: "0x",
|
|
792
|
+
oftCmd: "0x"
|
|
793
|
+
};
|
|
794
|
+
const hopMsgFee = await bridges.getHopMsgFee({
|
|
795
|
+
sendParam: composeMsgSendParam,
|
|
796
|
+
toToken
|
|
797
|
+
});
|
|
798
|
+
optionsV2Params.lzReceiveGas = 200000n;
|
|
799
|
+
optionsV2Params.nativeDropAddress = 0n;
|
|
800
|
+
optionsV2Params.lzComposeGas = originLayerzero.composeOptionGas || 800000n;
|
|
801
|
+
optionsV2Params.lzComposeValue = hopMsgFee * 120n / 100n;
|
|
802
|
+
const extraOptions2 = lzTonSdkV2.buildClass("md::OptionsV1", {
|
|
803
|
+
lzReceiveGas: BigInt(lzReceiveOptionGas),
|
|
804
|
+
lzReceiveValue: 0n,
|
|
805
|
+
nativeDropAddress: finalDstAddress,
|
|
806
|
+
nativeDropAmount: 0n
|
|
807
|
+
});
|
|
808
|
+
execTime.breakpoint();
|
|
809
|
+
try {
|
|
810
|
+
const response = await this.tonClient.provider(proxyAddress).get("_encodeOftSendAsComposeMessage", [
|
|
811
|
+
{ type: "int", value: 0n },
|
|
812
|
+
{ type: "int", value: BigInt(params.dstEid) },
|
|
813
|
+
{ type: "int", value: lzTonSdkV2.addressToBigInt(finalDstAddress) },
|
|
814
|
+
{ type: "cell", cell: extraOptions2 }
|
|
815
|
+
]);
|
|
816
|
+
_composeMessage = response.stack.readCell();
|
|
817
|
+
} catch (err) {
|
|
818
|
+
this.csl("TON quoteOFT", "red-500", "createComposePayload failed: %o", err);
|
|
819
|
+
throw "get compose message failed";
|
|
820
|
+
}
|
|
821
|
+
execTime.log("_encodeOftSendAsComposeMessage");
|
|
822
|
+
}
|
|
823
|
+
const extraOptions = lzTonSdkV2.buildClass("md::OptionsV2", optionsV2Params);
|
|
824
|
+
return oftBuild.OFTSend({
|
|
825
|
+
dstEid: BigInt(_dstEid),
|
|
826
|
+
to: BigInt(dstAddress),
|
|
827
|
+
minAmount: minAmountOut,
|
|
828
|
+
nativeFee: nativeFee2,
|
|
829
|
+
zroFee: 0n,
|
|
830
|
+
// ZRO token doesn't exist on TON
|
|
831
|
+
extraOptions,
|
|
832
|
+
composeMessage: _composeMessage
|
|
833
|
+
});
|
|
834
|
+
};
|
|
835
|
+
execTime.breakpoint();
|
|
836
|
+
const forwardPayload = await buildForwardPayload(to, 0n, minAmountLd);
|
|
837
|
+
execTime.log("buildForwardPayload");
|
|
838
|
+
const { composeMessage } = oftDecode.OFTSend(forwardPayload);
|
|
839
|
+
this.csl("TON quoteOFT", "blue-300", "composeMessage: %o", composeMessage);
|
|
840
|
+
execTime.breakpoint();
|
|
841
|
+
const lzSendResult = await this.tonClient.provider(proxyAddress).get("getLzSendMd", [
|
|
842
|
+
{ type: "cell", cell: forwardPayload },
|
|
843
|
+
{ type: "int", value: 2n },
|
|
844
|
+
// Msg type: SEND_OFT
|
|
845
|
+
{ type: "cell", cell: composeMessage }
|
|
846
|
+
]);
|
|
847
|
+
const lzSend = lzTonSdkV2.decodeClass("md::LzSend", lzSendResult.stack.readCell());
|
|
848
|
+
execTime.log("getLzSendMd", "lzSend: %o", lzSend);
|
|
849
|
+
const ulnAddress = bigIntToAddress(
|
|
850
|
+
computeTonUlnAddress("USDT", tonUlnManagerBigInt, BigInt(_dstEid))
|
|
851
|
+
);
|
|
852
|
+
this.csl("TON quoteOFT", "blue-300", "ulnAddress: %o", ulnAddress.toString());
|
|
853
|
+
const endpointAddress = bigIntToAddress(
|
|
854
|
+
computeTonEndpointAddress(
|
|
855
|
+
"USDT",
|
|
856
|
+
tonControllerBigInt,
|
|
857
|
+
BigInt(_dstEid)
|
|
858
|
+
)
|
|
859
|
+
);
|
|
860
|
+
this.csl("TON quoteOFT", "blue-300", "endpointAddress: %o", endpointAddress.toString());
|
|
861
|
+
const channelAddress = bigIntToAddress(
|
|
862
|
+
computeTonChannelAddress(
|
|
863
|
+
"USDT",
|
|
864
|
+
oftProxyBigInt,
|
|
865
|
+
BigInt(_dstEid),
|
|
866
|
+
dstProxyAddressBigInt,
|
|
867
|
+
tonControllerBigInt,
|
|
868
|
+
lzTonSdkV2.addressToBigInt(endpointAddress)
|
|
869
|
+
)
|
|
870
|
+
);
|
|
871
|
+
this.csl("TON quoteOFT", "blue-300", "channelAddress: %o", channelAddress.toString());
|
|
872
|
+
const connectionInitialStorage = buildUlnConnnection(
|
|
873
|
+
oftProxyBigInt,
|
|
874
|
+
BigInt(_dstEid),
|
|
875
|
+
dstProxyAddressBigInt,
|
|
876
|
+
tonUlnManagerBigInt,
|
|
877
|
+
lzTonSdkV2.addressToBigInt(ulnAddress)
|
|
878
|
+
);
|
|
879
|
+
this.csl("TON quoteOFT", "blue-300", "connectionInitialStorage: %o", connectionInitialStorage);
|
|
880
|
+
const ulnConnectionAddress = bigIntToAddress(
|
|
881
|
+
computeTonUlnConnectionAddress(
|
|
882
|
+
"USDT",
|
|
883
|
+
oftProxyBigInt,
|
|
884
|
+
BigInt(_dstEid),
|
|
885
|
+
dstProxyAddressBigInt,
|
|
886
|
+
tonUlnManagerBigInt,
|
|
887
|
+
lzTonSdkV2.addressToBigInt(ulnAddress)
|
|
888
|
+
)
|
|
889
|
+
);
|
|
890
|
+
this.csl("TON quoteOFT", "blue-300", "ulnConnectionAddress: %o", ulnConnectionAddress);
|
|
891
|
+
let customUlnSendConfig;
|
|
892
|
+
execTime.breakpoint();
|
|
893
|
+
try {
|
|
894
|
+
const ulnConnectionResult = await this.tonClient.provider(ulnConnectionAddress).get("getContractStorage", []);
|
|
895
|
+
const ulnConnectionStorage = lzTonSdkV2.decodeClass(
|
|
896
|
+
"UlnConnection",
|
|
897
|
+
ulnConnectionResult.stack.readCell()
|
|
898
|
+
);
|
|
899
|
+
customUlnSendConfig = ulnConnectionStorage.UlnSendConfigOApp;
|
|
900
|
+
} catch (error) {
|
|
901
|
+
customUlnSendConfig = ulnConfigs[toToken.chainName];
|
|
902
|
+
this.csl("TON quoteOFT", "gray-500", "get UlnConnectionStorage failed, get config from ulnConfigs: %o, error: %o", customUlnSendConfig, error);
|
|
903
|
+
}
|
|
904
|
+
execTime.log("getContractStorage(ulnConnection)");
|
|
905
|
+
this.csl("TON quoteOFT", "blue-300", "customUlnSendConfig: %o", customUlnSendConfig);
|
|
906
|
+
const mdUlnSend = lzTonSdkV2.buildClass("md::UlnSend", {
|
|
907
|
+
lzSend,
|
|
908
|
+
customUlnSendConfig,
|
|
909
|
+
connectionInitialStorage,
|
|
910
|
+
forwardingAddress: channelAddress
|
|
911
|
+
});
|
|
912
|
+
this.csl("TON quoteOFT", "blue-300", "mdUlnSend: %o", mdUlnSend);
|
|
913
|
+
execTime.breakpoint();
|
|
914
|
+
const quoteStack = (await this.tonClient.provider(ulnAddress).get("ulnQuote", [
|
|
915
|
+
{
|
|
916
|
+
type: "cell",
|
|
917
|
+
cell: mdUlnSend
|
|
918
|
+
}
|
|
919
|
+
])).stack;
|
|
920
|
+
execTime.log("get ulnQuote");
|
|
921
|
+
this.csl("TON quoteOFT", "blue-300", "quoteStack: %o", quoteStack);
|
|
922
|
+
const parsedArray = quoteStack.readTuple().skip(1).pop();
|
|
923
|
+
const parsedQuote = lzTonSdkV2.decodeClass("md::MsglibSendCallback", parsedArray[3]);
|
|
924
|
+
this.csl("TON quoteOFT", "blue-300", "parsedQuote: %o", parsedQuote);
|
|
925
|
+
const FEE_BUFFER = 130n;
|
|
926
|
+
const FEE_DIVISOR = 100n;
|
|
927
|
+
const nativeFee = parsedQuote.nativeFee * FEE_BUFFER / FEE_DIVISOR;
|
|
928
|
+
const zroFee = 0n;
|
|
929
|
+
this.csl("TON quoteOFT", "blue-300", "nativeFee: %o", nativeFee);
|
|
930
|
+
this.csl("TON quoteOFT", "blue-300", "zroFee: %o", zroFee);
|
|
931
|
+
this.csl("TON quoteOFT", "blue-600", "Fee: %o TON (includes 30% buffer)", Number(nativeFee) / 1e9);
|
|
932
|
+
this.csl("TON quoteOFT", "blue-600", "Amount received: %o USDT (min)", Number(minAmountLd) / 1e6);
|
|
933
|
+
execTime.breakpoint();
|
|
934
|
+
const sendForwardPayload = await buildForwardPayload(
|
|
935
|
+
to,
|
|
936
|
+
nativeFee,
|
|
937
|
+
minAmountLd
|
|
938
|
+
);
|
|
939
|
+
execTime.log("buildForwardPayload(send)", "sendForwardPayload: %o", sendForwardPayload);
|
|
940
|
+
execTime.breakpoint();
|
|
941
|
+
const storage = await this.tonClient.provider(proxyAddress).get("getContractStorage", []);
|
|
942
|
+
execTime.log("getContractStorage(proxyAddress)");
|
|
943
|
+
const cell = storage.stack.readCell();
|
|
944
|
+
const oftCell = oftDecode.UsdtOFT(cell);
|
|
945
|
+
const gasAsserts = oftDecode.GasAsserts(oftCell.gasAsserts);
|
|
946
|
+
const estimatedGas = gasAsserts.sendOFTGas * GAS_ASSERT_MULTIPLIER;
|
|
947
|
+
this.csl("TON quoteOFT", "blue-300", "estimatedGas: %o", estimatedGas);
|
|
948
|
+
const fwdAmount = nativeFee + estimatedGas;
|
|
949
|
+
this.csl("TON quoteOFT", "blue-300", "fwdAmount: %o", fwdAmount);
|
|
950
|
+
const totalValue = fwdAmount + ton.toNano(JETTON_TRANSFER_GAS);
|
|
951
|
+
this.csl("TON quoteOFT", "blue-300", "totalValue: %o", totalValue);
|
|
952
|
+
const transferCell = buildTonTransferCell({
|
|
953
|
+
toAddress: proxyAddress,
|
|
954
|
+
fromAddress: ton.Address.parse(refundTo),
|
|
955
|
+
fwdAmount,
|
|
956
|
+
jettonAmount: amountLd,
|
|
957
|
+
forwardPayload: sendForwardPayload
|
|
958
|
+
});
|
|
959
|
+
this.csl("TON quoteOFT", "blue-300", "transferCell: %o", transferCell);
|
|
960
|
+
const transaction = {
|
|
961
|
+
validUntil: Math.floor(Date.now() / 1e3) + 360,
|
|
962
|
+
messages: [
|
|
963
|
+
{
|
|
964
|
+
address: jettonWalletAddress.toString(),
|
|
965
|
+
amount: totalValue.toString(),
|
|
966
|
+
payload: transferCell.toBoc().toString("base64")
|
|
967
|
+
}
|
|
968
|
+
]
|
|
969
|
+
};
|
|
970
|
+
this.csl("TON quoteOFT", "blue-300", "transaction: %o", transaction);
|
|
971
|
+
result.sendParam = {
|
|
972
|
+
transaction,
|
|
973
|
+
estimateGas: estimatedGas
|
|
974
|
+
};
|
|
975
|
+
const nativeFeeUsd = Big__default.default(nativeFee.toString()).div(10 ** fromToken.nativeToken.decimals).times(core.getPrice(prices, fromToken.nativeToken.symbol));
|
|
976
|
+
result.fees.nativeFeeUsd = core.numberRemoveEndZero(nativeFeeUsd.toFixed(20));
|
|
977
|
+
const estimateGasUsd = Big__default.default(estimatedGas.toString()).div(10 ** fromToken.nativeToken.decimals).times(core.getPrice(prices, fromToken.nativeToken.symbol));
|
|
978
|
+
result.fees.nativeFee = Big__default.default(nativeFee.toString()).div(10 ** fromToken.nativeToken.decimals).toFixed(fromToken.nativeToken.decimals, 0);
|
|
979
|
+
result.fees.lzTokenFee = zroFee.toString();
|
|
980
|
+
execTime.breakpoint();
|
|
981
|
+
const ett = await this.estimateTransaction({
|
|
982
|
+
dry,
|
|
983
|
+
estimateGas: estimatedGas,
|
|
984
|
+
fromToken,
|
|
985
|
+
prices
|
|
986
|
+
});
|
|
987
|
+
execTime.log("estimateTransaction");
|
|
988
|
+
result.fees.estimateGasUsd = ett.estimateSourceGasUsd;
|
|
989
|
+
result.estimateSourceGasUsd = ett.estimateSourceGasUsd;
|
|
990
|
+
result.estimateSourceGas = ett.estimateSourceGas;
|
|
991
|
+
result.totalEstimateSourceGas = nativeFee + ett.estimateSourceGas;
|
|
992
|
+
result.fees.estimateGasUsd = core.numberRemoveEndZero(estimateGasUsd.toFixed(20));
|
|
993
|
+
result.estimateSourceGasUsd = core.numberRemoveEndZero(estimateGasUsd.toFixed(20));
|
|
994
|
+
result.estimateSourceGas = estimatedGas;
|
|
995
|
+
result.totalEstimateSourceGas = nativeFee + estimatedGas;
|
|
996
|
+
result.fees.legacyMeshFeeUsd = core.numberRemoveEndZero(Big__default.default(amountWei || 0).div(10 ** params.fromToken.decimals).times(bridges.USDT0_LEGACY_MESH_TRANSFTER_FEE).toFixed(params.fromToken.decimals));
|
|
997
|
+
result.outputAmount = core.numberRemoveEndZero(Big__default.default(Big__default.default(amountWei || 0).div(10 ** params.fromToken.decimals)).minus(result.fees.legacyMeshFeeUsd || 0).toFixed(params.fromToken.decimals, 0));
|
|
998
|
+
for (const feeKey in result.fees) {
|
|
999
|
+
if (excludeFees && excludeFees.includes(feeKey) || !/Usd$/.test(feeKey)) {
|
|
1000
|
+
continue;
|
|
1001
|
+
}
|
|
1002
|
+
result.totalFeesUsd = Big__default.default(result.totalFeesUsd || 0).plus(result.fees[feeKey] || 0);
|
|
1003
|
+
}
|
|
1004
|
+
result.totalFeesUsd = core.numberRemoveEndZero(Big__default.default(result.totalFeesUsd || 0).toFixed(20));
|
|
1005
|
+
execTime.logTotal("quoteOFT");
|
|
1006
|
+
return result;
|
|
1007
|
+
}
|
|
1008
|
+
async quoteOneClickProxy(params) {
|
|
1009
|
+
const {
|
|
1010
|
+
dry,
|
|
1011
|
+
proxyAddress,
|
|
1012
|
+
fromToken,
|
|
1013
|
+
refundTo,
|
|
1014
|
+
depositAddress,
|
|
1015
|
+
amountWei,
|
|
1016
|
+
prices
|
|
1017
|
+
} = params;
|
|
1018
|
+
const execTime = new core.ExecTime({ type: "OneClick TON", logStyle: "indigo-200", isDebug: core.OpenAPI.DEBUG });
|
|
1019
|
+
const result = { fees: {} };
|
|
1020
|
+
const forwardTonAmount = ton.toNano("0.085");
|
|
1021
|
+
const buffer = ton.toNano("0.01");
|
|
1022
|
+
execTime.breakpoint();
|
|
1023
|
+
const userJettonWallet = await this.getSenderJettonWallet(fromToken.contractAddress, refundTo);
|
|
1024
|
+
execTime.log("getSenderJettonWallet");
|
|
1025
|
+
this.csl("TON quoteOneClickProxy", "blue-300", "userJettonWallet: %o", userJettonWallet);
|
|
1026
|
+
const forwardPayload = ton.beginCell().storeAddress(ton.Address.parse(depositAddress)).endCell();
|
|
1027
|
+
const body = buildJettonWalletTransferBody({
|
|
1028
|
+
amount: amountWei,
|
|
1029
|
+
recipient: proxyAddress,
|
|
1030
|
+
refundTo,
|
|
1031
|
+
forwardTonAmount,
|
|
1032
|
+
forwardPayload
|
|
1033
|
+
});
|
|
1034
|
+
this.csl("TON quoteOneClickProxy", "blue-300", "body: %o", body);
|
|
1035
|
+
execTime.breakpoint();
|
|
1036
|
+
const ett = await this.estimateTransaction({
|
|
1037
|
+
dry,
|
|
1038
|
+
body,
|
|
1039
|
+
fromToken,
|
|
1040
|
+
prices
|
|
1041
|
+
});
|
|
1042
|
+
execTime.log("estimateTransaction");
|
|
1043
|
+
const totalValue = forwardTonAmount + buffer + ett.estimateSourceGas;
|
|
1044
|
+
this.csl("TON quoteOneClickProxy", "blue-300", "totalValue: %o", totalValue);
|
|
1045
|
+
const transaction = {
|
|
1046
|
+
validUntil: Math.floor(Date.now() / 1e3) + 360,
|
|
1047
|
+
messages: [
|
|
1048
|
+
{
|
|
1049
|
+
address: userJettonWallet.toString(),
|
|
1050
|
+
amount: totalValue.toString(),
|
|
1051
|
+
payload: body.toBoc().toString("base64")
|
|
1052
|
+
}
|
|
1053
|
+
]
|
|
1054
|
+
};
|
|
1055
|
+
this.csl("TON quoteOneClickProxy", "blue-300", "transaction: %o", transaction);
|
|
1056
|
+
result.sendParam = {
|
|
1057
|
+
transaction,
|
|
1058
|
+
body
|
|
1059
|
+
};
|
|
1060
|
+
result.fees.estimateGasUsd = ett.estimateSourceGasUsd;
|
|
1061
|
+
result.estimateSourceGas = ett.estimateSourceGas;
|
|
1062
|
+
result.totalEstimateSourceGas = ett.estimateSourceGas;
|
|
1063
|
+
result.estimateSourceGasUsd = ett.estimateSourceGasUsd;
|
|
1064
|
+
execTime.logTotal("quoteOneClickProxy");
|
|
1065
|
+
return result;
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
|
|
1069
|
+
exports.TonWallet = TonWallet;
|
|
1070
|
+
exports.default = TonWallet;
|
|
1071
|
+
//# sourceMappingURL=index.js.map
|
|
1072
|
+
//# sourceMappingURL=index.js.map
|