DEV Community

Peter
Peter

Posted on • Edited on • Originally published at soroban-snippet.vercel.app

Soroban Snippets: Guide to Soroban Smart Contracts for Web3 DApps

1. Installing Contract/WASM to Soroban

One of the first things to do when needing to deploy a Soroban contract is to deploy its .wasm file on the Soroban blockchain. The following tutorial guides developers through essential steps for ensuring their smart contracts are deployment-ready on Soroban. Notably, it distinguishes between installing and deploying a smart contract, shedding light on the former. The document introduces the use soroban-client tool and delves into the critical role of hostFunctionTypeUploadContractWasm in JavaScript. Practical usage is demonstrated through code snippets, explaining the interaction with the Soroban blockchain, transaction building, and signing process with Freighter. Overall, it stands as a valuable resource for developers navigating the intricacies of smart contract installation on the Soroban blockchain in a web environment.

Installing Contract/WASM to Soroban

2. Soroban Contract Deployment

Having successfully deployed a contract WASM on Soroban, the next step involves creating a contract instance. This tutorial explores the deployment process, emphasizing the utilization of the soroban-client js library to contract deployment on Soroban. It focuses on key elements such as the creation of a contract ID preimage and the construction of contract arguments. The guides will help web3 developers through the process of building a host function transaction. To enhance practical understanding there is also a working demo allowing users to interactively deploy a Soroban token, equipping developers with the knowledge and tools required for proficient Soroban smart contract deployment in JavaScript and TypeScript.

Soroban Contract Deployment

3. Execute Soroban Smart Contract

There are several approaches to invoking methods of a Soroban contract, and this demonstration introduces the capabilities of the Contract class in the soroban-client library. With interactive examples, it provides insights into seamless communication with smart contracts. Similar to the token contract discussed in Part 2, we'll implement a TypeScript code snippet that demonstrates the invocation process and transaction building using the contract.call() method for Soroban. This guide aims to showcase the practical application of the Contract class, enabling developers to effectively interact with and execute transactions on Soroban smart contracts.

Execute Soroban Smart Contract

4. Retrieve WebAssembly (WASM) Code from Smart Contract

You can do many things in your web application with the soroban-client. One of which is retrieving the WebAssembly (Wasm) code from a Soroban contract. Beyond gaining insights into the contract, its modules, and imports/exports, developers can leverage this feature for deploying an identical contract to Soroban without the need for recompilation or for inspecting the contract code. The contractData and contractCode steps outline a streamlined two-step procedure for obtaining the Wasm file associated with a Soroban smart contract. This entails querying the Soroban blockchain to retrieve the Wasm ID and subsequently using that ID to fetch the corresponding Wasm file. Soroban Snippet introduces a comprehensive function, elucidated in the code below, that facilitates the retrieval of the Wasm file from the blockchain as a downloadable file, providing developers with a powerful tool for a deeper understanding of Soroban contract intricacies.

Retrieve WebAssembly (WASM) Code from Smart Contract

5. Access Soroban Contract Storage State

In addition, Soroban has many storage types to avoid storage bloats and manage efficiency data on the chain. The provided tutorial will focus on the storage state and information of a Soroban smart contract, this guide sheds light on the key-value pairs stored within the contract. Specifically, it delves into the Instance storage, accessible through the Env.storage().instance() code in your Dapps. Subsequently, we again introduce again to contractData in the soroban-client JS library. This step involves constructing a LedgerKey for contract data, fetching ledger entries from the Soroban server, and extracting relevant information, such as instance storage. Of course, every tutorial will provide a snippet and runnable example to see what you can expect when using these methods.

Access Soroban Contract Storage State

6. Construct Soroban Contract ABI in JavaScript/TypeScript

If you ever find yourself needing to explore a Soroban contract's methods along with their inputs and outputs, the Soroban CLI comes with its inspect command. This command, when provided with the WebAssembly (Wasm) code, returns a detailed list of contract specifications. These specifications include functions, as well as custom data types like enums and structs, associated with the Rust code. However, when working on a web app, integrating this process might pose some challenges. Therefore, this tutorial endeavours to establish an approach for obtaining comprehensive insights into a Soroban contract that goes beyond just its Wasm code. The goal is to create a method that emulates the functionality of the inspect command. It's worth noting that this involves utilizing the WebAssembly.Module inherent to JavaScript and the Soroban convention for storing information in the contractspecv0 section.

Construct Soroban Contract ABI in JavaScript/TypeScript

7. Restore Expired Contract/WASM

As far as Testnet and Futurenet in Soroban, contract data have a lifetime. In order to maintain and use a smart contract on Soroban, there data and wasm on the network must be maintained. The process involves retrieving expiration details and utilizing the Operation.restoreFootprint() method to obtain ledger keys associated with the contract instance.

Restore Expired Contract/WASM

Top comments (0)