Skip to main content

Install WebAssembly (Wasm) bytecode using code

This process uploads the contract code to the Stellar Testnet in a transaction, the uploaded Wasm blob is a contract source, which can be thought of as a ‘class’ of a contract. Multiple instances of a contract can be deployed which share the same source, but have their own state.

Prerequisites

Before you begin, ensure you have the following installed to compile a smart contract:
  1. Rust and Cargo (for compiling smart contracts)
  2. The Stellar CLI

Initialize and build a sample Rust contract

We are initializing the default hello_world contract in soroban_examples to the directory hello-world. You may use the -w option to create with another example like account. After building the contract for release, the generated .wasm file is at the path hello-word/target/wasm32v1-none/release/hello_world.wasm

Upload Wasm to the Stellar blockchain

We can use one of the client SDKs, install necessary dependencies for your chosen programming language: Create a new directory for your project and navigate into it:

Running the install script

Different programming languages can be used to upload the .wasm file, its SHA256 hash is used for deploying the contract.
This guide will walk you through installing the Wasm of the contract using the JavaScript SDK: js-stellar-sdk.Create a new Node.js project with a JavaScript file, and install necessary dependencies:
Run the script with node index.js, it reads the Wasm file, gets account details, and uploads the contract in a transaction:
Replace "Your_Secret_Key" with your actual secret key.
The Stellar CLI can be used to generate identities, e.g. hello, and show its secret key.
Ensure that you handle secret and private keys securely in production environments and never expose them in your code repositories.
The returned upload transaction hash can be viewed with online tools: stellar.expert/explorer/testnet/tx/cef7a63667fe5b0ddcde5562d90e0a40bc04c69616916d1d7fa74a8571bbd82f Uploaded contracts are stored in ContractCodeEntry ledger entries. These entries are keyed by the hash of the Wasm used to upload them. You may use the Wasm hash for deployment with the Stellar CLI:
View the deployed contract using the returned identifier with online tools: stellar.expert/explorer/testnet/contract/CC6NAQE3ZHRQV3NPQB3F3NYEFBAMEABA4KQTM6A2V5V7PBR5H3UEU3MW