Skip to content

Conversation

@lijamie98
Copy link
Contributor

@lijamie98 lijamie98 commented May 20, 2025

Description

  • The CheckStellarAssetContract example shows how to check the existence of the SAC (Stellar Asset Contract) of a class
  • asset.

Context

  • Add example to help developer experience.

@codecov
Copy link

codecov bot commented May 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.18%. Comparing base (42733cc) to head (cea7361).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master     #688   +/-   ##
=========================================
  Coverage     80.18%   80.18%           
  Complexity     1227     1227           
=========================================
  Files           213      213           
  Lines          4820     4820           
  Branches        412      412           
=========================================
  Hits           3865     3865           
  Misses          703      703           
  Partials        252      252           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

LedgerEntryData entryData =
LedgerEntryData.fromXdrBase64(response.getEntries().get(0).getXdr());
if (entryData.getContractData() == null) return null;
return entryData.getContractData().getContract().getContractId().toXdrBase64();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return here is a base64 character; returning the C... address would be better.

+ getContractData(sorobanServer, assetNative));
System.out.println(
"The SAC ID of the asset ["
+ assetUSDC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
+ assetUSDC
+ assetUnknown

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

}
}

public static String getContractData(SorobanServer sorobanServer, Asset asset)
Copy link
Member

@overcat overcat May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to change this to boolean isContractDeployed(SorobanServer sorobanServer, String contractId)?

public class CheckStellarAssetContract {
  public static void main(String[] args) throws IOException {
    String rpcServerUrl = "https://soroban-testnet.stellar.org:443";
    Network network = Network.TESTNET;
    Asset assetNative = Asset.createNativeAsset();
    Asset assetUSDC =
        Asset.createNonNativeAsset(
            "USDC", "GDQOE23CFSUMSVQK4Y5JHPPYK73VYCNHZHA7ENKCV37P6SUEO6XQBKPP");
    Asset assetUnknown =
        Asset.createNonNativeAsset(
            "UNKN", "GDQOE23CFSUMSVQK4Y5JHPPYK73VYCNHZHA7ENKCV37P6SUEO6XQBKPP");

    try (SorobanServer sorobanServer = new SorobanServer(rpcServerUrl)) {
      for (Asset asset : List.of(assetNative, assetUSDC, assetUnknown)) {
        String contractId = asset.getContractId(network);
        System.out.printf(
            "The contract id for %s is %s, it is %s\n",
            asset,
            contractId,
            isContractDeployed(sorobanServer, contractId) ? "deployed" : "not deployed");
      }
    }
  }

  public static boolean isContractDeployed(SorobanServer sorobanServer, String contractId) {
    List<LedgerKey> ledgerKeys =
        List.of(
            LedgerKey.builder()
                .discriminant(LedgerEntryType.CONTRACT_DATA)
                .contractData(
                    LedgerKeyContractData.builder()
                        .contract(new Address(contractId).toSCAddress())
                        .key(Scv.toLedgerKeyContractInstance())
                        .durability(ContractDataDurability.PERSISTENT)
                        .build())
                .build());
    GetLedgerEntriesResponse response = sorobanServer.getLedgerEntries(ledgerKeys);
    return response.getEntries() != null && !response.getEntries().isEmpty();
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

@lijamie98 lijamie98 force-pushed the examples/check-stellar-asset-contract branch from f67e537 to cea7361 Compare May 22, 2025 22:36
@overcat overcat merged commit a1edad0 into lightsail-network:master May 23, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants