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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::{
    AccountIdOf, Balance, Config, HermesPollData, HermesPollInfo, HermesVotingInfo, HermesVotings,
};
use alloc::string::String;
use codec::{Decode, Encode};
use common::BoundedString;
use frame_support::dispatch::Weight;
use frame_support::log;
use frame_support::traits::Get;
use frame_support::BoundedVec;
use frame_support::RuntimeDebug;

#[derive(Encode, Decode, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo, Clone, Copy)]
pub enum VotingOption {
    Yes,
    No,
}

#[derive(Encode, Decode, Default, PartialEq, Eq, scale_info::TypeInfo)]
pub struct OldHermesPollInfo<AccountId, Moment> {
    /// Creator of poll
    pub creator: AccountId,
    /// Hermes Locked
    pub hermes_locked: Balance,
    /// Poll start timestamp
    pub poll_start_timestamp: Moment,
    /// Poll end timestamp
    pub poll_end_timestamp: Moment,
    /// Poll title
    pub title: String,
    /// Description
    pub description: String,
    /// Creator Hermes withdrawn
    pub creator_hermes_withdrawn: bool,
}

pub fn migrate<T: Config>() -> Weight {
    sp_runtime::runtime_logger::RuntimeLogger::init();
    migrate_voting_and_poll_data::<T>()
}

pub fn migrate_voting_and_poll_data<T: Config>() -> Weight {
    let mut weight: u64 = 0;

    HermesVotings::<T>::translate_values::<(VotingOption, Balance, bool), _>(
        |(voting_option, number_of_hermes, hermes_withdrawn)| {
            weight += 1;

            let new_voting_option;

            if voting_option == VotingOption::Yes {
                new_voting_option = "Yes";
            } else {
                new_voting_option = "No";
            }

            Some(HermesVotingInfo {
                voting_option: BoundedString::truncate_from(new_voting_option),
                number_of_hermes,
                hermes_withdrawn,
            })
        },
    );

    HermesPollData::<T>::translate_values::<OldHermesPollInfo<AccountIdOf<T>, T::Moment>, _>(|v| {
        weight += 1;

        let mut options = BoundedVec::default();
        options.try_push(BoundedString::truncate_from("Yes")).ok()?;
        options.try_push(BoundedString::truncate_from("No")).ok()?;

        Some(HermesPollInfo {
            creator: v.creator,
            hermes_locked: v.hermes_locked,
            poll_start_timestamp: v.poll_start_timestamp,
            poll_end_timestamp: v.poll_end_timestamp,
            title: BoundedString::truncate_from(v.title.as_str()),
            description: BoundedString::truncate_from(v.description.as_str()),
            creator_hermes_withdrawn: v.creator_hermes_withdrawn,
            options,
        })
    });

    log::info!(
        target: "runtime",
        "HermesVotingInfo migrated to new version with voting_option as a 'String' and HermesPollInfo migrated to new version with 'options' field"
    );

    T::DbWeight::get().reads_writes(weight, weight)
}