loadContractCodeForContractId method

Future<XdrContractCodeEntry?> loadContractCodeForContractId(
  1. String contractId
)
inherited

Loads the contract code entry (including source code - wasm bytes) for a given contract id.

Implementation

Future<XdrContractCodeEntry?> loadContractCodeForContractId(
    String contractId) async {
  XdrLedgerKey ledgerKey = XdrLedgerKey(XdrLedgerEntryType.CONTRACT_DATA);
  ledgerKey.contractData = XdrLedgerKeyContractData(
      Address.forContractId(contractId).toXdr(),
      XdrSCVal.forLedgerKeyContractInstance(),
      XdrContractDataDurability.PERSISTENT);

  GetLedgerEntriesResponse ledgerEntriesResponse =
      await getLedgerEntries([ledgerKey.toBase64EncodedXdrString()]);
  if (ledgerEntriesResponse.entries != null &&
      ledgerEntriesResponse.entries!.length > 0) {
    XdrLedgerEntryData ledgerEntryData =
        ledgerEntriesResponse.entries![0].ledgerEntryDataXdr;
    if (ledgerEntryData.contractData != null &&
        ledgerEntryData.contractData?.val.instance?.executable.wasmHash !=
            null) {
      String wasmId = Util.bytesToHex(ledgerEntryData
          .contractData!.val.instance!.executable.wasmHash!.hash);
      return await (loadContractCodeForWasmId(wasmId));
    }
  }
  return null;
}