This release introduces new features and fixes to the Stylus SDK. We've bumped the major version number due to minor backwards-incompatible changes. To adopt the latest version, edit your Cargo.toml
as follows
stylus-sdk = "0.4.1"
Check if address has code
The AddressVM
trait from the prelude
has a new method, has_code
, for detecting if an account has code.
use stylus-sdk::{prelude::*, contract, alloy_primitives::address};
let arbinaut = address!(361594F5429D23ECE0A88E4fBE529E1c49D524d8);
assert!(!arbinaut.has_code());
assert!(contract::address().has_code())
Note that this is insufficient to determine if an address is an EOA. During contract deployment, an account only gets its code at the very end, meaning that this method will return false
while the constructor is executing.
As part of this change, we've updated the method signature of Address::codehash
to return a B256
instead of an Option
. Previously the Option
was supposed to be None
if the account was not a contract. Splitting this out into two methods is a more sensible API.
Configurable Selectors
You may now override method selectors.
impl Contract {
#[selector(name = "otherName")]
pub fn renamed() {
...
}
#[selector(id = "otherFunc(uint64)")]
pub fn other_selector(value: U64) {
...
}
#[selector(id = 0xba5eba11)]
pub fn exact_selector() {
...
}
}
The above will now export
/**
* 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 IContract {
function otherName() external pure;
// note: selector was overridden to be 0x7cc01843.
function otherSelector(uint64 value) external pure;
// note: selector was overridden to be 0xba5eba11.
function exactSelector() external pure;
}
Reentrancy Changes
Because Call::new
is never correct in reentrant code, the reentrant
feature flag removes it. This shouldn't affect existing contracts since reentrant calls instead use Call::new_in
.