-
Notifications
You must be signed in to change notification settings - Fork 426
/
lib.rs
67 lines (57 loc) · 1.64 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#![cfg_attr(not(feature = "std"), no_std)]
pub use self::adder::{
Adder,
AdderRef,
};
#[ink::contract]
mod adder {
use accumulator::AccumulatorRef;
/// Increments the underlying `accumulator` value.
#[ink(storage)]
pub struct Adder {
/// The `accumulator` to store the value.
accumulator: AccumulatorRef,
}
impl Adder {
/// Creates a new `adder` from the given `accumulator`.
#[ink(constructor)]
pub fn new(accumulator: AccumulatorRef) -> Self {
Self { accumulator }
}
/// Increases the `accumulator` value by some amount.
#[ink(message)]
pub fn inc(&mut self, by: i32) {
self.accumulator.inc(by)
}
}
}
#[cfg(test)]
mod test {
use ink::primitives::Hash;
#[test]
fn cross_contract_call_works_off_chain() {
use super::*;
use accumulator::{
Accumulator,
AccumulatorRef,
};
// register Accumulator & Adder
let hash1 = Hash::from([10u8; 32]);
let hash2 = Hash::from([20u8; 32]);
ink::env::test::register_contract::<Accumulator>(hash1.as_ref());
ink::env::test::register_contract::<Adder>(hash2.as_ref());
let acc = AccumulatorRef::new(0)
.code_hash(hash1)
.endowment(0)
.salt_bytes([0u8; 0])
.instantiate();
let mut adder = AdderRef::new(acc.clone())
.code_hash(hash2)
.endowment(0)
.salt_bytes([0u8; 0])
.instantiate();
assert_eq!(acc.get(), 0);
adder.inc(1);
assert_eq!(acc.get(), 1);
}
}