SoroPet Shop
Arsalaan Alam
Posted on August 18, 2024
This is a submission for the Build Better on Stellar: Smart Contract Challenge : Create a Tutorial
Your Tutorial
https://youtu.be/lG5mXJUWtiY - (Video Submission)
Code (Github)
https://github.com/Arsalaan-Alam/SoroPet-Shop
What I Created
In this tutorial, I’ve built a fully functional Pet Shop application on the Stellar blockchain using Soroban smart contracts. The Pet Shop is a familiar concept for many developers, especially those coming from Ethereum’s ecosystem, but this tutorial reimagines it on the Stellar network using Soroban, a new smart contract platform. The application allows users to adopt and disown pets, with ownership stored on the blockchain. This tutorial guides developers step-by-step on how to create, deploy, and interact with smart contracts on Stellar using Rust & React providing a comprehensive introduction to building decentralized applications on Soroban.
Contract
The Petshop contract contains three primary functions that manage pet adoption on the blockchain:
adopt_pet: This function allows a user to adopt a pet by storing the adopter's address on the blockchain using the pet's unique ID.
disown_pet: This function enables a user to disown a pet, removing the ownership information from the blockchain for that specific pet ID.
get_pet_adopter: This function retrieves the address of the current adopter for a given pet ID, allowing anyone to check the ownership status of a pet.
Additionally, the contract includes:
- DataKey enum: This defines a storage key in the contract, specifically used to map each pet's unique ID to its adopter's address on the blockchain.
Frontend
On the frontend, there are three primary functions that interact with the smart contract:
fetchAllPetOwners(): This function retrieves the current owner of each pet by invoking the
get_pet_adopter
method on the contract. It iterates through all the pets, fetches the adopter's address for each pet from the blockchain, and updates the state with this information.adoptPet(): This function allows a user to adopt a pet by invoking the
adopt_pet
method on the contract. It sends the pet's ID and the user's address to the contract, storing the adoption on the blockchain. The function also handles state updates and user notifications.disownPet(): This function enables a user to disown a pet by invoking the
disown_pet
method on the contract. It sends the pet's ID to the contract, which then removes the adoption record from the blockchain. The function also manages state changes and user feedback.
I used the Create Soroban Dapp boilerplate provided by Palta Labs, which made the setup process for Soroban development straightforward. The Stellar SDK and Soroban-react libraries played key roles in integrating the frontend with the smart contracts (invoking / receiving response / XDR conversion).
Journey
The journey of creating this Pet Shop tutorial for Soroban has been very exciting. My goal was to develop a comprehensive guide that helps beginners build a decentralized application (dapp) on Soroban, showcasing its capabilities in an accessible and engaging way.
Inspired by the well-known Pet Shop tutorial in the Ethereum ecosystem, I aimed to bring a similar tutorial to Soroban. The Ethereum Pet Shop tutorial is renowned for its simplicity and effectiveness in demonstrating smart contract principles. By creating a tutorial that mirrors this popular example, I hoped to provide a familiar learning experience for those transitioning from Ethereum or are new to blockchain development.
The smart contract I wrote is relatively simple but this straightforward approach makes it easier for newcomers to grasp the fundamental concepts of Soroban and smart contract development. Another core component that I have tried to extensively cover is the integration of smart contracts with the frontend. A lot of folks struggle with this, thus I've provided a straightforward way of invoking contracts from your frontend, receiving data, decoding it & rendering it.
Overall, the tutorial is structured to be beginner-friendly, offering clear explanations and is very easy to follow. Through this project, I aimed to provide a valuable resource for those new to Soroban, helping them understand how to leverage it for building decentralized applications. I'm proud of the final product and excited to support new developers as they embark on their journey with Soroban.
Posted on August 18, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
August 19, 2024
August 19, 2024