π© Challenge #2: π Vending Machine
β οΈ Important: Please complete Challenge #1 first if you haven't already, as it contains essential instructions for all upcoming challenges.
π Cupcake Vending Machine on Stylus π§
π Overview This Rust-based smart contract acts as a blockchain-powered cupcake dispenser! Users can claim cupcakes every 5 seconds and check their balances at any time. Built with the Stylus SDK, it ensures seamless integration with Rust.
β¨ Features
- π§ Cupcake Distribution: Users claim cupcakes if 5 seconds have passed since the last request.
- π Balance Tracking: Check your cupcake stash anytime.
- β±οΈ Cooldown Timer: Prevents over-requesting.
π Project Goals
- 1οΈβ£ Smart Contract: Build a vending machine contract for cupcake claiming and tracking.
- 2οΈβ£ Frontend App: Create a user-friendly interface to request cupcakes and view balances.
- 3οΈβ£ Deploy: Launch on the Local Nitro-devnode.
π Letβs sweeten the blockchain experience! π©
Checkpoint 0: π¦ Environment Setup π
Before you begin, you need to install the following tools:
- Node (>= v18.17)
- Yarn (v1 or v2+)
- Git
Then download the challenge to your computer and install dependencies by running:
β οΈ IMPORTANT: Please make sure to run the below commands through WSL only. In PowerShell, you'll get an error because some files are not supported on Windows.
git clone -b vending-machine https://github.com/abhi152003/speedrun_stylus
cd speedrun_stylus
yarn install
In the same terminal, after all the dependencies have installed, run the below commands to start the local devnode in Docker. You'll need to spin up the Stylus nitro devnode by running the script through commands. This script will deploy the contract and generate the ABI so you can interact with the contracts written in RUST:
Contracts will be deployed through the cargo stylus command using the pre-funded account's private key so users can perform any transaction through the frontend while interacting with the contract.
cd speedrun_stylus # if not done
cd packages
cd cargo-stylus
cd vending_machine
Now open your Docker desktop and then return to your IDE and run bash run-dev-node.sh. This will spin up the nitro devnode in Docker. You can check it out in your Docker desktop. This will take some time to deploy the RUST contract, and then the script will automatically generate the ABI. You can view all these transactions in your terminal and Docker desktop. The Docker node is running at localhost:8547, but before running this command make sure about below thing
π¨ Fixing Line Endings and Running Shell Scripts in WSL on a CRLF-Based Windows System
β οΈ This guide provides step-by-step instructions to resolve the Command not found error caused by CRLF line endings in shell scripts when running in a WSL environment.
π οΈ Steps to Fix the Issue
Convert Line Endings to LF
Shell scripts created in Windows often have CRLF
line endings, which cause issues in Unix-like environments such as WSL. To fix this:
Using dos2unix
-
Install
dos2unix
(if not already installed):sudo apt install dos2unix
-
Convert the script's line endings:
dos2unix run-dev-node.sh
-
Make the Script Executable:
chmod +x run-dev-node.sh
-
Run the Script in WSL
bash run-dev-node.sh
In the same WSL terminal window or at the Docker Desktop terminal, you can easily view the details of your contract deployment, including the deployment transaction hash, which can be later used to verify the contract.
Then in a second WSL terminal window, you can run below commands to start your π± frontend:
cd speedrun_stylus ( if not done )
cd packages ( if not done )
cd nextjs
yarn run dev OR yarn dev
π± Open http://localhost:3000 to see the app.
π« Checkpoint 1: Frontend Magic
π You'll be redirected to the below page after you complete checkpoint 0
Then you have to click on the debug contracts to start interacting with your contract. Click on "Debug Contracts" from the Navbar or from the Debug Contracts Div placed in the middle of the screen
The interface allows you to:
- Send a Cupcake to a specified user address.
- Check the balance of Cupcakes held by a specific address.
- Display detailed contract information.
- Show the Cupcake cooldown time period.
- Track all transactions on the Block Explorer.
Below are the examples of above all interactions that can be done with the Vending Machine
smart contract written in the RUST
1. Cupcake
2. Balance Checking
After that, you can easily view all of your transactions from the Block Explorer Tab
πΌ Take a quick look at your deploy script run-dev-node.sh
in speedrun-rust/packages/cargo-stylus/vending_machine/run-dev-node.sh
.
π If you want to edit the frontend, navigate to speedrun_stylus/packages/nextjs/app
and open the specific page you want to modify. For instance: /debug/page.tsx
. For guidance on routing and configuring pages/layouts checkout the Next.js documentation.
Checkpoint 2: πΎ Deploy your contract! π°
π° You don't need to provide any specifications to deploy your contract because contracts are automatically deployed from the run-dev-node.sh
You can check that below :
The above command will automatically deploy the contract functions written inside speedrun_stylus/packages/cargo-stylus/vending_machine/src/lib.rs
This local account will deploy your contracts, allowing you to avoid entering a personal private key because the deployment happens using the pre-funded account's private key.
Checkpoint 3: π’ Ship your frontend! π
We are deploying all the RUST contracts at the
localhost:8547
endpoint where the nitro devnode is spinning up in Docker. You can check the network where your contract has been deployed in the frontend (http://localhost:3000):
π Deploy your NextJS App
vercel
Follow the steps to deploy to Vercel. Once you log in (email, github, etc), the default options should work. It'll give you a public URL.
If you want to redeploy to the same production URL you can run
yarn vercel --prod
. If you omit the--prod
flag it will deploy it to a preview/test URL.
Checkpoint 4: π Contract Verification
You can verify your smart contract by running:
cargo stylus verify -e http://127.0.0.1:8547 --deployment-tx "$deployment_tx"
# here deployment_tx can be received through the docker desktop's terminal when you have depoloyed your contract using the below command:
cargo stylus deploy -e http://127.0.0.1:8547 --private-key "$your_private_key"
# here you can use pre-funded account's private-key as well
It is okay if it says your contract is already verified.
π Next Steps
Explore more challenges or contribute to this project!
π Head to your next challenge here.