Skip to content

Commit

Permalink
Helper constants (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Dec 1, 2023
1 parent b99c0ae commit 2f62439
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 57 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ homepage = "https://github.com/Cardinal-Cryptography/drink"
license = "Apache-2.0"
readme = "README.md"
repository = "https://github.com/Cardinal-Cryptography/drink"
version = "0.8.3"
version = "0.8.4"

[workspace.dependencies]
anyhow = { version = "1.0.71" }
Expand Down Expand Up @@ -56,5 +56,5 @@ sp-runtime-interface = { version = "19.0.0" }

# Local dependencies

drink = { version = "0.8.3", path = "drink" }
drink-test-macro = { version = "0.8.3", path = "drink/test-macro" }
drink = { version = "0.8.4", path = "drink" }
drink-test-macro = { version = "0.8.4", path = "drink/test-macro" }
9 changes: 6 additions & 3 deletions drink/src/sandbox/contract_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ mod tests {
use pallet_contracts::Origin;

use super::*;
use crate::{minimal::RuntimeEvent, runtime::Runtime, MinimalRuntime, DEFAULT_GAS_LIMIT};
use crate::{
minimal::RuntimeEvent, runtime::Runtime, session::NO_SALT, MinimalRuntime,
DEFAULT_GAS_LIMIT,
};

fn compile_module(contract_name: &str) -> Vec<u8> {
let path = [
Expand Down Expand Up @@ -212,7 +215,7 @@ mod tests {
wasm_binary,
0,
vec![],
vec![],
NO_SALT,
MinimalRuntime::default_actor(),
DEFAULT_GAS_LIMIT,
None,
Expand Down Expand Up @@ -246,7 +249,7 @@ mod tests {
wasm_binary,
0,
vec![],
vec![],
NO_SALT,
actor.clone(),
DEFAULT_GAS_LIMIT,
None,
Expand Down
31 changes: 19 additions & 12 deletions drink/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use error::SessionError;

use self::mocking_api::MockingApi;
use crate::{
bundle::ContractBundle, errors::MessageResult, session::transcoding::TranscoderRegistry,
bundle::ContractBundle, errors::MessageResult, runtime::MinimalRuntime,
session::transcoding::TranscoderRegistry,
};

type BalanceOf<R> =
Expand All @@ -42,6 +43,12 @@ type BalanceOf<R> =
/// Without it, you would have to specify explicitly a compatible type, like:
/// `session.call::<String>(.., &[], ..)`.
pub const NO_ARGS: &[String] = &[];
/// Convenient value for an empty salt.
pub const NO_SALT: Vec<u8> = vec![];
/// Convenient value for no endowment.
///
/// Compatible with any runtime with `u128` as the balance type.
pub const NO_ENDOWMENT: Option<BalanceOf<MinimalRuntime>> = None;

/// Wrapper around `Sandbox` that provides a convenient API for interacting with multiple contracts.
///
Expand All @@ -55,7 +62,7 @@ pub const NO_ARGS: &[String] = &[];
/// # use drink::{
/// # session::Session,
/// # AccountId32,
/// # session::NO_ARGS,
/// # session::{NO_ARGS, NO_SALT, NO_ENDOWMENT},
/// # runtime::MinimalRuntime
/// # };
/// #
Expand All @@ -68,10 +75,10 @@ pub const NO_ARGS: &[String] = &[];
/// # fn main() -> Result<(), drink::session::error::SessionError> {
///
/// Session::<MinimalRuntime>::new()?
/// .deploy_and(contract_bytes(), "new", NO_ARGS, vec![], None, &get_transcoder())?
/// .call_and("foo", NO_ARGS, None)?
/// .deploy_and(contract_bytes(), "new", NO_ARGS, NO_SALT, NO_ENDOWMENT, &get_transcoder())?
/// .call_and("foo", NO_ARGS, NO_ENDOWMENT)?
/// .with_actor(bob())
/// .call_and("bar", NO_ARGS, None)?;
/// .call_and("bar", NO_ARGS, NO_ENDOWMENT)?;
/// # Ok(()) }
/// ```
///
Expand All @@ -83,7 +90,7 @@ pub const NO_ARGS: &[String] = &[];
/// # session::Session,
/// # AccountId32,
/// # runtime::MinimalRuntime,
/// # session::NO_ARGS
/// # session::{NO_ARGS, NO_ENDOWMENT, NO_SALT}
/// # };
/// # fn get_transcoder() -> Rc<ContractMessageTranscoder> {
/// # Rc::new(ContractMessageTranscoder::load("").unwrap())
Expand All @@ -94,10 +101,10 @@ pub const NO_ARGS: &[String] = &[];
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
///
/// let mut session = Session::<MinimalRuntime>::new()?;
/// let _address = session.deploy(contract_bytes(), "new", NO_ARGS, vec![], None, &get_transcoder())?;
/// let _result: u32 = session.call("foo", NO_ARGS, None)??;
/// let _address = session.deploy(contract_bytes(), "new", NO_ARGS, NO_SALT, NO_ENDOWMENT, &get_transcoder())?;
/// let _result: u32 = session.call("foo", NO_ARGS, NO_ENDOWMENT)??;
/// session.set_actor(bob());
/// session.call::<_, ()>("bar", NO_ARGS, None)??;
/// session.call::<_, ()>("bar", NO_ARGS, NO_ENDOWMENT)??;
/// # Ok(()) }
/// ```
///
Expand All @@ -106,20 +113,20 @@ pub const NO_ARGS: &[String] = &[];
/// # use drink::{
/// # local_contract_file,
/// # session::Session,
/// # session::NO_ARGS,
/// # session::{NO_ARGS, NO_SALT, NO_ENDOWMENT},
/// # runtime::MinimalRuntime,
/// # ContractBundle,
/// # };
///
/// # fn main() -> Result<(), drink::session::error::SessionError> {
/// // Simplest way, loading a bundle from the project's directory:
/// Session::<MinimalRuntime>::new()?
/// .deploy_bundle_and(local_contract_file!(), "new", NO_ARGS, vec![], None); /* ... */
/// .deploy_bundle_and(local_contract_file!(), "new", NO_ARGS, NO_SALT, NO_ENDOWMENT); /* ... */
///
/// // Or choosing the file explicitly:
/// let contract = ContractBundle::load("path/to/your.contract")?;
/// Session::<MinimalRuntime>::new()?
/// .deploy_bundle_and(contract, "new", NO_ARGS, vec![], None); /* ... */
/// .deploy_bundle_and(contract, "new", NO_ARGS, NO_SALT, NO_ENDOWMENT); /* ... */
/// # Ok(()) }
/// ```
pub struct Session<R: RuntimeWithContracts> {
Expand Down
6 changes: 3 additions & 3 deletions drink/test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type SynResult<T> = Result<T, syn::Error>;
/// fn testcase() {
/// Session::<MinimalRuntime>::new()
/// .unwrap()
/// .deploy(bytes(), "new", NO_ARGS, vec![], None, &transcoder())
/// .deploy(bytes(), "new", NO_ARGS, NO_SALT, NO_ENDOWMENT, &transcoder())
/// .unwrap();
/// }
/// ```
Expand Down Expand Up @@ -97,8 +97,8 @@ fn test_internal(_attr: TokenStream2, item: TokenStream2) -> SynResult<TokenStre
///
/// fn testcase() {
/// Session::<MinimalRuntime>::new()?
/// .deploy_bundle_and(BundleProvider::local()?, "new", NO_ARGS, vec![], None)
/// .deploy_bundle_and(BundleProvider::AnotherContract.bundle()?, "new", NO_ARGS, vec![], None)
/// .deploy_bundle_and(BundleProvider::local()?, "new", NO_ARGS, NO_SALT, NO_ENDOWMENT)
/// .deploy_bundle_and(BundleProvider::AnotherContract.bundle()?, "new", NO_ARGS, NO_SALT, NO_ENDOWMENT)
/// .unwrap();
/// }
/// ```
Expand Down
12 changes: 9 additions & 3 deletions examples/chain-extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod contract_calling_chain_extension {
mod tests {
use drink::{
create_minimal_runtime,
session::{Session, NO_ARGS},
session::{Session, NO_ARGS, NO_ENDOWMENT, NO_SALT},
};

use crate::CHAIN_EXTENSION_RETURN_VALUE;
Expand All @@ -54,8 +54,14 @@ mod tests {
#[drink::test]
fn we_can_test_chain_extension() -> Result<(), Box<dyn std::error::Error>> {
let result: u32 = Session::<RuntimeWithCE>::new()?
.deploy_bundle_and(BundleProvider::local()?, "new", NO_ARGS, vec![], None)?
.call("call_ce", NO_ARGS, None)??;
.deploy_bundle_and(
BundleProvider::local()?,
"new",
NO_ARGS,
NO_SALT,
NO_ENDOWMENT,
)?
.call("call_ce", NO_ARGS, NO_ENDOWMENT)??;

assert_eq!(result, CHAIN_EXTENSION_RETURN_VALUE);

Expand Down
31 changes: 23 additions & 8 deletions examples/cross-contract-call-tracing/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ mod tests {
pallet_contracts_debugging::{TracingExt, TracingExtT},
MinimalRuntime,
},
session::{contract_transcode::Value, Session, NO_ARGS},
session::{contract_transcode::Value, Session, NO_ARGS, NO_ENDOWMENT},
AccountId32,
};
use ink::storage::traits::Storable;
Expand Down Expand Up @@ -135,14 +135,29 @@ mod tests {
let mut session = Session::<MinimalRuntime>::new()?;
session.set_tracing_extension(TracingExt(Box::new(TestDebugger {})));

let outer_address =
session.deploy_bundle(BundleProvider::local()?, "new", NO_ARGS, vec![1], None)?;
let outer_address = session.deploy_bundle(
BundleProvider::local()?,
"new",
NO_ARGS,
vec![1],
NO_ENDOWMENT,
)?;
OUTER_ADDRESS.with(|a| *a.borrow_mut() = Some(outer_address.clone()));
let middle_address =
session.deploy_bundle(BundleProvider::local()?, "new", NO_ARGS, vec![2], None)?;
let middle_address = session.deploy_bundle(
BundleProvider::local()?,
"new",
NO_ARGS,
vec![2],
NO_ENDOWMENT,
)?;
MIDDLE_ADDRESS.with(|a| *a.borrow_mut() = Some(middle_address.clone()));
let inner_address =
session.deploy_bundle(BundleProvider::local()?, "new", NO_ARGS, vec![3], None)?;
let inner_address = session.deploy_bundle(
BundleProvider::local()?,
"new",
NO_ARGS,
vec![3],
NO_ENDOWMENT,
)?;
INNER_ADDRESS.with(|a| *a.borrow_mut() = Some(inner_address.clone()));

let value: u32 = session.call_with_address(
Expand All @@ -153,7 +168,7 @@ mod tests {
&*inner_address.to_string(),
"7",
],
None,
NO_ENDOWMENT,
)??;

assert_eq!(value, 22);
Expand Down
16 changes: 8 additions & 8 deletions examples/flipper/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod tests {

use drink::{
runtime::MinimalRuntime,
session::{Session, NO_ARGS},
session::{Session, NO_ARGS, NO_ENDOWMENT, NO_SALT},
};

#[drink::contract_bundle_provider]
Expand All @@ -46,8 +46,8 @@ mod tests {
fn initialization() -> Result<(), Box<dyn Error>> {
let contract = BundleProvider::local()?;
let init_value: bool = Session::<MinimalRuntime>::new()?
.deploy_bundle_and(contract, "new", &["true"], vec![], None)?
.call_and("get", NO_ARGS, None)?
.deploy_bundle_and(contract, "new", &["true"], NO_SALT, NO_ENDOWMENT)?
.call_and("get", NO_ARGS, NO_ENDOWMENT)?
.last_call_return()
.expect("Call was successful, so there should be a return")
.expect("Call was successful");
Expand All @@ -61,11 +61,11 @@ mod tests {
fn flipping() -> Result<(), Box<dyn Error>> {
let contract = BundleProvider::Flipper.bundle()?;
let init_value: bool = Session::<MinimalRuntime>::new()?
.deploy_bundle_and(contract, "new", &["true"], vec![], None)?
.call_and("flip", NO_ARGS, None)?
.call_and("flip", NO_ARGS, None)?
.call_and("flip", NO_ARGS, None)?
.call_and("get", NO_ARGS, None)?
.deploy_bundle_and(contract, "new", &["true"], NO_SALT, NO_ENDOWMENT)?
.call_and("flip", NO_ARGS, NO_ENDOWMENT)?
.call_and("flip", NO_ARGS, NO_ENDOWMENT)?
.call_and("flip", NO_ARGS, NO_ENDOWMENT)?
.call_and("get", NO_ARGS, NO_ENDOWMENT)?
.last_call_return()
.expect("Call was successful, so there should be a return")
.expect("Call was successful");
Expand Down
6 changes: 3 additions & 3 deletions examples/mocking/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mod tests {
use drink::{
mock_message,
runtime::MinimalRuntime,
session::{mocking_api::MockingApi, Session, NO_ARGS},
session::{mocking_api::MockingApi, Session, NO_ARGS, NO_ENDOWMENT, NO_SALT},
ContractMock,
};

Expand All @@ -65,8 +65,8 @@ mod tests {

// Now, we can deploy our proper contract and verify its behavior.
let result: (u8, u8) = session
.deploy_bundle_and(BundleProvider::local()?, "new", NO_ARGS, vec![], None)?
.call_and("forward_call", &[mock_address.to_string()], None)?
.deploy_bundle_and(BundleProvider::local()?, "new", NO_ARGS, NO_SALT, None)?
.call_and("forward_call", &[mock_address.to_string()], NO_ENDOWMENT)?
.last_call_return()
.expect("Call was successful, so there should be a return")
.expect("Call was successful");
Expand Down
Loading

0 comments on commit 2f62439

Please sign in to comment.