Skip to content

Commit 0be88d1

Browse files
committed
add getLibraries method to TonCenter client
1 parent f1f81bc commit 0be88d1

File tree

5 files changed

+2476
-23
lines changed

5 files changed

+2476
-23
lines changed

smartcontract/src/test/java/org/ton/ton4j/smartcontract/integrationtests/TestLibraryDeployer.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.math.BigInteger;
44
import java.util.Collections;
5+
import java.util.Map;
6+
57
import lombok.extern.slf4j.Slf4j;
68
import org.junit.Test;
79
import org.junit.runner.RunWith;
@@ -16,10 +18,15 @@
1618
import org.ton.ton4j.tl.liteserver.responses.LibraryEntry;
1719
import org.ton.ton4j.tl.liteserver.responses.LibraryResult;
1820
import org.ton.ton4j.toncenter.TonCenter;
21+
import org.ton.ton4j.toncenter.TonResponse;
22+
import org.ton.ton4j.toncenter.model.GetLibrariesResponse;
1923
import org.ton.ton4j.tonlib.types.SmcLibraryEntry;
2024
import org.ton.ton4j.tonlib.types.SmcLibraryResult;
2125
import org.ton.ton4j.utils.Utils;
2226

27+
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.Assert.assertTrue;
29+
2330
@Slf4j
2431
@RunWith(JUnit4.class)
2532
public class TestLibraryDeployer extends CommonTest {
@@ -52,6 +59,8 @@ public void testDeployLibraryDeployer() throws InterruptedException {
5259
10,
5360
"Deployment of LibraryDeployer will never happen. Lite-server will return an error, but the library will be deployed");
5461

62+
log.info("walletV5Code library hash(b64) in testnet {}", Utils.bytesToBase64(walletV5Code.getHash()));
63+
5564
SmcLibraryResult smcLibraryResult =
5665
tonlib.getLibraries(
5766
Collections.singletonList(
@@ -70,6 +79,8 @@ public void testDeployLibraryDeployer() throws InterruptedException {
7079
@Test
7180
public void testIfLibraryHasBeenDeployed() {
7281
Cell walletV5Code = CellBuilder.beginCell().fromBoc(WalletCodes.V5R1.getValue()).endCell();
82+
log.info("walletV5Code library hash(hex) in testnet {}", Utils.bytesToHex(walletV5Code.getHash()));
83+
log.info("walletV5Code library hash(b64) in testnet {}", Utils.bytesToBase64(walletV5Code.getHash()));
7384
SmcLibraryResult smcLibraryResult =
7485
tonlib.getLibraries(Collections.singletonList(Utils.bytesToBase64(walletV5Code.getHash())));
7586
// "IINLe3KxEhR+Gy+0V7hOdNGjDwT3N9T2KmaOlVLSty8="));
@@ -157,10 +168,22 @@ public void testDeployLibraryDeployerTonCenterClient() throws Exception {
157168
10,
158169
"Deployment of LibraryDeployer will never happen. Lite-server will return an error, but the library will be deployed");
159170

160-
// Note: TonCenter API doesn't provide a direct getLibraries method like AdnlLiteClient
161-
// To verify library deployment, you would need to use other methods or check through
162-
// a block explorer or other means
163-
log.info("Library should be deployed at this point. Verify through other means if needed.");
171+
TonResponse<GetLibrariesResponse> smcLibraryResult =
172+
tonCenter.getLibraries(
173+
Collections.singletonList(
174+
Utils.bytesToHex(walletV5Code.getHash())
175+
));
176+
log.info("response {}", smcLibraryResult.getResult());
177+
178+
// The response should be successful even if libraries don't exist
179+
assertTrue("Get libraries should be successful", smcLibraryResult.isSuccess());
180+
assertNotNull("Libraries response should not be null", smcLibraryResult.getResult());
181+
182+
for (Map.Entry<String, String> entry : smcLibraryResult.getResult().getLibraries().entrySet()) {
183+
184+
Cell libCell = Cell.fromBoc(entry.getValue());
185+
log.info("cell lib {}", libCell.toHex());
186+
}
164187
}
165188

166189
@Test
@@ -181,19 +204,4 @@ public void testIfLibraryHasBeenDeployedAdnlLiteClient() throws Exception {
181204
log.info("cell lib {}", libCell.toHex());
182205
}
183206
}
184-
185-
@Test
186-
public void testIfLibraryHasBeenDeployedTonCenterClient() throws Exception {
187-
TonCenter tonCenter =
188-
TonCenter.builder()
189-
.apiKey(TESTNET_API_KEY)
190-
.testnet()
191-
.build();
192-
Cell walletV5Code = CellBuilder.beginCell().fromBoc(WalletCodes.V5R1.getValue()).endCell();
193-
194-
// Note: TonCenter API doesn't provide a direct getLibraries method like AdnlLiteClient
195-
// This test is a placeholder to maintain consistency with the AdnlLiteClient version
196-
log.info("Library hash: {}", Utils.bytesToBase64(walletV5Code.getHash()));
197-
log.info("To check if the library has been deployed, use other methods or external tools");
198-
}
199207
}

toncenter/openapi-v2.json

Lines changed: 2387 additions & 1 deletion
Large diffs are not rendered by default.

toncenter/src/main/java/org/ton/ton4j/toncenter/TonCenter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,18 @@ public TonResponse<ConfigAllResponse> getConfigAll(Long seqno) {
570570
return executeGet("/getConfigAll", params, responseType);
571571
}
572572

573+
/** Get libraries codes */
574+
public TonResponse<GetLibrariesResponse> getLibraries(List<String> libraries) {
575+
Map<String, String> params = new HashMap<>();
576+
if (nonNull(libraries) && !libraries.isEmpty()) {
577+
// Join libraries with comma as the API expects array parameter
578+
params.put("libraries", String.join(",", libraries));
579+
}
580+
581+
Type responseType = new TypeToken<TonResponse<GetLibrariesResponse>>() {}.getType();
582+
return executeGet("/getLibraries", params, responseType);
583+
}
584+
573585
// ========== TRANSACTION METHODS ==========
574586

575587
/** Locate outcoming transaction of destination address by incoming message */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.ton.ton4j.toncenter.model;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import java.util.Map;
6+
7+
/** Response for getLibraries endpoint */
8+
@Data
9+
public class GetLibrariesResponse {
10+
@SerializedName("libraries")
11+
private Map<String, String> libraries;
12+
}

toncenter/src/test/java/org/ton/ton4j/toncenter/TonCenterTest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
66
import org.junit.runners.Parameterized;
7+
import org.ton.ton4j.cell.Cell;
8+
import org.ton.ton4j.cell.CellBuilder;
79
import org.ton.ton4j.toncenter.model.*;
810
import static org.ton.ton4j.toncenter.model.CommonResponses.*;
911

@@ -560,6 +562,38 @@ public void testGetConfigAll() {
560562
}
561563
}
562564

565+
@Test
566+
public void testGetLibraries() {
567+
if (network == Network.MAINNET) {
568+
log.info("Not yet implemented in MAINNET");
569+
return;
570+
}
571+
enforceRateLimit();
572+
TonCenter client = TonCenter.builder().apiKey(apiKey).network(network).build();
573+
574+
try {
575+
576+
// Test with some example library hashes (these may not exist, but tests the endpoint structure)
577+
List<String> libraryHashes = Arrays.asList(
578+
"IINLe3KxEhR+Gy+0V7hOdNGjDwT3N9T2KmaOlVLSty8=" // in testnet only
579+
// "20834b7b72b112147e1b2fb457b84e74d1a30f04f737d4f62a668e9552d2b72f" // in testnet only
580+
);
581+
582+
TonResponse<GetLibrariesResponse> response = client.getLibraries(libraryHashes);
583+
log.info("response {}", response.getResult());
584+
585+
// The response should be successful even if libraries don't exist
586+
assertTrue("Get libraries should be successful", response.isSuccess());
587+
assertNotNull("Libraries response should not be null", response.getResult());
588+
log.info("Libraries retrieved successfully");
589+
} catch (TonCenterApiException e) {
590+
// This is expected if the library hashes don't exist
591+
log.info("Libraries not found (expected): {}", e.getMessage());
592+
} finally {
593+
client.close();
594+
}
595+
}
596+
563597
// ========== TRANSACTION METHODS TESTS (3 endpoints) ==========
564598

565599
@Test
@@ -826,7 +860,7 @@ public void testApiKeyValidation() {
826860
@Test
827861
public void testAllEndpointsCount() {
828862
enforceRateLimit();
829-
// This test verifies that all 27 endpoints are implemented
863+
// This test verifies that all 28 endpoints are implemented
830864

831865
TonCenter client = TonCenter.builder().build();
832866

@@ -859,9 +893,10 @@ public void testAllEndpointsCount() {
859893
assertNotNull("getBlockHeader should exist", getMethod(client, "getBlockHeader"));
860894
assertNotNull("getOutMsgQueueSizes should exist", getMethod(client, "getOutMsgQueueSizes"));
861895

862-
// Config methods (2)
896+
// Config methods (3)
863897
assertNotNull("getConfigParam should exist", getMethod(client, "getConfigParam"));
864898
assertNotNull("getConfigAll should exist", getMethod(client, "getConfigAll"));
899+
assertNotNull("getLibraries should exist", getMethod(client, "getLibraries"));
865900

866901
// Transaction methods (3)
867902
assertNotNull("tryLocateTx should exist", getMethod(client, "tryLocateTx"));
@@ -877,7 +912,7 @@ public void testAllEndpointsCount() {
877912
assertNotNull("sendQuery should exist", getMethod(client, "sendQuery"));
878913
assertNotNull("estimateFee should exist", getMethod(client, "estimateFee"));
879914

880-
log.info("✅ All 27 TonCenter API endpoints are implemented!");
915+
log.info("✅ All 28 TonCenter API endpoints are implemented!");
881916
client.close();
882917
}
883918

0 commit comments

Comments
 (0)