2. Deploy to Testnet
To recap what we've done so far, in Setup:
- we set up our local environment to write Rust smart contracts
- installed the stellar-cli
- configured the stellar-cli to communicate with the Stellar Testnet via RPC
- and configured an identity to sign transactions
In Hello World we created a hello-world project, and learned how to test and build the HelloWorld contract. Now we are ready to deploy that contract to Testnet, and interact with it.
Configure a Source Account
When you deploy a smart contract to a network, you need to specify a source account's keypair that will be used to sign the transactions.
Let's generate a keypair called alice. You can use any name you want, but it might be nice to have some named keys that you can use for testing, such as alice, bob, and carol. Notice that the keypair's account will be funded using Friendbot.
stellar keys generate alice --network testnet --fund
You can see the public key of alice with:
stellar keys address alice
You can see all of the keys you generated, along with where on your filesystem their information is stored, with:
stellar keys ls -l
Deploy
To deploy your HelloWorld contract, run the following command:
- macOS/Linux
- Windows (PowerShell)
stellar contract deploy \
--wasm target/wasm32v1-none/release/hello_world.wasm \
--source-account alice \
--network testnet \
--alias hello_world
stellar contract deploy `
--wasm target/wasm32v1-none/release/hello_world.wasm `
--source-account alice `
--network testnet `
--alias hello_world
This returns the contract's id, starting with a C. In this example, we're going to use CACDYF3CYMJEJTIVFESQYZTN67GO2R5D5IUABTCUG3HXQSRXCSOROBAN, so replace it with your actual contract id.
We used the --alias flag in this deploy command which will create a .stellar/contract-ids/hello_world.json file that maps the alias hello_world to the contract id and network. This allows us to refer to this contract as its alias instead the contract id.
Interact
Using the code we wrote in Write a Contract and the resulting .wasm file we built in Build, run the following command to invoke the hello function.
In the background, the CLI is making RPC calls. For information on that checkout out the RPC reference page.
- macOS/Linux
- Windows (PowerShell)
stellar contract invoke \
--id CACDYF3CYMJEJTIVFESQYZTN67GO2R5D5IUABTCUG3HXQSRXCSOROBAN \
--source-account alice \
--network testnet \
-- \
hello \
--to RPC
stellar contract invoke `
--id CACDYF3CYMJEJTIVFESQYZTN67GO2R5D5IUABTCUG3HXQSRXCSOROBAN `
--source-account alice `
--network testnet `
-- `
hello `
--to RPC
The following output should appear.
["Hello", "RPC"]
The -- double-dash is required!
This is a general CLI pattern used by other commands like cargo run. Everything after the --, sometimes called slop, is passed to a child process. In this case, stellar contract invoke builds an implicit CLI on-the-fly for the hello method in your contract. It can do this because Soroban SDK embeds your contract's schema / interface types right in the .wasm file that gets deployed on-chain. You can also try:
stellar contract invoke ... -- --help
and
stellar contract invoke ... -- hello --help
Summary
In this lesson, we learned how to:
- deploy a contract to Testnet
- interact with a deployed contract
Next we'll add a new contract to this project, and see how our workspace can accommodate a multi-contract project. The new contract will show off a little bit of Soroban's storage capabilities.