github OffchainLabs/stylus-sdk-rs v0.8.0
v0.8.0 - Stylus Contract Test Framework!

19 hours ago

What's Changed

Version 0.8.0 of the Stylus SDK brings a brand new stylus_test crate providing a testing framework for all Stylus contracts and storage types. This crate is re-exported through the Stylus SDK under stylus_sdk::testing when targeting a native architecture. A new Host trait is defined in an internal stylus_core crate that defines all the hostio affordances Stylus contracts have. All global hostio invocations, such as stylus_sdk::msg::value() are now deprecated in favor of invoking hostios through a Host access available to contracts.

The Stylus #[storage] proc macro makes a .vm() method available on all contracts which offers all hostio methods. Moreover, a stylus_sdk::testing crate implements a test host for use in unit testing:

use stylus_sdk::{alloy_primitives::U256, prelude::*};

#[entrypoint]
#[storage]
pub struct Counter {
	number: StorageU256;
}

#[public]
impl Counter {
    pub fn number(&self) -> U256 {
        self.number.get()
    }
    pub fn increment(&mut self) {
        let number = self.number.get();
        self.set_number(number + U256::from(1));
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use stylus_sdk::testing::*;

    #[test]
    fn test_counter() {
        let vm = TestVM::default();
        let mut contract = Counter::from(&vm);

        assert_eq!(U256::ZERO, contract.number());

        contract.increment();
        assert_eq!(U256::from(1), contract.number());
    }
}

Caveats

  • All the code related to cross-contract calls is not yet updated to use the new Host trait, meaning it cannot be fully tested via the testing framework. This is because changing calls requires breaking changes, and will be done in the 1.0 release candidate for the SDK in the near future
  • In order to leverage and unit test contracts with stylus_test, contracts must use hostio access via the .vm() accessor instead of global hostio invocations, which are now deprecated

Full Changelog: v0.7.0...v0.8.0

Don't miss a new stylus-sdk-rs release

NewReleases is sending notifications on new releases.