soroban_sdk/_migrating.rs
1//! # Migrating from v22 to v23
2//!
3//! 1. [`contractevent` replaces `Events::publish`][v23_contractevent]
4//!
5//! 2. [`MuxedAddress` replaces `Address` as the `to` of the `TokenInterface::transfer`].
6//! This change concerns `soroban-token-sdk` and is documented in detail in
7//! `soroban-token-sdk` crate migration guide.
8//!
9//! 3. [Accessing archived persistent entries in tests no longer results in a panic][v23_archived_testing],
10//! automatic restoration is emulated instead. Note, that instance storage is a
11//! persistent entry as well.
12//!
13//! # Migrating from v21 to v22
14//!
15//! 1. [`Env::register`] and [`Env::register_at`] replace [`Env::register_contract`] and [`Env::register_contract_wasm`].
16//!
17//! [`register`] registers both native contracts previously registered with
18//! [`register_contract`] and Wasm contracts previously registered with
19//! [`register_contract_wasm`]. It accepts a tuple that is passed to the
20//! contracts constructor. Pass `()` if the contract has no constructor.
21//!
22//! ```
23//! use soroban_sdk::{contract, contractimpl, Env};
24//!
25//! #[contract]
26//! pub struct Contract;
27//!
28//! #[contractimpl]
29//! impl Contract {
30//! // ..
31//! }
32//!
33//! #[test]
34//! fn test() {
35//! # }
36//! # #[cfg(feature = "testutils")]
37//! # fn main() {
38//! let env = Env::default();
39//! let address = env.register(
40//! Contract, // 👈 👀 The contract being registered, or a Wasm `&[u8]`.
41//! (), // 👈 👀 The constructor arguments, or ().
42//! );
43//! // ..
44//! }
45//! # #[cfg(not(feature = "testutils"))]
46//! # fn main() { }
47//! ```
48//!
49//! [`register_at`] registers both native contracts previously registered
50//! with [`register_contract`] and Wasm contracts previously registered with
51//! [`register_contract_wasm`], and allows setting the address that the
52//! contract is registered at. It accepts a tuple that is passed to the
53//! contracts constructor. Pass `()` if the contract has no constructor.
54//!
55//! ```
56//! use soroban_sdk::{contract, contractimpl, Env, Address, testutils::Address as _};
57//!
58//! #[contract]
59//! pub struct Contract;
60//!
61//! #[contractimpl]
62//! impl Contract {
63//! // ..
64//! }
65//!
66//! #[test]
67//! fn test() {
68//! # }
69//! # #[cfg(feature = "testutils")]
70//! # fn main() {
71//! let env = Env::default();
72//! let address = Address::generate(&env);
73//! env.register_at(
74//! &address, // 👈 👀 The address to register the contract at.
75//! Contract, // 👈 👀 The contract being registered, or a Wasm `&[u8]`.
76//! (), // 👈 👀 The constructor arguments, or ().
77//! );
78//! // ..
79//! }
80//! # #[cfg(not(feature = "testutils"))]
81//! # fn main() { }
82//! ```
83//!
84//! 2. [`DeployerWithAddress::deploy_v2`] replaces [`DeployerWithAddress::deploy`].
85//!
86//! [`deploy_v2`] is the same as [`deploy`], except it accepts a list of
87//! arguments to be passed to the contracts constructor that will be called
88//! when it is deployed. For deploying existing contracts that do not have
89//! constructors, pass `()`.
90//!
91//! ```
92//! use soroban_sdk::{contract, contractimpl, BytesN, Env};
93//!
94//! #[contract]
95//! pub struct Contract;
96//!
97//! #[contractimpl]
98//! impl Contract {
99//! pub fn exec(env: Env, wasm_hash: BytesN<32>) {
100//! let salt = [0u8; 32];
101//! let deployer = env.deployer().with_current_contract(salt);
102//! // Pass `()` for contracts that have no contstructor, or have a
103//! // constructor and require no arguments. Pass arguments in a
104//! // tuple if any required.
105//! let contract_address = deployer.deploy_v2(wasm_hash, ());
106//! }
107//! }
108//!
109//! #[test]
110//! fn test() {
111//! # }
112//! # #[cfg(feature = "testutils")]
113//! # fn main() {
114//! let env = Env::default();
115//! let contract_address = env.register(Contract, ());
116//! let contract = ContractClient::new(&env, &contract_address);
117//! // Upload the contract code before deploying its instance.
118//! const WASM: &[u8] = include_bytes!("../doctest_fixtures/contract.wasm");
119//! let wasm_hash = env.deployer().upload_contract_wasm(WASM);
120//! contract.exec(&wasm_hash);
121//! }
122//! # #[cfg(not(feature = "testutils"))]
123//! # fn main() { }
124//! ```
125//!
126//! 2. Deprecated [`fuzz_catch_panic`]. Use [`Env::try_invoke_contract`] and the `try_` client functions instead.
127//!
128//! The `fuzz_catch_panic` function could be used in fuzz tests to catch a contract panic. Improved behavior can be found by invoking a contract with the `try_` variant of the invoke function contract clients.
129//!
130//! ```
131//! use libfuzzer_sys::fuzz_target;
132//! use soroban_sdk::{contract, contracterror, contractimpl, Env, testutils::arbitrary::*};
133//!
134//! #[contract]
135//! pub struct Contract;
136//!
137//! #[contracterror]
138//! #[derive(Debug, PartialEq)]
139//! pub enum Error {
140//! Overflow = 1,
141//! }
142//!
143//! #[contractimpl]
144//! impl Contract {
145//! pub fn add(x: u32, y: u32) -> Result<u32, Error> {
146//! x.checked_add(y).ok_or(Error::Overflow)
147//! }
148//! }
149//!
150//! #[derive(Arbitrary, Debug)]
151//! pub struct Input {
152//! pub x: u32,
153//! pub y: u32,
154//! }
155//!
156//! fuzz_target!(|input: Input| {
157//! let env = Env::default();
158//! let id = env.register(Contract, ());
159//! let client = ContractClient::new(&env, &id);
160//!
161//! let result = client.try_add(&input.x, &input.y);
162//! match result {
163//! // Returned if the function succeeds, and the value returned is
164//! // the type expected.
165//! Ok(Ok(_)) => {}
166//! // Returned if the function succeeds, and the value returned is
167//! // NOT the type expected.
168//! Ok(Err(_)) => panic!("unexpected type"),
169//! // Returned if the function fails, and the error returned is
170//! // recognised as part of the contract errors enum.
171//! Err(Ok(_)) => {}
172//! // Returned if the function fails, and the error returned is NOT
173//! // recognised, or the contract panic'd.
174//! Err(Err(_)) => panic!("unexpected error"),
175//! }
176//! });
177//!
178//! # fn main() { }
179//! ```
180//!
181//! 3. Events in test snapshots are now reduced to only contract events and system events. Diagnostic events will no longer appear in test snapshots.
182//!
183//! This will cause all test snapshot JSON files generated by the SDK to change when upgrading to this major version of the SDK. The change should be isolated to events and should omit only diagnostic events.
184//!
185//! [`Env::register`]: crate::Env::register
186//! [`register`]: crate::Env::register
187//! [`Env::register_at`]: crate::Env::register_at
188//! [`register_at`]: crate::Env::register_at
189//! [`Env::register_contract`]: crate::Env::register_contract
190//! [`register_contract`]: crate::Env::register_contract
191//! [`Env::register_contract_wasm`]: crate::Env::register_contract_wasm
192//! [`register_contract_wasm`]: crate::Env::register_contract_wasm
193//! [`DeployerWithAddress::deploy_v2`]: crate::deploy::DeployerWithAddress::deploy_v2
194//! [`deploy_v2`]: crate::deploy::DeployerWithAddress::deploy_v2
195//! [`DeployerWithAddress::deploy`]: crate::deploy::DeployerWithAddress::deploy
196//! [`deploy`]: crate::deploy::DeployerWithAddress::deploy
197//! [`fuzz_catch_panic`]: crate::testutils::arbitrary::fuzz_catch_panic
198//! [`Env::try_invoke_contract`]: crate::Env::try_invoke_contract
199//!
200//! # Migrating from v20 to v21
201//!
202//! 1. [`CustomAccountInterface::__check_auth`] function `signature_payload` parameter changes from type [`BytesN<32>`] to [`Hash<32>`].
203//!
204//! The two types are interchangeable. [`Hash<32>`] contains a [`BytesN<32>`] and can only be constructed in contexts where the value has been generated by a secure cryptographic function.
205//!
206//! To convert from a [`Hash<32>`] to a [`BytesN<32>`], use [`Hash<32>::to_bytes`] or [`Into::into`].
207//!
208//! Current implementations of the interface will see a build error, and should change [`BytesN<32>`] to [`Hash<32>`].
209//!
210//! ```
211//! use soroban_sdk::{
212//! auth::{Context, CustomAccountInterface}, contract,
213//! contracterror, contractimpl, crypto::Hash, Env,
214//! Vec,
215//! };
216//!
217//! #[contract]
218//! pub struct Contract;
219//!
220//! #[contracterror]
221//! pub enum Error {
222//! AnError = 1,
223//! // ...
224//! }
225//!
226//! #[contractimpl]
227//! impl CustomAccountInterface for Contract {
228//! type Signature = ();
229//! type Error = Error;
230//!
231//! fn __check_auth(
232//! env: Env,
233//! signature_payload: Hash<32>, // 👈 👀
234//! signatures: (),
235//! auth_contexts: Vec<Context>,
236//! ) -> Result<(), Self::Error> {
237//! // ...
238//! # todo!()
239//! }
240//! }
241//!
242//! # fn main() { }
243//! ```
244//!
245//! [`CustomAccountInterface::__check_auth`]: crate::auth::CustomAccountInterface::__check_auth
246//! [`BytesN<32>`]: crate::BytesN
247//! [`Hash<32>`]: crate::crypto::Hash
248//! [`Hash<32>::to_bytes`]: crate::crypto::Hash::to_bytes
249
250pub mod v23_archived_testing;
251pub mod v23_contractevent;