Skip to content

Commit 5e85805

Browse files
Purple Guyvlad0137645re
authored
Update Client 0.3.3 | Make TonClient Disposable, Fix Stack Parsing (continuation-team#91)
* GITBOOK-35: No subject * GITBOOK-36: No subject * Add StackItem parsing | Client 0.3.2 * implement JettonMinter.cs * Add NftCollection.cs * GITBOOK-37: No subject * update TonSdk.Contracts.csproj * GITBOOK-38: No subject * GITBOOK-39: No subject * GITBOOK-40: No subject * add sse client cancelation on disconnect * added tg wallet support * GITBOOK-42: No subject * update deps version * Update Transformers.cs continuation-team#86 (continuation-team#87) Co-authored-by: Purple Guy <[email protected]> * GITBOOK-45: No subject * add TonCenter v3, TonWhales API * remove console writeline * make TonClient.cs disposable (continuation-team#90) * change package version, fix stack item parsing --------- Co-authored-by: vlad013 <[email protected]> Co-authored-by: Daniel <[email protected]>
1 parent 2615764 commit 5e85805

File tree

7 files changed

+241
-14
lines changed

7 files changed

+241
-14
lines changed
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
using System.Threading.Tasks;
2+
using TonSdk.Client.Stack;
3+
using TonSdk.Core;
4+
using TonSdk.Core.Block;
5+
using TonSdk.Core.Boc;
6+
7+
namespace TonSdk.Client
8+
{
9+
public interface ITonClient
10+
{
11+
Jetton Jetton { get; }
12+
Nft Nft { get; }
13+
Wallet Wallet { get; }
14+
Dns Dns { get; }
15+
TonClientType GetClientType();
16+
17+
/// <summary>
18+
/// Retrieves the balance of the specified address.
19+
/// </summary>
20+
/// <param name="address">The address to retrieve the balance for.</param>
21+
/// <returns>The task result contains the balance as a Coins instance.</returns>
22+
Task<Coins> GetBalance(Address address);
23+
24+
/// <summary>
25+
/// Checks if a contract is deployed at the specified address.
26+
/// </summary>
27+
/// <param name="address">The address to check.</param>
28+
/// <returns>The task result indicates whether a contract is deployed (true) or not (false) at the specified address.</returns>
29+
Task<bool> IsContractDeployed(Address address);
30+
31+
/// <summary>
32+
/// Retrieves the address information for the specified address.
33+
/// </summary>
34+
/// <param name="address">The address object to retrieve information for.</param>
35+
/// <returns>An object containing the address information.</returns>
36+
Task<AddressInformationResult?> GetAddressInformation(Address address);
37+
38+
/// <summary>
39+
/// Retrieves the wallet information for the specified address.
40+
/// </summary>
41+
/// <param name="address">The address object to retrieve information for.</param>
42+
/// <returns>An object containing the wallet information.</returns>
43+
Task<WalletInformationResult?> GetWalletInformation(Address address);
44+
45+
/// <summary>
46+
/// Get up-to-date masterchain state.
47+
/// </summary>
48+
/// <returns>An object containing the masterchain information.</returns>
49+
Task<MasterchainInformationResult?> GetMasterchainInfo();
50+
51+
/// <summary>
52+
/// Look up block by either seqno, lt or unixTime.
53+
/// </summary>
54+
/// <returns>An object containing the block information.</returns>
55+
Task<BlockIdExtended> LookUpBlock(int workchain, long shard, long? seqno = null, ulong? lt = null, ulong? unixTime = null);
56+
57+
/// <summary>
58+
/// Get metadata of a given block.
59+
/// </summary>
60+
/// <returns>An object containing the block metadata.</returns>
61+
Task<BlockDataResult?> GetBlockData(int workchain,
62+
long shard,
63+
long seqno,
64+
string rootHash = null,
65+
string fileHash = null);
66+
67+
/// <summary>
68+
/// Get transactions of the given block.
69+
/// </summary>
70+
/// <param name="workchain"></param>
71+
/// <param name="shard"></param>
72+
/// <param name="seqno"></param>
73+
/// <param name="rootHash"></param>
74+
/// <param name="fileHash"></param>
75+
/// <param name="afterLt"></param>
76+
/// <param name="afterHash"></param>
77+
/// <param name="count"></param>
78+
/// <returns>An object containing the shards information.</returns>
79+
Task<BlockTransactionsResult?> GetBlockTransactions(
80+
int workchain,
81+
long shard,
82+
long seqno,
83+
string rootHash = null,
84+
string fileHash = null,
85+
ulong? afterLt = null,
86+
string afterHash = null,
87+
uint count = 10);
88+
89+
/// <summary>
90+
/// Retrieves transaction information for the specified address.
91+
/// </summary>
92+
/// <param name="address">The address object to retrieve transaction information for.</param>
93+
/// <param name="limit">The maximum number of transactions to retrieve (default: 10).</param>
94+
/// <param name="lt">The logical time of the transaction to start retrieving from (optional).</param>
95+
/// <param name="hash">The hash of the transaction to start retrieving from (optional).</param>
96+
/// <param name="to_lt">The logical time of the transaction to retrieve up to (optional).</param>
97+
/// <param name="archival">Specifies whether to retrieve transactions from archival nodes (optional).</param>
98+
/// <returns>An array of transaction information results.</returns>
99+
Task<TransactionsInformationResult[]> GetTransactions(Address address, uint limit = 10,
100+
ulong? lt = null, string hash = null, ulong? to_lt = null, bool? archival = null);
101+
102+
/// <summary>
103+
/// Executes a specific method on the specified address.
104+
/// </summary>
105+
/// <param name="address">The address object to execute the method on.</param>
106+
/// <param name="method">The name of the method to execute.</param>
107+
/// <param name="stackItems">The stack parameters for the method (optional).</param>
108+
/// <returns>The result of the executed method.</returns>
109+
Task<RunGetMethodResult?> RunGetMethod(Address address, string method, IStackItem[] stackItems);
110+
111+
/// <summary>
112+
/// Executes a specific method on the specified address.
113+
/// </summary>
114+
/// <param name="address">The address object to execute the method on.</param>
115+
/// <param name="method">The name of the method to execute.</param>
116+
/// <param name="stack">The stack parameters for the method (optional).</param>
117+
/// <returns>The result of the executed method.</returns>
118+
Task<RunGetMethodResult?> RunGetMethod(Address address, string method, string[][] stack);
119+
120+
/// <summary>
121+
/// Sends a Bag of Cells (BoC) to the network.
122+
/// </summary>
123+
/// <param name="boc">The Cell object representing the Bag of Cells.</param>
124+
/// <returns>The result of sending the Bag of Cells.</returns>
125+
Task<SendBocResult?> SendBoc(Cell boc);
126+
127+
/// <summary>
128+
/// Retrieves a configuration parameter by its ID.
129+
/// </summary>
130+
/// <param name="configId">The ID of the configuration parameter to retrieve.</param>
131+
/// <param name="seqno">The sequence number of the configuration parameter (optional).</param>
132+
/// <returns>The result of the configuration parameter retrieval.</returns>
133+
Task<ConfigParamResult?> GetConfigParam(int configId, int? seqno = null);
134+
135+
/// <summary>
136+
/// Get shards information.
137+
/// </summary>
138+
/// <param name="seqno">Masterchain seqno to fetch shards of.</param>
139+
/// <returns>An object containing the shards information.</returns>
140+
Task<ShardsInformationResult?> Shards(long seqno);
141+
142+
/// <summary>
143+
/// Estimates fee for the message
144+
/// </summary>
145+
/// <param name="message">The message for which you need to calculate the fees</param>
146+
/// <returns>The result of estimation fees.</returns>
147+
Task<IEstimateFeeResult> EstimateFee(MessageX message, bool ignoreChksig = true);
148+
}
149+
}

TonSdk.Client/src/Client/TonClient.cs

+18-10
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77

88
namespace TonSdk.Client
99
{
10-
public class TonClient
10+
public class TonClient : ITonClient, IDisposable
1111
{
12-
private TonClientType _type;
13-
private HttpApi _httpApi;
14-
private HttpApiV3 _httpApiV3;
15-
private HttpWhales _httpWhales;
16-
private LiteClientApi _liteClientApi;
12+
private readonly TonClientType _type;
13+
private readonly HttpApi _httpApi;
14+
private readonly HttpApiV3 _httpApiV3;
15+
private readonly HttpWhales _httpWhales;
16+
private readonly LiteClientApi _liteClientApi;
1717

18-
public Jetton Jetton { get; private set; }
19-
public Nft Nft { get; private set; }
20-
public Wallet Wallet { get; private set; }
21-
public Dns Dns { get; private set; }
18+
public Jetton Jetton { get; }
19+
public Nft Nft { get; }
20+
public Wallet Wallet { get; }
21+
public Dns Dns { get; }
2222

2323
public TonClient(TonClientType clientType, ITonClientOptions options)
2424
{
@@ -339,5 +339,13 @@ public async Task<bool> IsContractDeployed(Address address)
339339
_ => null
340340
};
341341
}
342+
343+
public void Dispose()
344+
{
345+
_httpApi?.Dispose();
346+
_httpApiV3?.Dispose();
347+
_httpWhales?.Dispose();
348+
_liteClientApi?.Dispose();
349+
}
342350
}
343351
}

TonSdk.Client/src/LiteClientApi/LiteClientApi.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
namespace TonSdk.Client
1616
{
17-
internal class LiteClientApi
17+
internal class LiteClientApi : IDisposable
1818
{
19-
private LiteClient _liteClient;
19+
private readonly LiteClient _liteClient;
2020

2121
internal LiteClientApi(string host, int port, string pubKey)
2222
{
@@ -516,5 +516,10 @@ private void LoadBinTreeR(CellSlice slice, ref List<BlockIdExtended> shards)
516516
LoadBinTreeR(slice.LoadRef().Parse(), ref shards);
517517
}
518518
}
519+
520+
public void Dispose()
521+
{
522+
_liteClient.Disconnect();
523+
}
519524
}
520525
}

TonSdk.Client/src/Models/Transformers.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,9 @@ internal static object ParseStackItem(object[] item)
10141014
string slice = isNegative ? valueStr.Substring(3) : valueStr.Substring(2);
10151015

10161016
if (slice.Length % 2 != 0)
1017+
{
10171018
slice = "0" + slice;
1019+
}
10181020

10191021
int length = slice.Length;
10201022
byte[] bytes = new byte[length / 2];
@@ -1023,8 +1025,16 @@ internal static object ParseStackItem(object[] item)
10231025
bytes[i / 2] = Convert.ToByte(slice.Substring(i, 2), 16);
10241026
}
10251027

1026-
var bigInt = new BigInteger(bytes.Reverse().ToArray());
1028+
if (bytes[0] >= 0x80)
1029+
{
1030+
byte[] temp = new byte[bytes.Length + 1];
1031+
Array.Copy(bytes, 0, temp, 1, bytes.Length);
1032+
bytes = temp;
1033+
}
10271034

1035+
Array.Reverse(bytes);
1036+
var bigInt = new BigInteger(bytes);
1037+
10281038
return isNegative ? 0 - bigInt : bigInt;
10291039
}
10301040
case "cell":

TonSdk.Client/src/TonSdk.Client.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<LangVersion>8.0</LangVersion>
77
<RootNamespace>TonSdk.Client</RootNamespace>
88
<PackageId>TonSdk.Client</PackageId>
9-
<Version>0.3.3</Version>
9+
<Version>0.3.4</Version>
1010
<Authors>Continuation Team</Authors>
1111
<PackageDescription>Ton Client library for work with HTTP API and Lite Servers.</PackageDescription>
1212
<RepositoryUrl>https://github.com/continuation-team/TonSdk.NET</RepositoryUrl>

TonSdk.Client/test/Client.test.cs

+49
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Numerics;
23
using System.Threading.Tasks;
34
using NUnit.Framework;
45
using TonSdk.Core;
@@ -13,6 +14,54 @@ public class ClientTest
1314
new HttpParameters() { Endpoint = "https://toncenter.com/api/v3/", ApiKey = "841a5532a6549cc38f465973eab609c9acb34bb8a15608c85468af38a2842cc9" });
1415
TonClient client_lite = new TonClient(TonClientType.LITECLIENT, new LiteClientParameters("5.9.10.47", 19949, "n4VDnSCUuSpjnCyUk9e3QOOd6o0ItSWYbTnW3Wnn8wk="));
1516

17+
[OneTimeTearDown]
18+
public void TearDown()
19+
{
20+
client.Dispose();
21+
client_lite.Dispose();
22+
clientv3.Dispose();
23+
}
24+
25+
private BigInteger ParseStackItemNum(string valueStr)
26+
{
27+
bool isNegative = valueStr[0] == '-';
28+
string slice = isNegative ? valueStr.Substring(3) : valueStr.Substring(2);
29+
30+
if (slice.Length % 2 != 0)
31+
{
32+
slice = "0" + slice;
33+
}
34+
35+
int length = slice.Length;
36+
byte[] bytes = new byte[length / 2];
37+
for (int i = 0; i < length; i += 2)
38+
{
39+
bytes[i / 2] = Convert.ToByte(slice.Substring(i, 2), 16);
40+
}
41+
42+
if (bytes[0] >= 0x80)
43+
{
44+
byte[] temp = new byte[bytes.Length + 1];
45+
Array.Copy(bytes, 0, temp, 1, bytes.Length);
46+
bytes = temp;
47+
}
48+
49+
Array.Reverse(bytes);
50+
var bigInt = new BigInteger(bytes);
51+
52+
return isNegative ? 0 - bigInt : bigInt;
53+
}
54+
55+
[Test]
56+
public void Test_ParseStackItemNum()
57+
{
58+
Assert.That(ParseStackItemNum("0xf") == 15, Is.EqualTo(true));
59+
Assert.That(ParseStackItemNum("0x0f") == 15, Is.EqualTo(true));
60+
Assert.That(ParseStackItemNum("0x10") == 16, Is.EqualTo(true));
61+
Assert.That(ParseStackItemNum("0x159ecd52") == 362728786, Is.EqualTo(true));
62+
Assert.That(ParseStackItemNum("0x9a") == 154, Is.EqualTo(true));
63+
}
64+
1665
[Test]
1766
public async Task Test_AddressBalance()
1867
{

TonSdk.Client/test/LiteClientApi.test.cs

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public void SetUpTest()
1717
{
1818
_client = new TonClient(TonClientType.LITECLIENT, new LiteClientParameters("5.9.10.47", 19949, "n4VDnSCUuSpjnCyUk9e3QOOd6o0ItSWYbTnW3Wnn8wk="));
1919
}
20+
21+
[TearDown]
22+
public void TearDownTest()
23+
{
24+
_client.Dispose();
25+
}
2026

2127
[Test]
2228
public async Task Test_GetAddressInformation()

0 commit comments

Comments
 (0)