diff --git a/Cargo.lock b/Cargo.lock index 9cfe735fd..b1368f2c8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1217,7 +1217,7 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "soroban-builtin-sdk-macros" version = "22.0.0" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0497816694bef2b103494c8c61b7c8a06a72c7d3#0497816694bef2b103494c8c61b7c8a06a72c7d3" +source = "git+https://github.com/stellar/rs-soroban-env?rev=60e9be87a157cc9b5132056e1314faabd485b5fb#60e9be87a157cc9b5132056e1314faabd485b5fb" dependencies = [ "itertools", "proc-macro2", @@ -1228,7 +1228,7 @@ dependencies = [ [[package]] name = "soroban-env-common" version = "22.0.0" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0497816694bef2b103494c8c61b7c8a06a72c7d3#0497816694bef2b103494c8c61b7c8a06a72c7d3" +source = "git+https://github.com/stellar/rs-soroban-env?rev=60e9be87a157cc9b5132056e1314faabd485b5fb#60e9be87a157cc9b5132056e1314faabd485b5fb" dependencies = [ "arbitrary", "crate-git-revision", @@ -1246,7 +1246,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "22.0.0" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0497816694bef2b103494c8c61b7c8a06a72c7d3#0497816694bef2b103494c8c61b7c8a06a72c7d3" +source = "git+https://github.com/stellar/rs-soroban-env?rev=60e9be87a157cc9b5132056e1314faabd485b5fb#60e9be87a157cc9b5132056e1314faabd485b5fb" dependencies = [ "soroban-env-common", "static_assertions", @@ -1255,7 +1255,7 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "22.0.0" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0497816694bef2b103494c8c61b7c8a06a72c7d3#0497816694bef2b103494c8c61b7c8a06a72c7d3" +source = "git+https://github.com/stellar/rs-soroban-env?rev=60e9be87a157cc9b5132056e1314faabd485b5fb#60e9be87a157cc9b5132056e1314faabd485b5fb" dependencies = [ "ark-bls12-381", "ark-ec", @@ -1290,7 +1290,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "22.0.0" -source = "git+https://github.com/stellar/rs-soroban-env?rev=0497816694bef2b103494c8c61b7c8a06a72c7d3#0497816694bef2b103494c8c61b7c8a06a72c7d3" +source = "git+https://github.com/stellar/rs-soroban-env?rev=60e9be87a157cc9b5132056e1314faabd485b5fb#60e9be87a157cc9b5132056e1314faabd485b5fb" dependencies = [ "itertools", "proc-macro2", @@ -1391,8 +1391,9 @@ dependencies = [ [[package]] name = "soroban-wasmi" -version = "0.36.0-soroban.22.0.0" -source = "git+https://github.com/stellar/wasmi?rev=122a74a7c491929e5ac9de876099154ef7c06d06#122a74a7c491929e5ac9de876099154ef7c06d06" +version = "0.36.1-soroban.22.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7044ea0ee6ff67039df1f232f0d3d98121f69a0409e944774912fc5f043c280f" dependencies = [ "arrayvec", "multi-stash", @@ -1441,7 +1442,7 @@ dependencies = [ [[package]] name = "stellar-xdr" version = "22.0.0" -source = "git+https://github.com/stellar/rs-stellar-xdr?rev=b5516843b6379e4e29520bf2ba156484f62edc46#b5516843b6379e4e29520bf2ba156484f62edc46" +source = "git+https://github.com/stellar/rs-stellar-xdr?rev=67be5955a15f1d3a4df83fe86e6ae107f687141b#67be5955a15f1d3a4df83fe86e6ae107f687141b" dependencies = [ "arbitrary", "base64 0.13.1", @@ -1806,8 +1807,9 @@ checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" [[package]] name = "wasmi_collections" -version = "0.36.0-soroban.22.0.0" -source = "git+https://github.com/stellar/wasmi?rev=122a74a7c491929e5ac9de876099154ef7c06d06#122a74a7c491929e5ac9de876099154ef7c06d06" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d1ff23df2c456c8b5d9a0ae7eed03a40f0c4520466b4aa87135c5fc557476e8" dependencies = [ "ahash", "hashbrown 0.14.5", @@ -1816,8 +1818,9 @@ dependencies = [ [[package]] name = "wasmi_core" -version = "0.36.0-soroban.22.0.0" -source = "git+https://github.com/stellar/wasmi?rev=122a74a7c491929e5ac9de876099154ef7c06d06#122a74a7c491929e5ac9de876099154ef7c06d06" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac1b21ded145eb313d44a5895442c28e18904fb95718dc83893779f55945d342" dependencies = [ "downcast-rs", "libm", diff --git a/Cargo.toml b/Cargo.toml index ce88c468e..b0ffc52f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,17 +26,17 @@ soroban-token-sdk = { version = "22.0.0-rc.1", path = "soroban-token-sdk" } [workspace.dependencies.soroban-env-common] version = "=22.0.0" git = "https://github.com/stellar/rs-soroban-env" -rev = "0497816694bef2b103494c8c61b7c8a06a72c7d3" +rev = "60e9be87a157cc9b5132056e1314faabd485b5fb" [workspace.dependencies.soroban-env-guest] version = "=22.0.0" git = "https://github.com/stellar/rs-soroban-env" -rev = "0497816694bef2b103494c8c61b7c8a06a72c7d3" +rev = "60e9be87a157cc9b5132056e1314faabd485b5fb" [workspace.dependencies.soroban-env-host] version = "=22.0.0" git = "https://github.com/stellar/rs-soroban-env" -rev = "0497816694bef2b103494c8c61b7c8a06a72c7d3" +rev = "60e9be87a157cc9b5132056e1314faabd485b5fb" [workspace.dependencies.stellar-strkey] version = "=0.0.9" @@ -46,7 +46,7 @@ version = "=22.0.0" default-features = false features = ["curr"] git = "https://github.com/stellar/rs-stellar-xdr" -rev = "b5516843b6379e4e29520bf2ba156484f62edc46" +rev = "67be5955a15f1d3a4df83fe86e6ae107f687141b" #[patch."https://github.com/stellar/rs-soroban-env"] #soroban-env-common = { path = "../rs-soroban-env/soroban-env-common" } diff --git a/deny.toml b/deny.toml index 7efb64b46..bfb266ad9 100644 --- a/deny.toml +++ b/deny.toml @@ -243,9 +243,8 @@ deny = [ # Certain crates/versions that will be skipped when doing duplicate detection. skip = [ - #{ name = "ansi_term", version = "=0.11.0" }, - { name = "hashbrown", version = "=0.14.5" }, - { name = "syn", version = "=2.0.77" }, + { name = "hashbrown", version = "=0.13.2" }, + { name = "syn", version = "=1.0.109" }, ] # Similarly to `skip` allows you to skip certain crates during duplicate # detection. Unlike skip, it also includes the entire tree of transitive diff --git a/soroban-sdk-macros/src/derive_enum_int.rs b/soroban-sdk-macros/src/derive_enum_int.rs index 6cc326c16..27e68b01a 100644 --- a/soroban-sdk-macros/src/derive_enum_int.rs +++ b/soroban-sdk-macros/src/derive_enum_int.rs @@ -94,7 +94,6 @@ pub fn derive_type_enum_int( // Output. let mut output = quote! { #spec_gen - const _: () = { fn assert_copy(v: #enum_ident) -> impl Copy { v } }; impl #path::TryFromVal<#path::Env, #path::Val> for #enum_ident { type Error = #path::ConversionError; @@ -144,7 +143,9 @@ pub fn derive_type_enum_int( type Error = #path::xdr::Error; #[inline(always)] fn try_into(self) -> Result<#path::xdr::ScVal, #path::xdr::Error> { - Ok((*self as u32).into()) + Ok(match self { + #(#try_intos,)* + }) } } @@ -152,7 +153,9 @@ pub fn derive_type_enum_int( type Error = #path::xdr::Error; #[inline(always)] fn try_into(self) -> Result<#path::xdr::ScVal, #path::xdr::Error> { - Ok((self as u32).into()) + Ok(match self { + #(#try_intos,)* + }) } } diff --git a/soroban-sdk-macros/src/derive_error_enum_int.rs b/soroban-sdk-macros/src/derive_error_enum_int.rs index bfa45f301..fd1dd9016 100644 --- a/soroban-sdk-macros/src/derive_error_enum_int.rs +++ b/soroban-sdk-macros/src/derive_error_enum_int.rs @@ -92,7 +92,6 @@ pub fn derive_type_error_enum_int( // Output. quote! { #spec_gen - const _: () = { fn assert_copy(v: #enum_ident) -> impl Copy { v } }; impl TryFrom<#path::Error> for #enum_ident { type Error = #path::Error; @@ -121,16 +120,16 @@ pub fn derive_type_error_enum_int( impl From<#enum_ident> for #path::Error { #[inline(always)] fn from(val: #enum_ident) -> #path::Error { - match val { - #(#into_errors,)* - } + <_ as From<&#enum_ident>>::from(&val) } } impl From<&#enum_ident> for #path::Error { #[inline(always)] fn from(val: &#enum_ident) -> #path::Error { - <_ as From<#enum_ident>>::from(*val) + match val { + #(#into_errors,)* + } } } @@ -159,16 +158,16 @@ pub fn derive_type_error_enum_int( impl From<#enum_ident> for #path::InvokeError { #[inline(always)] fn from(val: #enum_ident) -> #path::InvokeError { - match val { - #(#into_invoke_errors,)* - } + <_ as From<&#enum_ident>>::from(&val) } } impl From<&#enum_ident> for #path::InvokeError { #[inline(always)] fn from(val: &#enum_ident) -> #path::InvokeError { - <_ as From<#enum_ident>>::from(*val) + match val { + #(#into_invoke_errors,)* + } } } diff --git a/soroban-sdk/README.md b/soroban-sdk/README.md index 2aef6d4c3..83953317f 100644 --- a/soroban-sdk/README.md +++ b/soroban-sdk/README.md @@ -26,7 +26,7 @@ fn test() { # #[cfg(feature = "testutils")] # fn main() { let env = Env::default(); - let contract_id = env.register_contract(None, HelloContract); + let contract_id = env.register(HelloContract, ()); let client = HelloContractClient::new(&env, &contract_id); let words = client.hello(&symbol_short!("Dev")); diff --git a/soroban-sdk/src/address.rs b/soroban-sdk/src/address.rs index dfed7f87a..e735a6f23 100644 --- a/soroban-sdk/src/address.rs +++ b/soroban-sdk/src/address.rs @@ -124,18 +124,21 @@ impl TryFromVal for Val { } #[cfg(not(target_family = "wasm"))] -impl TryFrom<&Address> for ScVal { - type Error = ConversionError; - fn try_from(v: &Address) -> Result { - Ok(ScVal::try_from_val(&v.env, &v.obj.to_val())?) +impl From<&Address> for ScVal { + fn from(v: &Address) -> Self { + // This conversion occurs only in test utilities, and theoretically all + // values should convert to an ScVal because the Env won't let the host + // type to exist otherwise, unwrapping. Even if there are edge cases + // that don't, this is a trade off for a better test developer + // experience. + ScVal::try_from_val(&v.env, &v.obj.to_val()).unwrap() } } #[cfg(not(target_family = "wasm"))] -impl TryFrom
for ScVal { - type Error = ConversionError; - fn try_from(v: Address) -> Result { - (&v).try_into() +impl From
for ScVal { + fn from(v: Address) -> Self { + (&v).into() } } @@ -152,21 +155,19 @@ impl TryFromVal for Address { } #[cfg(not(target_family = "wasm"))] -impl TryFrom<&Address> for ScAddress { - type Error = ConversionError; - fn try_from(v: &Address) -> Result { - match ScVal::try_from_val(&v.env, &v.obj.to_val())? { - ScVal::Address(a) => Ok(a), - _ => Err(ConversionError), +impl From<&Address> for ScAddress { + fn from(v: &Address) -> Self { + match ScVal::try_from_val(&v.env, &v.obj.to_val()).unwrap() { + ScVal::Address(a) => a, + _ => panic!("expected ScVal::Address"), } } } #[cfg(not(target_family = "wasm"))] -impl TryFrom
for ScAddress { - type Error = ConversionError; - fn try_from(v: Address) -> Result { - (&v).try_into() +impl From
for ScAddress { + fn from(v: Address) -> Self { + (&v).into() } } diff --git a/soroban-sdk/src/bytes.rs b/soroban-sdk/src/bytes.rs index 706a87590..af4094a25 100644 --- a/soroban-sdk/src/bytes.rs +++ b/soroban-sdk/src/bytes.rs @@ -328,18 +328,21 @@ impl From<&Bytes> for Bytes { } #[cfg(not(target_family = "wasm"))] -impl TryFrom<&Bytes> for ScVal { - type Error = ConversionError; - fn try_from(v: &Bytes) -> Result { - Ok(ScVal::try_from_val(&v.env, &v.obj.to_val())?) +impl From<&Bytes> for ScVal { + fn from(v: &Bytes) -> Self { + // This conversion occurs only in test utilities, and theoretically all + // values should convert to an ScVal because the Env won't let the host + // type to exist otherwise, unwrapping. Even if there are edge cases + // that don't, this is a trade off for a better test developer + // experience. + ScVal::try_from_val(&v.env, &v.obj.to_val()).unwrap() } } #[cfg(not(target_family = "wasm"))] -impl TryFrom for ScVal { - type Error = ConversionError; - fn try_from(v: Bytes) -> Result { - (&v).try_into() +impl From for ScVal { + fn from(v: Bytes) -> Self { + (&v).into() } } diff --git a/soroban-sdk/src/deploy.rs b/soroban-sdk/src/deploy.rs index 71646d5ef..640b9399e 100644 --- a/soroban-sdk/src/deploy.rs +++ b/soroban-sdk/src/deploy.rs @@ -36,7 +36,7 @@ //! # #[cfg(feature = "testutils")] //! # fn main() { //! let env = Env::default(); -//! let contract_address = env.register_contract(None, Contract); +//! let contract_address = env.register(Contract, ()); //! let contract = ContractClient::new(&env, &contract_address); //! // Upload the contract code before deploying its instance. //! let wasm_hash = env.deployer().upload_contract_wasm(DEPLOYED_WASM); @@ -71,7 +71,7 @@ //! # #[cfg(feature = "testutils")] //! # fn main() { //! let env = Env::default(); -//! let contract_address = env.register_contract(None, Contract); +//! let contract_address = env.register(Contract, ()); //! let contract = ContractClient::new(&env, &contract_address); //! // Upload the contract code before deploying its instance. //! let wasm_hash = env.deployer().upload_contract_wasm(DEPLOYED_WASM_WITH_CTOR); diff --git a/soroban-sdk/src/env.rs b/soroban-sdk/src/env.rs index bc4ffec17..1d04b3a56 100644 --- a/soroban-sdk/src/env.rs +++ b/soroban-sdk/src/env.rs @@ -455,7 +455,7 @@ use crate::{ testutils::{ budget::Budget, Address as _, AuthSnapshot, AuthorizedInvocation, ConstructorArgs, ContractFunctionSet, EventsSnapshot, Generators, Ledger as _, MockAuth, MockAuthContract, - Snapshot, StellarAssetContract, StellarAssetIssuer, + Register, Snapshot, StellarAssetContract, StellarAssetIssuer, }, Bytes, BytesN, }; @@ -576,6 +576,136 @@ impl Env { env } + /// Register a contract with the [Env] for testing. + /// + /// Pass the contract type when the contract is defined in the current crate + /// and is being registered natively. Pass the contract wasm bytes when the + /// contract has been loaded as wasm. + /// + /// Pass the arguments for the contract's constructor, or `()` if none. + /// + /// Returns the address of the registered contract that is the same as the + /// contract id passed in. + /// + /// If you need to specify the address the contract should be registered at, + /// use [`register_at`]. + /// + /// ### Examples + /// Register a contract defined in the current crate, by specifying the type + /// name: + /// ``` + /// use soroban_sdk::{contract, contractimpl, testutils::Address as _, Address, BytesN, Env, Symbol}; + /// + /// #[contract] + /// pub struct Contract; + /// + /// #[contractimpl] + /// impl Contract { + /// pub fn __constructor(_env: Env, _input: u32) { + /// } + /// } + /// + /// #[test] + /// fn test() { + /// # } + /// # fn main() { + /// let env = Env::default(); + /// let contract_id = env.register(Contract, (123_u32,)); + /// } + /// ``` + /// Register a contract wasm, by specifying the wasm bytes: + /// ``` + /// use soroban_sdk::{testutils::Address as _, Address, BytesN, Env}; + /// + /// const WASM: &[u8] = include_bytes!("../doctest_fixtures/contract.wasm"); + /// + /// #[test] + /// fn test() { + /// # } + /// # fn main() { + /// let env = Env::default(); + /// let contract_id = env.register(WASM, ()); + /// } + /// ``` + pub fn register<'a, C, A>(&self, contract: C, constructor_args: A) -> Address + where + C: Register, + A: ConstructorArgs, + { + contract.register(self, None, constructor_args) + } + + /// Register a contract with the [Env] for testing. + /// + /// Passing a contract ID for the first arguments registers the contract + /// with that contract ID. + /// + /// Registering a contract that is already registered replaces it. + /// Use re-registration with caution as it does not exist in the real + /// (on-chain) environment. Specifically, the new contract's constructor + /// will be called again during re-registration. That behavior only exists + /// for this test utility and is not reproducible on-chain, where contract + /// Wasm updates don't cause constructor to be called. + /// + /// Pass the contract type when the contract is defined in the current crate + /// and is being registered natively. Pass the contract wasm bytes when the + /// contract has been loaded as wasm. + /// + /// Returns the address of the registered contract that is the same as the + /// contract id passed in. + /// + /// ### Examples + /// Register a contract defined in the current crate, by specifying the type + /// name: + /// ``` + /// use soroban_sdk::{contract, contractimpl, testutils::Address as _, Address, BytesN, Env, Symbol}; + /// + /// #[contract] + /// pub struct Contract; + /// + /// #[contractimpl] + /// impl Contract { + /// pub fn __constructor(_env: Env, _input: u32) { + /// } + /// } + /// + /// #[test] + /// fn test() { + /// # } + /// # fn main() { + /// let env = Env::default(); + /// let contract_id = Address::generate(&env); + /// env.register_at(&contract_id, Contract, (123_u32,)); + /// } + /// ``` + /// Register a contract wasm, by specifying the wasm bytes: + /// ``` + /// use soroban_sdk::{testutils::Address as _, Address, BytesN, Env}; + /// + /// const WASM: &[u8] = include_bytes!("../doctest_fixtures/contract.wasm"); + /// + /// #[test] + /// fn test() { + /// # } + /// # fn main() { + /// let env = Env::default(); + /// let contract_id = Address::generate(&env); + /// env.register_at(&contract_id, WASM, ()); + /// } + /// ``` + pub fn register_at( + &self, + contract_id: &Address, + contract: C, + constructor_args: A, + ) -> Address + where + C: Register, + A: ConstructorArgs, + { + contract.register(self, contract_id, constructor_args) + } + /// Register a contract with the [Env] for testing. /// /// Passing a contract ID for the first arguments registers the contract @@ -583,8 +713,7 @@ impl Env { /// contract ID that is assigned to the contract. /// /// If a contract has a constructor defined, then it will be called with - /// no arguments. If a constructor takes arguments, use - /// `register_contract_with_constructor`. + /// no arguments. If a constructor takes arguments, use `register`. /// /// Registering a contract that is already registered replaces it. /// Use re-registration with caution as it does not exist in the real @@ -617,6 +746,7 @@ impl Env { /// let contract_id = env.register_contract(None, HelloContract); /// } /// ``` + #[deprecated(note = "use `register`")] pub fn register_contract<'a, T: ContractFunctionSet + 'static>( &self, contract_id: impl Into>, @@ -642,30 +772,7 @@ impl Env { /// Wasm updates don't cause constructor to be called. /// /// Returns the address of the registered contract. - /// - /// ### Examples - /// ``` - /// use soroban_sdk::{contract, contractimpl, BytesN, Env, Symbol}; - /// - /// #[contract] - /// pub struct Contract; - /// - /// #[contractimpl] - /// impl Contract { - /// pub fn __constructor(_env: Env, _input: u32) { - /// } - /// } - /// - /// #[test] - /// fn test() { - /// # } - /// # fn main() { - /// let env = Env::default(); - /// let contract_id = env.register_contract_with_constructor( - /// None, Contract, (123_u32,)); - /// } - /// ``` - pub fn register_contract_with_constructor< + pub(crate) fn register_contract_with_constructor< 'a, T: ContractFunctionSet + 'static, A: ConstructorArgs, @@ -742,6 +849,7 @@ impl Env { /// env.register_contract_wasm(None, WASM); /// } /// ``` + #[deprecated(note = "use `register`")] pub fn register_contract_wasm<'a>( &self, contract_id: impl Into>, @@ -772,33 +880,17 @@ impl Env { /// Wasm updates don't cause constructor to be called. /// /// Returns the address of the registered contract. - /// - /// ### Examples - /// ``` - /// use soroban_sdk::{BytesN, Env, IntoVal}; - /// // This is Wasm for `constructor` test contract from this repo. - /// const WASM: &[u8] = include_bytes!("../doctest_fixtures/contract_with_constructor.wasm"); - /// - /// #[test] - /// fn test() { - /// # } - /// # fn main() { - /// let env = Env::default(); - /// env.register_contract_wasm_with_constructor( - /// None, WASM, (10_u32, 100_i64).into_val(&env)); - /// } - /// ``` - pub fn register_contract_wasm_with_constructor<'a>( + pub(crate) fn register_contract_wasm_with_constructor<'a>( &self, contract_id: impl Into>, contract_wasm: impl IntoVal, - constructor_args: Vec, + constructor_args: impl ConstructorArgs, ) -> Address { let wasm_hash: BytesN<32> = self.deployer().upload_contract_wasm(contract_wasm); self.register_contract_with_optional_contract_id_and_executable( contract_id, xdr::ContractExecutable::Wasm(xdr::Hash(wasm_hash.into())), - constructor_args, + constructor_args.into_val(self), ) } @@ -995,7 +1087,7 @@ impl Env { /// # } /// # fn main() { /// let env = Env::default(); - /// let contract_id = env.register_contract(None, HelloContract); + /// let contract_id = env.register(HelloContract, ()); /// /// let client = HelloContractClient::new(&env, &contract_id); /// let addr = Address::generate(&env); @@ -1014,7 +1106,7 @@ impl Env { /// ``` pub fn mock_auths(&self, auths: &[MockAuth]) { for a in auths { - self.register_contract(a.address, MockAuthContract); + self.register_at(a.address, MockAuthContract, ()); } let auths = auths .iter() @@ -1064,7 +1156,7 @@ impl Env { /// # } /// # fn main() { /// let env = Env::default(); - /// let contract_id = env.register_contract(None, HelloContract); + /// let contract_id = env.register(HelloContract, ()); /// /// env.mock_all_auths(); /// @@ -1117,8 +1209,8 @@ impl Env { /// # } /// # fn main() { /// let env = Env::default(); - /// let contract_a = env.register_contract(None, ContractA); - /// let contract_b = env.register_contract(None, ContractB); + /// let contract_a = env.register(ContractA, ()); + /// let contract_b = env.register(ContractB, ()); /// // The regular `env.mock_all_auths()` would result in the call /// // failure. /// env.mock_all_auths_allowing_non_root_auth(); @@ -1178,7 +1270,7 @@ impl Env { /// # fn main() { /// extern crate std; /// let env = Env::default(); - /// let contract_id = env.register_contract(None, Contract); + /// let contract_id = env.register(Contract, ()); /// let client = ContractClient::new(&env, &contract_id); /// env.mock_all_auths(); /// let address = Address::generate(&env); @@ -1282,7 +1374,7 @@ impl Env { /// # } /// # fn main() { /// let e: Env = Default::default(); - /// let account_contract = NoopAccountContractClient::new(&e, &e.register_contract(None, NoopAccountContract)); + /// let account_contract = NoopAccountContractClient::new(&e, &e.register(NoopAccountContract, ())); /// // Non-succesful call of `__check_auth` with a `contracterror` error. /// assert_eq!( /// e.try_invoke_contract_check_auth::( diff --git a/soroban-sdk/src/events.rs b/soroban-sdk/src/events.rs index 241779357..6d6e9a986 100644 --- a/soroban-sdk/src/events.rs +++ b/soroban-sdk/src/events.rs @@ -43,7 +43,7 @@ const TOPIC_BYTES_LENGTH_LIMIT: u32 = 32; /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); -/// # let contract_id = env.register_contract(None, Contract); +/// # let contract_id = env.register(Contract, ()); /// # ContractClient::new(&env, &contract_id).f(); /// # } /// # #[cfg(not(feature = "testutils"))] diff --git a/soroban-sdk/src/ledger.rs b/soroban-sdk/src/ledger.rs index a91801610..4b6b5a5a0 100644 --- a/soroban-sdk/src/ledger.rs +++ b/soroban-sdk/src/ledger.rs @@ -31,7 +31,7 @@ use crate::{env::internal, unwrap::UnwrapInfallible, BytesN, Env, TryIntoVal}; /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); -/// # let contract_id = env.register_contract(None, Contract); +/// # let contract_id = env.register(Contract, ()); /// # ContractClient::new(&env, &contract_id).f(); /// # } /// # #[cfg(not(feature = "testutils"))] diff --git a/soroban-sdk/src/lib.rs b/soroban-sdk/src/lib.rs index c032484ea..a4b8aa5cb 100644 --- a/soroban-sdk/src/lib.rs +++ b/soroban-sdk/src/lib.rs @@ -26,7 +26,7 @@ //! # #[cfg(feature = "testutils")] //! # fn main() { //! let env = Env::default(); -//! let contract_id = env.register_contract(None, HelloContract); +//! let contract_id = env.register(HelloContract, ()); //! let client = HelloContractClient::new(&env, &contract_id); //! //! let words = client.hello(&symbol_short!("Dev")); @@ -197,7 +197,7 @@ pub use soroban_sdk_macros::symbol_short; /// let env = Env::default(); /// /// // Register the contract defined in this crate. -/// let contract_id = env.register_contract(None, Contract); +/// let contract_id = env.register(Contract, ()); /// /// // Create a client for calling the contract. /// let client = ContractClient::new(&env, &contract_id); @@ -244,7 +244,7 @@ pub use soroban_sdk_macros::symbol_short; /// let env = Env::default(); /// /// // Register the contract defined in this crate. -/// let contract_id = env.register_contract(None, Contract); +/// let contract_id = env.register(Contract, ()); /// /// // Create a client for calling the contract. /// let client = ContractClient::new(&env, &contract_id); @@ -296,7 +296,7 @@ pub use soroban_sdk_macros::contracterror; /// let contract_a_id = env.register_contract_wasm(None, contract_a::WASM); /// /// // Register contract B defined in this crate. -/// let contract_b_id = env.register_contract(None, ContractB); +/// let contract_b_id = env.register(ContractB, ()); /// /// // Create a client for calling contract B. /// let client = ContractBClient::new(&env, &contract_b_id); @@ -342,7 +342,7 @@ pub use soroban_sdk_macros::contractimport; /// # #[cfg(feature = "testutils")] /// # fn main() { /// let env = Env::default(); -/// let contract_id = env.register_contract(None, HelloContract); +/// let contract_id = env.register(HelloContract, ()); /// let client = HelloContractClient::new(&env, &contract_id); /// /// let words = client.hello(&symbol_short!("Dev")); @@ -383,7 +383,7 @@ pub use soroban_sdk_macros::contract; /// # #[cfg(feature = "testutils")] /// # fn main() { /// let env = Env::default(); -/// let contract_id = env.register_contract(None, HelloContract); +/// let contract_id = env.register(HelloContract, ()); /// let client = HelloContractClient::new(&env, &contract_id); /// /// let words = client.hello(&symbol_short!("Dev")); @@ -423,7 +423,7 @@ pub use soroban_sdk_macros::contractimpl; /// # #[cfg(feature = "testutils")] /// # fn main() { /// let env = Env::default(); -/// let contract_id = env.register_contract(None, HelloContract); +/// let contract_id = env.register(HelloContract, ()); /// let client = HelloContractClient::new(&env, &contract_id); /// /// let words = client.hello(&symbol_short!("Dev")); @@ -503,7 +503,7 @@ pub use soroban_sdk_macros::contractmeta; /// # #[cfg(feature = "testutils")] /// # fn main() { /// let env = Env::default(); -/// let contract_id = env.register_contract(None, Contract); +/// let contract_id = env.register(Contract, ()); /// let client = ContractClient::new(&env, &contract_id); /// /// assert_eq!(client.increment(&1), 1); @@ -577,7 +577,7 @@ pub use soroban_sdk_macros::contractmeta; /// # #[cfg(feature = "testutils")] /// # fn main() { /// let env = Env::default(); -/// let contract_id = env.register_contract(None, Contract); +/// let contract_id = env.register(Contract, ()); /// let client = ContractClient::new(&env, &contract_id); /// /// assert_eq!(client.get(), None); @@ -637,7 +637,7 @@ pub use soroban_sdk_macros::contracttype; /// let env = Env::default(); /// /// // Register the hello contract. -/// let contract_id = env.register_contract(None, HelloContract); +/// let contract_id = env.register(HelloContract, ()); /// /// // Create a client for the hello contract, that was constructed using /// // the trait. diff --git a/soroban-sdk/src/map.rs b/soroban-sdk/src/map.rs index cf0f3877f..05582a77f 100644 --- a/soroban-sdk/src/map.rs +++ b/soroban-sdk/src/map.rs @@ -225,18 +225,21 @@ where } #[cfg(not(target_family = "wasm"))] -impl TryFrom<&Map> for ScVal { - type Error = ConversionError; - fn try_from(v: &Map) -> Result { - Ok(ScVal::try_from_val(&v.env, &v.obj.to_val())?) +impl From<&Map> for ScVal { + fn from(v: &Map) -> Self { + // This conversion occurs only in test utilities, and theoretically all + // values should convert to an ScVal because the Env won't let the host + // type to exist otherwise, unwrapping. Even if there are edge cases + // that don't, this is a trade off for a better test developer + // experience. + ScVal::try_from_val(&v.env, &v.obj.to_val()).unwrap() } } #[cfg(not(target_family = "wasm"))] -impl TryFrom> for ScVal { - type Error = ConversionError; - fn try_from(v: Map) -> Result { - (&v).try_into() +impl From> for ScVal { + fn from(v: Map) -> Self { + (&v).into() } } @@ -244,7 +247,7 @@ impl TryFrom> for ScVal { impl TryFromVal> for ScVal { type Error = ConversionError; fn try_from_val(_e: &Env, v: &Map) -> Result { - v.try_into() + Ok(v.into()) } } diff --git a/soroban-sdk/src/num.rs b/soroban-sdk/src/num.rs index cf321b597..a3ed92ac6 100644 --- a/soroban-sdk/src/num.rs +++ b/soroban-sdk/src/num.rs @@ -110,22 +110,25 @@ macro_rules! impl_num_wrapping_val_type { } #[cfg(not(target_family = "wasm"))] - impl TryFrom<&$wrapper> for ScVal { - type Error = ConversionError; - fn try_from(v: &$wrapper) -> Result { + impl From<&$wrapper> for ScVal { + fn from(v: &$wrapper) -> Self { + // This conversion occurs only in test utilities, and theoretically all + // values should convert to an ScVal because the Env won't let the host + // type to exist otherwise, unwrapping. Even if there are edge cases + // that don't, this is a trade off for a better test developer + // experience. if let Ok(ss) = <$small>::try_from(v.val) { - ScVal::try_from(ss) + ScVal::try_from(ss).unwrap() } else { - Ok(ScVal::try_from_val(&v.env, &v.to_val())?) + ScVal::try_from_val(&v.env, &v.to_val()).unwrap() } } } #[cfg(not(target_family = "wasm"))] - impl TryFrom<$wrapper> for ScVal { - type Error = ConversionError; - fn try_from(v: $wrapper) -> Result { - (&v).try_into() + impl From<$wrapper> for ScVal { + fn from(v: $wrapper) -> Self { + (&v).into() } } diff --git a/soroban-sdk/src/prng.rs b/soroban-sdk/src/prng.rs index 8b0425c9f..b91e0df1c 100644 --- a/soroban-sdk/src/prng.rs +++ b/soroban-sdk/src/prng.rs @@ -120,7 +120,7 @@ impl Prng { /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); - /// # let contract_id = env.register_contract(None, Contract); + /// # let contract_id = env.register(Contract, ()); /// # env.as_contract(&contract_id, || { /// # env.prng().seed(Bytes::from_array(&env, &[1; 32])); /// let mut value: u64 = 0; @@ -143,7 +143,7 @@ impl Prng { /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); - /// # let contract_id = env.register_contract(None, Contract); + /// # let contract_id = env.register(Contract, ()); /// # env.as_contract(&contract_id, || { /// # env.prng().seed(Bytes::from_array(&env, &[1; 32])); /// let mut value = [0u8; 32]; @@ -188,7 +188,7 @@ impl Prng { /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); - /// # let contract_id = env.register_contract(None, Contract); + /// # let contract_id = env.register(Contract, ()); /// # env.as_contract(&contract_id, || { /// # env.prng().seed(Bytes::from_array(&env, &[1; 32])); /// let value: u64 = env.prng().gen(); @@ -210,7 +210,7 @@ impl Prng { /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); - /// # let contract_id = env.register_contract(None, Contract); + /// # let contract_id = env.register(Contract, ()); /// # env.as_contract(&contract_id, || { /// # env.prng().seed(Bytes::from_array(&env, &[1; 32])); /// let value: [u8; 32] = env.prng().gen(); @@ -258,7 +258,7 @@ impl Prng { /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); - /// # let contract_id = env.register_contract(None, Contract); + /// # let contract_id = env.register(Contract, ()); /// # env.as_contract(&contract_id, || { /// # env.prng().seed(Bytes::from_array(&env, &[1; 32])); /// // Get a value of length 32 bytes. @@ -307,7 +307,7 @@ impl Prng { /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); - /// # let contract_id = env.register_contract(None, Contract); + /// # let contract_id = env.register(Contract, ()); /// # env.as_contract(&contract_id, || { /// # env.prng().seed(Bytes::from_array(&env, &[1; 32])); /// // Get a value in the range of 1 to 100, inclusive. @@ -347,7 +347,7 @@ impl Prng { /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); - /// # let contract_id = env.register_contract(None, Contract); + /// # let contract_id = env.register(Contract, ()); /// # env.as_contract(&contract_id, || { /// # env.prng().seed(Bytes::from_array(&env, &[1; 32])); /// // Get a value in the range of 1 to 100, inclusive. diff --git a/soroban-sdk/src/storage.rs b/soroban-sdk/src/storage.rs index 9bd0a3ea0..f2e74ec46 100644 --- a/soroban-sdk/src/storage.rs +++ b/soroban-sdk/src/storage.rs @@ -53,7 +53,7 @@ use crate::{ /// # #[cfg(feature = "testutils")] /// # fn main() { /// # let env = Env::default(); -/// # let contract_id = env.register_contract(None, Contract); +/// # let contract_id = env.register(Contract, ()); /// # ContractClient::new(&env, &contract_id).f(); /// # } /// # #[cfg(not(feature = "testutils"))] diff --git a/soroban-sdk/src/string.rs b/soroban-sdk/src/string.rs index 1c48ecb30..671624865 100644 --- a/soroban-sdk/src/string.rs +++ b/soroban-sdk/src/string.rs @@ -137,18 +137,21 @@ impl From<&String> for String { } #[cfg(not(target_family = "wasm"))] -impl TryFrom<&String> for ScVal { - type Error = ConversionError; - fn try_from(v: &String) -> Result { - Ok(ScVal::try_from_val(&v.env, &v.obj.to_val())?) +impl From<&String> for ScVal { + fn from(v: &String) -> Self { + // This conversion occurs only in test utilities, and theoretically all + // values should convert to an ScVal because the Env won't let the host + // type to exist otherwise, unwrapping. Even if there are edge cases + // that don't, this is a trade off for a better test developer + // experience. + ScVal::try_from_val(&v.env, &v.obj.to_val()).unwrap() } } #[cfg(not(target_family = "wasm"))] -impl TryFrom for ScVal { - type Error = ConversionError; - fn try_from(v: String) -> Result { - (&v).try_into() +impl From for ScVal { + fn from(v: String) -> Self { + (&v).into() } } diff --git a/soroban-sdk/src/symbol.rs b/soroban-sdk/src/symbol.rs index 7f11fd3be..6389d382b 100644 --- a/soroban-sdk/src/symbol.rs +++ b/soroban-sdk/src/symbol.rs @@ -136,23 +136,26 @@ impl TryFromVal for Symbol { } #[cfg(not(target_family = "wasm"))] -impl TryFrom<&Symbol> for ScVal { - type Error = ConversionError; - fn try_from(v: &Symbol) -> Result { +impl From<&Symbol> for ScVal { + fn from(v: &Symbol) -> Self { + // This conversion occurs only in test utilities, and theoretically all + // values should convert to an ScVal because the Env won't let the host + // type to exist otherwise, unwrapping. Even if there are edge cases + // that don't, this is a trade off for a better test developer + // experience. if let Ok(ss) = SymbolSmall::try_from(v.val) { - Ok(ScVal::try_from(ss)?) + ScVal::try_from(ss).unwrap() } else { - let e: Env = v.env.clone().try_into()?; - Ok(ScVal::try_from_val(&e, &v.to_val())?) + let e: Env = v.env.clone().try_into().unwrap(); + ScVal::try_from_val(&e, &v.to_val()).unwrap() } } } #[cfg(not(target_family = "wasm"))] -impl TryFrom for ScVal { - type Error = ConversionError; - fn try_from(v: Symbol) -> Result { - (&v).try_into() +impl From for ScVal { + fn from(v: Symbol) -> Self { + (&v).into() } } @@ -160,7 +163,7 @@ impl TryFrom for ScVal { impl TryFromVal for ScVal { type Error = ConversionError; fn try_from_val(_e: &Env, v: &Symbol) -> Result { - v.try_into() + Ok(v.into()) } } diff --git a/soroban-sdk/src/tests.rs b/soroban-sdk/src/tests.rs index 268ffb75f..9d6c0a962 100644 --- a/soroban-sdk/src/tests.rs +++ b/soroban-sdk/src/tests.rs @@ -17,6 +17,7 @@ mod contract_snapshot; mod contract_store; mod contract_timepoint; mod contract_udt_enum; +mod contract_udt_enum_error; mod contract_udt_option; mod contract_udt_struct; mod contract_udt_struct_tuple; diff --git a/soroban-sdk/src/tests/auth/auth_10_one.rs b/soroban-sdk/src/tests/auth/auth_10_one.rs index f23ffecff..6de963a0b 100644 --- a/soroban-sdk/src/tests/auth/auth_10_one.rs +++ b/soroban-sdk/src/tests/auth/auth_10_one.rs @@ -22,7 +22,7 @@ impl Contract { #[test] fn test() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let a = Address::generate(&e); diff --git a/soroban-sdk/src/tests/auth/auth_15_one_repeat.rs b/soroban-sdk/src/tests/auth/auth_15_one_repeat.rs index d299b08b8..ba37f4e4d 100644 --- a/soroban-sdk/src/tests/auth/auth_15_one_repeat.rs +++ b/soroban-sdk/src/tests/auth/auth_15_one_repeat.rs @@ -26,7 +26,7 @@ impl Contract { #[test] fn test() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let a = Address::generate(&e); diff --git a/soroban-sdk/src/tests/auth/auth_17_no_consume_requirement.rs b/soroban-sdk/src/tests/auth/auth_17_no_consume_requirement.rs index 663f61063..f1d6a5cf8 100644 --- a/soroban-sdk/src/tests/auth/auth_17_no_consume_requirement.rs +++ b/soroban-sdk/src/tests/auth/auth_17_no_consume_requirement.rs @@ -31,7 +31,7 @@ impl Contract { #[test] fn test() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let a = Address::generate(&e); diff --git a/soroban-sdk/src/tests/auth/auth_20_deep_one_address.rs b/soroban-sdk/src/tests/auth/auth_20_deep_one_address.rs index 4adcdc7c8..99183ec29 100644 --- a/soroban-sdk/src/tests/auth/auth_20_deep_one_address.rs +++ b/soroban-sdk/src/tests/auth/auth_20_deep_one_address.rs @@ -31,8 +31,8 @@ impl ContractB { #[test] fn test() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractAClient::new(&e, &contract_a_id); let a = Address::generate(&e); @@ -61,8 +61,8 @@ fn test() { #[should_panic = "HostError: Error(Auth, InvalidAction)"] fn test_auth_tree() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractAClient::new(&e, &contract_a_id); let a = Address::generate(&e); diff --git a/soroban-sdk/src/tests/auth/auth_30_deep_one_address_repeat.rs b/soroban-sdk/src/tests/auth/auth_30_deep_one_address_repeat.rs index 10a02ed61..b9c11140b 100644 --- a/soroban-sdk/src/tests/auth/auth_30_deep_one_address_repeat.rs +++ b/soroban-sdk/src/tests/auth/auth_30_deep_one_address_repeat.rs @@ -32,8 +32,8 @@ impl ContractB { #[test] fn test() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractAClient::new(&e, &contract_a_id); let a = Address::generate(&e); @@ -76,8 +76,8 @@ fn test() { #[should_panic = "HostError: Error(Auth, InvalidAction)"] fn test_auth_tree() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractAClient::new(&e, &contract_a_id); let a = Address::generate(&e); diff --git a/soroban-sdk/src/tests/auth/auth_35_deep_one_address_repeat_grouped.rs b/soroban-sdk/src/tests/auth/auth_35_deep_one_address_repeat_grouped.rs index 7cd8e5cb9..e70448866 100644 --- a/soroban-sdk/src/tests/auth/auth_35_deep_one_address_repeat_grouped.rs +++ b/soroban-sdk/src/tests/auth/auth_35_deep_one_address_repeat_grouped.rs @@ -32,8 +32,8 @@ impl ContractB { #[test] fn test() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractAClient::new(&e, &contract_a_id); let a = Address::generate(&e); @@ -76,8 +76,8 @@ fn test() { #[should_panic = "HostError: Error(Auth, InvalidAction)"] fn test_auth_tree() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractAClient::new(&e, &contract_a_id); let a = Address::generate(&e); diff --git a/soroban-sdk/src/tests/auth/auth_40_multi_one_address.rs b/soroban-sdk/src/tests/auth/auth_40_multi_one_address.rs index e9d151475..c797227f9 100644 --- a/soroban-sdk/src/tests/auth/auth_40_multi_one_address.rs +++ b/soroban-sdk/src/tests/auth/auth_40_multi_one_address.rs @@ -32,8 +32,8 @@ impl ContractB { #[test] fn test_auth_not_allowed_with_separated_tree() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractAClient::new(&e, &contract_a_id); let a = Address::generate(&e); @@ -66,8 +66,8 @@ fn test_auth_not_allowed_with_separated_tree() { #[test] fn test_auth_as_tree() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractAClient::new(&e, &contract_a_id); let a = Address::generate(&e); diff --git a/soroban-sdk/src/tests/budget.rs b/soroban-sdk/src/tests/budget.rs index be8ba657c..8391b8990 100644 --- a/soroban-sdk/src/tests/budget.rs +++ b/soroban-sdk/src/tests/budget.rs @@ -18,7 +18,7 @@ impl Contract { #[test] fn test_budget() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); e.budget().reset_default(); diff --git a/soroban-sdk/src/tests/contract_add_i32.rs b/soroban-sdk/src/tests/contract_add_i32.rs index 813dcf9ac..9721f8cb3 100644 --- a/soroban-sdk/src/tests/contract_add_i32.rs +++ b/soroban-sdk/src/tests/contract_add_i32.rs @@ -18,7 +18,7 @@ impl Contract { #[test] fn test_functional() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let a = 10i32; let b = 12i32; diff --git a/soroban-sdk/src/tests/contract_assert.rs b/soroban-sdk/src/tests/contract_assert.rs index ffb295d8e..d92681331 100644 --- a/soroban-sdk/src/tests/contract_assert.rs +++ b/soroban-sdk/src/tests/contract_assert.rs @@ -22,7 +22,7 @@ impl Contract { #[should_panic(expected = "Error(Contract, #1")] fn test_invoke_expect_error() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); ContractClient::new(&e, &contract_id).assert(&0); } @@ -30,7 +30,7 @@ fn test_invoke_expect_error() { #[test] fn test_try_invoke() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let res = ContractClient::new(&e, &contract_id).try_assert(&0); assert_eq!(res, Err(Ok(soroban_sdk::Error::from_contract_error(1)))); diff --git a/soroban-sdk/src/tests/contract_docs.rs b/soroban-sdk/src/tests/contract_docs.rs index 89fbe201d..eeac38cc5 100644 --- a/soroban-sdk/src/tests/contract_docs.rs +++ b/soroban-sdk/src/tests/contract_docs.rs @@ -18,7 +18,7 @@ mod fn_ { #[test] fn test_functional() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let client = ContractClient::new(&env, &contract_id); client.add(); } diff --git a/soroban-sdk/src/tests/contract_duration.rs b/soroban-sdk/src/tests/contract_duration.rs index 8b991f4f4..03804c477 100644 --- a/soroban-sdk/src/tests/contract_duration.rs +++ b/soroban-sdk/src/tests/contract_duration.rs @@ -12,7 +12,7 @@ impl Contract { #[test] fn test_functional() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let client = ContractClient::new(&env, &contract_id); let t: Duration = xdr::ScVal::Duration(xdr::Duration(0)).into_val(&env); diff --git a/soroban-sdk/src/tests/contract_fn.rs b/soroban-sdk/src/tests/contract_fn.rs index 352ded1b5..7eaf18164 100644 --- a/soroban-sdk/src/tests/contract_fn.rs +++ b/soroban-sdk/src/tests/contract_fn.rs @@ -18,7 +18,7 @@ impl Contract { #[test] fn test_functional() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let a = 10i32; let b = 12i32; diff --git a/soroban-sdk/src/tests/contract_invoke.rs b/soroban-sdk/src/tests/contract_invoke.rs index 7ce8ce29f..6567bfcc5 100644 --- a/soroban-sdk/src/tests/contract_invoke.rs +++ b/soroban-sdk/src/tests/contract_invoke.rs @@ -15,7 +15,7 @@ impl Contract { #[should_panic(expected = "I panicked")] fn test_invoke_expect_string() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); ContractClient::new(&e, &contract_id).panic(); } @@ -24,7 +24,7 @@ fn test_invoke_expect_string() { #[should_panic(expected = "Error(WasmVm, InvalidAction)")] fn test_invoke_expect_error() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); ContractClient::new(&e, &contract_id).panic(); } @@ -34,7 +34,7 @@ fn test_try_invoke() { use soroban_env_host::xdr::{ScErrorCode, ScErrorType}; let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let res = ContractClient::new(&e, &contract_id).try_panic(); assert_eq!( diff --git a/soroban-sdk/src/tests/contract_invoke_arg_count.rs b/soroban-sdk/src/tests/contract_invoke_arg_count.rs index 32cd30c09..ee5d43dc9 100644 --- a/soroban-sdk/src/tests/contract_invoke_arg_count.rs +++ b/soroban-sdk/src/tests/contract_invoke_arg_count.rs @@ -32,9 +32,9 @@ impl AddContract { fn test_correct_arg_count() { let e = Env::default(); - let add_contract_id = e.register_contract(None, AddContract); + let add_contract_id = e.register(AddContract, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 10i32; @@ -48,9 +48,9 @@ fn test_correct_arg_count() { fn test_too_few_args() { let e = Env::default(); - let add_contract_id = e.register_contract(None, AddContract); + let add_contract_id = e.register(AddContract, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 10i32; @@ -62,9 +62,9 @@ fn test_too_few_args() { fn test_too_many_args() { let e = Env::default(); - let add_contract_id = e.register_contract(None, AddContract); + let add_contract_id = e.register(AddContract, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 10i32; diff --git a/soroban-sdk/src/tests/contract_overlapping_type_fn_names.rs b/soroban-sdk/src/tests/contract_overlapping_type_fn_names.rs index cc46c4475..ad3411993 100644 --- a/soroban-sdk/src/tests/contract_overlapping_type_fn_names.rs +++ b/soroban-sdk/src/tests/contract_overlapping_type_fn_names.rs @@ -20,7 +20,7 @@ impl Contract { #[test] fn test_functional() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let client = ContractClient::new(&env, &contract_id); let s = client.state(); diff --git a/soroban-sdk/src/tests/contract_snapshot.rs b/soroban-sdk/src/tests/contract_snapshot.rs index 3a4086f71..8014cd62b 100644 --- a/soroban-sdk/src/tests/contract_snapshot.rs +++ b/soroban-sdk/src/tests/contract_snapshot.rs @@ -17,7 +17,7 @@ impl Contract { #[test] fn test() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let contract_id_xdr = xdr::ScAddress::try_from(&contract_id).unwrap(); let client = ContractClient::new(&e, &contract_id); @@ -28,7 +28,7 @@ fn test() { let e = Env::from_ledger_snapshot(snapshot); let contract_id = Address::try_from_val(&e, &contract_id_xdr).unwrap(); - e.register_contract(&contract_id, Contract); + e.register_at(&contract_id, Contract, ()); let client = ContractClient::new(&e, &contract_id); assert_eq!(client.get(&2), 4); diff --git a/soroban-sdk/src/tests/contract_store.rs b/soroban-sdk/src/tests/contract_store.rs index a8d8efac3..f929789f5 100644 --- a/soroban-sdk/src/tests/contract_store.rs +++ b/soroban-sdk/src/tests/contract_store.rs @@ -61,7 +61,7 @@ fn test_storage() { e.ledger().set_min_temp_entry_ttl(50); e.ledger().set_max_entry_ttl(20_000); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); // Smoke test instance bump before putting any data into it. @@ -186,7 +186,7 @@ fn test_temp_storage_extension_past_max_ttl_panics() { let e = Env::default(); e.ledger().set_min_temp_entry_ttl(50); e.ledger().set_max_entry_ttl(20_000); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); e.as_contract(&contract_id, || { e.storage().temporary().set(&DataKey::Key(11), &2222_i32); diff --git a/soroban-sdk/src/tests/contract_timepoint.rs b/soroban-sdk/src/tests/contract_timepoint.rs index ea1b6069a..19a777b04 100644 --- a/soroban-sdk/src/tests/contract_timepoint.rs +++ b/soroban-sdk/src/tests/contract_timepoint.rs @@ -12,7 +12,7 @@ impl Contract { #[test] fn test_functional() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let client = ContractClient::new(&env, &contract_id); let t: Timepoint = xdr::ScVal::Timepoint(xdr::TimePoint(0)).into_val(&env); diff --git a/soroban-sdk/src/tests/contract_udt_enum.rs b/soroban-sdk/src/tests/contract_udt_enum.rs index 31f2275f2..7e194c236 100644 --- a/soroban-sdk/src/tests/contract_udt_enum.rs +++ b/soroban-sdk/src/tests/contract_udt_enum.rs @@ -34,7 +34,7 @@ impl Contract { #[test] fn test_functional() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let client = ContractClient::new(&env, &contract_id); let a = Udt::Aaa; diff --git a/soroban-sdk/src/tests/contract_udt_enum_error.rs b/soroban-sdk/src/tests/contract_udt_enum_error.rs new file mode 100644 index 000000000..fbe61034f --- /dev/null +++ b/soroban-sdk/src/tests/contract_udt_enum_error.rs @@ -0,0 +1,57 @@ +use crate::{self as soroban_sdk}; +use soroban_sdk::{contract, contracterror, contractimpl, contracttype, Env}; + +#[contract] +pub struct Contract; + +#[contracttype] +pub enum Flag { + A = 0, + B = 1, +} + +#[contracterror] +pub enum Error { + AnError = 1, +} + +#[contractimpl] +impl Contract { + pub fn f(flag: Flag) -> Result<(), Error> { + match flag { + Flag::A => Ok(()), + Flag::B => Err(Error::AnError), + } + } +} + +// The assertions in the following tests intentionally don't use assert_eq, and +// unwraps the object using Rust's pattern matching, so that no derives like +// Debug, Eq, PartialEq, etc are required on the error enum type because this +// test serves to ensure that none of the generated code for the error type or +// the client that uses it is dependent on derives that may or may not be +// available. + +#[test] +fn test_success() { + let env = Env::default(); + let contract_id = env.register(Contract, ()); + let client = ContractClient::new(&env, &contract_id); + + // See comment above about why this assertion is a match. + let Ok(Ok(())) = client.try_f(&Flag::A) else { + panic!("unexpected value returned"); + }; +} + +#[test] +fn test_error() { + let env = Env::default(); + let contract_id = env.register(Contract, ()); + let client = ContractClient::new(&env, &contract_id); + + // See comment above about why this assertion is a match. + let Err(Ok(Error::AnError)) = client.try_f(&Flag::B) else { + panic!("unexpected value returned"); + }; +} diff --git a/soroban-sdk/src/tests/contract_udt_option.rs b/soroban-sdk/src/tests/contract_udt_option.rs index 18cde33d2..ae55979c8 100644 --- a/soroban-sdk/src/tests/contract_udt_option.rs +++ b/soroban-sdk/src/tests/contract_udt_option.rs @@ -21,7 +21,7 @@ impl Contract { #[test] fn test_functional() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let a = Udt { a: 5, b: None }; let b = Udt { a: 10, b: Some(1) }; diff --git a/soroban-sdk/src/tests/contract_udt_struct.rs b/soroban-sdk/src/tests/contract_udt_struct.rs index f602b5ec4..af6ce8363 100644 --- a/soroban-sdk/src/tests/contract_udt_struct.rs +++ b/soroban-sdk/src/tests/contract_udt_struct.rs @@ -47,7 +47,7 @@ impl Contract { #[test] fn test_functional() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let a = Udt { a: 5, b: 7 }; let b = Udt { a: 10, b: 14 }; @@ -58,7 +58,7 @@ fn test_functional() { #[test] fn test_long_names_functional() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let a = UdtWithLongName { this_is_a_very_long_name_12345: 1_000_000_000_000, diff --git a/soroban-sdk/src/tests/contract_udt_struct_tuple.rs b/soroban-sdk/src/tests/contract_udt_struct_tuple.rs index f7e623991..872a8221d 100644 --- a/soroban-sdk/src/tests/contract_udt_struct_tuple.rs +++ b/soroban-sdk/src/tests/contract_udt_struct_tuple.rs @@ -35,7 +35,7 @@ fn test_conversion() { #[test] fn test_functional() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let a = Udt(5, 7); let b = Udt(10, 14); diff --git a/soroban-sdk/src/tests/contractimport.rs b/soroban-sdk/src/tests/contractimport.rs index 064188346..bccd95dcc 100644 --- a/soroban-sdk/src/tests/contractimport.rs +++ b/soroban-sdk/src/tests/contractimport.rs @@ -46,9 +46,9 @@ impl Contract { fn test_functional() { let e = Env::default(); - let add_contract_id = e.register_contract_wasm(None, addcontract::WASM); + let add_contract_id = e.register(addcontract::WASM, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 10u64; @@ -62,9 +62,9 @@ fn test_register_at_id() { let e = Env::default(); let add_contract_id = Address::from_contract_id(&e, [1; 32]); - e.register_contract_wasm(&add_contract_id, addcontract::WASM); + e.register_at(&add_contract_id, addcontract::WASM, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 10u64; @@ -76,12 +76,12 @@ fn test_register_at_id() { #[test] fn test_reregister_wasm() { let e = Env::default(); - let add_contract_id = e.register_contract_wasm(None, addcontract_u128::WASM); + let add_contract_id = e.register(addcontract_u128::WASM, ()); // Reregister the contract with different code replacing the code. This is // the contract we expect to be executed. - e.register_contract_wasm(&add_contract_id, addcontract::WASM); + e.register_at(&add_contract_id, addcontract::WASM, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 10u64; @@ -95,12 +95,12 @@ fn test_reregister_over_wasm_with_rust_impl() { let e = Env::default(); // Register a contract with wasm. - let other_contract_id = e.register_contract_wasm(None, addcontract::WASM); + let other_contract_id = e.register(addcontract::WASM, ()); // Reregister the contract with a rust impl instead that does something // different. - e.register_contract(&other_contract_id, subcontract::Contract); + e.register_at(&other_contract_id, subcontract::Contract, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 12u64; diff --git a/soroban-sdk/src/tests/contractimport_with_error.rs b/soroban-sdk/src/tests/contractimport_with_error.rs index 30da50697..fba3aa1f3 100644 --- a/soroban-sdk/src/tests/contractimport_with_error.rs +++ b/soroban-sdk/src/tests/contractimport_with_error.rs @@ -13,7 +13,7 @@ pub struct Contract; #[contractimpl] impl Contract { - pub fn hello_with(env: Env, contract_id: Address, flag: u32) -> Symbol { + pub fn hello_with(env: Env, contract_id: Address, flag: errcontract::Flag) -> Symbol { errcontract::Client::new(&env, &contract_id).hello(&flag) } } @@ -22,11 +22,11 @@ impl Contract { fn test_functional() { let e = Env::default(); - let err_contract_id = e.register_contract_wasm(None, errcontract::WASM); + let err_contract_id = e.register(errcontract::WASM, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); - let z = client.hello_with(&err_contract_id, &0); + let z = client.hello_with(&err_contract_id, &errcontract::Flag::A); assert!(z == symbol_short!("hello")); } diff --git a/soroban-sdk/src/tests/env.rs b/soroban-sdk/src/tests/env.rs index 1042045d0..d5adb0f07 100644 --- a/soroban-sdk/src/tests/env.rs +++ b/soroban-sdk/src/tests/env.rs @@ -44,8 +44,8 @@ fn default_and_from_snapshot_same_settings() { assert!(env1.host().source_account_address().unwrap().is_some()); assert!(env2.host().source_account_address().unwrap().is_some()); - let c1addr = env1.register_contract(None, Contract); - let c2addr = env2.register_contract(None, Contract); + let c1addr = env1.register(Contract, ()); + let c2addr = env2.register(Contract, ()); let c1client = ContractClient::new(&env1, &c1addr); let c2client = ContractClient::new(&env2, &c2addr); @@ -83,21 +83,21 @@ fn register_contract_deploys_predictable_contract_ids() { let env1 = Env::default(); let env2 = Env::from_snapshot(env1.to_snapshot()); - let env1addr1 = env1.register_contract(None, Contract); + let env1addr1 = env1.register(Contract, ()); println!("env1 addr1 {:?}", env1addr1.contract_id()); - let env1addr2 = env1.register_contract(None, Contract); + let env1addr2 = env1.register(Contract, ()); println!("env1 addr2 {:?}", env1addr2.contract_id()); - let env2addr1 = env2.register_contract(None, Contract); + let env2addr1 = env2.register(Contract, ()); println!("env2 addr1 {:?}", env2addr1.contract_id()); - let env2addr2 = env2.register_contract(None, Contract); + let env2addr2 = env2.register(Contract, ()); println!("env2 addr2 {:?}", env2addr2.contract_id()); let env3 = Env::from_snapshot(env1.to_snapshot()); - let env1addr3 = env1.register_contract(None, Contract); + let env1addr3 = env1.register(Contract, ()); println!("env1 addr3 {:?}", env1addr3.contract_id()); - let env2addr3 = env2.register_contract(None, Contract); + let env2addr3 = env2.register(Contract, ()); println!("env2 addr3 {:?}", env2addr3.contract_id()); - let env3addr3 = env3.register_contract(None, Contract); + let env3addr3 = env3.register(Contract, ()); println!("env3 addr3 {:?}", env3addr3.contract_id()); // Check that contracts deployed in the envs are consistent and predictable. @@ -132,9 +132,9 @@ fn test_snapshot_file() { assert!(!p2.exists()); { let e3 = Env::default(); // When dropped will be written to p1. - let _ = e3.register_contract(None, Contract); + let _ = e3.register(Contract, ()); } // Env dropped, written to p1. - let c = e1.register_contract(None, Contract); + let c = e1.register(Contract, ()); assert!(p1.exists()); assert!(!p2.exists()); e1.as_contract(&c, || {}); @@ -163,11 +163,11 @@ fn test_snapshot_file_disabled() { let _ = std::fs::remove_file(&p2); { let e1 = Env::default(); - let _ = e1.register_contract(None, Contract); + let _ = e1.register(Contract, ()); let e2 = Env::new_with_config(EnvTestConfig { capture_snapshot_at_drop: false, }); - let _ = e2.register_contract(None, Contract); + let _ = e2.register(Contract, ()); assert!(!p1.exists()); assert!(!p2.exists()); } @@ -190,12 +190,12 @@ fn test_snapshot_file_disabled_after_creation() { let _ = std::fs::remove_file(&p2); { let e1 = Env::default(); - let _ = e1.register_contract(None, Contract); + let _ = e1.register(Contract, ()); let mut e2 = Env::default(); e2.set_config(EnvTestConfig { capture_snapshot_at_drop: false, }); - let _ = e2.register_contract(None, Contract); + let _ = e2.register(Contract, ()); assert!(!p1.exists()); assert!(!p2.exists()); } diff --git a/soroban-sdk/src/tests/max_ttl.rs b/soroban-sdk/src/tests/max_ttl.rs index 1cab646de..3f4c41763 100644 --- a/soroban-sdk/src/tests/max_ttl.rs +++ b/soroban-sdk/src/tests/max_ttl.rs @@ -7,7 +7,7 @@ pub struct Contract; #[test] fn max() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); e.ledger().set_sequence_number(1); e.ledger().set_max_entry_ttl(5); diff --git a/soroban-sdk/src/tests/prng.rs b/soroban-sdk/src/tests/prng.rs index 28213b859..ac8790a98 100644 --- a/soroban-sdk/src/tests/prng.rs +++ b/soroban-sdk/src/tests/prng.rs @@ -9,7 +9,7 @@ pub struct TestPrngContract; fn test_prng_seed() { let e = Env::default(); e.host().set_base_prng_seed([0; 32]).unwrap(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { e.prng().seed(bytes!( &e, @@ -19,7 +19,7 @@ fn test_prng_seed() { }); let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.host().set_base_prng_seed([2; 32]).unwrap(); e.as_contract(&id, || { e.prng().seed(bytes!( @@ -33,7 +33,7 @@ fn test_prng_seed() { #[test] fn test_prng_shuffle() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { let v = vec![&e, 1, 2, 3]; @@ -49,7 +49,7 @@ fn test_prng_shuffle() { #[test] fn test_vec_shuffle() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { let v = vec![&e, 1, 2, 3]; @@ -69,7 +69,7 @@ fn test_vec_shuffle() { #[test] fn test_prng_fill_u64() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { let mut v: u64 = 0; @@ -83,7 +83,7 @@ fn test_prng_fill_u64() { #[test] fn test_prng_gen_u64() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { assert_eq!(e.prng().gen::(), 6775509081846337106); @@ -94,7 +94,7 @@ fn test_prng_gen_u64() { #[test] fn test_prng_gen_range_u64() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { assert_eq!(e.prng().gen_range::(..), 6775509081846337106); @@ -114,7 +114,7 @@ fn test_prng_gen_range_u64() { #[should_panic(expected = "Error(Value, InvalidInput)")] fn test_prng_gen_range_u64_panic_on_invalid_range() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { e.prng().gen_range::(u64::MAX..u64::MAX); @@ -124,7 +124,7 @@ fn test_prng_gen_range_u64_panic_on_invalid_range() { #[test] fn test_prng_fill_bytes() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { let mut v = Bytes::from_array(&e, &[0u8; 32]); @@ -156,7 +156,7 @@ fn test_prng_fill_bytes() { #[test] fn test_prng_gen_len_bytes() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { assert_eq!( @@ -185,7 +185,7 @@ fn test_prng_gen_len_bytes() { #[test] fn test_prng_fill_bytesn() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { let mut v = BytesN::from_array(&e, &[0u8; 32]); @@ -217,7 +217,7 @@ fn test_prng_fill_bytesn() { #[test] fn test_prng_gen_bytesn() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { assert_eq!( @@ -246,7 +246,7 @@ fn test_prng_gen_bytesn() { #[test] fn test_prng_fill_slice() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { let mut buf = [0u8; 32]; @@ -273,7 +273,7 @@ fn test_prng_fill_slice() { #[test] fn test_prng_fill_array() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { let mut v = [0u8; 32]; @@ -299,7 +299,7 @@ fn test_prng_fill_array() { #[test] fn test_prng_gen_array() { let e = Env::default(); - let id = e.register_contract(None, TestPrngContract); + let id = e.register(TestPrngContract, ()); e.as_contract(&id, || { assert_eq!( diff --git a/soroban-sdk/src/tests/storage_testutils.rs b/soroban-sdk/src/tests/storage_testutils.rs index 0c730cef7..411f8a231 100644 --- a/soroban-sdk/src/tests/storage_testutils.rs +++ b/soroban-sdk/src/tests/storage_testutils.rs @@ -12,7 +12,7 @@ pub struct Contract; #[test] fn all() { let e = Env::default(); - let id = e.register_contract(None, Contract); + let id = e.register(Contract, ()); e.as_contract(&id, || { e.storage().instance().set(&1, &2); @@ -49,8 +49,8 @@ fn ttl_getters() { e.ledger().set_min_persistent_entry_ttl(100); e.ledger().set_min_temp_entry_ttl(10); - let contract_a = e.register_contract(None, Contract); - let contract_b = e.register_contract(None, Contract); + let contract_a = e.register(Contract, ()); + let contract_b = e.register(Contract, ()); let setup = || { e.storage().persistent().set(&1, &3); e.storage().temporary().set(&2, &4); @@ -113,7 +113,7 @@ fn temp_entry_expiration() { let e = Env::default(); e.ledger().set_sequence_number(1000); e.ledger().set_min_temp_entry_ttl(100); - let contract = e.register_contract(None, Contract); + let contract = e.register(Contract, ()); e.as_contract(&contract, || { e.storage().temporary().set(&1, &2); @@ -145,7 +145,7 @@ fn test_persistent_entry_expiration() { e.ledger().set_sequence_number(1000); e.ledger().set_min_persistent_entry_ttl(100); - let contract = e.register_contract(None, Contract); + let contract = e.register(Contract, ()); e.as_contract(&contract, || { e.storage().persistent().set(&1, &2); diff --git a/soroban-sdk/src/tests/token_client.rs b/soroban-sdk/src/tests/token_client.rs index bf12fa2ef..9d4ab68f0 100644 --- a/soroban-sdk/src/tests/token_client.rs +++ b/soroban-sdk/src/tests/token_client.rs @@ -73,7 +73,7 @@ fn test_mock_all_auth() { let sac = env.register_stellar_asset_contract_v2(admin); let token_contract_id = sac.address(); - let contract_id = env.register_contract(None, TestContract); + let contract_id = env.register(TestContract, ()); let client = TestContractClient::new(&env, &contract_id); client.init(&token_contract_id); @@ -121,7 +121,7 @@ fn test_mock_auth() { let admin = Address::generate(&env); let token_contract_id = env.register_stellar_asset_contract_v2(admin).address(); - let contract_id = env.register_contract(None, TestContract); + let contract_id = env.register(TestContract, ()); let client = TestContractClient::new(&env, &contract_id); client.init(&token_contract_id); diff --git a/soroban-sdk/src/testutils.rs b/soroban-sdk/src/testutils.rs index 61b7e0ef3..8aa6e0c3b 100644 --- a/soroban-sdk/src/testutils.rs +++ b/soroban-sdk/src/testutils.rs @@ -23,6 +23,36 @@ use soroban_ledger_snapshot::LedgerSnapshot; pub use crate::env::EnvTestConfig; +pub trait Register { + fn register<'i, I, A>(self, env: &Env, id: I, args: A) -> crate::Address + where + I: Into>, + A: ConstructorArgs; +} + +impl Register for C +where + C: ContractFunctionSet + 'static, +{ + fn register<'i, I, A>(self, env: &Env, id: I, args: A) -> crate::Address + where + I: Into>, + A: ConstructorArgs, + { + env.register_contract_with_constructor(id, self, args) + } +} + +impl<'w> Register for &'w [u8] { + fn register<'i, I, A>(self, env: &Env, id: I, args: A) -> crate::Address + where + I: Into>, + A: ConstructorArgs, + { + env.register_contract_wasm_with_constructor(id, self, args) + } +} + pub trait ConstructorArgs: IntoVal> {} impl ConstructorArgs for Vec {} diff --git a/soroban-sdk/src/vec.rs b/soroban-sdk/src/vec.rs index 46f66bc9b..d7ae8eefb 100644 --- a/soroban-sdk/src/vec.rs +++ b/soroban-sdk/src/vec.rs @@ -231,46 +231,46 @@ where use super::xdr::{ScVal, ScVec, VecM}; #[cfg(not(target_family = "wasm"))] -impl TryFrom<&Vec> for ScVal { - type Error = ConversionError; - fn try_from(v: &Vec) -> Result { - Ok(ScVal::try_from_val(&v.env, &v.obj.to_val())?) +impl From<&Vec> for ScVal { + fn from(v: &Vec) -> Self { + // This conversion occurs only in test utilities, and theoretically all + // values should convert to an ScVal because the Env won't let the host + // type to exist otherwise, unwrapping. Even if there are edge cases + // that don't, this is a trade off for a better test developer + // experience. + ScVal::try_from_val(&v.env, &v.obj.to_val()).unwrap() } } #[cfg(not(target_family = "wasm"))] -impl TryFrom<&Vec> for ScVec { - type Error = ConversionError; - fn try_from(v: &Vec) -> Result { - if let ScVal::Vec(Some(vec)) = ScVal::try_from(v)? { - Ok(vec) +impl From<&Vec> for ScVec { + fn from(v: &Vec) -> Self { + if let ScVal::Vec(Some(vec)) = ScVal::try_from(v).unwrap() { + vec } else { - Err(ConversionError) + panic!("expected ScVec") } } } #[cfg(not(target_family = "wasm"))] -impl TryFrom> for VecM { - type Error = ConversionError; - fn try_from(v: Vec) -> Result { - Ok(ScVec::try_from(v)?.0) +impl From> for VecM { + fn from(v: Vec) -> Self { + ScVec::from(v).0 } } #[cfg(not(target_family = "wasm"))] -impl TryFrom> for ScVal { - type Error = ConversionError; - fn try_from(v: Vec) -> Result { - (&v).try_into() +impl From> for ScVal { + fn from(v: Vec) -> Self { + (&v).into() } } #[cfg(not(target_family = "wasm"))] -impl TryFrom> for ScVec { - type Error = ConversionError; - fn try_from(v: Vec) -> Result { - (&v).try_into() +impl From> for ScVec { + fn from(v: Vec) -> Self { + (&v).into() } } diff --git a/soroban-sdk/test_snapshots/tests/contract_udt_enum_error/test_error.1.json b/soroban-sdk/test_snapshots/tests/contract_udt_enum_error/test_error.1.json new file mode 100644 index 000000000..4d0c8fda5 --- /dev/null +++ b/soroban-sdk/test_snapshots/tests/contract_udt_enum_error/test_error.1.json @@ -0,0 +1,216 @@ +{ + "generators": { + "address": 1, + "nonce": 0 + }, + "auth": [ + [], + [] + ], + "ledger": { + "protocol_version": 22, + "sequence_number": 0, + "timestamp": 0, + "network_id": "0000000000000000000000000000000000000000000000000000000000000000", + "base_reserve": 0, + "min_persistent_entry_ttl": 4096, + "min_temp_entry_ttl": 16, + "max_entry_ttl": 6312000, + "ledger_entries": [ + [ + { + "contract_data": { + "contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM", + "key": "ledger_key_contract_instance", + "durability": "persistent" + } + }, + [ + { + "last_modified_ledger_seq": 0, + "data": { + "contract_data": { + "ext": "v0", + "contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM", + "key": "ledger_key_contract_instance", + "durability": "persistent", + "val": { + "contract_instance": { + "executable": { + "wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + "storage": null + } + } + } + }, + "ext": "v0" + }, + 4095 + ] + ], + [ + { + "contract_code": { + "hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + } + }, + [ + { + "last_modified_ledger_seq": 0, + "data": { + "contract_code": { + "ext": "v0", + "hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "code": "" + } + }, + "ext": "v0" + }, + 4095 + ] + ] + ] + }, + "events": [ + { + "event": { + "ext": "v0", + "contract_id": null, + "type_": "diagnostic", + "body": { + "v0": { + "topics": [ + { + "symbol": "fn_call" + }, + { + "bytes": "0000000000000000000000000000000000000000000000000000000000000001" + }, + { + "symbol": "__constructor" + } + ], + "data": "void" + } + } + }, + "failed_call": false + }, + { + "event": { + "ext": "v0", + "contract_id": null, + "type_": "diagnostic", + "body": { + "v0": { + "topics": [ + { + "symbol": "fn_call" + }, + { + "bytes": "0000000000000000000000000000000000000000000000000000000000000001" + }, + { + "symbol": "f" + } + ], + "data": { + "u32": 1 + } + } + } + }, + "failed_call": false + }, + { + "event": { + "ext": "v0", + "contract_id": "0000000000000000000000000000000000000000000000000000000000000001", + "type_": "diagnostic", + "body": { + "v0": { + "topics": [ + { + "symbol": "fn_return" + }, + { + "symbol": "f" + } + ], + "data": { + "error": { + "contract": 1 + } + } + } + } + }, + "failed_call": true + }, + { + "event": { + "ext": "v0", + "contract_id": "0000000000000000000000000000000000000000000000000000000000000001", + "type_": "diagnostic", + "body": { + "v0": { + "topics": [ + { + "symbol": "error" + }, + { + "error": { + "contract": 1 + } + } + ], + "data": { + "string": "escalating Ok(ScErrorType::Contract) frame-exit to Err" + } + } + } + }, + "failed_call": true + }, + { + "event": { + "ext": "v0", + "contract_id": null, + "type_": "diagnostic", + "body": { + "v0": { + "topics": [ + { + "symbol": "error" + }, + { + "error": { + "contract": 1 + } + } + ], + "data": { + "vec": [ + { + "string": "contract try_call failed" + }, + { + "symbol": "f" + }, + { + "vec": [ + { + "u32": 1 + } + ] + } + ] + } + } + } + }, + "failed_call": false + } + ] +} \ No newline at end of file diff --git a/soroban-sdk/test_snapshots/tests/contract_udt_enum_error/test_success.1.json b/soroban-sdk/test_snapshots/tests/contract_udt_enum_error/test_success.1.json new file mode 100644 index 000000000..17ed13020 --- /dev/null +++ b/soroban-sdk/test_snapshots/tests/contract_udt_enum_error/test_success.1.json @@ -0,0 +1,148 @@ +{ + "generators": { + "address": 1, + "nonce": 0 + }, + "auth": [ + [], + [] + ], + "ledger": { + "protocol_version": 22, + "sequence_number": 0, + "timestamp": 0, + "network_id": "0000000000000000000000000000000000000000000000000000000000000000", + "base_reserve": 0, + "min_persistent_entry_ttl": 4096, + "min_temp_entry_ttl": 16, + "max_entry_ttl": 6312000, + "ledger_entries": [ + [ + { + "contract_data": { + "contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM", + "key": "ledger_key_contract_instance", + "durability": "persistent" + } + }, + [ + { + "last_modified_ledger_seq": 0, + "data": { + "contract_data": { + "ext": "v0", + "contract": "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM", + "key": "ledger_key_contract_instance", + "durability": "persistent", + "val": { + "contract_instance": { + "executable": { + "wasm": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + }, + "storage": null + } + } + } + }, + "ext": "v0" + }, + 4095 + ] + ], + [ + { + "contract_code": { + "hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" + } + }, + [ + { + "last_modified_ledger_seq": 0, + "data": { + "contract_code": { + "ext": "v0", + "hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + "code": "" + } + }, + "ext": "v0" + }, + 4095 + ] + ] + ] + }, + "events": [ + { + "event": { + "ext": "v0", + "contract_id": null, + "type_": "diagnostic", + "body": { + "v0": { + "topics": [ + { + "symbol": "fn_call" + }, + { + "bytes": "0000000000000000000000000000000000000000000000000000000000000001" + }, + { + "symbol": "__constructor" + } + ], + "data": "void" + } + } + }, + "failed_call": false + }, + { + "event": { + "ext": "v0", + "contract_id": null, + "type_": "diagnostic", + "body": { + "v0": { + "topics": [ + { + "symbol": "fn_call" + }, + { + "bytes": "0000000000000000000000000000000000000000000000000000000000000001" + }, + { + "symbol": "f" + } + ], + "data": { + "u32": 0 + } + } + } + }, + "failed_call": false + }, + { + "event": { + "ext": "v0", + "contract_id": "0000000000000000000000000000000000000000000000000000000000000001", + "type_": "diagnostic", + "body": { + "v0": { + "topics": [ + { + "symbol": "fn_return" + }, + { + "symbol": "f" + } + ], + "data": "void" + } + } + }, + "failed_call": false + } + ] +} \ No newline at end of file diff --git a/tests/account/src/lib.rs b/tests/account/src/lib.rs index bce7c204b..7ddbe9026 100644 --- a/tests/account/src/lib.rs +++ b/tests/account/src/lib.rs @@ -44,8 +44,8 @@ mod test { #[test] fn test() { let e = Env::default(); - let test_contract_id = e.register_contract(None, TestContract); - let contract_id = e.register_contract(None, Contract); + let test_contract_id = e.register(TestContract, ()); + let contract_id = e.register(Contract, ()); e.set_auths(&[MockAuth { address: &contract_id, diff --git a/tests/add_i128/src/lib.rs b/tests/add_i128/src/lib.rs index a611b5820..765c743f2 100644 --- a/tests/add_i128/src/lib.rs +++ b/tests/add_i128/src/lib.rs @@ -20,7 +20,7 @@ mod test { #[test] fn test_add() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 2i128.pow(70); diff --git a/tests/add_u128/src/lib.rs b/tests/add_u128/src/lib.rs index 9f20679cd..595ff53c2 100644 --- a/tests/add_u128/src/lib.rs +++ b/tests/add_u128/src/lib.rs @@ -20,7 +20,7 @@ mod test { #[test] fn test_add() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 2u128.pow(70); diff --git a/tests/add_u64/src/lib.rs b/tests/add_u64/src/lib.rs index 3a2bcd97e..78dc60692 100644 --- a/tests/add_u64/src/lib.rs +++ b/tests/add_u64/src/lib.rs @@ -20,7 +20,7 @@ mod test { #[test] fn test_add() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 10u64; diff --git a/tests/alloc/src/lib.rs b/tests/alloc/src/lib.rs index 49e722fd5..99953bea0 100644 --- a/tests/alloc/src/lib.rs +++ b/tests/alloc/src/lib.rs @@ -30,7 +30,7 @@ mod test { #[test] fn test_add() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let list = client.num_list(&5); diff --git a/tests/auth/src/lib.rs b/tests/auth/src/lib.rs index ffd7f3ae9..175207c33 100644 --- a/tests/auth/src/lib.rs +++ b/tests/auth/src/lib.rs @@ -33,7 +33,7 @@ mod test_a { fn test_with_mock_all_auth() { let e = Env::default(); - let contract_id = e.register_contract(None, ContractA); + let contract_id = e.register(ContractA, ()); let client = ContractAClient::new(&e, &contract_id); let a = Address::generate(&e); @@ -60,7 +60,7 @@ mod test_a { fn test_with_mock_auth() { let e = Env::default(); - let contract_id = e.register_contract(None, ContractA); + let contract_id = e.register(ContractA, ()); let client = ContractAClient::new(&e, &contract_id); let a = Address::generate(&e); @@ -97,10 +97,10 @@ mod test_a { fn test_with_real_contract_auth_approve() { let e = Env::default(); - let contract_id = e.register_contract(None, ContractA); + let contract_id = e.register(ContractA, ()); let client = ContractAClient::new(&e, &contract_id); - let a = e.register_contract(None, auth_approve::Contract); + let a = e.register(auth_approve::Contract, ()); let a_xdr: ScAddress = (&a).try_into().unwrap(); let r = client @@ -143,10 +143,10 @@ mod test_a { fn test_with_real_contract_auth_decline() { let e = Env::default(); - let contract_id = e.register_contract(None, ContractA); + let contract_id = e.register(ContractA, ()); let client = ContractAClient::new(&e, &contract_id); - let a = e.register_contract(None, auth_decline::Contract); + let a = e.register(auth_decline::Contract, ()); let a_xdr: ScAddress = (&a).try_into().unwrap(); let r = client @@ -251,8 +251,8 @@ mod test_b { fn test_with_mock_all_auth() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractBClient::new(&e, &contract_b_id); let a = Address::generate(&e); @@ -286,8 +286,8 @@ mod test_b { fn test_with_mock_auth() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractBClient::new(&e, &contract_b_id); let a = Address::generate(&e); @@ -336,11 +336,11 @@ mod test_b { fn test_with_real_contract_auth_approve() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractBClient::new(&e, &contract_b_id); - let a = e.register_contract(None, auth_approve::Contract); + let a = e.register(auth_approve::Contract, ()); let a_xdr: ScAddress = (&a).try_into().unwrap(); let r = client @@ -399,11 +399,11 @@ mod test_b { fn test_with_real_contract_auth_decline() { let e = Env::default(); - let contract_a_id = e.register_contract(None, ContractA); - let contract_b_id = e.register_contract(None, ContractB); + let contract_a_id = e.register(ContractA, ()); + let contract_b_id = e.register(ContractB, ()); let client = ContractBClient::new(&e, &contract_b_id); - let a = e.register_contract(None, auth_decline::Contract); + let a = e.register(auth_decline::Contract, ()); let a_xdr: ScAddress = (&a).try_into().unwrap(); let r = client diff --git a/tests/constructor/src/lib.rs b/tests/constructor/src/lib.rs index cdaa2a33f..3868bf933 100644 --- a/tests/constructor/src/lib.rs +++ b/tests/constructor/src/lib.rs @@ -37,7 +37,7 @@ impl Contract { #[test] fn test_constructor() { let env = Env::default(); - let contract_id = env.register_contract_with_constructor(None, Contract, (100_u32, 1000_i64)); + let contract_id = env.register(Contract, (100_u32, 1000_i64)); let client = ContractClient::new(&env, &contract_id); assert_eq!(client.get_data(&DataKey::Persistent(100)), Some(1000)); assert_eq!(client.get_data(&DataKey::Temp(200)), Some(2000)); @@ -52,26 +52,26 @@ fn test_constructor() { #[should_panic(expected = "constructor invocation has failed with error")] fn test_passing_no_constructor_arguments_causes_panic() { let env = Env::default(); - let _ = env.register_contract(None, Contract); + let _ = env.register(Contract, ()); } #[test] #[should_panic(expected = "constructor invocation has failed with error")] fn test_missing_constructor_arguments_causes_panic() { let env = Env::default(); - let _ = env.register_contract_with_constructor(None, Contract, (100_u32,)); + let _ = env.register(Contract, (100_u32,)); } #[test] #[should_panic(expected = "constructor invocation has failed with error")] fn test_passing_extra_constructor_arguments_causes_panic() { let env = Env::default(); - let _ = env.register_contract_with_constructor(None, Contract, (100_u32, 1000_i64, 123_u32)); + let _ = env.register(Contract, (100_u32, 1000_i64, 123_u32)); } #[test] #[should_panic(expected = "constructor invocation has failed with error")] fn test_passing_incorrectly_typed_constructor_arguments_causes_panic() { let env = Env::default(); - let _ = env.register_contract_with_constructor(None, Contract, (100_u32, 1000_u32)); + let _ = env.register(Contract, (100_u32, 1000_u32)); } diff --git a/tests/empty/src/lib.rs b/tests/empty/src/lib.rs index f8912a36d..27638b625 100644 --- a/tests/empty/src/lib.rs +++ b/tests/empty/src/lib.rs @@ -18,7 +18,7 @@ mod test { #[test] fn test_hello() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); client.empty(); diff --git a/tests/empty2/src/lib.rs b/tests/empty2/src/lib.rs index eac8bc747..e86d9cd36 100644 --- a/tests/empty2/src/lib.rs +++ b/tests/empty2/src/lib.rs @@ -16,7 +16,7 @@ mod test { #[test] fn test_hello() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let _client = ContractClient::new(&e, &contract_id); } } diff --git a/tests/errors/src/lib.rs b/tests/errors/src/lib.rs index 4091cca10..a18757674 100644 --- a/tests/errors/src/lib.rs +++ b/tests/errors/src/lib.rs @@ -1,32 +1,43 @@ #![no_std] use soroban_sdk::{ - contract, contracterror, contractimpl, panic_with_error, symbol_short, Env, Symbol, + contract, contracterror, contractimpl, contracttype, panic_with_error, symbol_short, Env, + Symbol, }; #[contract] pub struct Contract; +#[contracttype] +#[derive(PartialEq)] +pub enum Flag { + A = 0, + B = 1, + C = 2, + D = 3, + E = 4, +} + #[contracterror] -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq)] pub enum Error { AnError = 1, } #[contractimpl] impl Contract { - pub fn hello(env: Env, flag: u32) -> Result { + pub fn hello(env: Env, flag: Flag) -> Result { env.storage() .persistent() .set(&symbol_short!("persisted"), &true); - if flag == 0 { + if flag == Flag::A { Ok(symbol_short!("hello")) - } else if flag == 1 { + } else if flag == Flag::B { Err(Error::AnError) - } else if flag == 2 { + } else if flag == Flag::C { panic_with_error!(&env, Error::AnError) - } else if flag == 3 { + } else if flag == Flag::D { panic!("an error") - } else if flag == 4 { + } else if flag == Flag::E { panic_with_error!(&env, soroban_sdk::Error::from_contract_error(9)) } else { unimplemented!() @@ -46,15 +57,15 @@ impl Contract { mod test { use soroban_sdk::{symbol_short, xdr, Env, InvokeError}; - use crate::{Contract, ContractClient, Error}; + use crate::{Contract, ContractClient, Error, Flag}; #[test] fn hello_ok() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); - let res = client.hello(&0); + let res = client.hello(&Flag::A); assert_eq!(res, symbol_short!("hello")); assert!(client.persisted()); } @@ -62,10 +73,10 @@ mod test { #[test] fn try_hello_ok() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); - let res = client.try_hello(&0); + let res = client.try_hello(&Flag::A); assert_eq!(res, Ok(Ok(symbol_short!("hello")))); assert!(client.persisted()); } @@ -73,10 +84,10 @@ mod test { #[test] fn try_hello_error() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); - let res = client.try_hello(&1); + let res = client.try_hello(&Flag::B); assert_eq!(res, Err(Ok(Error::AnError))); assert!(!client.persisted()); } @@ -84,10 +95,10 @@ mod test { #[test] fn try_hello_error_panic() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); - let res = client.try_hello(&2); + let res = client.try_hello(&Flag::C); assert_eq!(res, Err(Ok(Error::AnError))); assert!(!client.persisted()); } @@ -95,10 +106,10 @@ mod test { #[test] fn try_hello_error_panic_string() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); - let res = client.try_hello(&3); + let res = client.try_hello(&Flag::D); assert_eq!(res, Err(Err(InvokeError::Abort))); assert!(!client.persisted()); } @@ -106,10 +117,10 @@ mod test { #[test] fn try_hello_error_unexpected_contract_error() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); - let res = client.try_hello(&4); + let res = client.try_hello(&Flag::E); assert_eq!(res, Err(Err(InvokeError::Contract(9)))); assert!(!client.persisted()); } diff --git a/tests/events/src/lib.rs b/tests/events/src/lib.rs index c0b2b41f6..00748d182 100644 --- a/tests/events/src/lib.rs +++ b/tests/events/src/lib.rs @@ -28,7 +28,7 @@ mod test { #[test] fn test_pub_event() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let client = ContractClient::new(&env, &contract_id); client.hello(); diff --git a/tests/fuzz/fuzz/fuzz_targets/fuzz_target_1.rs b/tests/fuzz/fuzz/fuzz_targets/fuzz_target_1.rs index 9cfed4463..04025122e 100644 --- a/tests/fuzz/fuzz/fuzz_targets/fuzz_target_1.rs +++ b/tests/fuzz/fuzz/fuzz_targets/fuzz_target_1.rs @@ -2,7 +2,10 @@ use libfuzzer_sys::fuzz_target; -use soroban_sdk::{testutils::arbitrary::{arbitrary,Arbitrary,SorobanArbitrary}, Env, IntoVal, U256}; +use soroban_sdk::{ + testutils::arbitrary::{arbitrary, Arbitrary, SorobanArbitrary}, + Env, IntoVal, U256, +}; use test_fuzz::{Contract, ContractClient}; @@ -18,7 +21,7 @@ fuzz_target!(|input: Input| { let a: U256 = input.a.into_val(&env); let b: U256 = input.b.into_val(&env); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let client = ContractClient::new(&env, &contract_id); let _ = client.run(&a, &b); diff --git a/tests/import_contract/src/lib.rs b/tests/import_contract/src/lib.rs index 7ce18bafa..19daa3de2 100644 --- a/tests/import_contract/src/lib.rs +++ b/tests/import_contract/src/lib.rs @@ -26,9 +26,9 @@ mod test { #[test] fn test_add() { let e = Env::default(); - let add_contract_id = e.register_contract_wasm(None, addcontract::WASM); + let add_contract_id = e.register(addcontract::WASM, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 10u64; diff --git a/tests/invoke_contract/src/lib.rs b/tests/invoke_contract/src/lib.rs index c1e1801f4..7450bd6c0 100644 --- a/tests/invoke_contract/src/lib.rs +++ b/tests/invoke_contract/src/lib.rs @@ -36,9 +36,9 @@ mod test { fn test_add() { let e = Env::default(); - let add_contract_id = e.register_contract(None, AddContract); + let add_contract_id = e.register(AddContract, ()); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let x = 10i32; diff --git a/tests/logging/src/lib.rs b/tests/logging/src/lib.rs index 83b4c8a08..99d498bdd 100644 --- a/tests/logging/src/lib.rs +++ b/tests/logging/src/lib.rs @@ -37,7 +37,7 @@ mod test { #[test] fn test_logging() { let env = Env::default(); - let contract_id = env.register_contract(None, Contract); + let contract_id = env.register(Contract, ()); let client = ContractClient::new(&env, &contract_id); client.hello(); diff --git a/tests/modular/src/test.rs b/tests/modular/src/test.rs index 74988fe18..c8677d5ff 100644 --- a/tests/modular/src/test.rs +++ b/tests/modular/src/test.rs @@ -7,7 +7,7 @@ use soroban_sdk::Env; fn test() { let env = Env::default(); - let id = env.register_contract(None, Contract); + let id = env.register(Contract, ()); let client = ContractClient::new(&env, &id); assert_eq!(client.zero(), 0); diff --git a/tests/multiimpl/src/lib.rs b/tests/multiimpl/src/lib.rs index e9c578ef5..976f911ae 100644 --- a/tests/multiimpl/src/lib.rs +++ b/tests/multiimpl/src/lib.rs @@ -32,7 +32,7 @@ mod test { #[test] fn test_hello() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); client.empty(); diff --git a/tests/udt/src/lib.rs b/tests/udt/src/lib.rs index 2b4c640b2..7ac903344 100644 --- a/tests/udt/src/lib.rs +++ b/tests/udt/src/lib.rs @@ -79,7 +79,7 @@ mod test { #[test] fn test_add() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let udt = UdtStruct { diff --git a/tests/workspace_contract/src/lib.rs b/tests/workspace_contract/src/lib.rs index 864c51c54..a62c353a9 100644 --- a/tests/workspace_contract/src/lib.rs +++ b/tests/workspace_contract/src/lib.rs @@ -22,7 +22,7 @@ mod test { fn test_add() { let e = Env::default(); - let contract_id = e.register_contract(None, Contract); + let contract_id = e.register(Contract, ()); let client = ContractClient::new(&e, &contract_id); let z = client.value();