DEV Community

darkvallen
darkvallen

Posted on • Updated on

Deploying and Invoking Soroban Smart Contract using Soroban Python SDK

In my previous post, I showed you how to set up the Soroban Python SDK on Windows. In this post, we'll use the SDK to deploy and invoke Soroban smart contracts on the Futurenet network.

Preparation

Before we begin, make sure you've downloaded the following files from my GitHub repository:

soroban_deploy_contract.py - Python Script to Deploy Contract on Soroban Futurenet Network
soroban_invoke_contract_function.py - Python Script to Invoke Contract on Soroban Futurenet Network
soroban_hello_world_contract.wasm - Soroban Hello World Smart Contract

Scripts & Contract
You can download these files here,you can use any other contract file,but in this case i'm using Soroban Hello World Smart Contract from here . Additionally, make sure you've created a Futurenet account at Stellar Laboratory and funded it.
Create Account

Deploying a Contract

To deploy a contract, open the soroban_deploy_contract.py file and adjust the following parameters:

secret = "S---" #Put your Account Secret Key here
rpc_server_url = "https://horizon-futurenet.stellar.cash:443/soroban/rpc"
network_passphrase = Network.FUTURENET_NETWORK_PASSPHRASE
contract_file_path = "./soroban_hello_world_contract.wasm" 
Enter fullscreen mode Exit fullscreen mode

secret: the Secret Key of the Account that we're gonna use to deploy the Smart Contract. Replace the value with your own account secret key.
rpc_server_url: the URL of the RPC server you'll use to interact with the Soroban Futurenet network.
network_passphrase: the passphrase you'll use to select the network. This can be changed depending on the network you're interacting with; in this case, we'll use Futurenet.
contract_file_path: the path to the smart contract file you want to deploy. Replace the value with your own contract file path, or use the soroban_hello_world_contract.wasm file included in the GitHub repository.

Save the soroban_deploy_contract.py file with your changes and then run the script in a command prompt using this command :

python soroban_deploy_contract.py
Enter fullscreen mode Exit fullscreen mode

Contract Deployed
Once the script has finished running, you'll receive a contract ID. Be sure to save this for later.

contract id: 74a28bbf401e0ed625921dc08b565b2179dba548c905594ed4de2565a3b02e78
Enter fullscreen mode Exit fullscreen mode

Invoking a Contract

To invoke a contract, open the soroban_invoke_contract_function.py file and adjust the following parameters:

secret = "SDMVWEWUZVYDACEM3G2X2XT4W4BP3OSZHX3KXVOQTJMDIUQQLKAIWK7Z"
rpc_server_url = "https://horizon-futurenet.stellar.cash:443/soroban/rpc"
contract_id = "940985b099a50abf77b8e3a245471d1912f5c1d8502da07128619aa778d90c1f"
network_passphrase = Network.FUTURENET_NETWORK_PASSPHRASE
Enter fullscreen mode Exit fullscreen mode

secret: the secret key of the account you'll use to invoke the smart contract. Replace the value with your own account secret key.

contract_id: the contract ID of the contract you want to invoke. Replace the value with the contract ID you received from deploying contract before.

tx = (
    TransactionBuilder(source, network_passphrase)
    .set_timeout(300)
    .append_invoke_contract_function_op(
        contract_id=contract_id,
        function_name="hello",
        parameters=[Symbol("World")],
        source=kp.public_key,
    )
    .build()
)
Enter fullscreen mode Exit fullscreen mode

function_name: the name of the function you want to invoke.
parameters: any arguments needed by the function.

Save the soroban_invoke_contract_function.py file with your changes and then run the script in a command prompt using this command :

python soroban_invoke_contract_function.py
Enter fullscreen mode Exit fullscreen mode

Invoke Contract
Once the script has finished running, you'll receive a response with the result :

transaction result: ['Hello', 'World']
Enter fullscreen mode Exit fullscreen mode

Closing

In this blog post, we have gone over the steps to Deploy and Invoke Soroban Smart Contract using Soroban Python SDK. You can deploy or invoke other smart contracts by modifying the scripts.

Top comments (0)