Deep Dive into Soroban-CLI : soroban contract
Tags: soroban, sorobanathon, sorobanathonequinox, stellar
Published: 2023-03-16 19:44:08

Hi there! This is the first post of the "Deep Dive into Soroban-CLI" series where we'll explore the soroban-cli in depth. Soroban-CLI is a command-line tool for interacting with soroban smart contracts on the Stellar network. Soroban-CLI provides a set of subcommands that can be used to perform various tasks related to smart contract development and deployment on the Stellar network.

In this series, we'll dive into each of the soroban-cli subcommands and subcommands of subcommands. I'll explain the function of each subcommands with usage example.

## `soroban` Main Subcommands
Here's the main subcommands of `soroban` command :
- `contract`     
- `config`        
- `serve`         
- `events`        
- `lab`          
- `version`      
- `completion`

##`soroban contract` Subcommands
In this first series i will only explain `contract` subcommand since it have many subcommands. The 'contract' subcommand provides tools for smart contract developers to interact with the smart contracts itself such as deploy and invoke your smart contracts on the Stellar network. Here's the `soroban contract` subcommands:
- **`bindings`**

The `bindings` subcommand is used to generate client code bindings for a contract in rust or json.
Usage :

```
soroban contract bindings --wasm <WASM FILE> --output <JSON/RUST>
```
Usage Example :
```
$ soroban contract bindings --wasm soroban_hello_world_contract.wasm --output rust

pub const WASM: &[u8] = soroban_sdk::contractfile!(
    file =
    "soroban_hello_world_contract.wasm",
    sha256 = "514348aa3014d6ba14d11a79c852217937e6b3339fdf9673eb4041ec74495dd6"
);
#[soroban_sdk::contractclient(name = "Client")]
pub trait Contract {
    fn hello(
        env: soroban_sdk::Env,
        to: soroban_sdk::Symbol,
    ) -> soroban_sdk::Vec<soroban_sdk::Symbol>;
}
```
- `deploy`

The `deploy` subcommand is used to deploy a smart contract to the network and start using it on the network.After the contract deployed you will get the contract id.
Usage:
```
soroban contract deploy --secret-key <SECRET_KEY> --rpc-url <RPC_URL> --network-passphrase <NETWORK_PASSPHRASE> <--wasm <WASM>|--wasm-hash <WASM_HASH>>
```
Usage Example :

```
$ soroban contract deploy --wasm soroban_hello_world_contract.wasm
success
success
a82d0ff70786d93a2a67abb7d2170ce04b3fa88592e733c14b520574fea1ead9
```
- `inspect`

The `inspect` subcommand is used to inspect a WebAssembly (WASM) file and display information about the contract functions, metadata, and other details. With this subcommand, you can view the details of your compiled smart contract and ensure that it is correctly configured.
Usage:

```
soroban contract inspect --wasm <WASM FILE>
```
Usage Example : 

```
$ soroban contract inspect --wasm /soroban_hello_world_contract.wasm
File: soroban_hello_world_contract.wasm
Env Meta: AAAAAAAAAAAAAAAd
 • Interface Version: 29
Contract Spec: AAAAAAAAAAAAAAAFaGVsbG8AAAAAAAABAAAAAAAAAAJ0bwAAAAAACAAAAAEAAAPqAAAACA==
 • Function: hello ([ScSpecFunctionInputV0 { doc: StringM(), name: StringM(to), type_: Symbol }]) -> ([Vec(ScSpecTypeVec { element_type: Symbol })])
```
- `install`

The `install` subcommand is used to install a WASM file on the Stellar network without creating a contract instance, which can be useful for testing purposes.
Usage :

```
soroban contract install --wasm <WASM> --secret-key <SECRET_KEY> --rpc-url <RPC_URL> --network-passphrase <NETWORK_PASSPHRASE>
```
Usage Example:

```
$ soroban contract install --wasm soroban_hello_world_contract.wasm
success
7df76fac7450a53ad3af7cd55b1758406e92a1dffe87c3e384ec078f0a8ca44b
```
- `invoke`

The `invoke` subcommand is used to invoke a function in a smart contract. With this subcommand, you can call a function in your deployed smart contract and pass arguments to it.
Usage :

```
soroban contract invoke --id <CONTRACT_ID> --fn <FUNCTION> --secret-key <SECRET_KEY> --rpc-url <RPC_URL> --network-passphrase <NETWORK_PASSPHRASE>
```
Usage Example :

```
$ soroban contract invoke --id a82d0ff70786d93a2a67abb7d2170ce04b3fa88592e733c14b520574fea1ead9 --fn hello -- --to World
success
["Hello","World"]
```
- `optimize`

The `optimize` subcommand is used to optimize a WASM file. With this subcommand, you can optimize your compiled smart contract to reduce its size and improve its performance on the network.
Usage :

```
soroban contract optimize [OPTIONS] --wasm <WASM>

OPTIONS:
        --wasm <WASM>            Path to wasm binary
        --wasm-out <WASM_OUT>    Path to write the optimized WASM file to (defaults to same location
                                 as --wasm with .optimized.wasm suffix)
```
Usage Example :

```
$ soroban contract optimize --wasm soroban_hello_world_contract.wasm
Reading: soroban_hello_world_contract.wasm (389 bytes)
Writing to: soroban_hello_world_contract.optimized.wasm...
Optimized: soroban_hello_world_contract.optimized.wasm (362 bytes)
```
- `read`

The 'read' subcommand is used to print the current value of a contract-data ledger entry.
Usage :

```
soroban contract read [OPTIONS] --id <CONTRACT_ID>

OPTIONS:
        --id <CONTRACT_ID>     Contract ID to invoke
        --key <KEY>            Storage key (symbols only)
        --key-xdr <KEY_XDR>    Storage key (base64-encoded XDR)
        --output <OUTPUT>      Type of output to generate [default: string] [possible values: string, json, xdr]
```
Usage Example :

```
$ soroban contract read --id 1 --key STATE
STATE,"{""count"":12,""last_incr"":7}"
```
##Conclusion
The `soroban contract` subcommands provides a powerful set of tools for smart contract developers to invoke, deploy, and test their smart contracts on the Stellar network. By using these subcommands, you can develop and deploy smart contracts on the Stellar network with ease. I will explain the other main subcommands in the next post of this series. Happy Sorobaning!