lib.rs 4.36 KB
Newer Older
Ricardo Rius's avatar
Ricardo Rius committed
1
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
2
//
3
4
5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
10
11
12
13
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
14
15
16

#![cfg_attr(not(feature = "std"), no_std)]

17
use ink_lang as ink;
18
19
20

#[ink::contract(version = "0.1.0")]
mod delegator {
21
22
    use accumulator::Accumulator;
    use adder::Adder;
23
24
25
26
27
28
    use ink_core::storage2::{
        traits::{
            PackedLayout,
            SpreadLayout,
        },
        Lazy,
29
30
31
    };
    use subber::Subber;

32
33
34
35
36
37
    /// Specifies the state of the delegator.
    ///
    /// In `Adder` state the delegator will delegate to the `Adder` contract
    /// and in `Subber` state will delegate to the `Subber` contract.
    ///
    /// The initial state is `Adder`.
38
39
40
41
42
43
44
45
46
47
48
49
50
    #[derive(
        Debug,
        Copy,
        Clone,
        PartialEq,
        Eq,
        scale::Encode,
        scale::Decode,
        SpreadLayout,
        PackedLayout,
    )]
    #[cfg_attr(
        feature = "std",
51
        derive(::scale_info::TypeInfo, ::ink_core::storage2::traits::StorageLayout)
52
    )]
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    pub enum Which {
        Adder,
        Subber,
    }

    /// Delegates calls to an adder or subber contract to mutate
    /// a value in an accumulator contract.
    ///
    /// In order to deploy the delegator smart contract we first
    /// have to manually put the code of the accumulator, adder
    /// and subber smart contracts, receive their code hashes from
    /// the signalled events and put their code hash into our
    /// delegator smart contract.
    #[ink(storage)]
    struct Delegator {
        /// Says which of adder or subber is currently in use.
69
        which: Which,
70
        /// The accumulator smart contract.
71
        accumulator: Lazy<Accumulator>,
72
        /// The adder smart contract.
73
        adder: Lazy<Adder>,
74
        /// The subber smart contract.
75
        subber: Lazy<Subber>,
76
77
78
79
80
81
82
83
84
85
    }

    impl Delegator {
        /// Instantiate a delegator with the given sub-contract codes.
        #[ink(constructor)]
        fn new(
            init_value: i32,
            accumulator_code_hash: Hash,
            adder_code_hash: Hash,
            subber_code_hash: Hash,
86
87
        ) -> Self {
            let total_balance = Self::env().balance();
88
            let accumulator = Accumulator::new(init_value)
89
                .endowment(total_balance / 4)
90
                .using_code(accumulator_code_hash)
91
                .instantiate()
92
93
                .expect("failed at instantiating the `Accumulator` contract");
            let adder = Adder::new(accumulator.clone())
94
                .endowment(total_balance / 4)
95
                .using_code(adder_code_hash)
96
                .instantiate()
97
98
                .expect("failed at instantiating the `Adder` contract");
            let subber = Subber::new(accumulator.clone())
99
                .endowment(total_balance / 4)
100
                .using_code(subber_code_hash)
101
                .instantiate()
102
                .expect("failed at instantiating the `Subber` contract");
103
104
105
106
107
108
            Self {
                which: Which::Adder,
                accumulator: Lazy::new(accumulator),
                adder: Lazy::new(adder),
                subber: Lazy::new(subber),
            }
109
110
111
112
113
        }

        /// Returns the accumulator's value.
        #[ink(message)]
        fn get(&self) -> i32 {
114
            self.accumulator.get()
115
116
117
118
119
        }

        /// Delegates the call to either `Adder` or `Subber`.
        #[ink(message)]
        fn change(&mut self, by: i32) {
120
            match self.which {
121
122
123
124
125
126
127
128
                Which::Adder => self.adder.inc(by),
                Which::Subber => self.subber.dec(by),
            }
        }

        /// Switches the delegator.
        #[ink(message)]
        fn switch(&mut self) {
129
            match self.which {
130
                Which::Adder => {
131
                    self.which = Which::Subber;
132
133
                }
                Which::Subber => {
134
                    self.which = Which::Adder;
135
136
137
138
139
                }
            }
        }
    }
}