lib.rs 4.58 KB
Newer Older
1
// Copyright 2018-2021 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
#[ink::contract]
20
mod delegator {
21
22
    use accumulator::Accumulator;
    use adder::Adder;
23
    use ink_storage::{
24
25
26
27
28
        traits::{
            PackedLayout,
            SpreadLayout,
        },
        Lazy,
29
30
31
    };
    use subber::Subber;

32
    /// Specifies the state of the `delegator` contract.
33
    ///
34
    /// In `Adder` state the `delegator` contract will delegate to the `Adder` contract
35
36
37
    /// 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_storage::traits::StorageLayout)
52
    )]
53
54
55
56
57
    pub enum Which {
        Adder,
        Subber,
    }

58
59
    /// Delegates calls to an `adder` or `subber` contract to mutate
    /// a value in an `accumulator` contract.
60
    ///
61
    /// In order to deploy the `delegator` smart contract we first
62
63
    /// have to manually put the code of the `accumulator`, `adder`
    /// and `subber` smart contracts, receive their code hashes from
64
    /// the signalled events and put their code hash into our
65
    /// `delegator` smart contract.
66
    #[ink(storage)]
67
    pub struct Delegator {
68
        /// 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
    }

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

116
        /// Returns the `accumulator` value.
117
        #[ink(message)]
118
        pub fn get(&self) -> i32 {
119
            self.accumulator.get()
120
121
122
123
        }

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

131
        /// Switches the `delegator` contract.
132
        #[ink(message)]
133
        pub fn switch(&mut self) {
134
            match self.which {
135
                Which::Adder => {
136
                    self.which = Which::Subber;
137
138
                }
                Which::Subber => {
139
                    self.which = Which::Adder;
140
141
142
143
144
                }
            }
        }
    }
}