#How to generate contract spec/metadata using js-soroban-client?

7 messages · Page 1 of 1 (latest)

willow silo
#

👋 I'm trying to get a contract spec using js-soroban-client but I'm not sure how.

First I tried following this guide: https://soroban.stellar.org/api/methods/getLedgerEntries#requesting-a-contracts-wasm-code

And I ended with this:

`server.getLedgerEntries(hash).then((response) => {
const entry = response.entries?.[0];

const wasmLedgerKey = getLedgerKeyWasmId(entry.xdr);

server.getLedgerEntries(wasmLedgerKey).then((response) => {
const entry = response.entries?.[0];

const entryData = xdr.LedgerEntryData.fromXDR(entry.xdr, "base64");

const contractCode = entryData.contractCode().code().toString();
console.log(contractCode);

});
});`

But I'm not sure if I'm in the correct path, or how to generate it from the contractCode. I see in the docs ContractSpec that I may need to use scValToNative but I'm not sure how would that work. I also know that I get the contractspecv0 but again I'm not sure how to read it properly.

I would like to have something similar to the CLI version of soroban contract inspect but without the need of having the wasm file. For example to get the list of available functions from the contract id.

Thanks for your help!

For reading the current value of ledger entries directly.

zealous bloom
#

I parse the byte code in php as it was easiesier with preg_match preg_match('/contractenvmetav0(.*?)contractspecv0/', $byteCode, $match) == 1 and preg_match('/contractspecv0(.*?)contractmetav0/', $byteCode, $match) == 1

#

and then load the match in ``` $metaEntry = XdrSCSpecEntry::decode(new XdrBuffer($specBytes));

willow silo
#

Thanks @zealous bloom ! I'll have a look. What would $match be in this case?

So, no way to do this in js?

worn cedar
#

@willow silo once you've gotten the contract wasm you can use the wasmparser library to extract the metadata. It'd probably be good to add this to the SDK itself

#

@webassemblyjs/wasm-parser