Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Add javascript example to get the WASM content (#473)
Browse files Browse the repository at this point in the history
* Add javascript example to get the WASM content

* Update to preview 10
  • Loading branch information
vinamogit authored Jul 24, 2023
1 parent f09cf67 commit 5997a4a
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions api/methods/getLedgerEntries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ When you deploy a contract, first the code is "installed" (i.e. it is uploaded o

#### Request the `LedgerKey` for the Contract Code

##### Python

```python
from stellar_sdk import xdr

Expand All @@ -230,6 +232,31 @@ print(
# OUTPUT: AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFA==
```

##### JavaScript

```javascript
const { xdr } = require("soroban-client")
function getLedgerKeyContractCode(contractId) {
let ledgerKey = xdr.LedgerKey.contractData(
new xdr.LedgerKeyContractData({
contract: xdr.ScAddress.scAddressTypeContract(contractId),
key: new xdr.ScVal.scvLedgerKeyContractInstance(),
durability: xdr.ContractDataDurability.persistent(),
bodyType: xdr.ContractEntryBodyType.dataEntry(),
})
);

return ledgerKey.toXDR('base64');
}

console.log(
getLedgerKeyContractCode(
"af9a2527e3b3b5571d63b0246ba32b7d31a5323766df7c60dfc0b3e3ba6fdf23"
)
)
// OUTPUT: AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFA==
```

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.

```json
Expand Down Expand Up @@ -268,6 +295,8 @@ And the response we get contains the `LedgerEntryData` that can be used to find

Now take the `xdr` field from the previous response's `result` object, and create a `LedgerKey` from the hash contained inside.

##### Python

```python
from stellar_sdk import xdr

Expand All @@ -293,8 +322,50 @@ print(
# OUTPUT: AAAAB2QWKBCU9yGjzDJNxaEZpxEB6A8XsD2S/lKK/sViOLiC
```

##### JavaScript

```javascript
const { xdr } = require("soroban-client")

function getLedgerKeyWasmId(contractCodeLedgerEntryData) {

let entry = xdr.LedgerEntryData.fromXDR(
contractCodeLedgerEntryData,
"base64"
);

let instance = new xdr.ScContractInstance({
executable: entry.contractData()
.body()
.value()
.val()
});

let ledgerKey = xdr.LedgerKey.contractCode(
new xdr.LedgerKeyContractCode({
hash: xdr.ContractExecutable.contractExecutableWasm(instance.executable())
.wasmHash()
.instance()
.executable()
.wasmHash(),
bodyType: xdr.ContractEntryBodyType.dataEntry()
})
);

return ledgerKey.toXDR('base64');
}

console.log(
getLedgerKeyWasmId(
"AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFAAAABIAAAAAZBYoEJT3IaPMMk3FoRmnEQHoDxewPZL+Uor+xWI4uII="
)
)
// OUTPUT: AAAAB2QWKBCU9yGjzDJNxaEZpxEB6A8XsD2S/lKK/sViOLiC
```

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 Soroban-RPC endpoint.


```json
{
"jsonrpc": "2.0",
Expand Down

0 comments on commit 5997a4a

Please sign in to comment.