CoolFace
Apppublic

666lcz/knowledge_base

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
json-rpc.md195 linesDownload Raw Back to build
1---2title: JSON-RPC API Quick Start3---4 5Welcome to the guide for making remote procedure calls (RPC) to the Sui network. This document walks you through how to connect and interact with the Sui network using the Sui JSON-RPC API. Use the RPC layer to send your dApp transactions to [Sui validators](../learn/architecture/validators.md) for verification.6 7This guide is useful for developers interested in Sui network interactions via API and should be used in conjunction with the [SuiJSON format](sui-json.md) for aligning JSON inputs with Move Call arguments.8 9For a similar guide on Sui network interactions via CLI, refer to the [Sui Client CLI](cli-client.md) documentation.10 11Follow the instructions to [install Sui binaries](install.md#install-sui-binaries).12 13## Connect to a Sui network14 15You can connect to a Sui Full node on a Sui network. Follow the guidance in the [Connect to a Sui Network](../build/connect-sui-network.md) topic to start making RPC calls to the Sui network.16 17To configure your own Sui Full node, see [Configure a Sui Full node](fullnode.md).18 19## Sui SDKs20 21You can sign transactions and interact with the Sui network using any of the following:22 23- [Sui Rust SDK](rust-sdk.md), a collection of Rust language JSON-RPC wrapper and crypto utilities.24- [Sui TypeScript SDK](https://github.com/MystenLabs/sui/tree/main/sdk/typescript) and [reference files](https://www.npmjs.com/package/@mysten/sui.js).25- [Sui API Reference](https://docs.sui.io/sui-jsonrpc) for all available methods.26 27## Sui JSON-RPC examples28 29The following sections demonstrate how to use the Sui JSON-RPC API with cURL commands. See the [Sui API Reference](https://docs.sui.io/sui-jsonrpc) for the latest list of all available methods.30 31### RPC discover32 33Sui RPC server supports OpenRPC’s [service discovery method](https://spec.open-rpc.org/#service-discovery-method).34A `rpc.discover` method is added to provide documentation describing our JSON-RPC APIs service.35 36```shell37curl --location --request POST $SUI_RPC_HOST \38--header 'Content-Type: application/json' \39--data-raw '{ "jsonrpc":"2.0", "method":"rpc.discover","id":1}'40```41 42### Transfer object43 44The examples in this section demonstrate how to create transfer transactions. To use the example commands, replace the values between double brackets ({{ example_ID }} with actual values.45 46Objects IDs for `{{coin_object_id}}` and `{{gas_object_id}}` must47be owned by the address specified for `{{owner_address}}` for the command to succeed. Use [`sui_getOwnedObjects`](#sui_getOwnedObjects) to return object IDs.48 49**Important:** As a security best practice, you should serialize data from the JSON-RPC service locally in the same location as the signer. This reduces the risk of trusting data from the service directly.50 51#### Create an unsigned transaction to transfer a Sui coin from one address to another52 53```shell54curl --location --request POST $SUI_RPC_HOST \55--header 'Content-Type: application/json' \56--data-raw '{57  "jsonrpc": "2.0",58  "id": 1,59  "method": "sui_transferObject",60  "params":[61    "{{owner_address}}",62    "{{object_id}}",63    "{{gas_object_id}}",64    {{gas_budget}},65    "{{to_address}}"],66}' | json_pp67```68 69A response resembles the following:70 71```json72{73  "id": 1,74  "jsonrpc": "2.0",75  "result": {76    "tx_bytes": "VHJhbnNhY3Rpb25EYXRhOjoAAFHe8jecgzoGWyGlZ1sJ2KBFN8aZF7NIkDsM+3X8mrVCa7adg9HnVqUBAAAAAAAAACDOlrjlT0A18D0DqJLTU28ChUfRFtgHprmuOGCHYdv8YVHe8jecgzoGWyGlZ1sJ2KBFN8aZdZnY6h3kyWFtB38Wyg6zjN7KzAcBAAAAAAAAACDxI+LSHrFUxU0G8bPMXhF+46hpchJ22IHlpPv4FgNvGOgDAAAAAAAA="77  }78}79```80 81#### Sign a transaction using the Sui keytool82 83```shell84sui keytool sign --address <owner_address> --data <tx_bytes>85```86 87The keytool creates a key and then returns the signature and public key information.88 89#### Execute a transaction with a serialized signature90 91```shell92curl --location --request POST $SUI_RPC_HOST \93--header 'Content-Type: application/json' \94--data-raw '{95  "jsonrpc": "2.0",96  "id": 1,97  "method": "sui_executeTransactionBlockSerializedSig",98  "params": [99    "{{tx_bytes}}",100    "{{signature}}",101    "{{request_type}}"102  ]103}' | json_pp104```105 106`signature` is a Base64 encoded `flag || signature || pubkey`.107 108Native transfer by `sui_transferObject` supports any object that allows for public transfers. Some objects cannot be transferred natively and require a [Move call](#sui_movecall). See [Transactions](../learn/transactions.md#native-transaction) for more information about native transfers.109 110### Invoke Move functions111 112The example command in this section demonstrate how to call Move functions.113 114#### Execute a Move call transaction115 116Execute a Move call transaction by calling the specified function in117the module of a given package (smart contracts in Sui are written in118the [Move](move/index.md) language):119 120```shell121curl --location --request POST $SUI_RPC_HOST \122--header 'Content-Type: application/json' \123--data-raw '{124  "jsonrpc": "2.0",125  "method": "sui_moveCall",126  "params": [127    "{{owner_address}}",128    "0x2",129    "coin",130    "transfer",131    ["0x2::sui::sui"],132    ["{{object_id}}", "{{recipient_address}}"],133    "{{gas_object_id}}",134     2000135  ],136  "id": 1137}' | json_pp138```139 140Arguments are passed in, and type is inferred from the function141signature. Gas usage is capped by the `gas_budget`. The `transfer`142function is described in more detail in the [Sui CLI client](cli-client.md#calling-move-code) documentation.143 144The `transfer` function in the `Coin` module serves the same145purpose as ([`sui_transferObject`](#sui_TransferObject)). It is used for illustration purposes, as a native transfer is more efficient.146 147To learn more about which `args` a Move call accepts, see [SuiJSON](sui-json.md).148 149### Publish a Move package150 151```shell152curl --location --request POST $SUI_RPC_HOST \153--header 'Content-Type: application/json' \154--data-raw '{155  "jsonrpc":"2.0",156  "method":"sui_publish",157  "params":[158    "{{owner_address}}",159    ["{{vector_of_compiled_modules}}"],160    ["{{vector_of_dependency_ids}}"],161    "{{gas_object_id}}",162     10000163   ],164  "id":1165}' | json_pp166```167 168This endpoint performs proper verification and linking to make169sure the package is valid. If some modules have [initializers](move/debug-publish.md#module-initializers), these initializers execute in Move (which means new Move objects can be created in the process of publishing a Move package). Gas budget is required because of the need to execute module initializers.170 171To publish a Move module, you also need to include `{{vector_of_compiled_modules}}` along with the `{{vector_of_dependency_ids}}`. To generate the values for these fields, use the `sui move` command. The `sui move` command supports printing the bytecode as base64 and dependency object IDs:172 173```174sui move <move-module-path> build --dump-bytecode-as-base64175```176 177Assuming that the location of the package's sources is in the `PATH_TO_PACKAGE` environment variable an example command resembles the following:178 179```180sui move $PATH_TO_PACKAGE/my_move_package build --dump-bytecode-as-base64181 182{183  "modules": "oRzrCwU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BwEIAAIJAAcIBQELBwEJAAEIBgEIAQEJAAIHCwIBCQALAgEJAAMDBwsHAQkABwgFAQYLBwEJAAZCQVNLRVQHTUFOQUdFRARDb2luAklEA1NVSQhUcmFuc2ZlcglUeENvbnRleHQHUmVzZXJ2ZQRidXJuBGluaXQObWFuYWdlZF9zdXBwbHkEbWludApzdWlfc3VwcGx5DHRvdGFsX3N1cHBseQtkdW1teV9maWVsZAJpZAtWZXJzaW9uZWRJRAx0cmVhc3VyeV9jYXALVHJlYXN1cnlDYXADc3VpB21hbmFnZWQFdmFsdWUId2l0aGRyYXcPY3JlYXRlX2N1cnJlbmN5Bm5ld19pZAR6ZXJvDHNoYXJlX29iamVjdARqb2luAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgMIAAAAAAAAAAAAAgEOAQECBA8IBhELBwEIABMLAgEIAxQLAgEIBAABAAAIFg4BOAAMBAsBCgAPADgBCgAPAQoECgI4AgwFCwAPAgsECwI4AwwDCwULAwIBAAAAEA8JEgAKADgEDAEKABEKCwEKADgFCwA4BhIBOAcCAgEAAAMECwAQAjgIAgMBAAAFHA4BOAkMBAoEDgI4CCEDDgsAAQsDAQcAJwoADwELATgKCgAPAgsCOAsLBAsADwALAzgMAgQBAAADBAsAEAE4CQIFAQAAAwQLABAAOA0CAQEBAgEDAA==",184  "dependencies": ["0x0000000000000000000000000000000000000000000000000000000000000001"],185}186Build Successful187```188 189Copy the output base64 representation of the compiled Move module along with the dependency IDs into the190REST publish endpoint.191 192The command generates a package object that represents the published Move code. You can use the package ID as an argument for subsequent Move calls to functions defined in this package.193 194**Note:** If your package has dependencies that are unpublished, include the `--with-unpublished-dependencies` flag to have the modules in those packages added to the bytecode.195 
666lcz/knowledge_base · CoolFace