Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delegator build_call example #1752

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions integration-tests/delegator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
43 changes: 43 additions & 0 deletions integration-tests/delegator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[package]
name = "delegator"
version = "4.0.1"
authors = ["[your_name] <[your_email]>"]
edition = "2021"
publish = false

[dependencies]
ink = { version = "4.0.1", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }

# Contracts that will be used for cross contract calls.
accumulator = { path = "accumulator", default-features = false, features = ["ink-as-dependency"] }
adder = { path = "adder", default-features = false, features = ["ink-as-dependency"] }
subber = { path = "subber", default-features = false, features = ["ink-as-dependency"] }

[dev-dependencies]
ink_e2e = { version = "4.0.1" }

[lib]
name = "delegator"
path = "lib.rs"
crate-type = ["cdylib"]
# crate-type = [
# # Used for normal contract Wasm blobs.
# "cdylib",
# # Used for ABI generation.
# "rlib",
# ]

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
"accumulator/std",
"adder/std",
"subber/std",
]
ink-as-dependency = []
e2e-tests = []
6 changes: 6 additions & 0 deletions integration-tests/delegator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# `Delegator` Smart Contract

The `Delegator` smart contract is our showcase for executing other smart
contracts on-chain. The key difference with the other `multi_contract_caller` contract is now the
"called" contracts are already instantiated, in stead of instantiating the contracts at the same
time as the `Delegator` contract.
9 changes: 9 additions & 0 deletions integration-tests/delegator/accumulator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
33 changes: 33 additions & 0 deletions integration-tests/delegator/accumulator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "accumulator"
version = "4.0.1"
authors = ["[your_name] <[your_email]>"]
edition = "2021"

[dependencies]
ink = { version = "4.0.1", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
ink_e2e = "4.0.1"

[lib]
name = "accumulator"
path = "lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
# Used for ABI generation.
"rlib",
]

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
]
ink-as-dependency = []
e2e-tests = []
174 changes: 174 additions & 0 deletions integration-tests/delegator/accumulator/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub use self::accumulator::{Accumulator, AccumulatorRef};

#[ink::contract]
pub mod accumulator {
/// Holds a simple `i32` value that can be incremented and decremented.
#[ink(storage)]
pub struct Accumulator {
value: i32,
}

impl Accumulator {
/// Initializes the value to the initial value.
#[ink(constructor)]
pub fn new(init_value: i32) -> Self {
Self { value: init_value }
}

/// Mutates the internal value.
#[ink(message)]
pub fn inc(&mut self, by: i32) {
// Debug message to check whether the contract gets called by the
// `adder` or `subber` contract.
let caller = self.env().caller();
let message = ink::prelude::format!("got a call from {:?}", caller);
ink::env::debug_println!("{}", &message);

self.value += by;
}

/// Returns the current state.
#[ink(message)]
pub fn get(&self) -> i32 {
self.value
}
}

#[cfg(test)]
mod tests {
use super::*;

#[ink::test]
fn get() {
let accumulator = Accumulator::new(10);
assert_eq!(accumulator.value, 10);
assert_eq!(accumulator.value, accumulator.get());
}

#[ink::test]
fn increase() {
let value = 10;
let mut accumulator = Accumulator::new(value);
assert_eq!(accumulator.value, accumulator.get());
let increase = 10;
accumulator.inc(increase);
assert_eq!(accumulator.value, value + increase);
assert_eq!(accumulator.value, accumulator.get());
}

#[ink::test]
fn decrease() {
let value = 10;
let mut accumulator = Accumulator::new(value);
assert_eq!(accumulator.value, accumulator.get());
let decrease = -10;
accumulator.inc(decrease);
assert_eq!(accumulator.value, value + decrease);
assert_eq!(accumulator.value, accumulator.get());
}
}

#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
use super::*;
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;

#[ink_e2e::test]
async fn get(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let init_value = 10;
let constructor = AccumulatorRef::new(init_value);
let contract_account_id = client
.instantiate("accumulator", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiation failed")
.account_id;

// Build `get` message and execute
let get_message = ink_e2e::build_message::<AccumulatorRef>(contract_account_id.clone())
.call(|accumulator| accumulator.get());
let get_result = client
.call_dry_run(&ink_e2e::alice(), &get_message, 0, None)
.await;
assert_eq!(get_result.return_value(), init_value);
Ok(())
}

#[ink_e2e::test]
async fn increase(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let init_value = 10;
let constructor = AccumulatorRef::new(init_value);
let contract_account_id = client
.instantiate("accumulator", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiation failed")
.account_id;

// Build `get` message and execute
let get_message = ink_e2e::build_message::<AccumulatorRef>(contract_account_id.clone())
.call(|accumulator| accumulator.get());
let get_result = client
.call_dry_run(&ink_e2e::alice(), &get_message, 0, None)
.await;
assert_eq!(get_result.return_value(), init_value);

// Build `increase` message and execute
let increase = 10;
let increase_message =
ink_e2e::build_message::<AccumulatorRef>(contract_account_id.clone())
.call(|accumulator| accumulator.inc(increase));
let increase_result = client
.call(&ink_e2e::alice(), increase_message, 0, None)
.await;
assert!(increase_result.is_ok());

// Build `get` message and execute
let get_message = ink_e2e::build_message::<AccumulatorRef>(contract_account_id.clone())
.call(|accumulator| accumulator.get());
let get_result = client
.call_dry_run(&ink_e2e::alice(), &get_message, 0, None)
.await;
assert_eq!(get_result.return_value(), init_value + increase);
Ok(())
}

#[ink_e2e::test]
async fn decrease(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let init_value = 10;
let constructor = AccumulatorRef::new(init_value);
let contract_account_id = client
.instantiate("accumulator", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiation failed")
.account_id;

// Build `get` message and execute
let get_message = ink_e2e::build_message::<AccumulatorRef>(contract_account_id.clone())
.call(|accumulator| accumulator.get());
let get_result = client
.call_dry_run(&ink_e2e::alice(), &get_message, 0, None)
.await;
assert_eq!(get_result.return_value(), init_value);

// Build `increase` message and execute
let decrease = -10;
let increase_message =
ink_e2e::build_message::<AccumulatorRef>(contract_account_id.clone())
.call(|accumulator| accumulator.inc(decrease));
let increase_result = client
.call(&ink_e2e::alice(), increase_message, 0, None)
.await;
assert!(increase_result.is_ok());

// Build `get` message and execute
let get_message = ink_e2e::build_message::<AccumulatorRef>(contract_account_id.clone())
.call(|accumulator| accumulator.get());
let get_result = client
.call_dry_run(&ink_e2e::alice(), &get_message, 0, None)
.await;
assert_eq!(get_result.return_value(), init_value + decrease);
Ok(())
}
}
}
37 changes: 37 additions & 0 deletions integration-tests/delegator/adder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "adder"
version = "4.0.1"
authors = ["[your_name] <[your_email]>"]
edition = "2021"

[dependencies]
ink = { version = "4.0.1", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }

# Contract that will be used for cross contract calls.
accumulator = { path = "../accumulator", default-features = false, features = ["ink-as-dependency"] }

[dev-dependencies]
ink_e2e = "4.0.1"

[lib]
name = "adder"
path = "lib.rs"
crate-type = [
# Used for normal contract Wasm blobs.
"cdylib",
# Used for ABI generation.
"rlib",
]

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
"accumulator/std",
]
ink-as-dependency = []
e2e-tests = []
Loading