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