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
 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
// This file is part of the Polymesh distribution (https://github.com/PolymathNetwork/Polymesh).
// Copyright (c) 2020 Polymath

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.

// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

//! Runtime API definition for pips module.
use codec::Codec;
use pallet_pips::VoteCount;
use sp_std::vec::Vec;

/// This module contains some types which require transformations to avoid serde issues with
/// `u128` type.
/// For instance, `Balance` is capped (or expanded) to `u64` in `VoteCount`.
pub mod capped {
    use pallet_pips::{Vote as CoreVote, VoteCount as CoreVoteCount};

    use codec::{Decode, Encode};
    use sp_runtime::traits::{SaturatedConversion, UniqueSaturatedInto};

    #[cfg(feature = "std")]
    use serde::{Deserialize, Serialize};

    #[derive(Eq, PartialEq, Encode, Decode)]
    #[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
    #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
    pub enum VoteCount {
        /// Proposal was found and has the following votes.
        ProposalFound {
            /// Stake for
            ayes: u64,
            /// Stake against
            nays: u64,
        },
        /// Proposal was not for given index.
        ProposalNotFound,
    }

    impl<Balance> From<CoreVoteCount<Balance>> for VoteCount
    where
        Balance: UniqueSaturatedInto<u64>,
    {
        fn from(vote_count: CoreVoteCount<Balance>) -> Self {
            match vote_count {
                CoreVoteCount::ProposalFound { ayes, nays } => VoteCount::ProposalFound {
                    ayes: ayes.saturated_into(),
                    nays: nays.saturated_into(),
                },
                CoreVoteCount::ProposalNotFound => VoteCount::ProposalNotFound,
            }
        }
    }

    #[derive(Eq, PartialEq)]
    #[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
    #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
    pub enum Vote {
        Yes(u64),
        No(u64),
    }

    impl<Balance> From<CoreVote<Balance>> for Vote
    where
        Balance: UniqueSaturatedInto<u64>,
    {
        fn from(core_vote: CoreVote<Balance>) -> Self {
            match core_vote {
                CoreVote(true, amount) => Vote::Yes(amount.saturated_into()),
                CoreVote(false, amount) => Vote::No(amount.saturated_into()),
            }
        }
    }
}

sp_api::decl_runtime_apis! {
    /// The API to interact with Pips governance.
    pub trait PipsApi<AccountId, Balance>
    where
        AccountId: Codec,
        Balance: Codec
    {
        /// Retrieve votes for a proposal for a given `pips_index`.
        fn get_votes(pips_index: u32) -> VoteCount<Balance>;

        /// Retrieve proposals started by `address`.
        fn proposed_by(address: AccountId) -> Vec<u32>;

        /// Retrieve proposals `address` voted on.
        fn voted_on(address: AccountId) -> Vec<u32>;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn should_serialize_pips_votes() {
        let votes = VoteCount::ProposalFound {
            ayes: 3141u64,
            nays: 5926u64,
        };

        assert_eq!(
            serde_json::to_string(&votes).unwrap(),
            r#"{"ProposalFound":{"ayes":3141,"nays":5926}}"#,
        );

        // should not panic
        serde_json::to_value(&votes).unwrap();
    }
}