Invoke Soroban Smart Contracts on Stellar Testnet
This article explains how to use one of the Soroban provided examples
We’ll Deploy and Invoke Soroban Smart Contracts where one contract calls another.
TL;DR: Contract B’s add_with
function invokes Contract A’s add
function to do some addition.
**Ok, so I used chatGPT to help write this, but the code has been tested by a human and the output was from the actual contract!
Guess that is a rarity?!
https://developers.stellar.org/docs/build/smart-contracts/example-contracts/cross-contract-call
my version : https://github.com/RGGH/cross_contract/
If you want to see how to deploy and invoke, take a look at the README.md file and take note of the deployed contract ID’s – make sure you get them the right way round! 😊
I had a bit of trouble with the repo example from Soroban, but it was due to (my) paths for the contract import.
Introduction
We’ll walk through the process of deploying and invoking Soroban smart contracts on the Stellar testnet using the stellar-cli
tool. We’ll demonstrate how to deploy two contracts, Contract A and Contract B, and then invoke a function in Contract B that interacts with Contract A.
Prerequisites:
- Stellar-CLI installed on your machine.
- Two Soroban smart contracts (Contract A and Contract B) written and compiled into WebAssembly (WASM) files.
- A Stellar testnet account (
alice
in this example).
Step 1: Deploy Contract A
The first step is to deploy Contract A. Use the following command (make sure you use your specific source account name, if you’re anything other than Alice!)
stellar contract deploy \
--wasm target/wasm32-unknown-unknown/release/soroban_cross_contract_a_contract.wasm \
--source-account alice \
--network testnet
Expected Output: Once the contract is successfully deployed, you should see the following confirmation message along with the contract ID:
Contract ID: CAMKBQ7M7APPDJQJKDY7JBW3XE4A3O2UUP54SVLJYRD5MDC5CTKCP4CM
Step 2: Deploy Contract B
Now, deploy Contract B using a similar command:
stellar contract deploy \
--wasm target/wasm32-unknown-unknown/release/soroban_cross_contract_b_contract.wasm \
--source-account alice \
--network testnet
Expected Output: Once the deployment is complete, you’ll receive a confirmation message with the contract ID for Contract B:
Contract ID: CB4MW5VDJEGB65MP5LLSDVFHJVTEXVKSHRPI6DBAIQP4IIPSXJM2FOML
Step 3: Invoke Contract B’s add_with
Function
To invoke the add_with
function in Contract B, which interacts with Contract A, use the following command:
stellar contract invoke \
--id CB4MW5VDJEGB65MP5LLSDVFHJVTEXVKSHRPI6DBAIQP4IIPSXJM2FOML \
--network testnet \
--source-account alice \
--send=yes \
-- \
add_with \
--contract CAMKBQ7M7APPDJQJKDY7JBW3XE4A3O2UUP54SVLJYRD5MDC5CTKCP4CM \
--x 5 \
--y 7
Expected Output: After invoking the function, you should receive the following result:
12
This result confirms that Contract B successfully invoked Contract A’s functionality and computed the sum of the two numbers (5 and 7).
Conclusion
Deploy and Invoke Soroban Smart Contracts:
By following these steps, we’ve successfully deployed two Soroban smart contracts on the Stellar testnet and invoked a function in one contract that interacts with another. The stellar-cli
tool makes it fairly easy to manage smart contract deployments and invocations.
For more advanced use cases, refer to the Soroban documentation for further details on how to build, deploy, and interact with smart contracts on the Stellar network.
Understanding the Stellar Contract Invoke Command
Why is there a space before increment, (after the 2x “–“)
Example Command
Here’s an example of the stellar contract invoke
command:
stellar contract invoke \
--source-account alice \
--id CDDLN5OBF7KZHK5RA675CAOT7BJL6L4KAHSBYZGALOKNKEELOSEDAOW6 \
--network testnet \
-- increment \
--incr 4
Let’s explore each part of the command in detail.
Breaking Down the Command
1. stellar contract invoke
This is the base command to interact with a contract on the Stellar blockchain. The invoke
subcommand is used to execute a specific function or operation defined in the contract.
2. --source-account
The --source-account
flag specifies the account initiating the transaction. In this example, alice
is the source account. Ensure that this account has sufficient funds and permissions to execute the contract.
3. --id
The --id
flag identifies the contract you want to interact with. It takes the form of a unique identifier (contract ID), such as:
CDDLN5OBF7KZHK5RA675CAOT7BJL6L4KAHSBYZGALOKNKEELOSEDAOW6
This ID is typically generated when the contract is deployed.
4. --network
The --network
flag specifies which Stellar network to use. Options include:
testnet
: For testing purposes, transactions do not affect the main Stellar blockchain.mainnet
: For live transactions on the production blockchain.
In the example, testnet
is used for safe testing.
5. --
The --
separator indicates the end of flags and options and the start of positional arguments. This is critical when the positional arguments could be interpreted as options or flags themselves.
For example, increment
is the name of the contract function being invoked. Placing it after --
ensures the CLI interprets it as a function name rather than a flag.
6. increment
This is the name of the function or method being invoked in the contract. Contracts often expose multiple functions, and you must specify the one you wish to call.
7. --incr
The --incr
flag provides an argument to the increment
function. In this case, the argument is 4
, which might indicate the number by which a counter is incremented. Arguments and their flags will vary based on the contract’s design.
Why Use --
?
The --
separator plays a vital role in preventing conflicts between options and positional arguments. Without it, the CLI might misinterpret increment
as another flag or option. Using --
ensures the command behaves as expected, particularly when arguments resemble option names.
Tips for Using the Command
- Validate Your Environment: Ensure you’re on the correct network (testnet or mainnet) to avoid unintended transactions.
- Verify Contract ID: Double-check the contract ID to ensure you’re interacting with the intended contract.
- Understand Function Parameters: Familiarize yourself with the contract’s ABI (Application Binary Interface) to know which functions are available and what parameters they accept.
- Test First: Use the
testnet
environment to test your commands before deploying them on themainnet
.
Tips for deploying and invoking using “alias”
see : https://developers.stellar.org/meetings/2024/09/12
The UNIX way :
> id
> stellar contract deploy --source-account alice --wasm /home/dev/rust/jan2/target/wasm32-unknown-unknown/release/increment.wasm --network testnet > id
ℹ️ Skipping install because wasm already installed
ℹ️ Using wasm hash 6ca5f6b83c3f105b92c73bd7e954f5f3d75b698321c6cd516aedd0a15bdf4186
ℹ️ Simulating deploy transaction…
🌎 Submitting deploy transaction…
ℹ️ Transaction hash is 47be718bb9f77dd366f8991abcac10eaefefc7614e5b56edd4ebfbf937132d41
🔗 https://stellar.expert/explorer/testnet/tx/47be718bb9f77dd366f8991abcac10eaefefc7614e5b56edd4ebfbf937132d41
ℹ️ Signing transaction: 47be718bb9f77dd366f8991abcac10eaefefc7614e5b56edd4ebfbf937132d41
🔗 https://stellar.expert/explorer/testnet/contract/CDF5AQSJP7XENGSSKFKXMLUFQO24T3C67PSOXTYBLBFP4J6MO4TMHW5A
✅ Deployed!
> cat id
CDF5AQSJP7XENGSSKFKXMLUFQO24T3C67PSOXTYBLBFP4J6MO4TMHW5A
$(cat id)
> stellar contract invoke --source-account alice --id $(cat id) --network testnet -- increment --incr 4
ℹ️ Signing transaction: e305976ec2f9d55a14ded7d05be2053853a9a5df88ce6dfe79f75cbd8d8332c2
2025-01-02T10:01:09.608700Z INFO contract_event: soroban_cli::log::event: 1: AAAAAQAAAAAAAAABy9BCSX/uRppSUVV2LoWDtcnsXvvk688BWEr+J8x3JsMAAAABAAAAAAAAAAIAAAADAAAABAAAAA4AAAAJaW5jcmVhc2VkAAAAAAAAAwAAAAQ= {"in_successful_contract_call":true,"event":{"ext":"v0","contract_id":"cbd042497fee469a525155762e8583b5c9ec5efbe4ebcf01584afe27cc7726c3","type_":"contract","body":{"v0":{"topics":[{"u32":4},{"string":"increased"}],"data":{"u32":4}}}}}
create an alias instead
> stellar contract deploy --source-account alice --wasm /home/dev/rust/jan2/target/wasm32-unknown-unknown/release/increment.wasm --network testnet --alias hello
ℹ️ Skipping install because wasm already installed
ℹ️ Using wasm hash 6ca5f6b83c3f105b92c73bd7e954f5f3d75b698321c6cd516aedd0a15bdf4186
ℹ️ Simulating deploy transaction…
🌎 Submitting deploy transaction…
ℹ️ Transaction hash is ab5a3631f358774ab4ea94032cca13a0af3a7aa727dad40e315990e5a9c51e71
🔗 https://stellar.expert/explorer/testnet/tx/ab5a3631f358774ab4ea94032cca13a0af3a7aa727dad40e315990e5a9c51e71
ℹ️ Signing transaction: ab5a3631f358774ab4ea94032cca13a0af3a7aa727dad40e315990e5a9c51e71
🔗 https://stellar.expert/explorer/testnet/contract/CBC3T43XBMWAP673MWD24FWJHPH7FCMHVGW77IOTXL2ZJQ66FYOYCMA3
✅ Deployed!
CBC3T43XBMWAP673MWD24FWJHPH7FCMHVGW77IOTXL2ZJQ66FYOYCMA3
> stellar contract invoke --source-account alice --id hello --network testnet -- increment --incr 4
ℹ️ Signing transaction: 174a74794959e961f5516ca3266c514ca93193ab6a1eb4ff72360cfd1d003635
2025-01-02T10:16:45.988341Z INFO contract_event: soroban_cli::log::event: 1: AAAAAQAAAAAAAAABRbnzdwssB/v7ZYeuFsk7z/KJh6mt/6HTuvWUw94uHYEAAAABAAAAAAAAAAIAAAADAAAABAAAAA4AAAAJaW5jcmVhc2VkAAAAAAAAAwAAAAQ= {"in_successful_contract_call":true,"event":{"ext":"v0","contract_id":"45b9f3770b2c07fbfb6587ae16c93bcff28987a9adffa1d3baf594c3de2e1d81","type_":"contract","body":{"v0":{"topics":[{"u32":4},{"string":"increased"}],"data":{"u32":4}}}}}
Conclusion
The stellar contract invoke
command is a powerful way to interact with smart contracts on the Stellar blockchain.
If you want to get started without the set up, try Okashi.dev as it will save you doing a lot of the deploy/invoke parts and allow you to just test the logic of your smart contract.
thanks for reading