1 unstable release
| 0.1.0 | Jul 29, 2025 |
|---|
#34 in #arbitrum
86 downloads per month
16KB
Stylus Cache SDK
A Rust SDK for Stylus contract caching utilities with AutoCacheOptIn event support.
Features
- 🚀 Easy Integration: Simple functions for contract caching
- 📝 Event Support: Built-in
AutoCacheOptInevent - 🛠️ Trait-based: Optional
Cacheabletrait for standardized implementation - 📚 Well Documented: Comprehensive documentation and examples
Installation
Add this to your Cargo.toml:
[dependencies]
stylus-cache-sdk = "0.1.0"
Quick Start
Basic Usage
use stylus_cache_sdk::{is_contract_cacheable, AutoCacheOptIn, emit_cache_opt_in};
use stylus_sdk::prelude::*;
use alloy_primitives::Address;
#[public]
impl MyContract {
pub fn is_cacheable(&self) -> bool {
is_contract_cacheable()
}
pub fn opt_into_cache(&self, contract_addr: Address) {
emit_cache_opt_in(contract_addr);
}
}
Using the Cacheable Trait
use stylus_cache_sdk::Cacheable;
use stylus_sdk::prelude::*;
use alloy_primitives::Address;
#[public]
impl MyContract {
// Contract implementation
}
impl Cacheable for MyContract {}
// Now you can use:
// contract.is_cacheable() -> bool
// contract.opt_into_cache(address) -> emits AutoCacheOptIn event
Updated Counter Contract Example
#![cfg_attr(not(any(test, feature = "export-abi")), no_main)]
#![cfg_attr(not(any(test, feature = "export-abi")), no_std)]
#[macro_use]
extern crate alloc;
use stylus_sdk::{alloy_primitives::U256, prelude::*};
use stylus_cache_sdk::{is_contract_cacheable, Cacheable};
sol_storage! {
#[entrypoint]
pub struct Counter {
uint256 number;
}
}
#[public]
impl Counter {
pub fn is_cacheable(&self) -> bool {
is_contract_cacheable()
}
pub fn numbers(&self) -> U256 {
self.number.get()
}
// ... rest of your contract methods
}
impl Cacheable for Counter {}
API Reference
Functions
is_contract_cacheable() -> bool: Returns whether the contract is cacheableemit_cache_opt_in(contract_addr: Address): Emits the AutoCacheOptIn event
Events
AutoCacheOptIn(address indexed contract_addr): Event for cache opt-in
Traits
Cacheable: Trait providing default caching functionality
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Dependencies
~12MB
~213K SLoC