-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathTonClient.cs
More file actions
428 lines (398 loc) · 21.3 KB
/
TonClient.cs
File metadata and controls
428 lines (398 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Threading.Tasks;
using TonSdk.Client.Stack;
using TonSdk.Core;
using TonSdk.Core.Block;
using TonSdk.Core.Boc;
namespace TonSdk.Client
{
public class TonClient : ITonClient, IDisposable
{
private readonly TonClientType _type;
private readonly HttpApi _httpApi;
private readonly HttpApiV3 _httpApiV3;
private readonly HttpWhales _httpWhales;
private readonly LiteClientApi _liteClientApi;
public Jetton Jetton { get; }
public Nft Nft { get; }
public Wallet Wallet { get; }
public Dns Dns { get; }
public TonClient(TonClientType clientType, ITonClientOptions options)
{
Jetton = new Jetton(this);
Nft = new Nft(this);
Wallet = new Wallet(this);
Dns = new Dns(this);
switch (clientType)
{
case TonClientType.HTTP_TONCENTERAPIV2:
{
if (!(options is HttpParameters))
throw new Exception("Wrong provided options for HTTP Client type, use HttpParameters instead.");
var opts = (HttpParameters)options;
_httpApi = new HttpApi(opts);
break;
}
case TonClientType.HTTP_TONCENTERAPIV3:
{
if (!(options is HttpParameters))
throw new Exception("Wrong provided options for HTTP Client type, use HttpParameters instead.");
var opts = (HttpParameters)options;
_httpApiV3 = new HttpApiV3(opts);
break;
}
case TonClientType.HTTP_TONWHALESAPI:
{
if (!(options is HttpParameters))
throw new Exception("Wrong provided options for HTTP Client type, use HttpParameters instead.");
var opts = (HttpParameters)options;
_httpWhales = new HttpWhales(opts);
break;
}
case TonClientType.LITECLIENT:
{
if (!(options is LiteClientParameters))
throw new Exception("Wrong provided options for Lite Client type, use LiteClientParameters instead.");
var opts = (LiteClientParameters)options;
_liteClientApi = new LiteClientApi(opts.Host, opts.Port, opts.PeerPublicKey);
break;
}
}
_type = clientType;
}
public TonClientType GetClientType() => _type;
/// <summary>
/// Retrieves the balance of the specified address.
/// </summary>
/// <param name="address">The address to retrieve the balance for.</param>
/// <param name="block">Can be placed to fetch in specific block, requires LiteClient (optional).</param>
/// <returns>The task result contains the balance as a Coins instance.</returns>
public async Task<Coins> GetBalance(Address address, BlockIdExtended? block = null)
{
return (await GetAddressInformation(address, block))?.Balance;
}
/// <summary>
/// Checks if a contract is deployed at the specified address.
/// </summary>
/// <param name="address">The address to check.</param>
/// <param name="block">Can be placed to fetch in specific block, requires LiteClient (optional).</param>
/// <returns>The task result indicates whether a contract is deployed (true) or not (false) at the specified address.</returns>
public async Task<bool> IsContractDeployed(Address address, BlockIdExtended? block = null)
{
return (await GetAddressInformation(address, block))?.State == AccountState.Active;
}
/// <summary>
/// Retrieves the address information for the specified address.
/// </summary>
/// <param name="address">The address object to retrieve information for.</param>
/// <param name="block">Can be placed to fetch in specific block, requires LiteClient (optional).</param>
/// <returns>An object containing the address information.</returns>
public async Task<AddressInformationResult?> GetAddressInformation(Address address, BlockIdExtended? block = null)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.GetAddressInformation(address),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.GetAddressInformation(address),
TonClientType.HTTP_TONWHALESAPI => await _httpWhales.GetAddressInformation(address),
TonClientType.LITECLIENT => await _liteClientApi.GetAddressInformation(address, block),
_ => null
};
}
/// <summary>
/// Retrieves the wallet information for the specified address.
/// </summary>
/// <param name="address">The address object to retrieve information for.</param>
/// <param name="block">Can be placed to fetch in specific block, requires LiteClient (optional).</param>
/// <returns>An object containing the wallet information.</returns>
public async Task<WalletInformationResult?> GetWalletInformation(Address address, BlockIdExtended? block = null)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.GetWalletInformation(address),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.GetWalletInformation(address),
TonClientType.HTTP_TONWHALESAPI => await _httpWhales.GetWalletInformation(address),
TonClientType.LITECLIENT => await _liteClientApi.GetWalletInformation(address, block),
_ => null
};
}
/// <summary>
/// Get up-to-date masterchain state.
/// </summary>
/// <returns>An object containing the masterchain information.</returns>
public async Task<MasterchainInformationResult?> GetMasterchainInfo()
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.GetMasterchainInfo(),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.GetMasterchainInfo(),
TonClientType.LITECLIENT => await _liteClientApi.GetMasterchainInfo(),
TonClientType.HTTP_TONWHALESAPI => throw new Exception("Method not supported by HTTP_TONWHALESAPI client."),
_ => null
};
}
/// <summary>
/// Look up block by either seqno, lt or unixTime.
/// </summary>
/// <returns>An object containing the block information.</returns>
public async Task<BlockIdExtended?> LookUpBlock(int workchain, long shard, long? seqno = null, ulong? lt = null, ulong? unixTime = null)
{
if (seqno == null && lt == null && unixTime == null)
throw new ArgumentException("Seqno, lt or unixTime should be defined");
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.LookUpBlock(workchain, shard, seqno, lt, unixTime),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.LookUpBlock(workchain, shard, seqno, lt, unixTime),
TonClientType.LITECLIENT => await _liteClientApi.LookUpBlock(workchain, shard, seqno, lt, unixTime),
TonClientType.HTTP_TONWHALESAPI => throw new Exception("Method not supported by HTTP_TONWHALESAPI client."),
_ => null
};
}
/// <summary>
/// Get metadata of a given block.
/// </summary>
/// <returns>An object containing the block metadata.</returns>
public async Task<BlockDataResult?> GetBlockData(int workchain,
long shard,
long seqno,
string rootHash = null,
string fileHash = null)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.GetBlockHeader(workchain, shard, seqno, rootHash, fileHash),
TonClientType.HTTP_TONCENTERAPIV3 => throw new Exception("Method not supported by HTTP_TONCENTERAPIV3 client."),
TonClientType.LITECLIENT => throw new Exception("Method not supported yet."),
TonClientType.HTTP_TONWHALESAPI => throw new Exception("Method not supported by HTTP_TONWHALESAPI client."),
_ => null
};
}
/// <summary>
/// Get transactions of the given block.
/// </summary>
/// <param name="workchain"></param>
/// <param name="shard"></param>
/// <param name="seqno"></param>
/// <param name="rootHash"></param>
/// <param name="fileHash"></param>
/// <param name="afterLt"></param>
/// <param name="afterHash"></param>
/// <param name="count"></param>
/// <returns>An object containing the shards information.</returns>
public async Task<BlockTransactionsResult?> GetBlockTransactions(
int workchain,
long shard,
long seqno,
string rootHash = null,
string fileHash = null,
ulong? afterLt = null,
string afterHash = null,
uint count = 10)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.GetBlockTransactions(workchain, shard, seqno, rootHash, fileHash, afterLt, afterHash, count),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.GetBlockTransactions(workchain, shard, seqno, rootHash, fileHash, afterLt, afterHash, count),
TonClientType.LITECLIENT => await _liteClientApi.GetBlockTransactions(workchain, shard, seqno, rootHash, fileHash, afterLt, afterHash, count),
TonClientType.HTTP_TONWHALESAPI => throw new Exception("Method not supported by HTTP_TONWHALESAPI client."),
_ => null
};
}
/// <summary>
/// Get transactions of the given block (extended data).
/// </summary>
/// <param name="workchain"></param>
/// <param name="shard"></param>
/// <param name="seqno"></param>
/// <param name="rootHash"></param>
/// <param name="fileHash"></param>
/// <param name="afterLt"></param>
/// <param name="afterHash"></param>
/// <param name="count"></param>
/// <returns>An object containing the shards information.</returns>
public async Task<BlockTransactionsResultExtended?> GetBlockTransactionsExtended(
int workchain,
long shard,
long seqno,
string rootHash = null,
string fileHash = null,
ulong? afterLt = null,
string afterHash = null,
uint count = 10)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => throw new Exception("Method not supported by HTTP_TONCENTERAPIV2 client."),
TonClientType.HTTP_TONCENTERAPIV3 => throw new Exception("Method not supported by HTTP_TONCENTERAPIV3 client."),
TonClientType.LITECLIENT => await _liteClientApi.GetBlockTransactionsExtended(workchain, shard, seqno, rootHash, fileHash, afterLt, afterHash, count),
TonClientType.HTTP_TONWHALESAPI => throw new Exception("Method not supported by HTTP_TONWHALESAPI client."),
_ => null
};
}
/// <summary>
/// Retrieves transaction information for the specified address.
/// </summary>
/// <param name="address">The address object to retrieve transaction information for.</param>
/// <param name="limit">The maximum number of transactions to retrieve (default: 10).</param>
/// <param name="lt">The logical time of the transaction to start retrieving from (optional).</param>
/// <param name="hash">The hash of the transaction to start retrieving from (optional).</param>
/// <param name="to_lt">The logical time of the transaction to retrieve up to (optional).</param>
/// <param name="archival">Specifies whether to retrieve transactions from archival nodes (optional).</param>
/// <returns>An array of transaction information results.</returns>
public async Task<TransactionsInformationResult[]?> GetTransactions(Address address, uint limit = 10,
ulong? lt = null, string hash = null, ulong? to_lt = null, bool? archival = null)
{
if(_type == TonClientType.LITECLIENT && (lt == null || hash == null))
throw new ArgumentException("From lt and hash, must be defined for LiteClient type.");
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.GetTransactions(address, limit, lt, hash, to_lt, archival),
TonClientType.HTTP_TONWHALESAPI => await _httpWhales.GetTransactions(address, limit, lt, hash, to_lt, archival),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.GetTransactions(address, limit, lt, hash, to_lt, archival),
TonClientType.LITECLIENT => await _liteClientApi.GetTransactions(address, limit, (long)lt, hash),
_ => null
};
}
/// <summary>
/// Retrieves transaction information for the specified parameters.
/// </summary>
/// <param name="msgHash">Message hash of transaction.</param>
/// <param name="bodyHash">Body hash of transaction.</param>
/// <param name="opcode">Opcode of message in hex or signed 32-bit decimal form.</param>
/// <param name="direction">Direction of message.</param>
/// <param name="offset">Skip first N rows. Use with limit to batch read.</param>
/// <param name="count">Limit number of queried rows. Use with offset to batch read.</param>
/// <returns>An array of transaction information results.</returns>
public Task<TransactionsInformationResultExtended[]> GetTransactionsByMessage(string msgHash = null, string bodyHash = null, string opcode = null, MessageDirection? direction = null, int? offset = null, int? count = 10)
{
if (string.IsNullOrEmpty(msgHash) && string.IsNullOrEmpty(bodyHash) && string.IsNullOrEmpty(opcode))
throw new ArgumentException("At least one of msgHash, bodyHash, opcode should be specified");
if(_type != TonClientType.HTTP_TONCENTERAPIV3)
throw new NotSupportedException($"Method not supported for type {_type}");
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV3 => _httpApiV3.GetTransactionsByMessage(msgHash, bodyHash, opcode, direction, offset, count),
_ => null
};
}
/// <summary>
/// Executes a specific method on the specified address.
/// </summary>
/// <param name="address">The address object to execute the method on.</param>
/// <param name="method">The name of the method to execute.</param>
/// <param name="stackItems">The stack parameters for the method (optional).</param>
/// <param name="block">Can be placed to fetch in specific block, requeres LiteClient (optional).</param>
/// <returns>The result of the executed method.</returns>
public async Task<RunGetMethodResult?> RunGetMethod(Address address, string method, IStackItem[] stackItems,
BlockIdExtended? block = null)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.RunGetMethod(address, method,
StackUtils.PackInString(stackItems)),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.RunGetMethod(address, method,
StackUtils.PackInStringV3(stackItems)),
TonClientType.HTTP_TONWHALESAPI => await _httpWhales.RunGetMethod(address, method,
StackUtils.PackInString(stackItems)),
_ => await _liteClientApi.RunGetMethod(address, method, stackItems, block)
};
}
/// <summary>
/// Executes a specific method on the specified address.
/// </summary>
/// <param name="address">The address object to execute the method on.</param>
/// <param name="method">The name of the method to execute.</param>
/// <param name="stack">The stack parameters for the method (optional).</param>
/// <returns>The result of the executed method.</returns>
public async Task<RunGetMethodResult?> RunGetMethod(Address address, string method, string[][] stack)
{
if (_type != TonClientType.HTTP_TONCENTERAPIV2 || _type != TonClientType.HTTP_TONWHALESAPI)
throw new ArgumentException("string[][] stack, must be defined with HTTP_TONCENTERAPIV2 type.");
return await _httpApi.RunGetMethod(address, method, stack);
}
/// <summary>
/// Sends a Bag of Cells (BoC) to the network.
/// </summary>
/// <param name="boc">The Cell object representing the Bag of Cells.</param>
/// <returns>The result of sending the Bag of Cells.</returns>
public async Task<SendBocResult?> SendBoc(Cell boc)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.SendBoc(boc),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.SendBoc(boc),
TonClientType.LITECLIENT => await _liteClientApi.SendBoc(boc),
TonClientType.HTTP_TONWHALESAPI => await _httpWhales.SendBoc(boc),
_ => null
};
}
/// <summary>
/// Sends a Bag of Cells (BoC) to the network.
/// </summary>
/// <param name="boc">The Cell object representing the Bag of Cells.</param>
/// <returns>The result of sending the Bag of Cells.</returns>
public async Task<SendBocResult?> SendBocReturnHash(Cell boc)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.SendBocReturnHash(boc),
_ => null
};
}
/// <summary>
/// Retrieves a configuration parameter by its ID.
/// </summary>
/// <param name="configId">The ID of the configuration parameter to retrieve.</param>
/// <param name="seqno">The sequence number of the configuration parameter (optional).</param>
/// <returns>The result of the configuration parameter retrieval.</returns>
public async Task<ConfigParamResult?> GetConfigParam(int configId, int? seqno = null)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.GetConfigParam(configId, seqno),
TonClientType.HTTP_TONWHALESAPI => await _httpWhales.GetConfigParam(configId, seqno),
TonClientType.HTTP_TONCENTERAPIV3 => throw new Exception("Method not supported by HTTP_TONCENTERAPIV3 client."),
TonClientType.LITECLIENT => await _liteClientApi.GetConfigParam(configId),
_ => null
};
}
/// <summary>
/// Get shards information.
/// </summary>
/// <param name="seqno">Masterchain seqno to fetch shards of.</param>
/// <returns>An object containing the shards information.</returns>
public async Task<ShardsInformationResult?> Shards(long seqno)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.Shards(seqno),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.Shards(seqno),
TonClientType.LITECLIENT => await _liteClientApi.GetShards(seqno),
TonClientType.HTTP_TONWHALESAPI => throw new Exception("Method not supported by HTTP_TONCENTERAPIV3 client."),
_ => null
};
}
/// <summary>
/// Estimates fee for the message
/// </summary>
/// <param name="message">The message for which you need to calculate the fees</param>
/// <returns>The result of estimation fees.</returns>
public async Task<IEstimateFeeResult?> EstimateFee(MessageX message, bool ignoreChksig = true)
{
return _type switch
{
TonClientType.HTTP_TONCENTERAPIV2 => await _httpApi.EstimateFee(message, ignoreChksig),
TonClientType.HTTP_TONCENTERAPIV3 => await _httpApiV3.EstimateFee(message, ignoreChksig),
TonClientType.HTTP_TONWHALESAPI => await _httpWhales.EstimateFee(message, ignoreChksig),
TonClientType.LITECLIENT => throw new Exception("Method cannot be called with LiteClient. Use other client type instead."),
_ => null
};
}
public void Dispose()
{
_httpApi?.Dispose();
_httpApiV3?.Dispose();
_httpWhales?.Dispose();
_liteClientApi?.Dispose();
}
}
}