Skip to main content
When you deploy a contract, first the code is “installed” (i.e., it is uploaded onto the blockchain). This creates a LedgerEntry containing the Wasm byte-code, which is uniquely identified by its hash (that is, the hash of the uploaded code itself). Then, when the contract is “deployed,” we create a LedgerEntry with a reference to that code’s hash. So fetching the contract code is a two-step process:
  1. First, we look up the contract itself, to see which code hash it is referencing.
  2. Then, we can look up the raw Wasm byte-code using that hash.
We then take our output from this function, and use it as the element in the keys array parameter in our call to the getLedgerEntries method.
And the response we get contains the LedgerEntryData that can be used to find the hash we must use to request the Wasm byte-code. This hash is the LedgerKey that’s been associated with the deployed contract code.
Now take the xdr field from the previous response’s result object, and create a LedgerKey from the hash contained inside.
Now, finally we have a LedgerKey that correspond to the Wasm byte-code that has been deployed under the ContractId we started out with so very long ago. This LedgerKey can be used in a final request to the Stellar-RPC endpoint.
And the response we get contains (even more) LedgerEntryData that we can decode and parse to get the actual, deployed, real-life contract byte-code. We’ll leave that exercise up to you. You can check out what is contained using the “View XDR” page of the Stellar Lab.