stellar / js-stellar-sdk

Main Stellar client library for the JavaScript language.
https://stellar.github.io/js-stellar-sdk/
Apache License 2.0
628 stars 311 forks source link

Add a `SorobanRpc.Server.getWasm(contractId)` method #961

Closed Shaptic closed 4 months ago

Shaptic commented 4 months ago

Is your feature request related to a problem? Please describe. Building ledger entries by hand sucks and you need to understand too much about Soroban internals.

Describe the solution you'd like See title, add a method to the server:

class Server {
  // ...
  getWasm(contractId: string): Promise<Buffer>;
}

which will make the correct underlying getLedgerEntries call for you.

Describe alternatives you've considered Doing it manually:

const contractLedgerKey = new Contract(contractId).getFootprint();
const response = await server.getLedgerEntries(contractLedgerKey);
const wasmHash = response.entries[0].val.contractData().val().contractInstance().executable().wasmHash();
const ledgerKeyWasmHash = xdr.LedgerKey.contractCode(new xdr.LedgerKeyContractCode({ hash: wasmHash }));
const responseWasm = await server.getLedgerEntries(ledgerKeyWasmHash);
return responseWasm.entries[0].val.contractCode().code();

This is gross.

Additional context See stellar-sdk#960, a review comment from @willemneal.

This is documented in the API reference for getLedgerEntries, but it'd still be better to make it built in.

Shaptic commented 4 months ago

Closed by #960