Skip to main content
This guide will walk you through the process of installing and deploying a smart contract using js-stellar-sdk. We will cover the setup of a sample Rust contract, creating a Node.js project to manage the deployment, and finally, installing the Wasm of the contract and deploying it to the network

Prerequisites

Before you begin, ensure you have the following installed:
  1. Rust and Cargo (for compiling smart contracts)
  2. Node.js and npm (for running JavaScript deployment scripts)
  3. Stellar CLI

Initialize a sample Rust Contract

This sequence of commands creates a new directory for your project, initializes a new Soroban smart contract within that directory, and builds the contract. The build generates .wasm file in the path hello-world/target/wasm32v1-none/release/hello_world.wasm

Create a Node Project

  1. Create a new directory for your Node.js project and navigate into it:
  1. Initialize a new Node.js project and install necessary dependencies:

Set Up Deployment Scripts

Imports

Import necessary modules in your JavaScript file (save it as deploy.mjs to enable ES module imports):

Installing the Wasm on the network

Create a function to upload the compiled Wasm file:
This function reads the compiled Wasm file, retrieves account details from the network, and installs the bytecode using uploadContractWasm Stellar operation, which when wrapped in a transaction is sent to the network.

Deploy contract

Deploy the contract by referencing the Wasm hash:
This function uses the Wasm hash to deploy the contract using the createCustomContract stellar operation, which when wrapped in a transaction is sent to the network, generating a contract address.

Building, Signing and Sending the Transaction

Handle the building, signing, and sending of transactions:
This function constructs a transaction, signs it, and submits it to the network, handling any necessary retries for transaction confirmation.

Running the Script

Execute the deployment script:
Replace “Your_Secret_Key” with your actual secret key. This script initiates the upload of the Wasm file and deploys the contract, resulting in a contract address where the contract is deployed. This is just demo code, so ensure that you handle secrets and private keys securely in production environments and never expose them in your code repositories. This guide should provide you with a clear path to installing and deploying your smart contracts using Javascript code.

Complete Script

Here are all the snippets stacked together in a single file for convenience: