Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/offchainlabs/stylus-vending-machine
https://github.com/offchainlabs/stylus-vending-machine
Last synced: 7 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/offchainlabs/stylus-vending-machine
- Owner: OffchainLabs
- License: mit
- Created: 2024-09-10T20:17:34.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-10T20:20:50.000Z (2 months ago)
- Last Synced: 2024-09-10T23:43:35.655Z (2 months ago)
- Language: Rust
- Size: 35.2 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Stylus Cupcake Example
An example project for writing Arbitrum Stylus programs in Rust using the [stylus-sdk](https://github.com/OffchainLabs/stylus-sdk-rs). It includes a Rust implementation of a vending machine Ethereum smart contract. Below is the interface for the VendingMachine contract:
```js
interface IVendingMachine {
// Function to distribute a cupcake to a user
function giveCupcakeTo(address userAddress) external returns (bool);// Getter function for the cupcake balance of a user
function getCupcakeBalanceFor(address userAddress) external view returns (uint);
}
```### ABI Export
You can export the Solidity ABI for your program by using the `cargo stylus` tool as follows:
```bash
cargo stylus export-abi
```which outputs:
```js
/**
* This file was automatically generated by Stylus and represents a Rust program.
* For more information, please see [The Stylus SDK](https://github.com/OffchainLabs/stylus-sdk-rs).
*/interface VendingMachine {
function giveCupcakeTo(address user_address) external returns (bool);function getCupcakeBalanceFor(address user_address) external view returns (uint256);
}
```## Deploying
You can use the `cargo stylus` command to also deploy your program to the Stylus testnet. We can use the tool to first check
our program compiles to valid WASM for Stylus and will succeed a deployment onchain without transacting. By default, this will use the Stylus testnet public RPC endpoint. See here for [Stylus testnet information](https://docs.arbitrum.io/stylus/reference/testnet-information).```bash
cargo stylus check
```Next, we deploy:
```bash
cargo stylus deploy \
--private-key-path=
```## Calling Your Program
This example includes how to call and transact with your program in Rust using ethers-rs under `examples/vending_machine.rs`. Your programs are also Ethereum ABI equivalent if using the Stylus SDK, meaning they can be called and transacted with using any other Ethereum tooling.
By using the program address from your deployment step above and your wallet, you can attempt to call the vending machine program and interact with it.
To run the example, set the following env vars or place them in a `.env` file this project, then:
```
STYLUS_PROGRAM_ADDRESS=
PRIV_KEY_PATH=
RPC_URL=https://stylus-testnet.arbitrum.io/rpc
USER_ADDRESS=
```Alternatively, you can copy the `.env-sample` into a `.env` file:
```
cp .env-sample .env
```Next, run:
```
cargo run --example vending_machine --target=
```Where you can find `YOUR_ARCHITECTURE` by running `rustc -vV | grep host`. For M1 Apple computers, for example, this is `aarch64-apple-darwin` and for most Linux x86 it is `x86_64-unknown-linux-gnu`.