1212import org .stellar .sdk .xdr .LedgerKey .LedgerKeyContractData ;
1313
1414/**
15- * This example shows how to check the existence of the SAC (Stellar Asset Contract) of a class asset.
15+ * This example shows how to check the existence of the SAC (Stellar Asset Contract) of a class
16+ * asset.
1617 */
1718public class CheckStellarAssetContract {
1819 public static void main (String [] args ) throws IOException {
1920 String rpcServerUrl = "https://soroban-testnet.stellar.org:443" ;
21+ Network network = Network .TESTNET ;
2022 Asset assetNative = Asset .createNativeAsset ();
2123 Asset assetUSDC =
2224 Asset .createNonNativeAsset (
@@ -26,28 +28,18 @@ public static void main(String[] args) throws IOException {
2628 "UNKN" , "GDQOE23CFSUMSVQK4Y5JHPPYK73VYCNHZHA7ENKCV37P6SUEO6XQBKPP" );
2729
2830 try (SorobanServer sorobanServer = new SorobanServer (rpcServerUrl )) {
29- System .out .println (
30- "The SAC ID of the asset ["
31- + assetNative
32- + "] is "
33- + getContractData (sorobanServer , assetNative ));
34- System .out .println (
35- "The SAC ID of the asset ["
36- + assetUSDC
37- + "] is "
38- + getContractData (sorobanServer , assetNative ));
39- System .out .println (
40- "The SAC ID of the asset ["
41- + assetUSDC
42- + "] is unknown ("
43- + getContractData (sorobanServer , assetUnknown )
44- + ")" );
31+ for (Asset asset : List .of (assetNative , assetUSDC , assetUnknown )) {
32+ String contractId = asset .getContractId (network );
33+ System .out .printf (
34+ "The contract id for %s is %s, it is %s\n " ,
35+ asset ,
36+ contractId ,
37+ isContractDeployed (sorobanServer , contractId ) ? "deployed" : "not deployed" );
38+ }
4539 }
4640 }
4741
48- public static String getContractData (SorobanServer sorobanServer , Asset asset )
49- throws IOException {
50- String contractId = asset .getContractId (Network .TESTNET );
42+ public static boolean isContractDeployed (SorobanServer sorobanServer , String contractId ) {
5143 List <LedgerKey > ledgerKeys =
5244 List .of (
5345 LedgerKey .builder ()
@@ -60,13 +52,6 @@ public static String getContractData(SorobanServer sorobanServer, Asset asset)
6052 .build ())
6153 .build ());
6254 GetLedgerEntriesResponse response = sorobanServer .getLedgerEntries (ledgerKeys );
63- if (response .getEntries () != null && !response .getEntries ().isEmpty ()) {
64- LedgerEntryData entryData =
65- LedgerEntryData .fromXdrBase64 (response .getEntries ().get (0 ).getXdr ());
66- if (entryData .getContractData () == null ) return null ;
67- return entryData .getContractData ().getContract ().getContractId ().toXdrBase64 ();
68- } else {
69- return null ;
70- }
55+ return response .getEntries () != null && !response .getEntries ().isEmpty ();
7156 }
7257}
0 commit comments