diff --git a/Cargo.lock b/Cargo.lock index 4d70268b9..edab8bb9a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4732,6 +4732,7 @@ dependencies = [ "tracing-subscriber", "ulid", "ureq", + "walkdir", "wasm-opt", "wasmparser 0.90.0", "which", diff --git a/FULL_HELP_DOCS.md b/FULL_HELP_DOCS.md index 83fd92726..877618d97 100644 --- a/FULL_HELP_DOCS.md +++ b/FULL_HELP_DOCS.md @@ -4,13 +4,15 @@ This document contains the help content for the `stellar` command-line program. ## `stellar` -With the Stellar CLI you can: +Work seamlessly with Stellar accounts, contracts, and assets from the command line. -- build, deploy and interact with contracts -- set identities to sign with -- configure networks -- generate keys -- more! +- Generate and manage keys and accounts +- Build, deploy, and interact with contracts +- Deploy asset contracts +- Stream events +- Start local testnets +- Decode, encode XDR +- More! For additional information see: @@ -18,21 +20,21 @@ For additional information see: - Smart Contract Docs: https://developers.stellar.org/docs/build/smart-contracts/overview - CLI Docs: https://developers.stellar.org/docs/tools/stellar-cli -The easiest way to get started is to generate a new identity: +To get started generate a new identity: stellar keys generate alice -You can use identities with the `--source` flag in other commands later. +Use keys with the `--source` flag in other commands. -Commands that relate to smart contract interactions are organized under the `contract` subcommand. List them: +Commands that work with contracts are organized under the `contract` subcommand. List them: stellar contract --help -A Soroban contract has its interface schema types embedded in the binary that gets deployed on-chain, making it possible to dynamically generate a custom CLI for each. The invoke subcommand makes use of this: +Use contracts like a CLI: stellar contract invoke --id CCR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OTE2 --source alice --network testnet -- --help -Anything after the `--` double dash (the "slop") is parsed as arguments to the contract-specific CLI, generated on-the-fly from the embedded schema. For the hello world example, with a function called `hello` that takes one string argument `to`, here's how you invoke it: +Anything after the `--` double dash (the "slop") is parsed as arguments to the contract-specific CLI, generated on-the-fly from the contract schema. For the hello world example, with a function called `hello` that takes one string argument `to`, here's how you invoke it: stellar contract invoke --id CCR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OTE2 --source alice --network testnet -- hello --to world @@ -136,15 +138,15 @@ For more information on the functions available to the `stellar asset` contract, ###### **Subcommands:** -* `completion` — Print shell completion code for the specified shell * `contract` — Tools for smart contract developers * `events` — Watch the network for contract events * `keys` — Create and manage identities including keys and addresses -* `xdr` — Decode and encode XDR * `network` — Start and configure networks -* `version` — Print version information * `tx` — Sign, Simulate, and Send transactions +* `xdr` — Decode and encode XDR +* `completion` — Print shell completion code for the specified shell * `cache` — Cache for transactions and contract specs +* `version` — Print version information ###### **Options:** @@ -159,28 +161,6 @@ For more information on the functions available to the `stellar asset` contract, -## `stellar completion` - -Print shell completion code for the specified shell - -Ensure the completion package for your shell is installed, e.g. bash-completion for bash. - -To enable autocomplete in the current bash shell, run: `source <(stellar completion --shell bash)` - -To enable autocomplete permanently, run: `echo "source <(stellar completion --shell bash)" >> ~/.bashrc` - - -**Usage:** `stellar completion --shell ` - -###### **Options:** - -* `--shell ` — The shell type - - Possible values: `bash`, `elvish`, `fish`, `powershell`, `zsh` - - - - ## `stellar contract` Tools for smart contract developers @@ -361,7 +341,7 @@ Extend the time to live ledger of a contract-data ledger entry. If no keys are specified the contract itself is extended. -**Usage:** `stellar contract extend [OPTIONS] --ledgers-to-extend --durability --source-account ` +**Usage:** `stellar contract extend [OPTIONS] --ledgers-to-extend --source-account ` ###### **Options:** @@ -519,6 +499,7 @@ Initialize a Soroban project with an example contract * `-f`, `--frontend-template ` — An optional flag to pass in a url for a frontend template repository. Default value: `` +* `--overwrite` — Overwrite all existing files. @@ -629,7 +610,7 @@ Optimize a WASM file Print the current value of a contract-data ledger entry -**Usage:** `stellar contract read [OPTIONS] --durability --source-account ` +**Usage:** `stellar contract read [OPTIONS] --source-account ` ###### **Options:** @@ -676,7 +657,7 @@ Restore an evicted value for a contract-data legder entry. If no keys are specificed the contract itself is restored. -**Usage:** `stellar contract restore [OPTIONS] --durability --source-account ` +**Usage:** `stellar contract restore [OPTIONS] --source-account ` ###### **Options:** @@ -722,9 +703,7 @@ Watch the network for contract events ###### **Options:** -* `--start-ledger ` — The first [ledger sequence] number in the range to pull events - - [ledger sequence]: https://developers.stellar.org/docs/encyclopedia/ledger-headers#ledger-sequence +* `--start-ledger ` — The first ledger sequence number in the range to pull events https://developers.stellar.org/docs/learn/encyclopedia/network-configuration/ledger-headers#ledger-sequence * `--cursor ` — The cursor corresponding to the start of the event range * `--output ` — Output formatting options for event stream @@ -915,412 +894,432 @@ Given an identity return its private key -## `stellar xdr` +## `stellar network` -Decode and encode XDR +Start and configure networks -**Usage:** `stellar xdr [CHANNEL] ` +**Usage:** `stellar network ` ###### **Subcommands:** -* `types` — View information about types -* `guess` — Guess the XDR type -* `decode` — Decode XDR -* `encode` — Encode XDR -* `version` — Print version information +* `add` — Add a new network +* `rm` — Remove a network +* `ls` — List networks +* `start` — ⚠️ Deprecated: use `stellar container start` instead +* `stop` — ⚠️ Deprecated: use `stellar container stop` instead +* `container` — Commands to start, stop and get logs for a quickstart container -###### **Arguments:** -* `` — Channel of XDR to operate on - Default value: `+curr` +## `stellar network add` - Possible values: `+curr`, `+next` +Add a new network +**Usage:** `stellar network add [OPTIONS] --rpc-url --network-passphrase ` +###### **Arguments:** +* `` — Name of network -## `stellar xdr types` +###### **Options:** -View information about types +* `--rpc-url ` — RPC server endpoint +* `--network-passphrase ` — Network passphrase to sign the transaction sent to the rpc server +* `--global` — Use global config +* `--config-dir ` — Location of config directory, default is "." -**Usage:** `stellar xdr types ` -###### **Subcommands:** -* `list` — -* `schema` — +## `stellar network rm` +Remove a network +**Usage:** `stellar network rm [OPTIONS] ` -## `stellar xdr types list` +###### **Arguments:** -**Usage:** `stellar xdr types list [OPTIONS]` +* `` — Network to remove ###### **Options:** -* `--output ` - - Default value: `plain` - - Possible values: `plain`, `json`, `json-formatted` +* `--global` — Use global config +* `--config-dir ` — Location of config directory, default is "." +## `stellar network ls` -## `stellar xdr types schema` +List networks -**Usage:** `stellar xdr types schema [OPTIONS] --type ` +**Usage:** `stellar network ls [OPTIONS]` ###### **Options:** -* `--type ` — XDR type to decode -* `--output ` - - Default value: `json-schema-draft201909` +* `--global` — Use global config +* `--config-dir ` — Location of config directory, default is "." +* `-l`, `--long` — Get more info about the networks - Possible values: `json-schema-draft7`, `json-schema-draft201909` +## `stellar network start` +⚠️ Deprecated: use `stellar container start` instead -## `stellar xdr guess` +Start network -Guess the XDR type +Start a container running a Stellar node, RPC, API, and friendbot (faucet). -**Usage:** `stellar xdr guess [OPTIONS] [FILE]` +`stellar network start NETWORK [OPTIONS]` -###### **Arguments:** +By default, when starting a testnet container, without any optional arguments, it will run the equivalent of the following docker command: -* `` — File to decode, or stdin if omitted +`docker run --rm -p 8000:8000 --name stellar stellar/quickstart:testing --testnet --enable rpc,horizon` -###### **Options:** +**Usage:** `stellar network start [OPTIONS] ` -* `--input ` +###### **Arguments:** - Default value: `single-base64` +* `` — Network to start - Possible values: `single`, `single-base64`, `stream`, `stream-base64`, `stream-framed` + Possible values: `local`, `testnet`, `futurenet`, `pubnet` -* `--output ` - Default value: `list` +###### **Options:** - Possible values: `list` +* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock +* `--name ` — Optional argument to specify the container name +* `-l`, `--limits ` — Optional argument to specify the limits for the local network only +* `-p`, `--ports-mapping ` — Argument to specify the `HOST_PORT:CONTAINER_PORT` mapping -* `--certainty ` — Certainty as an arbitrary value + Default value: `8000:8000` +* `-t`, `--image-tag-override ` — Optional argument to override the default docker image tag for the given network +* `-v`, `--protocol-version ` — Optional argument to specify the protocol version for the local network only - Default value: `2` +## `stellar network stop` -## `stellar xdr decode` +⚠️ Deprecated: use `stellar container stop` instead -Decode XDR +Stop a network started with `network start`. For example, if you ran `stellar network start local`, you can use `stellar network stop local` to stop it. -**Usage:** `stellar xdr decode [OPTIONS] --type [FILES]...` +**Usage:** `stellar network stop [OPTIONS] ` ###### **Arguments:** -* `` — Files to decode, or stdin if omitted +* `` — Container to stop ###### **Options:** -* `--type ` — XDR type to decode -* `--input ` +* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock - Default value: `stream-base64` - Possible values: `single`, `single-base64`, `stream`, `stream-base64`, `stream-framed` -* `--output ` +## `stellar network container` - Default value: `json` +Commands to start, stop and get logs for a quickstart container - Possible values: `json`, `json-formatted`, `rust-debug`, `rust-debug-formatted` +**Usage:** `stellar network container ` +###### **Subcommands:** +* `logs` — Get logs from a running network container +* `start` — Start a container running a Stellar node, RPC, API, and friendbot (faucet) +* `stop` — Stop a network container started with `network container start` -## `stellar xdr encode` -Encode XDR +## `stellar network container logs` -**Usage:** `stellar xdr encode [OPTIONS] --type [FILES]...` +Get logs from a running network container + +**Usage:** `stellar network container logs [OPTIONS] ` ###### **Arguments:** -* `` — Files to encode, or stdin if omitted +* `` — Container to get logs from ###### **Options:** -* `--type ` — XDR type to encode -* `--input ` +* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock - Default value: `json` - Possible values: `json` -* `--output ` +## `stellar network container start` - Default value: `single-base64` +Start a container running a Stellar node, RPC, API, and friendbot (faucet). - Possible values: `single`, `single-base64` +`stellar network container start NETWORK [OPTIONS]` +By default, when starting a testnet container, without any optional arguments, it will run the equivalent of the following docker command: +`docker run --rm -p 8000:8000 --name stellar stellar/quickstart:testing --testnet --enable rpc,horizon` +**Usage:** `stellar network container start [OPTIONS] ` -## `stellar xdr version` +###### **Arguments:** -Print version information +* `` — Network to start -**Usage:** `stellar xdr version` + Possible values: `local`, `testnet`, `futurenet`, `pubnet` +###### **Options:** -## `stellar network` +* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock +* `--name ` — Optional argument to specify the container name +* `-l`, `--limits ` — Optional argument to specify the limits for the local network only +* `-p`, `--ports-mapping ` — Argument to specify the `HOST_PORT:CONTAINER_PORT` mapping -Start and configure networks + Default value: `8000:8000` +* `-t`, `--image-tag-override ` — Optional argument to override the default docker image tag for the given network +* `-v`, `--protocol-version ` — Optional argument to specify the protocol version for the local network only -**Usage:** `stellar network ` -###### **Subcommands:** -* `add` — Add a new network -* `rm` — Remove a network -* `ls` — List networks -* `start` — ⚠️ Deprecated: use `stellar container start` instead -* `stop` — ⚠️ Deprecated: use `stellar container stop` instead -* `container` — Commands to start, stop and get logs for a quickstart container +## `stellar network container stop` +Stop a network container started with `network container start` +**Usage:** `stellar network container stop [OPTIONS] ` -## `stellar network add` +###### **Arguments:** -Add a new network +* `` — Container to stop -**Usage:** `stellar network add [OPTIONS] --rpc-url --network-passphrase ` +###### **Options:** -###### **Arguments:** +* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock -* `` — Name of network -###### **Options:** -* `--rpc-url ` — RPC server endpoint -* `--network-passphrase ` — Network passphrase to sign the transaction sent to the rpc server -* `--global` — Use global config -* `--config-dir ` — Location of config directory, default is "." +## `stellar tx` +Sign, Simulate, and Send transactions +**Usage:** `stellar tx ` -## `stellar network rm` +###### **Subcommands:** -Remove a network +* `simulate` — Simulate a transaction envelope from stdin +* `hash` — Calculate the hash of a transaction envelope from stdin -**Usage:** `stellar network rm [OPTIONS] ` -###### **Arguments:** -* `` — Network to remove +## `stellar tx simulate` + +Simulate a transaction envelope from stdin + +**Usage:** `stellar tx simulate [OPTIONS] --source-account ` ###### **Options:** +* `--rpc-url ` — RPC server endpoint +* `--network-passphrase ` — Network passphrase to sign the transaction sent to the rpc server +* `--network ` — Name of network to use from config +* `--source-account ` — Account that signs the final transaction. Alias `source`. Can be an identity (--source alice), a secret key (--source SC36…), or a seed phrase (--source "kite urban…") +* `--hd-path ` — If using a seed phrase, which hierarchical deterministic path to use, e.g. `m/44'/148'/{hd_path}`. Example: `--hd-path 1`. Default: `0` * `--global` — Use global config * `--config-dir ` — Location of config directory, default is "." -## `stellar network ls` +## `stellar tx hash` -List networks +Calculate the hash of a transaction envelope from stdin -**Usage:** `stellar network ls [OPTIONS]` +**Usage:** `stellar tx hash [OPTIONS]` ###### **Options:** -* `--global` — Use global config -* `--config-dir ` — Location of config directory, default is "." -* `-l`, `--long` — Get more info about the networks +* `--rpc-url ` — RPC server endpoint +* `--network-passphrase ` — Network passphrase to sign the transaction sent to the rpc server +* `--network ` — Name of network to use from config -## `stellar network start` +## `stellar xdr` -⚠️ Deprecated: use `stellar container start` instead +Decode and encode XDR -Start network +**Usage:** `stellar xdr [CHANNEL] ` -Start a container running a Stellar node, RPC, API, and friendbot (faucet). +###### **Subcommands:** -`stellar network start NETWORK [OPTIONS]` +* `types` — View information about types +* `guess` — Guess the XDR type +* `decode` — Decode XDR +* `encode` — Encode XDR +* `version` — Print version information -By default, when starting a testnet container, without any optional arguments, it will run the equivalent of the following docker command: +###### **Arguments:** -`docker run --rm -p 8000:8000 --name stellar stellar/quickstart:testing --testnet --enable-soroban-rpc` +* `` — Channel of XDR to operate on -**Usage:** `stellar network start [OPTIONS] ` + Default value: `+curr` -###### **Arguments:** + Possible values: `+curr`, `+next` -* `` — Network to start - Possible values: `local`, `testnet`, `futurenet`, `pubnet` -###### **Options:** +## `stellar xdr types` -* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock -* `-l`, `--limits ` — Optional argument to specify the limits for the local network only -* `-p`, `--ports-mapping ` — Argument to specify the `HOST_PORT:CONTAINER_PORT` mapping +View information about types - Default value: `8000:8000` -* `-t`, `--image-tag-override ` — Optional argument to override the default docker image tag for the given network -* `-v`, `--protocol-version ` — Optional argument to specify the protocol version for the local network only +**Usage:** `stellar xdr types ` +###### **Subcommands:** +* `list` — +* `schema` — -## `stellar network stop` -⚠️ Deprecated: use `stellar container stop` instead -Stop a network started with `network start`. For example, if you ran `stellar network start local`, you can use `stellar network stop local` to stop it. +## `stellar xdr types list` -**Usage:** `stellar network stop [OPTIONS] ` +**Usage:** `stellar xdr types list [OPTIONS]` -###### **Arguments:** +###### **Options:** -* `` — Network to stop +* `--output ` - Possible values: `local`, `testnet`, `futurenet`, `pubnet` + Default value: `plain` + Possible values: `plain`, `json`, `json-formatted` -###### **Options:** -* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock +## `stellar xdr types schema` -## `stellar network container` +**Usage:** `stellar xdr types schema [OPTIONS] --type ` -Commands to start, stop and get logs for a quickstart container +###### **Options:** -**Usage:** `stellar network container ` +* `--type ` — XDR type to decode +* `--output ` -###### **Subcommands:** + Default value: `json-schema-draft201909` -* `logs` — Tail logs of a running network container -* `start` — Start network -* `stop` — Stop a network started with `network container start`. For example, if you ran `network container start local`, you can use `network container stop local` to stop it + Possible values: `json-schema-draft7`, `json-schema-draft201909` -## `stellar network container logs` -Tail logs of a running network container +## `stellar xdr guess` + +Guess the XDR type -**Usage:** `stellar network container logs [OPTIONS] ` +**Usage:** `stellar xdr guess [OPTIONS] [FILE]` ###### **Arguments:** -* `` — Network to tail +* `` — File to decode, or stdin if omitted - Possible values: `local`, `testnet`, `futurenet`, `pubnet` +###### **Options:** +* `--input ` -###### **Options:** + Default value: `single-base64` -* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock + Possible values: `single`, `single-base64`, `stream`, `stream-base64`, `stream-framed` +* `--output ` + Default value: `list` -## `stellar network container start` + Possible values: `list` -Start network +* `--certainty ` — Certainty as an arbitrary value -Start a container running a Stellar node, RPC, API, and friendbot (faucet). + Default value: `2` -`stellar network start NETWORK [OPTIONS]` -By default, when starting a testnet container, without any optional arguments, it will run the equivalent of the following docker command: -`docker run --rm -p 8000:8000 --name stellar stellar/quickstart:testing --testnet --enable-soroban-rpc` +## `stellar xdr decode` -**Usage:** `stellar network container start [OPTIONS] ` +Decode XDR + +**Usage:** `stellar xdr decode [OPTIONS] --type [FILES]...` ###### **Arguments:** -* `` — Network to start +* `` — Files to decode, or stdin if omitted - Possible values: `local`, `testnet`, `futurenet`, `pubnet` +###### **Options:** +* `--type ` — XDR type to decode +* `--input ` -###### **Options:** + Default value: `stream-base64` -* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock -* `-l`, `--limits ` — Optional argument to specify the limits for the local network only -* `-p`, `--ports-mapping ` — Argument to specify the `HOST_PORT:CONTAINER_PORT` mapping + Possible values: `single`, `single-base64`, `stream`, `stream-base64`, `stream-framed` - Default value: `8000:8000` -* `-t`, `--image-tag-override ` — Optional argument to override the default docker image tag for the given network -* `-v`, `--protocol-version ` — Optional argument to specify the protocol version for the local network only +* `--output ` + Default value: `json` + Possible values: `json`, `json-formatted`, `rust-debug`, `rust-debug-formatted` -## `stellar network container stop` -Stop a network started with `network container start`. For example, if you ran `network container start local`, you can use `network container stop local` to stop it -**Usage:** `stellar network container stop [OPTIONS] ` -###### **Arguments:** +## `stellar xdr encode` + +Encode XDR -* `` — Network to stop +**Usage:** `stellar xdr encode [OPTIONS] --type [FILES]...` - Possible values: `local`, `testnet`, `futurenet`, `pubnet` +###### **Arguments:** +* `` — Files to encode, or stdin if omitted ###### **Options:** -* `-d`, `--docker-host ` — Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock +* `--type ` — XDR type to encode +* `--input ` + Default value: `json` + Possible values: `json` -## `stellar version` +* `--output ` -Print version information + Default value: `single-base64` -**Usage:** `stellar version` + Possible values: `single`, `single-base64` -## `stellar tx` -Sign, Simulate, and Send transactions +## `stellar xdr version` -**Usage:** `stellar tx ` +Print version information -###### **Subcommands:** +**Usage:** `stellar xdr version` -* `simulate` — Simulate a transaction envelope from stdin +## `stellar completion` -## `stellar tx simulate` +Print shell completion code for the specified shell -Simulate a transaction envelope from stdin +Ensure the completion package for your shell is installed, e.g. bash-completion for bash. -**Usage:** `stellar tx simulate [OPTIONS] --source-account ` +To enable autocomplete in the current bash shell, run: `source <(stellar completion --shell bash)` + +To enable autocomplete permanently, run: `echo "source <(stellar completion --shell bash)" >> ~/.bashrc` + + +**Usage:** `stellar completion --shell ` ###### **Options:** -* `--rpc-url ` — RPC server endpoint -* `--network-passphrase ` — Network passphrase to sign the transaction sent to the rpc server -* `--network ` — Name of network to use from config -* `--source-account ` — Account that signs the final transaction. Alias `source`. Can be an identity (--source alice), a secret key (--source SC36…), or a seed phrase (--source "kite urban…") -* `--hd-path ` — If using a seed phrase, which hierarchical deterministic path to use, e.g. `m/44'/148'/{hd_path}`. Example: `--hd-path 1`. Default: `0` -* `--global` — Use global config -* `--config-dir ` — Location of config directory, default is "." +* `--shell ` — The shell type + + Possible values: `bash`, `elvish`, `fish`, `powershell`, `zsh` + @@ -1393,3 +1392,11 @@ Read cached action +## `stellar version` + +Print version information + +**Usage:** `stellar version` + + + diff --git a/cmd/crates/soroban-test/src/lib.rs b/cmd/crates/soroban-test/src/lib.rs index 8df6a97c0..544e2d59e 100644 --- a/cmd/crates/soroban-test/src/lib.rs +++ b/cmd/crates/soroban-test/src/lib.rs @@ -30,7 +30,8 @@ use assert_fs::{fixture::FixtureError, prelude::PathChild, TempDir}; use fs_extra::dir::CopyOptions; use soroban_cli::{ - commands::{config, contract::invoke, global, keys, network, NetworkRunnable}, + commands::{contract::invoke, global, keys, NetworkRunnable}, + config::{self, network}, CommandParser, }; diff --git a/cmd/crates/soroban-test/tests/it/config.rs b/cmd/crates/soroban-test/tests/it/config.rs index 5704237ed..70dfaa693 100644 --- a/cmd/crates/soroban-test/tests/it/config.rs +++ b/cmd/crates/soroban-test/tests/it/config.rs @@ -3,7 +3,8 @@ use soroban_test::{AssertExt, TestEnv}; use std::{fs, path::Path}; use crate::util::{add_key, add_test_id, SecretKind, DEFAULT_SEED_PHRASE}; -use soroban_cli::commands::network::{self, LOCAL_NETWORK_PASSPHRASE}; +use soroban_cli::commands::network; +use soroban_cli::config::network::passphrase::LOCAL as LOCAL_NETWORK_PASSPHRASE; fn ls(sandbox: &TestEnv) -> Vec { sandbox diff --git a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs index 16c13dd25..1c766095f 100644 --- a/cmd/crates/soroban-test/tests/it/integration/hello_world.rs +++ b/cmd/crates/soroban-test/tests/it/integration/hello_world.rs @@ -1,8 +1,10 @@ use predicates::boolean::PredicateBooleanExt; -use soroban_cli::commands::{ +use soroban_cli::{ + commands::{ + contract::{self, fetch}, + txn_result::TxnResult, + }, config::{locator, secret}, - contract::{self, fetch}, - txn_result::TxnResult, }; use soroban_rpc::GetLatestLedgerResponse; use soroban_test::{AssertExt, TestEnv, LOCAL_NETWORK_PASSPHRASE}; @@ -295,6 +297,18 @@ async fn contract_data_read() { .assert() .success() .stdout(predicates::str::starts_with("COUNTER,2")); + + // ensure default durability = persistent works + sandbox + .new_assert_cmd("contract") + .arg("read") + .arg("--id") + .arg(id) + .arg("--key") + .arg(KEY) + .assert() + .success() + .stdout(predicates::str::starts_with("COUNTER,2")); } #[tokio::test] diff --git a/cmd/crates/soroban-test/tests/it/integration/tx.rs b/cmd/crates/soroban-test/tests/it/integration/tx.rs index 9f5204a8d..bcb880b18 100644 --- a/cmd/crates/soroban-test/tests/it/integration/tx.rs +++ b/cmd/crates/soroban-test/tests/it/integration/tx.rs @@ -30,3 +30,22 @@ async fn txn_simulate() { assembled_str ); } + +#[tokio::test] +async fn txn_hash() { + let sandbox = &TestEnv::new(); + + let xdr_base64 = "AAAAAgAAAACVk/0xt9tV/cUbF53iwQ3tkKLlq9zG2wV5qd9lRjZjlQAHt/sAFsKTAAAABAAAAAEAAAAAAAAAAAAAAABmOg6nAAAAAAAAAAEAAAAAAAAAGAAAAAAAAAABfcHs35M1GZ/JkY2+DHMs4dEUaqjynMnDYK/Gp0eulN8AAAAIdHJhbnNmZXIAAAADAAAAEgAAAAEFO1FR2Wg49QFY5KPOFAQ0bV5fN+7LD2GSQvOaHSH44QAAABIAAAAAAAAAAJWT/TG321X9xRsXneLBDe2QouWr3MbbBXmp32VGNmOVAAAACgAAAAAAAAAAAAAAADuaygAAAAABAAAAAQAAAAEFO1FR2Wg49QFY5KPOFAQ0bV5fN+7LD2GSQvOaHSH44QAAAY9SyLSVABbC/QAAABEAAAABAAAAAwAAAA8AAAASYXV0aGVudGljYXRvcl9kYXRhAAAAAAANAAAAJUmWDeWIDoxodDQXD2R2YFuP5K65ooYyx5lc87qDHZdjHQAAAAAAAAAAAAAPAAAAEGNsaWVudF9kYXRhX2pzb24AAAANAAAAcnsidHlwZSI6IndlYmF1dGhuLmdldCIsImNoYWxsZW5nZSI6ImhnMlRhOG8wWTliWFlyWlMyZjhzWk1kRFp6ektCSXhQNTZSd1FaNE90bTgiLCJvcmlnaW4iOiJodHRwOi8vbG9jYWxob3N0OjQ1MDcifQAAAAAADwAAAAlzaWduYXR1cmUAAAAAAAANAAAAQBcpuTFMxzkAdBs+5VIyJCBHaNuwEAva+kZVET4YuHVKF8gNII567RhxsnhBBSo5dDvssTN6vf2i42eEty66MtoAAAAAAAAAAX3B7N+TNRmfyZGNvgxzLOHRFGqo8pzJw2CvxqdHrpTfAAAACHRyYW5zZmVyAAAAAwAAABIAAAABBTtRUdloOPUBWOSjzhQENG1eXzfuyw9hkkLzmh0h+OEAAAASAAAAAAAAAACVk/0xt9tV/cUbF53iwQ3tkKLlq9zG2wV5qd9lRjZjlQAAAAoAAAAAAAAAAAAAAAA7msoAAAAAAAAAAAEAAAAAAAAAAwAAAAYAAAABfcHs35M1GZ/JkY2+DHMs4dEUaqjynMnDYK/Gp0eulN8AAAAUAAAAAQAAAAYAAAABBTtRUdloOPUBWOSjzhQENG1eXzfuyw9hkkLzmh0h+OEAAAAUAAAAAQAAAAeTiL4Gr2piUAmsXTev1ZzJ4kE2NUGZ0QMObd05iAMyzAAAAAMAAAAGAAAAAX3B7N+TNRmfyZGNvgxzLOHRFGqo8pzJw2CvxqdHrpTfAAAAEAAAAAEAAAACAAAADwAAAAdCYWxhbmNlAAAAABIAAAABBTtRUdloOPUBWOSjzhQENG1eXzfuyw9hkkLzmh0h+OEAAAABAAAAAAAAAACVk/0xt9tV/cUbF53iwQ3tkKLlq9zG2wV5qd9lRjZjlQAAAAYAAAABBTtRUdloOPUBWOSjzhQENG1eXzfuyw9hkkLzmh0h+OEAAAAVAAABj1LItJUAAAAAAEyTowAAGMgAAAG4AAAAAAADJBsAAAABRjZjlQAAAEASFnAIzNqpfdzv6yT0rSLMUDFgt7a/inCHurNCG55Jp8Imho04qRH+JNdkq0BgMC7yAJqH4N6Y2iGflFt3Lp4L"; + + let expected_hash = "bcc9fa60c8f6607c981d6e1c65d77ae07617720113f9080fe5883d8e4a331a68"; + + let hash = sandbox + .new_assert_cmd("tx") + .arg("hash") + .write_stdin(xdr_base64.as_bytes()) + .assert() + .success() + .stdout_as_str(); + + assert_eq!(hash.trim(), expected_hash); +} diff --git a/cmd/crates/soroban-test/tests/it/util.rs b/cmd/crates/soroban-test/tests/it/util.rs index 6bf5f8c22..f424ea1ae 100644 --- a/cmd/crates/soroban-test/tests/it/util.rs +++ b/cmd/crates/soroban-test/tests/it/util.rs @@ -1,8 +1,8 @@ use std::path::Path; -use soroban_cli::commands::{ +use soroban_cli::{ + commands::contract, config::{locator::KeyType, secret::Secret}, - contract, }; use soroban_test::{TestEnv, Wasm, TEST_ACCOUNT}; diff --git a/cmd/soroban-cli/Cargo.toml b/cmd/soroban-cli/Cargo.toml index 3b0b90153..6d40ddd90 100644 --- a/cmd/soroban-cli/Cargo.toml +++ b/cmd/soroban-cli/Cargo.toml @@ -130,3 +130,4 @@ ureq = { version = "2.9.1", features = ["json"] } assert_cmd = "2.0.4" assert_fs = "1.0.7" predicates = "2.1.5" +walkdir = "2.5.0" diff --git a/cmd/soroban-cli/src/commands/cache/actionlog/ls.rs b/cmd/soroban-cli/src/commands/cache/actionlog/ls.rs index cb7a958c6..360512bba 100644 --- a/cmd/soroban-cli/src/commands/cache/actionlog/ls.rs +++ b/cmd/soroban-cli/src/commands/cache/actionlog/ls.rs @@ -1,7 +1,6 @@ use clap::command; -use super::super::super::config::locator; -use crate::commands::config::data; +use crate::config::{data, locator}; #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/cmd/soroban-cli/src/commands/cache/actionlog/read.rs b/cmd/soroban-cli/src/commands/cache/actionlog/read.rs index 67b4358f1..4991ac6ef 100644 --- a/cmd/soroban-cli/src/commands/cache/actionlog/read.rs +++ b/cmd/soroban-cli/src/commands/cache/actionlog/read.rs @@ -1,7 +1,6 @@ use std::{fs, io, path::PathBuf}; -use super::super::super::config::locator; -use crate::commands::config::data; +use crate::config::{data, locator}; #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/cmd/soroban-cli/src/commands/cache/clean.rs b/cmd/soroban-cli/src/commands/cache/clean.rs index bea0a43d4..e2378aa6c 100644 --- a/cmd/soroban-cli/src/commands/cache/clean.rs +++ b/cmd/soroban-cli/src/commands/cache/clean.rs @@ -1,7 +1,6 @@ use std::{fs, io::ErrorKind}; -use super::super::config::locator; -use crate::commands::config::data; +use crate::config::{data, locator}; #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/cmd/soroban-cli/src/commands/cache/path.rs b/cmd/soroban-cli/src/commands/cache/path.rs index 337608735..a382516b9 100644 --- a/cmd/soroban-cli/src/commands/cache/path.rs +++ b/cmd/soroban-cli/src/commands/cache/path.rs @@ -1,5 +1,4 @@ -use super::super::config::locator; -use crate::commands::config::data; +use crate::config::{data, locator}; #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs index b2b6c3b1c..84e2b1762 100644 --- a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs +++ b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs @@ -7,12 +7,10 @@ use stellar_strkey::DecodeError; use crate::wasm; use crate::{ - commands::{ - config::{self, locator}, - contract::fetch, - global, + commands::{contract::fetch, global, NetworkRunnable}, + config::{ + self, locator, network::{self, Network}, - NetworkRunnable, }, get_spec::{self, get_remote_contract_spec}, }; diff --git a/cmd/soroban-cli/src/commands/contract/deploy/asset.rs b/cmd/soroban-cli/src/commands/contract/deploy/asset.rs index e912d2b98..98276a86d 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/asset.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/asset.rs @@ -13,11 +13,11 @@ use std::{array::TryFromSliceError, fmt::Debug, num::ParseIntError}; use crate::{ commands::{ - config::{self, data}, - global, network, + global, txn_result::{TxnEnvelopeResult, TxnResult}, NetworkRunnable, }, + config::{self, data, network}, rpc::{Client, Error as SorobanRpcError}, utils::{contract_id_hash_from_asset, parsing::parse_asset}, }; diff --git a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs index b85b65bcc..4c1aa5d55 100644 --- a/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/deploy/wasm.rs @@ -16,14 +16,14 @@ use soroban_env_host::{ }; use crate::commands::{ - config::{data, locator}, contract::{self, id::wasm::get_contract_id}, - global, network, + global, txn_result::{TxnEnvelopeResult, TxnResult}, NetworkRunnable, }; use crate::{ - commands::{config, contract::install, HEADING_RPC}, + commands::{contract::install, HEADING_RPC}, + config::{self, data, locator, network}, rpc::{self, Client}, utils, wasm, }; diff --git a/cmd/soroban-cli/src/commands/contract/extend.rs b/cmd/soroban-cli/src/commands/contract/extend.rs index 89bdc7620..03c7c70cf 100644 --- a/cmd/soroban-cli/src/commands/contract/extend.rs +++ b/cmd/soroban-cli/src/commands/contract/extend.rs @@ -10,11 +10,11 @@ use soroban_env_host::xdr::{ use crate::{ commands::{ - config::{self, data, locator}, - global, network, + global, txn_result::{TxnEnvelopeResult, TxnResult}, NetworkRunnable, }, + config::{self, data, locator, network}, key, rpc::{self, Client}, wasm, Pwd, diff --git a/cmd/soroban-cli/src/commands/contract/fetch.rs b/cmd/soroban-cli/src/commands/contract/fetch.rs index 42f474558..7c6c84cb1 100644 --- a/cmd/soroban-cli/src/commands/contract/fetch.rs +++ b/cmd/soroban-cli/src/commands/contract/fetch.rs @@ -19,9 +19,11 @@ use soroban_env_host::{ use soroban_spec::read::FromWasmError; use stellar_strkey::DecodeError; -use super::super::config::{self, locator}; -use crate::commands::network::{self, Network}; use crate::commands::{global, NetworkRunnable}; +use crate::config::{ + self, locator, + network::{self, Network}, +}; use crate::{ rpc::{self, Client}, Pwd, diff --git a/cmd/soroban-cli/src/commands/contract/id/asset.rs b/cmd/soroban-cli/src/commands/contract/id/asset.rs index e036b7939..31b55f8d1 100644 --- a/cmd/soroban-cli/src/commands/contract/id/asset.rs +++ b/cmd/soroban-cli/src/commands/contract/id/asset.rs @@ -1,6 +1,6 @@ use clap::{arg, command, Parser}; -use crate::commands::config; +use crate::config; use crate::utils::contract_id_hash_from_asset; use crate::utils::parsing::parse_asset; @@ -20,7 +20,7 @@ pub enum Error { #[error(transparent)] ParseError(#[from] crate::utils::parsing::Error), #[error(transparent)] - ConfigError(#[from] crate::commands::config::Error), + ConfigError(#[from] config::Error), #[error(transparent)] Xdr(#[from] soroban_env_host::xdr::Error), } diff --git a/cmd/soroban-cli/src/commands/contract/id/wasm.rs b/cmd/soroban-cli/src/commands/contract/id/wasm.rs index 9c02f07d2..14824b145 100644 --- a/cmd/soroban-cli/src/commands/contract/id/wasm.rs +++ b/cmd/soroban-cli/src/commands/contract/id/wasm.rs @@ -5,7 +5,7 @@ use soroban_env_host::xdr::{ HashIdPreimageContractId, Limits, PublicKey, ScAddress, Uint256, WriteXdr, }; -use crate::commands::config; +use crate::config; #[derive(Parser, Debug, Clone)] #[group(skip)] @@ -22,7 +22,7 @@ pub enum Error { #[error(transparent)] ParseError(#[from] crate::utils::parsing::Error), #[error(transparent)] - ConfigError(#[from] crate::commands::config::Error), + ConfigError(#[from] config::Error), #[error(transparent)] Xdr(#[from] xdr::Error), #[error("cannot parse salt {0}")] diff --git a/cmd/soroban-cli/src/commands/contract/init.rs b/cmd/soroban-cli/src/commands/contract/init.rs index 21b9d48bb..7318c1d7f 100644 --- a/cmd/soroban-cli/src/commands/contract/init.rs +++ b/cmd/soroban-cli/src/commands/contract/init.rs @@ -47,6 +47,9 @@ pub struct Cmd { long_help = "An optional flag to pass in a url for a frontend template repository." )] pub frontend_template: String, + + #[arg(long, long_help = "Overwrite all existing files.")] + pub overwrite: bool, } fn possible_example_values() -> ValueParser { @@ -89,7 +92,12 @@ impl Cmd { println!("ℹ️ Initializing project at {}", self.project_path); let project_path = Path::new(&self.project_path); - init(project_path, &self.frontend_template, &self.with_example)?; + init( + project_path, + &self.frontend_template, + &self.with_example, + self.overwrite, + )?; Ok(()) } @@ -103,13 +111,14 @@ fn init( project_path: &Path, frontend_template: &str, with_examples: &[String], + overwrite: bool, ) -> Result<(), Error> { // create a project dir, and copy the contents of the base template (contract-init-template) into it create_dir_all(project_path).map_err(|e| { eprintln!("Error creating new project directory: {project_path:?}"); e })?; - copy_template_files(project_path)?; + copy_template_files(project_path, overwrite)?; if !check_internet_connection() { println!("⚠️ It doesn't look like you're connected to the internet. We're still able to initialize a new project, but additional examples and the frontend template will not be included."); @@ -127,7 +136,7 @@ fn init( clone_repo(frontend_template, fe_template_dir.path())?; // copy the frontend template files into the project - copy_frontend_files(fe_template_dir.path(), project_path)?; + copy_frontend_files(fe_template_dir.path(), project_path, overwrite)?; } // if there are --with-example flags, include the example contracts @@ -142,17 +151,17 @@ fn init( clone_repo(SOROBAN_EXAMPLES_URL, examples_dir.path())?; // copy the example contracts into the project - copy_example_contracts(examples_dir.path(), project_path, with_examples)?; + copy_example_contracts(examples_dir.path(), project_path, with_examples, overwrite)?; } Ok(()) } -fn copy_template_files(project_path: &Path) -> Result<(), Error> { +fn copy_template_files(project_path: &Path, overwrite: bool) -> Result<(), Error> { for item in TemplateFiles::iter() { let mut to = project_path.join(item.as_ref()); - - if file_exists(&to) { + let exists = file_exists(&to); + if exists && !overwrite { println!( "ℹ️ Skipped creating {} as it already exists", &to.to_string_lossy() @@ -184,7 +193,11 @@ fn copy_template_files(project_path: &Path) -> Result<(), Error> { to = project_path.join(item_parent_path).join("Cargo.toml"); } - println!("➕ Writing {}", &to.to_string_lossy()); + if exists { + println!("🔄 Overwriting {}", &to.to_string_lossy()); + } else { + println!("➕ Writing {}", &to.to_string_lossy()); + } write(&to, file_contents).map_err(|e| { eprintln!("Error writing file: {to:?}"); e @@ -193,7 +206,7 @@ fn copy_template_files(project_path: &Path) -> Result<(), Error> { Ok(()) } -fn copy_contents(from: &Path, to: &Path) -> Result<(), Error> { +fn copy_contents(from: &Path, to: &Path, overwrite: bool) -> Result<(), Error> { let contents_to_exclude_from_copy = [ ".git", ".github", @@ -223,24 +236,26 @@ fn copy_contents(from: &Path, to: &Path) -> Result<(), Error> { eprintln!("Error creating directory: {new_path:?}"); e })?; - copy_contents(&path, &new_path)?; + copy_contents(&path, &new_path, overwrite)?; } else { - if file_exists(&new_path) { - if new_path.to_string_lossy().contains(".gitignore") { - append_contents(&path, &new_path)?; - } - if new_path.to_string_lossy().contains("README.md") { + let exists = file_exists(&new_path); + let new_path_str = new_path.to_string_lossy(); + if exists { + let append = + new_path_str.contains(".gitignore") || new_path_str.contains("README.md"); + if append { append_contents(&path, &new_path)?; } - println!( - "ℹ️ Skipped creating {} as it already exists", - &new_path.to_string_lossy() - ); - continue; + if overwrite && !append { + println!("🔄 Overwriting {new_path_str}"); + } else { + println!("ℹ️ Skipped creating {new_path_str} as it already exists"); + continue; + } + } else { + println!("➕ Writing {new_path_str}"); } - - println!("➕ Writing {}", &new_path.to_string_lossy()); copy(&path, &new_path).map_err(|e| { eprintln!( "Error copying from {:?} to {:?}", @@ -302,7 +317,12 @@ fn clone_repo(from_url: &str, to_path: &Path) -> Result<(), Error> { Ok(()) } -fn copy_example_contracts(from: &Path, to: &Path, contracts: &[String]) -> Result<(), Error> { +fn copy_example_contracts( + from: &Path, + to: &Path, + contracts: &[String], + overwrite: bool, +) -> Result<(), Error> { let project_contracts_path = to.join("contracts"); for contract in contracts { println!("ℹ️ Initializing example contract: {contract}"); @@ -315,7 +335,7 @@ fn copy_example_contracts(from: &Path, to: &Path, contracts: &[String]) -> Resul e })?; - copy_contents(&from_contract_path, &to_contract_path)?; + copy_contents(&from_contract_path, &to_contract_path, overwrite)?; edit_contract_cargo_file(&to_contract_path)?; } @@ -354,9 +374,9 @@ fn edit_contract_cargo_file(contract_path: &Path) -> Result<(), Error> { Ok(()) } -fn copy_frontend_files(from: &Path, to: &Path) -> Result<(), Error> { +fn copy_frontend_files(from: &Path, to: &Path, overwrite: bool) -> Result<(), Error> { println!("ℹ️ Initializing with frontend template"); - copy_contents(from, to)?; + copy_contents(from, to, overwrite)?; edit_package_json_files(to) } @@ -447,7 +467,13 @@ fn get_merged_file_delimiter(file_path: &Path) -> String { #[cfg(test)] mod tests { use itertools::Itertools; - use std::fs::{self, read_to_string}; + use std::{ + collections::HashMap, + fs::{self, read_to_string}, + path::PathBuf, + time::SystemTime, + }; + use walkdir::WalkDir; use super::*; @@ -458,7 +484,8 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join(TEST_PROJECT_NAME); let with_examples = vec![]; - init(project_dir.as_path(), "", &with_examples).unwrap(); + let overwrite = false; + init(project_dir.as_path(), "", &with_examples, overwrite).unwrap(); assert_base_template_files_exist(&project_dir); assert_default_hello_world_contract_files_exist(&project_dir); @@ -476,7 +503,8 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join(TEST_PROJECT_NAME); let with_examples = ["alloc".to_owned()]; - init(project_dir.as_path(), "", &with_examples).unwrap(); + let overwrite = false; + init(project_dir.as_path(), "", &with_examples, overwrite).unwrap(); assert_base_template_files_exist(&project_dir); assert_default_hello_world_contract_files_exist(&project_dir); @@ -499,7 +527,8 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join("project"); let with_examples = ["account".to_owned(), "atomic_swap".to_owned()]; - init(project_dir.as_path(), "", &with_examples).unwrap(); + let overwrite = false; + init(project_dir.as_path(), "", &with_examples, overwrite).unwrap(); assert_base_template_files_exist(&project_dir); assert_default_hello_world_contract_files_exist(&project_dir); @@ -523,7 +552,8 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join("project"); let with_examples = ["invalid_example".to_owned(), "atomic_swap".to_owned()]; - assert!(init(project_dir.as_path(), "", &with_examples,).is_err()); + let overwrite = false; + assert!(init(project_dir.as_path(), "", &with_examples, overwrite).is_err()); temp_dir.close().unwrap(); } @@ -533,10 +563,12 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join(TEST_PROJECT_NAME); let with_examples = vec![]; + let overwrite = false; init( project_dir.as_path(), "https://github.com/stellar/soroban-astro-template", &with_examples, + overwrite, ) .unwrap(); @@ -556,15 +588,73 @@ mod tests { temp_dir.close().unwrap(); } + #[test] + fn test_init_with_overwrite() { + let temp_dir = tempfile::tempdir().unwrap(); + let project_dir = temp_dir.path().join(TEST_PROJECT_NAME); + let with_examples = vec![]; + + // First initialization + init( + project_dir.as_path(), + "https://github.com/stellar/soroban-astro-template", + &with_examples, + false, + ) + .unwrap(); + + // Get initial modification times + let initial_mod_times = get_mod_times(&project_dir); + + // Second initialization with overwrite + init( + project_dir.as_path(), + "https://github.com/stellar/soroban-astro-template", + &with_examples, + true, // overwrite = true + ) + .unwrap(); + + // Get new modification times + let new_mod_times = get_mod_times(&project_dir); + + // Compare modification times + for (path, initial_time) in initial_mod_times { + let new_time = new_mod_times.get(&path).expect("File should still exist"); + assert!( + new_time > &initial_time, + "File {} should have a later modification time", + path.display() + ); + } + + temp_dir.close().unwrap(); + } + + fn get_mod_times(dir: &Path) -> HashMap { + let mut mod_times = HashMap::new(); + for entry in WalkDir::new(dir) { + let entry = entry.unwrap(); + if entry.file_type().is_file() { + let path = entry.path().to_owned(); + let metadata = fs::metadata(&path).unwrap(); + mod_times.insert(path, metadata.modified().unwrap()); + } + } + mod_times + } + #[test] fn test_init_from_within_an_existing_project() { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join("./"); let with_examples = vec![]; + let overwrite = false; init( project_dir.as_path(), "https://github.com/stellar/soroban-astro-template", &with_examples, + overwrite, ) .unwrap(); @@ -591,10 +681,12 @@ mod tests { let temp_dir = tempfile::tempdir().unwrap(); let project_dir = temp_dir.path().join(TEST_PROJECT_NAME); let with_examples = vec![]; + let overwrite = false; init( project_dir.as_path(), "https://github.com/stellar/soroban-astro-template", &with_examples, + overwrite, ) .unwrap(); @@ -603,6 +695,7 @@ mod tests { project_dir.as_path(), "https://github.com/stellar/soroban-astro-template", &with_examples, + overwrite, ) .unwrap(); diff --git a/cmd/soroban-cli/src/commands/contract/inspect.rs b/cmd/soroban-cli/src/commands/contract/inspect.rs index e66bf83fd..36a1a2302 100644 --- a/cmd/soroban-cli/src/commands/contract/inspect.rs +++ b/cmd/soroban-cli/src/commands/contract/inspect.rs @@ -5,7 +5,7 @@ use std::{fmt::Debug, path::PathBuf}; use tracing::debug; use super::SpecOutput; -use crate::{commands::config::locator, wasm}; +use crate::{config::locator, wasm}; #[derive(Parser, Debug, Clone)] #[group(skip)] diff --git a/cmd/soroban-cli/src/commands/contract/install.rs b/cmd/soroban-cli/src/commands/contract/install.rs index d7ebef16d..9d2e474a3 100644 --- a/cmd/soroban-cli/src/commands/contract/install.rs +++ b/cmd/soroban-cli/src/commands/contract/install.rs @@ -11,12 +11,12 @@ use soroban_env_host::xdr::{ }; use super::restore; -use crate::commands::network; use crate::commands::txn_result::{TxnEnvelopeResult, TxnResult}; -use crate::commands::{config::data, global, NetworkRunnable}; +use crate::commands::{global, NetworkRunnable}; +use crate::config::{self, data, network}; use crate::key; use crate::rpc::{self, Client}; -use crate::{commands::config, utils, wasm}; +use crate::{utils, wasm}; const CONTRACT_META_SDK_KEY: &str = "rssdkver"; const PUBLIC_NETWORK_PASSPHRASE: &str = "Public Global Stellar Network ; September 2015"; diff --git a/cmd/soroban-cli/src/commands/contract/invoke.rs b/cmd/soroban-cli/src/commands/contract/invoke.rs index 22462cbcb..041613ba2 100644 --- a/cmd/soroban-cli/src/commands/contract/invoke.rs +++ b/cmd/soroban-cli/src/commands/contract/invoke.rs @@ -23,15 +23,13 @@ use soroban_env_host::{ use soroban_spec::read::FromWasmError; -use super::super::{ - config::{self, locator}, - events, -}; +use super::super::events; use crate::commands::txn_result::{TxnEnvelopeResult, TxnResult}; use crate::commands::NetworkRunnable; use crate::get_spec::{self, get_remote_contract_spec}; use crate::{ - commands::{config::data, global, network}, + commands::global, + config::{self, data, locator, network}, rpc, Pwd, }; use soroban_spec_tools::{contract, Spec}; diff --git a/cmd/soroban-cli/src/commands/contract/read.rs b/cmd/soroban-cli/src/commands/contract/read.rs index 70e2bd09e..bef3f3737 100644 --- a/cmd/soroban-cli/src/commands/contract/read.rs +++ b/cmd/soroban-cli/src/commands/contract/read.rs @@ -13,10 +13,8 @@ use soroban_env_host::{ }; use crate::{ - commands::{ - config::{self, locator}, - global, NetworkRunnable, - }, + commands::{global, NetworkRunnable}, + config::{self, locator}, key, rpc::{self, Client, FullLedgerEntries, FullLedgerEntry}, }; diff --git a/cmd/soroban-cli/src/commands/contract/restore.rs b/cmd/soroban-cli/src/commands/contract/restore.rs index 2a6669920..192af3140 100644 --- a/cmd/soroban-cli/src/commands/contract/restore.rs +++ b/cmd/soroban-cli/src/commands/contract/restore.rs @@ -11,12 +11,12 @@ use stellar_strkey::DecodeError; use crate::{ commands::{ - config::{self, data, locator}, contract::extend, - global, network, + global, txn_result::{TxnEnvelopeResult, TxnResult}, NetworkRunnable, }, + config::{self, data, locator, network}, key, rpc::{self, Client}, wasm, Pwd, diff --git a/cmd/soroban-cli/src/commands/events.rs b/cmd/soroban-cli/src/commands/events.rs index 63bdfa04d..a755c33d0 100644 --- a/cmd/soroban-cli/src/commands/events.rs +++ b/cmd/soroban-cli/src/commands/events.rs @@ -3,18 +3,16 @@ use std::io; use soroban_env_host::xdr::{self, Limits, ReadXdr}; -use super::{ - config::{self, locator}, - global, network, NetworkRunnable, -}; +use super::{global, NetworkRunnable}; +use crate::config::{self, locator, network}; use crate::rpc; #[derive(Parser, Debug, Clone)] #[group(skip)] pub struct Cmd { - /// The first [ledger sequence] number in the range to pull events - /// - /// [ledger sequence]: https://developers.stellar.org/docs/encyclopedia/ledger-headers#ledger-sequence + #[allow(clippy::doc_markdown)] + /// The first ledger sequence number in the range to pull events + /// https://developers.stellar.org/docs/learn/encyclopedia/network-configuration/ledger-headers#ledger-sequence #[arg(long, conflicts_with = "cursor", required_unless_present = "cursor")] start_ledger: Option, /// The cursor corresponding to the start of the event range. diff --git a/cmd/soroban-cli/src/commands/keys/add.rs b/cmd/soroban-cli/src/commands/keys/add.rs index 2868c7371..d8f528bae 100644 --- a/cmd/soroban-cli/src/commands/keys/add.rs +++ b/cmd/soroban-cli/src/commands/keys/add.rs @@ -1,6 +1,6 @@ use clap::command; -use super::super::config::{locator, secret}; +use crate::config::{locator, secret}; #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/cmd/soroban-cli/src/commands/keys/fund.rs b/cmd/soroban-cli/src/commands/keys/fund.rs index b6c088f13..d7100c6cb 100644 --- a/cmd/soroban-cli/src/commands/keys/fund.rs +++ b/cmd/soroban-cli/src/commands/keys/fund.rs @@ -1,6 +1,6 @@ use clap::command; -use crate::commands::network; +use crate::config::network; use super::address; diff --git a/cmd/soroban-cli/src/commands/keys/generate.rs b/cmd/soroban-cli/src/commands/keys/generate.rs index 5c6c119ca..16e6a7fdb 100644 --- a/cmd/soroban-cli/src/commands/keys/generate.rs +++ b/cmd/soroban-cli/src/commands/keys/generate.rs @@ -1,9 +1,7 @@ use clap::{arg, command}; -use crate::commands::network; - use super::super::config::{ - locator, + locator, network, secret::{self, Secret}, }; diff --git a/cmd/soroban-cli/src/commands/keys/show.rs b/cmd/soroban-cli/src/commands/keys/show.rs index b99478cbc..58c47740c 100644 --- a/cmd/soroban-cli/src/commands/keys/show.rs +++ b/cmd/soroban-cli/src/commands/keys/show.rs @@ -1,6 +1,6 @@ use clap::arg; -use super::super::config::{locator, secret}; +use crate::config::{locator, secret}; #[derive(thiserror::Error, Debug)] pub enum Error { diff --git a/cmd/soroban-cli/src/commands/mod.rs b/cmd/soroban-cli/src/commands/mod.rs index 0d5fcff57..081439254 100644 --- a/cmd/soroban-cli/src/commands/mod.rs +++ b/cmd/soroban-cli/src/commands/mod.rs @@ -3,9 +3,10 @@ use std::str::FromStr; use async_trait::async_trait; use clap::{command, error::ErrorKind, CommandFactory, FromArgMatches, Parser}; +use crate::config; + pub mod cache; pub mod completion; -pub mod config; pub mod contract; pub mod events; pub mod global; @@ -18,13 +19,16 @@ pub mod version; pub mod txn_result; pub const HEADING_RPC: &str = "Options (RPC)"; -const ABOUT: &str = "With the Stellar CLI you can: +const ABOUT: &str = + "Work seamlessly with Stellar accounts, contracts, and assets from the command line. -- build, deploy and interact with contracts -- set identities to sign with -- configure networks -- generate keys -- more! +- Generate and manage keys and accounts +- Build, deploy, and interact with contracts +- Deploy asset contracts +- Stream events +- Start local testnets +- Decode, encode XDR +- More! For additional information see: @@ -35,21 +39,21 @@ For additional information see: // long_about is shown when someone uses `--help`; short help when using `-h` const LONG_ABOUT: &str = " -The easiest way to get started is to generate a new identity: +To get started generate a new identity: stellar keys generate alice -You can use identities with the `--source` flag in other commands later. +Use keys with the `--source` flag in other commands. -Commands that relate to smart contract interactions are organized under the `contract` subcommand. List them: +Commands that work with contracts are organized under the `contract` subcommand. List them: stellar contract --help -A Soroban contract has its interface schema types embedded in the binary that gets deployed on-chain, making it possible to dynamically generate a custom CLI for each. The invoke subcommand makes use of this: +Use contracts like a CLI: stellar contract invoke --id CCR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OTE2 --source alice --network testnet -- --help -Anything after the `--` double dash (the \"slop\") is parsed as arguments to the contract-specific CLI, generated on-the-fly from the embedded schema. For the hello world example, with a function called `hello` that takes one string argument `to`, here's how you invoke it: +Anything after the `--` double dash (the \"slop\") is parsed as arguments to the contract-specific CLI, generated on-the-fly from the contract schema. For the hello world example, with a function called `hello` that takes one string argument `to`, here's how you invoke it: stellar contract invoke --id CCR6QKTWZQYW6YUJ7UP7XXZRLWQPFRV6SWBLQS4ZQOSAF4BOUD77OTE2 --source alice --network testnet -- hello --to world "; @@ -125,30 +129,35 @@ impl FromStr for Root { #[derive(Parser, Debug)] pub enum Cmd { - /// Print shell completion code for the specified shell. - #[command(long_about = completion::LONG_ABOUT)] - Completion(completion::Cmd), /// Tools for smart contract developers #[command(subcommand)] Contract(contract::Cmd), /// Watch the network for contract events Events(events::Cmd), + /// Create and manage identities including keys and addresses #[command(subcommand)] Keys(keys::Cmd), - /// Decode and encode XDR - Xdr(stellar_xdr::cli::Root), + /// Start and configure networks #[command(subcommand)] Network(network::Cmd), - /// Print version information - Version(version::Cmd), + /// Sign, Simulate, and Send transactions #[command(subcommand)] Tx(tx::Cmd), + + /// Decode and encode XDR + Xdr(stellar_xdr::cli::Root), + + /// Print shell completion code for the specified shell. + #[command(long_about = completion::LONG_ABOUT)] + Completion(completion::Cmd), /// Cache for transactions and contract specs #[command(subcommand)] Cache(cache::Cmd), + /// Print version information + Version(version::Cmd), } #[derive(thiserror::Error, Debug)] diff --git a/cmd/soroban-cli/src/commands/network/add.rs b/cmd/soroban-cli/src/commands/network/add.rs index b6a2ddd38..20b1afa7b 100644 --- a/cmd/soroban-cli/src/commands/network/add.rs +++ b/cmd/soroban-cli/src/commands/network/add.rs @@ -1,4 +1,4 @@ -use super::super::config::{locator, secret}; +use crate::config::{locator, network, secret}; use clap::command; #[derive(thiserror::Error, Debug)] @@ -17,7 +17,7 @@ pub struct Cmd { pub name: String, #[command(flatten)] - pub network: super::Network, + pub network: network::Network, #[command(flatten)] pub config_locator: locator::Args, diff --git a/cmd/soroban-cli/src/commands/network/container.rs b/cmd/soroban-cli/src/commands/network/container.rs index 16e5d73be..511c0e11b 100644 --- a/cmd/soroban-cli/src/commands/network/container.rs +++ b/cmd/soroban-cli/src/commands/network/container.rs @@ -10,19 +10,17 @@ pub type StopCmd = stop::Cmd; #[derive(Debug, clap::Subcommand)] pub enum Cmd { - /// Tail logs of a running network container + /// Get logs from a running network container Logs(logs::Cmd), - /// Start network - /// /// Start a container running a Stellar node, RPC, API, and friendbot (faucet). /// - /// `stellar network start NETWORK [OPTIONS]` + /// `stellar network container start NETWORK [OPTIONS]` /// /// By default, when starting a testnet container, without any optional arguments, it will run the equivalent of the following docker command: /// - /// `docker run --rm -p 8000:8000 --name stellar stellar/quickstart:testing --testnet --enable-soroban-rpc` + /// `docker run --rm -p 8000:8000 --name stellar stellar/quickstart:testing --testnet --enable rpc,horizon` Start(start::Cmd), - /// Stop a network started with `network container start`. For example, if you ran `network container start local`, you can use `network container stop local` to stop it. + /// Stop a network container started with `network container start`. Stop(stop::Cmd), } diff --git a/cmd/soroban-cli/src/commands/network/container/logs.rs b/cmd/soroban-cli/src/commands/network/container/logs.rs index e37ceb098..99b36af9b 100644 --- a/cmd/soroban-cli/src/commands/network/container/logs.rs +++ b/cmd/soroban-cli/src/commands/network/container/logs.rs @@ -1,8 +1,8 @@ use futures_util::TryStreamExt; -use crate::commands::network::container::shared::{ - connect_to_docker, Error as ConnectionError, Network, DOCKER_HOST_HELP, -}; +use crate::commands::network::container::shared::Error as ConnectionError; + +use super::shared::{Args, Name}; #[derive(thiserror::Error, Debug)] pub enum Error { @@ -15,17 +15,17 @@ pub enum Error { #[derive(Debug, clap::Parser, Clone)] pub struct Cmd { - /// Network to tail - pub network: Network, + #[command(flatten)] + pub container_args: Args, - #[arg(short = 'd', long, help = DOCKER_HOST_HELP, env = "DOCKER_HOST")] - pub docker_host: Option, + /// Container to get logs from + pub name: String, } impl Cmd { pub async fn run(&self) -> Result<(), Error> { - let container_name = format!("stellar-{}", self.network); - let docker = connect_to_docker(&self.docker_host).await?; + let container_name = Name(self.name.clone()).get_internal_container_name(); + let docker = self.container_args.connect_to_docker().await?; let logs_stream = &mut docker.logs( &container_name, Some(bollard::container::LogsOptions { diff --git a/cmd/soroban-cli/src/commands/network/container/shared.rs b/cmd/soroban-cli/src/commands/network/container/shared.rs index 9da8a1bea..f819f3ed3 100644 --- a/cmd/soroban-cli/src/commands/network/container/shared.rs +++ b/cmd/soroban-cli/src/commands/network/container/shared.rs @@ -31,7 +31,81 @@ pub enum Error { UnsupportedURISchemeError { uri: String }, } -#[derive(ValueEnum, Debug, Clone, PartialEq)] +#[derive(Debug, clap::Parser, Clone)] +pub struct Args { + /// Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock + #[arg(short = 'd', long, help = DOCKER_HOST_HELP, env = "DOCKER_HOST")] + pub docker_host: Option, +} + +impl Args { + pub(crate) fn get_additional_flags(&self) -> String { + self.docker_host + .as_ref() + .map(|docker_host| format!("--docker-host {docker_host}")) + .unwrap_or_default() + } + + pub(crate) async fn connect_to_docker(&self) -> Result { + // if no docker_host is provided, use the default docker host: + // "unix:///var/run/docker.sock" on unix machines + // "npipe:////./pipe/docker_engine" on windows machines + let host = self.docker_host.as_ref().map_or_else( + || DEFAULT_DOCKER_HOST.to_string(), + std::string::ToString::to_string, + ); + + // this is based on the `connect_with_defaults` method which has not yet been released in the bollard crate + // https://github.com/fussybeaver/bollard/blob/0972b1aac0ad5c08798e100319ddd0d2ee010365/src/docker.rs#L660 + let connection = match host.clone() { + // if tcp or http, use connect_with_http_defaults + // if unix and host starts with "unix://" use connect_with_unix + // if windows and host starts with "npipe://", use connect_with_named_pipe + // else default to connect_with_unix + h if h.starts_with("tcp://") || h.starts_with("http://") => { + Docker::connect_with_http_defaults() + } + #[cfg(unix)] + h if h.starts_with("unix://") => { + Docker::connect_with_unix(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION) + } + #[cfg(windows)] + h if h.starts_with("npipe://") => { + Docker::connect_with_named_pipe(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION) + } + _ => { + return Err(Error::UnsupportedURISchemeError { + uri: host.to_string(), + }); + } + }?; + + match check_docker_connection(&connection).await { + Ok(()) => Ok(connection), + // If we aren't able to connect with the defaults, or with the provided docker_host + // try to connect with the default docker desktop socket since that is a common use case for devs + #[allow(unused_variables)] + Err(e) => { + // if on unix, try to connect to the default docker desktop socket + #[cfg(unix)] + { + let docker_desktop_connection = try_docker_desktop_socket(&host)?; + match check_docker_connection(&docker_desktop_connection).await { + Ok(()) => Ok(docker_desktop_connection), + Err(err) => Err(err)?, + } + } + + #[cfg(windows)] + { + Err(e)? + } + } + } + } +} + +#[derive(ValueEnum, Debug, Copy, Clone, PartialEq)] pub enum Network { Local, Testnet, @@ -52,61 +126,14 @@ impl fmt::Display for Network { } } -pub async fn connect_to_docker(docker_host: &Option) -> Result { - // if no docker_host is provided, use the default docker host: - // "unix:///var/run/docker.sock" on unix machines - // "npipe:////./pipe/docker_engine" on windows machines - - let host = docker_host - .clone() - .unwrap_or(DEFAULT_DOCKER_HOST.to_string()); - - // this is based on the `connect_with_defaults` method which has not yet been released in the bollard crate - // https://github.com/fussybeaver/bollard/blob/0972b1aac0ad5c08798e100319ddd0d2ee010365/src/docker.rs#L660 - let connection = match host.clone() { - // if tcp or http, use connect_with_http_defaults - // if unix and host starts with "unix://" use connect_with_unix - // if windows and host starts with "npipe://", use connect_with_named_pipe - // else default to connect_with_unix - h if h.starts_with("tcp://") || h.starts_with("http://") => { - Docker::connect_with_http_defaults() - } - #[cfg(unix)] - h if h.starts_with("unix://") => { - Docker::connect_with_unix(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION) - } - #[cfg(windows)] - h if h.starts_with("npipe://") => { - Docker::connect_with_named_pipe(&h, DEFAULT_TIMEOUT, API_DEFAULT_VERSION) - } - _ => { - return Err(Error::UnsupportedURISchemeError { - uri: host.to_string(), - }); - } - }?; - - match check_docker_connection(&connection).await { - Ok(()) => Ok(connection), - // If we aren't able to connect with the defaults, or with the provided docker_host - // try to connect with the default docker desktop socket since that is a common use case for devs - #[allow(unused_variables)] - Err(e) => { - // if on unix, try to connect to the default docker desktop socket - #[cfg(unix)] - { - let docker_desktop_connection = try_docker_desktop_socket(&host)?; - match check_docker_connection(&docker_desktop_connection).await { - Ok(()) => Ok(docker_desktop_connection), - Err(err) => Err(err)?, - } - } +pub struct Name(pub String); +impl Name { + pub fn get_internal_container_name(&self) -> String { + format!("stellar-{}", self.0) + } - #[cfg(windows)] - { - Err(e)? - } - } + pub fn get_external_container_name(&self) -> String { + self.0.to_string() } } diff --git a/cmd/soroban-cli/src/commands/network/container/start.rs b/cmd/soroban-cli/src/commands/network/container/start.rs index cc9619f1d..46348c297 100644 --- a/cmd/soroban-cli/src/commands/network/container/start.rs +++ b/cmd/soroban-cli/src/commands/network/container/start.rs @@ -7,9 +7,9 @@ use bollard::{ }; use futures_util::TryStreamExt; -use crate::commands::network::container::shared::{ - connect_to_docker, Error as ConnectionError, Network, DOCKER_HOST_HELP, -}; +use crate::commands::network::container::shared::{Error as ConnectionError, Network}; + +use super::shared::{Args, Name}; const DEFAULT_PORT_MAPPING: &str = "8000:8000"; const DOCKER_IMAGE: &str = "docker.io/stellar/quickstart"; @@ -17,19 +17,23 @@ const DOCKER_IMAGE: &str = "docker.io/stellar/quickstart"; #[derive(thiserror::Error, Debug)] pub enum Error { #[error("⛔ ️Failed to connect to docker: {0}")] - ConnectionError(#[from] ConnectionError), + DockerConnectionFailed(#[from] ConnectionError), #[error("⛔ ️Failed to create container: {0}")] - BollardErr(#[from] bollard::errors::Error), + CreateContainerFailed(#[from] bollard::errors::Error), } #[derive(Debug, clap::Parser, Clone)] pub struct Cmd { + #[command(flatten)] + pub container_args: Args, + /// Network to start pub network: Network, - #[arg(short = 'd', long, help = DOCKER_HOST_HELP, env = "DOCKER_HOST")] - pub docker_host: Option, + /// Optional argument to specify the container name + #[arg(long)] + pub name: Option, /// Optional argument to specify the limits for the local network only #[arg(short = 'l', long)] @@ -51,137 +55,152 @@ pub struct Cmd { impl Cmd { pub async fn run(&self) -> Result<(), Error> { println!("ℹ️ Starting {} network", &self.network); - run_docker_command(self).await + self.run_docker_command().await } -} -async fn run_docker_command(cmd: &Cmd) -> Result<(), Error> { - let docker = connect_to_docker(&cmd.docker_host).await?; - - let image = get_image_name(cmd); - docker - .create_image( - Some(CreateImageOptions { - from_image: image.clone(), + async fn run_docker_command(&self) -> Result<(), Error> { + let docker = self.container_args.connect_to_docker().await?; + + let image = self.get_image_name(); + docker + .create_image( + Some(CreateImageOptions { + from_image: image.clone(), + ..Default::default() + }), + None, + None, + ) + .try_collect::>() + .await?; + + let config = Config { + image: Some(image), + cmd: Some(self.get_container_args()), + attach_stdout: Some(true), + attach_stderr: Some(true), + host_config: Some(HostConfig { + auto_remove: Some(true), + port_bindings: Some(self.get_port_mapping()), ..Default::default() }), - None, - None, - ) - .try_collect::>() - .await?; - - let container_args = get_container_args(cmd); - let port_mapping = get_port_mapping(cmd); - - let config = Config { - image: Some(image), - cmd: Some(container_args), - attach_stdout: Some(true), - attach_stderr: Some(true), - host_config: Some(HostConfig { - auto_remove: Some(true), - port_bindings: Some(port_mapping), ..Default::default() - }), - ..Default::default() - }; - - let container_name = format!("stellar-{}", cmd.network); - let create_container_response = docker - .create_container( - Some(CreateContainerOptions { - name: container_name.clone(), - ..Default::default() - }), - config, - ) - .await?; - - docker - .start_container( - &create_container_response.id, - None::>, - ) - .await?; - println!("✅ Container started: {container_name}"); - let stop_message = format!( - "ℹ️ To stop this container run: stellar network stop {network} {additional_flags}", - network = &cmd.network, - additional_flags = if cmd.docker_host.is_some() { - format!("--docker-host {}", cmd.docker_host.as_ref().unwrap()) - } else { - String::new() + }; + + let create_container_response = docker + .create_container( + Some(CreateContainerOptions { + name: self.container_name().get_internal_container_name(), + ..Default::default() + }), + config, + ) + .await?; + + docker + .start_container( + &create_container_response.id, + None::>, + ) + .await?; + println!( + "✅ Container started: {}", + self.container_name().get_external_container_name() + ); + self.print_log_message(); + self.print_stop_message(); + Ok(()) + } + + fn get_image_name(&self) -> String { + // this can be overriden with the `-t` flag + let mut image_tag = match &self.network { + Network::Pubnet => "latest", + Network::Futurenet => "future", + _ => "testing", // default to testing for local and testnet + }; + + if let Some(image_override) = &self.image_tag_override { + println!( + "Overriding docker image tag to use '{image_override}' instead of '{image_tag}'" + ); + image_tag = image_override; } - ); - println!("{stop_message}"); - Ok(()) -} + format!("{DOCKER_IMAGE}:{image_tag}") + } -fn get_container_args(cmd: &Cmd) -> Vec { - [ - format!("--{}", cmd.network), - "--enable rpc,horizon".to_string(), - get_protocol_version_arg(cmd), - get_limits_arg(cmd), - ] - .iter() - .filter(|&s| !s.is_empty()) - .cloned() - .collect() -} + fn get_container_args(&self) -> Vec { + [ + format!("--{}", self.network), + "--enable rpc,horizon".to_string(), + self.get_protocol_version_arg(), + self.get_limits_arg(), + ] + .iter() + .filter(|&s| !s.is_empty()) + .cloned() + .collect() + } + + // The port mapping in the bollard crate is formatted differently than the docker CLI. In the docker CLI, we usually specify exposed ports as `-p HOST_PORT:CONTAINER_PORT`. But with the bollard crate, it is expecting the port mapping to be a map of the container port (with the protocol) to the host port. + fn get_port_mapping(&self) -> HashMap>> { + let mut port_mapping_hash = HashMap::new(); + for port_mapping in &self.ports_mapping { + let ports_vec: Vec<&str> = port_mapping.split(':').collect(); + let from_port = ports_vec[0]; + let to_port = ports_vec[1]; + + port_mapping_hash.insert( + format!("{to_port}/tcp"), + Some(vec![PortBinding { + host_ip: None, + host_port: Some(from_port.to_string()), + }]), + ); + } -fn get_image_name(cmd: &Cmd) -> String { - // this can be overriden with the `-t` flag - let mut image_tag = match cmd.network { - Network::Pubnet => "latest", - Network::Futurenet => "future", - _ => "testing", // default to testing for local and testnet - }; - - if let Some(image_override) = &cmd.image_tag_override { - println!("Overriding docker image tag to use '{image_override}' instead of '{image_tag}'"); - image_tag = image_override; + port_mapping_hash } - format!("{DOCKER_IMAGE}:{image_tag}") -} + fn container_name(&self) -> Name { + Name(self.name.clone().unwrap_or(self.network.to_string())) + } -// The port mapping in the bollard crate is formatted differently than the docker CLI. In the docker CLI, we usually specify exposed ports as `-p HOST_PORT:CONTAINER_PORT`. But with the bollard crate, it is expecting the port mapping to be a map of the container port (with the protocol) to the host port. -fn get_port_mapping(cmd: &Cmd) -> HashMap>> { - let mut port_mapping_hash = HashMap::new(); - for port_mapping in &cmd.ports_mapping { - let ports_vec: Vec<&str> = port_mapping.split(':').collect(); - let from_port = ports_vec[0]; - let to_port = ports_vec[1]; - - port_mapping_hash.insert( - format!("{to_port}/tcp"), - Some(vec![PortBinding { - host_ip: None, - host_port: Some(from_port.to_string()), - }]), + fn print_log_message(&self) { + let log_message = format!( + "ℹ️ To see the logs for this container run: stellar network container logs {container_name} {additional_flags}", + container_name = self.container_name().get_external_container_name(), + additional_flags = self.container_args.get_additional_flags(), ); + println!("{log_message}"); } - port_mapping_hash -} + fn print_stop_message(&self) { + let stop_message = + format!( + "ℹ️ To stop this container run: stellar network container stop {container_name} {additional_flags}", + container_name = self.container_name().get_external_container_name(), + additional_flags = self.container_args.get_additional_flags(), + ); + println!("{stop_message}"); + } -fn get_protocol_version_arg(cmd: &Cmd) -> String { - if cmd.network == Network::Local && cmd.protocol_version.is_some() { - let version = cmd.protocol_version.as_ref().unwrap(); - format!("--protocol-version {version}") - } else { - String::new() + fn get_protocol_version_arg(&self) -> String { + if self.network == Network::Local && self.protocol_version.is_some() { + let version = self.protocol_version.as_ref().unwrap(); + format!("--protocol-version {version}") + } else { + String::new() + } } -} -fn get_limits_arg(cmd: &Cmd) -> String { - if cmd.network == Network::Local && cmd.limits.is_some() { - let limits = cmd.limits.as_ref().unwrap(); - format!("--limits {limits}") - } else { - String::new() + fn get_limits_arg(&self) -> String { + if self.network == Network::Local && self.limits.is_some() { + let limits = self.limits.as_ref().unwrap(); + format!("--limits {limits}") + } else { + String::new() + } } } diff --git a/cmd/soroban-cli/src/commands/network/container/stop.rs b/cmd/soroban-cli/src/commands/network/container/stop.rs index c511b5e4d..733c86436 100644 --- a/cmd/soroban-cli/src/commands/network/container/stop.rs +++ b/cmd/soroban-cli/src/commands/network/container/stop.rs @@ -1,29 +1,58 @@ -use crate::commands::network::container::shared::{ - connect_to_docker, Error as ConnectionError, Network, DOCKER_HOST_HELP, -}; +use crate::commands::network::container::shared::Error as BollardConnectionError; + +use super::shared::{Args, Name}; #[derive(thiserror::Error, Debug)] pub enum Error { - #[error("Failed to stop container: {0}")] - StopContainerError(#[from] ConnectionError), + #[error("⛔ Failed to connect to docker: {0}")] + DockerConnectionFailed(#[from] BollardConnectionError), + + #[error("⛔ Container {container_name} not found")] + ContainerNotFound { + container_name: String, + #[source] + source: bollard::errors::Error, + }, + + #[error("⛔ Failed to stop container: {0}")] + ContainerStopFailed(#[from] bollard::errors::Error), } #[derive(Debug, clap::Parser, Clone)] pub struct Cmd { - /// Network to stop - pub network: Network, + #[command(flatten)] + pub container_args: Args, - #[arg(short = 'd', long, help = DOCKER_HOST_HELP, env = "DOCKER_HOST")] - pub docker_host: Option, + /// Container to stop + pub name: String, } impl Cmd { pub async fn run(&self) -> Result<(), Error> { - let container_name = format!("stellar-{}", self.network); - let docker = connect_to_docker(&self.docker_host).await?; - println!("ℹ️ Stopping container: {container_name}"); - docker.stop_container(&container_name, None).await.unwrap(); - println!("✅ Container stopped: {container_name}"); + let container_name = Name(self.name.clone()); + let docker = self.container_args.connect_to_docker().await?; + println!( + "ℹ️ Stopping container: {}", + container_name.get_external_container_name() + ); + docker + .stop_container(&container_name.get_internal_container_name(), None) + .await + .map_err(|e| { + let msg = e.to_string(); + if msg.contains("No such container") { + Error::ContainerNotFound { + container_name: container_name.get_external_container_name(), + source: e, + } + } else { + Error::ContainerStopFailed(e) + } + })?; + println!( + "✅ Container stopped: {}", + container_name.get_external_container_name() + ); Ok(()) } } diff --git a/cmd/soroban-cli/src/commands/network/mod.rs b/cmd/soroban-cli/src/commands/network/mod.rs index 435492295..d0b4f32a3 100644 --- a/cmd/soroban-cli/src/commands/network/mod.rs +++ b/cmd/soroban-cli/src/commands/network/mod.rs @@ -1,20 +1,9 @@ -use std::str::FromStr; +use clap::Parser; -use clap::{arg, Parser}; -use phf::phf_map; -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use stellar_strkey::ed25519::PublicKey; - -use crate::{ - commands::HEADING_RPC, - rpc::{self, Client}, -}; +use crate::rpc::{self}; use super::config::locator; -pub const LOCAL_NETWORK_PASSPHRASE: &str = "Standalone Network ; February 2017"; - pub mod add; pub mod container; pub mod ls; @@ -38,7 +27,7 @@ pub enum Cmd { /// /// By default, when starting a testnet container, without any optional arguments, it will run the equivalent of the following docker command: /// - /// `docker run --rm -p 8000:8000 --name stellar stellar/quickstart:testing --testnet --enable-soroban-rpc` + /// `docker run --rm -p 8000:8000 --name stellar stellar/quickstart:testing --testnet --enable rpc,horizon` Start(container::StartCmd), /// ⚠️ Deprecated: use `stellar container stop` instead /// @@ -115,165 +104,3 @@ impl Cmd { Ok(()) } } - -#[derive(Debug, clap::Args, Clone, Default)] -#[group(skip)] -pub struct Args { - /// RPC server endpoint - #[arg( - long = "rpc-url", - requires = "network_passphrase", - required_unless_present = "network", - env = "STELLAR_RPC_URL", - help_heading = HEADING_RPC, - )] - pub rpc_url: Option, - /// Network passphrase to sign the transaction sent to the rpc server - #[arg( - long = "network-passphrase", - requires = "rpc_url", - required_unless_present = "network", - env = "STELLAR_NETWORK_PASSPHRASE", - help_heading = HEADING_RPC, - )] - pub network_passphrase: Option, - /// Name of network to use from config - #[arg( - long, - required_unless_present = "rpc_url", - env = "STELLAR_NETWORK", - help_heading = HEADING_RPC, - )] - pub network: Option, -} - -impl Args { - pub fn get(&self, locator: &locator::Args) -> Result { - if let Some(name) = self.network.as_deref() { - if let Ok(network) = locator.read_network(name) { - return Ok(network); - } - } - if let (Some(rpc_url), Some(network_passphrase)) = - (self.rpc_url.clone(), self.network_passphrase.clone()) - { - Ok(Network { - rpc_url, - network_passphrase, - }) - } else { - Err(Error::Network) - } - } -} - -#[derive(Debug, clap::Args, Serialize, Deserialize, Clone)] -#[group(skip)] -pub struct Network { - /// RPC server endpoint - #[arg( - long = "rpc-url", - env = "STELLAR_RPC_URL", - help_heading = HEADING_RPC, - )] - pub rpc_url: String, - /// Network passphrase to sign the transaction sent to the rpc server - #[arg( - long, - env = "STELLAR_NETWORK_PASSPHRASE", - help_heading = HEADING_RPC, - )] - pub network_passphrase: String, -} - -impl Network { - pub async fn helper_url(&self, addr: &str) -> Result { - use http::Uri; - tracing::debug!("address {addr:?}"); - let rpc_uri = Uri::from_str(&self.rpc_url) - .map_err(|_| Error::InvalidUrl(self.rpc_url.to_string()))?; - if self.network_passphrase.as_str() == LOCAL_NETWORK_PASSPHRASE { - let auth = rpc_uri.authority().unwrap().clone(); - let scheme = rpc_uri.scheme_str().unwrap(); - Ok(Uri::builder() - .authority(auth) - .scheme(scheme) - .path_and_query(format!("/friendbot?addr={addr}")) - .build()?) - } else { - let client = Client::new(&self.rpc_url)?; - let network = client.get_network().await?; - tracing::debug!("network {network:?}"); - let uri = client.friendbot_url().await?; - tracing::debug!("URI {uri:?}"); - Uri::from_str(&format!("{uri}?addr={addr}")).map_err(|e| { - tracing::error!("{e}"); - Error::InvalidUrl(uri.to_string()) - }) - } - } - - #[allow(clippy::similar_names)] - pub async fn fund_address(&self, addr: &PublicKey) -> Result<(), Error> { - let uri = self.helper_url(&addr.to_string()).await?; - tracing::debug!("URL {uri:?}"); - let response = match uri.scheme_str() { - Some("http") => hyper::Client::new().get(uri.clone()).await?, - Some("https") => { - let https = hyper_tls::HttpsConnector::new(); - hyper::Client::builder() - .build::<_, hyper::Body>(https) - .get(uri.clone()) - .await? - } - _ => { - return Err(Error::InvalidUrl(uri.to_string())); - } - }; - let body = hyper::body::to_bytes(response.into_body()).await?; - let res = serde_json::from_slice::(&body) - .map_err(|e| Error::FailedToParseJSON(uri.to_string(), e))?; - tracing::debug!("{res:#?}"); - if let Some(detail) = res.get("detail").and_then(Value::as_str) { - if detail.contains("createAccountAlreadyExist") { - eprintln!("Account already exists"); - } - } else if res.get("successful").is_none() { - return Err(Error::InproperResponse(res.to_string())); - } - Ok(()) - } - - pub fn rpc_uri(&self) -> Result { - http::Uri::from_str(&self.rpc_url).map_err(|_| Error::InvalidUrl(self.rpc_url.to_string())) - } -} - -pub static DEFAULTS: phf::Map<&'static str, (&'static str, &'static str)> = phf_map! { - "local" => ( - "http://localhost:8000/rpc", - "Standalone Network ; February 2017", - ), - "futurenet" => ( - "https://rpc-futurenet.stellar.org:443", - "Test SDF Future Network ; October 2022", - ), - "testnet" => ( - "https://soroban-testnet.stellar.org", - "Test SDF Network ; September 2015", - ), - "mainnet" => ( - "Bring Your Own: https://developers.stellar.org/docs/data/rpc/rpc-providers", - "Public Global Stellar Network ; September 2015", - ), -}; - -impl From<&(&str, &str)> for Network { - /// Convert the return value of `DEFAULTS.get()` into a Network - fn from(n: &(&str, &str)) -> Self { - Self { - rpc_url: n.0.to_string(), - network_passphrase: n.1.to_string(), - } - } -} diff --git a/cmd/soroban-cli/src/commands/tx/hash.rs b/cmd/soroban-cli/src/commands/tx/hash.rs new file mode 100644 index 000000000..8d8ec6d82 --- /dev/null +++ b/cmd/soroban-cli/src/commands/tx/hash.rs @@ -0,0 +1,34 @@ +use hex; + +use crate::{commands::global, config::network, utils::transaction_hash}; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error(transparent)] + TxEnvelopeFromStdin(#[from] super::xdr::Error), + #[error(transparent)] + XdrToBase64(#[from] soroban_env_host::xdr::Error), + #[error(transparent)] + Config(#[from] network::Error), +} + +// Command to return the transaction hash submitted to a network +/// e.g. `cat file.txt | soroban tx hash` +#[derive(Debug, clap::Parser, Clone, Default)] +#[group(skip)] +pub struct Cmd { + #[clap(flatten)] + pub network: network::Args, +} + +impl Cmd { + pub fn run(&self, global_args: &global::Args) -> Result<(), Error> { + let tx = super::xdr::unwrap_envelope_v1(super::xdr::tx_envelope_from_stdin()?)?; + let network = &self.network.get(&global_args.locator)?; + println!( + "{}", + hex::encode(transaction_hash(&tx, &network.network_passphrase)?) + ); + Ok(()) + } +} diff --git a/cmd/soroban-cli/src/commands/tx/mod.rs b/cmd/soroban-cli/src/commands/tx/mod.rs index 48161e3c7..59f07228a 100644 --- a/cmd/soroban-cli/src/commands/tx/mod.rs +++ b/cmd/soroban-cli/src/commands/tx/mod.rs @@ -2,6 +2,7 @@ use clap::Parser; use super::global; +pub mod hash; pub mod simulate; pub mod xdr; @@ -9,6 +10,8 @@ pub mod xdr; pub enum Cmd { /// Simulate a transaction envelope from stdin Simulate(simulate::Cmd), + /// Calculate the hash of a transaction envelope from stdin + Hash(hash::Cmd), } #[derive(thiserror::Error, Debug)] @@ -16,12 +19,16 @@ pub enum Error { /// An error during the simulation #[error(transparent)] Simulate(#[from] simulate::Error), + /// An error during hash calculation + #[error(transparent)] + Hash(#[from] hash::Error), } impl Cmd { pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> { match self { Cmd::Simulate(cmd) => cmd.run(global_args).await?, + Cmd::Hash(cmd) => cmd.run(global_args)?, }; Ok(()) } diff --git a/cmd/soroban-cli/src/commands/config/alias.rs b/cmd/soroban-cli/src/config/alias.rs similarity index 100% rename from cmd/soroban-cli/src/commands/config/alias.rs rename to cmd/soroban-cli/src/config/alias.rs diff --git a/cmd/soroban-cli/src/commands/config/data.rs b/cmd/soroban-cli/src/config/data.rs similarity index 100% rename from cmd/soroban-cli/src/commands/config/data.rs rename to cmd/soroban-cli/src/config/data.rs diff --git a/cmd/soroban-cli/src/commands/config/locator.rs b/cmd/soroban-cli/src/config/locator.rs similarity index 99% rename from cmd/soroban-cli/src/commands/config/locator.rs rename to cmd/soroban-cli/src/config/locator.rs index 5dc4dae85..79c72f275 100644 --- a/cmd/soroban-cli/src/commands/config/locator.rs +++ b/cmd/soroban-cli/src/config/locator.rs @@ -63,7 +63,7 @@ pub enum Error { #[error(transparent)] String(#[from] std::string::FromUtf8Error), #[error(transparent)] - Secret(#[from] crate::commands::config::secret::Error), + Secret(#[from] crate::config::secret::Error), #[error(transparent)] Json(#[from] serde_json::Error), #[error("cannot access config dir for alias file")] @@ -80,7 +80,7 @@ pub struct Args { pub global: bool, /// Location of config directory, default is "." - #[arg(long, help_heading = "TESTING_OPTIONS")] + #[arg(long)] pub config_dir: Option, } diff --git a/cmd/soroban-cli/src/commands/config/mod.rs b/cmd/soroban-cli/src/config/mod.rs similarity index 97% rename from cmd/soroban-cli/src/commands/config/mod.rs rename to cmd/soroban-cli/src/config/mod.rs index bb55103c1..3632d7e31 100644 --- a/cmd/soroban-cli/src/commands/config/mod.rs +++ b/cmd/soroban-cli/src/config/mod.rs @@ -13,17 +13,14 @@ use crate::{ use self::{network::Network, secret::Secret}; -use super::{keys, network}; - pub mod alias; pub mod data; pub mod locator; +pub mod network; pub mod secret; #[derive(thiserror::Error, Debug)] pub enum Error { - #[error(transparent)] - Identity(#[from] keys::Error), #[error(transparent)] Network(#[from] network::Error), #[error(transparent)] diff --git a/cmd/soroban-cli/src/config/network.rs b/cmd/soroban-cli/src/config/network.rs new file mode 100644 index 000000000..ba63c016f --- /dev/null +++ b/cmd/soroban-cli/src/config/network.rs @@ -0,0 +1,200 @@ +use std::str::FromStr; + +use clap::arg; +use phf::phf_map; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use stellar_strkey::ed25519::PublicKey; + +use crate::{ + commands::HEADING_RPC, + rpc::{self, Client}, +}; + +use super::locator; +pub mod passphrase; + +#[derive(thiserror::Error, Debug)] +pub enum Error { + #[error(transparent)] + Config(#[from] locator::Error), + + #[error("network arg or rpc url and network passphrase are required if using the network")] + Network, + #[error(transparent)] + Http(#[from] http::Error), + #[error(transparent)] + Rpc(#[from] rpc::Error), + #[error(transparent)] + Hyper(#[from] hyper::Error), + #[error("Failed to parse JSON from {0}, {1}")] + FailedToParseJSON(String, serde_json::Error), + #[error("Invalid URL {0}")] + InvalidUrl(String), + #[error("Inproper response {0}")] + InproperResponse(String), + #[error("Currently not supported on windows. Please visit:\n{0}")] + WindowsNotSupported(String), +} + +#[derive(Debug, clap::Args, Clone, Default)] +#[group(skip)] +pub struct Args { + /// RPC server endpoint + #[arg( + long = "rpc-url", + requires = "network_passphrase", + required_unless_present = "network", + env = "STELLAR_RPC_URL", + help_heading = HEADING_RPC, + )] + pub rpc_url: Option, + /// Network passphrase to sign the transaction sent to the rpc server + #[arg( + long = "network-passphrase", + requires = "rpc_url", + required_unless_present = "network", + env = "STELLAR_NETWORK_PASSPHRASE", + help_heading = HEADING_RPC, + )] + pub network_passphrase: Option, + /// Name of network to use from config + #[arg( + long, + required_unless_present = "rpc_url", + env = "STELLAR_NETWORK", + help_heading = HEADING_RPC, + )] + pub network: Option, +} + +impl Args { + pub fn get(&self, locator: &locator::Args) -> Result { + if let Some(name) = self.network.as_deref() { + if let Ok(network) = locator.read_network(name) { + return Ok(network); + } + } + if let (Some(rpc_url), Some(network_passphrase)) = + (self.rpc_url.clone(), self.network_passphrase.clone()) + { + Ok(Network { + rpc_url, + network_passphrase, + }) + } else { + Err(Error::Network) + } + } +} + +#[derive(Debug, clap::Args, Serialize, Deserialize, Clone)] +#[group(skip)] +pub struct Network { + /// RPC server endpoint + #[arg( + long = "rpc-url", + env = "STELLAR_RPC_URL", + help_heading = HEADING_RPC, + )] + pub rpc_url: String, + /// Network passphrase to sign the transaction sent to the rpc server + #[arg( + long, + env = "STELLAR_NETWORK_PASSPHRASE", + help_heading = HEADING_RPC, + )] + pub network_passphrase: String, +} + +impl Network { + pub async fn helper_url(&self, addr: &str) -> Result { + use http::Uri; + tracing::debug!("address {addr:?}"); + let rpc_uri = Uri::from_str(&self.rpc_url) + .map_err(|_| Error::InvalidUrl(self.rpc_url.to_string()))?; + if self.network_passphrase.as_str() == passphrase::LOCAL { + let auth = rpc_uri.authority().unwrap().clone(); + let scheme = rpc_uri.scheme_str().unwrap(); + Ok(Uri::builder() + .authority(auth) + .scheme(scheme) + .path_and_query(format!("/friendbot?addr={addr}")) + .build()?) + } else { + let client = Client::new(&self.rpc_url)?; + let network = client.get_network().await?; + tracing::debug!("network {network:?}"); + let uri = client.friendbot_url().await?; + tracing::debug!("URI {uri:?}"); + Uri::from_str(&format!("{uri}?addr={addr}")).map_err(|e| { + tracing::error!("{e}"); + Error::InvalidUrl(uri.to_string()) + }) + } + } + + #[allow(clippy::similar_names)] + pub async fn fund_address(&self, addr: &PublicKey) -> Result<(), Error> { + let uri = self.helper_url(&addr.to_string()).await?; + tracing::debug!("URL {uri:?}"); + let response = match uri.scheme_str() { + Some("http") => hyper::Client::new().get(uri.clone()).await?, + Some("https") => { + let https = hyper_tls::HttpsConnector::new(); + hyper::Client::builder() + .build::<_, hyper::Body>(https) + .get(uri.clone()) + .await? + } + _ => { + return Err(Error::InvalidUrl(uri.to_string())); + } + }; + let body = hyper::body::to_bytes(response.into_body()).await?; + let res = serde_json::from_slice::(&body) + .map_err(|e| Error::FailedToParseJSON(uri.to_string(), e))?; + tracing::debug!("{res:#?}"); + if let Some(detail) = res.get("detail").and_then(Value::as_str) { + if detail.contains("createAccountAlreadyExist") { + eprintln!("Account already exists"); + } + } else if res.get("successful").is_none() { + return Err(Error::InproperResponse(res.to_string())); + } + Ok(()) + } + + pub fn rpc_uri(&self) -> Result { + http::Uri::from_str(&self.rpc_url).map_err(|_| Error::InvalidUrl(self.rpc_url.to_string())) + } +} + +pub static DEFAULTS: phf::Map<&'static str, (&'static str, &'static str)> = phf_map! { + "local" => ( + "http://localhost:8000/rpc", + passphrase::LOCAL, + ), + "futurenet" => ( + "https://rpc-futurenet.stellar.org:443", + passphrase::FUTURENET, + ), + "testnet" => ( + "https://soroban-testnet.stellar.org", + passphrase::TESTNET, + ), + "mainnet" => ( + "Bring Your Own: https://developers.stellar.org/docs/data/rpc/rpc-providers", + passphrase::MAINNET, + ), +}; + +impl From<&(&str, &str)> for Network { + /// Convert the return value of `DEFAULTS.get()` into a Network + fn from(n: &(&str, &str)) -> Self { + Self { + rpc_url: n.0.to_string(), + network_passphrase: n.1.to_string(), + } + } +} diff --git a/cmd/soroban-cli/src/config/network/passphrase.rs b/cmd/soroban-cli/src/config/network/passphrase.rs new file mode 100644 index 000000000..bdb534cdb --- /dev/null +++ b/cmd/soroban-cli/src/config/network/passphrase.rs @@ -0,0 +1,4 @@ +pub const LOCAL: &str = "Standalone Network ; February 2017"; +pub const TESTNET: &str = "Test SDF Network ; September 2015"; +pub const FUTURENET: &str = "Test SDF Future Network ; October 2022"; +pub const MAINNET: &str = "Public Global Stellar Network ; September 2015"; diff --git a/cmd/soroban-cli/src/commands/config/secret.rs b/cmd/soroban-cli/src/config/secret.rs similarity index 100% rename from cmd/soroban-cli/src/commands/config/secret.rs rename to cmd/soroban-cli/src/config/secret.rs diff --git a/cmd/soroban-cli/src/get_spec.rs b/cmd/soroban-cli/src/get_spec.rs index e21722794..cd9477de2 100644 --- a/cmd/soroban-cli/src/get_spec.rs +++ b/cmd/soroban-cli/src/get_spec.rs @@ -7,9 +7,8 @@ use soroban_env_host::xdr::{ use soroban_spec::read::FromWasmError; pub use soroban_spec_tools::contract as contract_spec; -use crate::commands::config::{self, locator}; -use crate::commands::network; -use crate::commands::{config::data, global}; +use crate::commands::global; +use crate::config::{self, data, locator, network}; use crate::rpc; #[derive(thiserror::Error, Debug)] diff --git a/cmd/soroban-cli/src/key.rs b/cmd/soroban-cli/src/key.rs index 7a0ccb844..885295936 100644 --- a/cmd/soroban-cli/src/key.rs +++ b/cmd/soroban-cli/src/key.rs @@ -56,7 +56,7 @@ pub struct Args { )] pub wasm_hash: Option, /// Storage entry durability - #[arg(long, value_enum, required = true, default_value = "persistent")] + #[arg(long, value_enum, default_value = "persistent")] pub durability: Durability, } diff --git a/cmd/soroban-cli/src/lib.rs b/cmd/soroban-cli/src/lib.rs index f71ce55e2..0268a40e0 100644 --- a/cmd/soroban-cli/src/lib.rs +++ b/cmd/soroban-cli/src/lib.rs @@ -12,6 +12,7 @@ mod cli; pub use cli::main; pub mod commands; +pub mod config; pub mod fee; pub mod get_spec; pub mod key;