lib.rs 4.17 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 23 24 25 26 27 28
    use accumulator::Accumulator;
    use adder::Adder;
    use ink_core::storage::{
        self,
        Flush,
    };
    use subber::Subber;

29 30 31 32 33 34
    /// 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`.
35
    #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode, Flush)]
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 68 69 70 71 72 73 74
    #[cfg_attr(feature = "ink-generate-abi", derive(type_metadata::Metadata))]
    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.
        which: storage::Value<Which>,
        /// The accumulator smart contract.
        accumulator: storage::Value<Accumulator>,
        /// The adder smart contract.
        adder: storage::Value<Adder>,
        /// The subber smart contract.
        subber: storage::Value<Subber>,
    }

    impl Delegator {
        /// Instantiate a delegator with the given sub-contract codes.
        #[ink(constructor)]
        fn new(
            &mut self,
            init_value: i32,
            accumulator_code_hash: Hash,
            adder_code_hash: Hash,
            subber_code_hash: Hash,
        ) {
            self.which.set(Which::Adder);
            let total_balance = self.env().balance();
            let accumulator = Accumulator::new(init_value)
75
                .endowment(total_balance / 4)
76
                .using_code(accumulator_code_hash)
77
                .instantiate()
78 79
                .expect("failed at instantiating the `Accumulator` contract");
            let adder = Adder::new(accumulator.clone())
80
                .endowment(total_balance / 4)
81
                .using_code(adder_code_hash)
82
                .instantiate()
83 84
                .expect("failed at instantiating the `Adder` contract");
            let subber = Subber::new(accumulator.clone())
85
                .endowment(total_balance / 4)
86
                .using_code(subber_code_hash)
87
                .instantiate()
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                .expect("failed at instantiating the `Subber` contract");
            self.accumulator.set(accumulator);
            self.adder.set(adder);
            self.subber.set(subber);
        }

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

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

        /// Switches the delegator.
        #[ink(message)]
        fn switch(&mut self) {
            match *self.which {
                Which::Adder => {
                    *self.which = Which::Subber;
                }
                Which::Subber => {
                    *self.which = Which::Adder;
                }
            }
        }
    }
}