stellar / stellar-rpc

RPC server for Soroban contracts.
19 stars 21 forks source link

JSON Support #124

Closed janewang closed 3 months ago

janewang commented 8 months ago

Developers have the option to request data in JSON format. By default, responses are in the current XDR+JSON format unless specified otherwise in the request. If JSON format is requested, the system will dynamically convert the XDR data to JSON for easier readability and use by developers.

In order to:

The initial implementation is then backward compatible.

Example Usage for JSON response:

curl -X POST \
-H 'Content-Type: application/json' \
-H 'Accept': application/json \
-d '{
 "jsonrpc": "2.0",
 "id": 8675309,
 "method": "simulateTransaction",
 "params": {
   "xdrFormat": "json",
   "transaction": <in JSON>,
   "resourceConfig": {
     "instructionLeeway": 3000000
   }
 }
}' \
https://soroban-testnet.stellar.org:443 | jq

We have developed a canonical XDR ⇔ JSON schema for conversion between XDR and JSON: Stellar JSON

See https://docs.google.com/document/d/1tTuzL7HBHedp9TE-LonD1QV67SaBSReQ8YFj9xQIgtA/edit#heading=h.uedyb8nrk3a9

mollykarcher commented 6 months ago

@janewang FYI unpackxdr being at the top-level breaks the json-rpc spec. I updated the description here to move it to within params. Cool with you?

Shaptic commented 5 months ago

@janewang @mollykarcher suggestion: asking the user to pass a parameter "format": "xdr" | "json" rather than unpackxdr

janewang commented 5 months ago

@Shaptic I think that's where we started with. However, the format of the rpc response is technically already in JSON, with XDR strings.

Shaptic commented 5 months ago

xdr_format: "json" | "base64" then? The idea is to not lock us into just JSON for unpacking XDR in the future :man_shrugging:

janewang commented 5 months ago

Fine with preserving the option to unpack XDR to another format. However, I think after we unpack to JSON, convert to other formats should not be the responsibility at this layer.

I think xdrFormat: "json" | "base64" is reasonable.