#Getting contract entries with js-soroban-client

1 messages · Page 1 of 1 (latest)

ocean pumice
#

I'm trying to get contract entries by keys from ledger and got stuck at the very first step.
At the docs https://github.com/stellar/js-soroban-client/blob/main/docs/reference/readme.md#xdr I see:

new SorobanClient.xdr.LedgerKeyAccount({
    accountId: 'GASOCNHNNLYFNMDJYQ3XFMI7BYHIOCFW3GJEOWRPEGK2TDPGTG2E5EDW'
  })

It doesn't work, and I can get accountId only from keyPair.xdrAccountId(). keyPair may be got from user address with SorobanClient.Keypair.fromPublicKey('...'). But it doesn't work with smart contract public key.

And then I should probably use something different from xdr.LedgerKeyAccount.

GitHub

Main Soroban client library for the Javascript language - stellar/js-soroban-client

wooden relic
ocean pumice
wooden relic
#

404 usually means the resource does not exist, either the account or the contract (may be expired?)

ocean pumice
#

Yes, the contract actually expired today, but we redeployed it. I can see our token balance in Frighter.

Yes, I tried server.getAccount recently, but it's about accounts, not smart contracts as I see.

I'm trying to get a smart contract entity value by a key, but none of them work.

I also want to get our custom token balance but I don't get how to at the moment.

wooden relic
#

if you have the contract and its wasm, you could generate the binding code. This code includes (de)serialization functions for the contract types.

#

you could use that to query the ledgerEntries

ocean pumice
#

Yep, we've generated contract bindings and I can see keys there but I have no clue where queries are.

ocean pumice
# ocean pumice Thank you, I tried but it returns 404 anyway. I also tried getLedgerEntries but ...

I made it with getLedgerEntries:

const ledgerKey = SorobanClient.xdr.LedgerKey.contractData(
    new SorobanClient.xdr.LedgerKeyContractData({
      bodyType: SorobanClient.xdr.ContractEntryBodyType.dataEntry(),
      contract: new SorobanClient.Contract(CONTRACT_ADDRESS).address().toScAddress(),
      durability: SorobanClient.xdr.ContractDataDurability.persistent(),
      key: SorobanClient.xdr.ScVal.scvSymbol('counter'),
    }),
  )
  server.getLedgerEntries([ledgerKey]).then((response) => {
    const ledgerData = response.entries?.[0]
    if (ledgerData) {
      console.log('key:', ledgerData.key)
      console.log('value:', ledgerData.xdr)
      console.log('lastModified:', ledgerData.lastModifiedLedgerSeq)
    }
    console.log('latestLedger:', response.latestLedger)
  })
#

It returns only response.latestLedger.

ocean pumice
#

Finally I got some xdr with a correct RPC server and persistent values, because key doesn't return instance values.

Now I'm trying to parse getLedgerEntries returned xdr with no luck.