/
lib.rs
237 lines (202 loc) · 8.03 KB
/
lib.rs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2021-2022 Zeitgeist PM LLC.
//
// This file is part of Zeitgeist.
//
// Zeitgeist 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, either version 3 of the License, or (at
// your option) any later version.
//
// Zeitgeist 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 Zeitgeist. If not, see <https://www.gnu.org/licenses/>.
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
pub mod migrations;
mod mock;
mod tests;
pub use pallet::*;
pub use zeitgeist_primitives::traits::MarketCommonsPalletApi;
#[frame_support::pallet]
mod pallet {
use crate::MarketCommonsPalletApi;
use core::marker::PhantomData;
use frame_support::{
dispatch::DispatchResult,
ensure,
pallet_prelude::{StorageMap, StorageValue, ValueQuery},
storage::PrefixIterator,
traits::{Get, Hooks, NamedReservableCurrency, StorageVersion, Time},
Blake2_128Concat, PalletId, Parameter,
};
use parity_scale_codec::MaxEncodedLen;
use sp_runtime::{
traits::{
AccountIdConversion, AtLeast32Bit, CheckedAdd, MaybeSerializeDeserialize, Member,
Saturating,
},
ArithmeticError, DispatchError, SaturatedConversion,
};
use zeitgeist_primitives::types::{Market, PoolId};
/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);
pub type MomentOf<T> = <<T as Config>::Timestamp as frame_support::traits::Time>::Moment;
type MarketOf<T> = Market<
<T as frame_system::Config>::AccountId,
<T as frame_system::Config>::BlockNumber,
MomentOf<T>,
>;
#[pallet::call]
impl<T: Config> Pallet<T> {}
#[pallet::config]
pub trait Config: frame_system::Config {
/// Native token
//
// Reserve identifiers can be pallet ids or any other sequence of bytes.
type Currency: NamedReservableCurrency<Self::AccountId, ReserveIdentifier = [u8; 8]>;
/// The identifier of individual markets.
type MarketId: AtLeast32Bit
+ Copy
+ Default
+ MaxEncodedLen
+ MaybeSerializeDeserialize
+ Member
+ Parameter;
// TODO(#837): Remove when on-chain arbitrage is removed!
/// The prefix used to calculate the prize pool accounts.
#[pallet::constant]
type PredictionMarketsPalletId: Get<PalletId>;
/// Time tracker
type Timestamp: Time<Moment = u64>;
}
#[pallet::error]
pub enum Error<T> {
/// A market with the provided ID does not exist.
MarketDoesNotExist,
/// Market does not have an stored associated pool id.
MarketPoolDoesNotExist,
/// It is not possible to fetch the latest market ID when
/// no market has been created.
NoMarketHasBeenCreated,
/// Market does not have a report
NoReport,
/// There's a pool registered for this market already.
PoolAlreadyExists,
}
#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(PhantomData<T>);
impl<T> Pallet<T>
where
T: Config,
{
// Stored and returns the next market id.
//
// Retrieval is based on the stored ID plus one, recording the same incremented number
// on the storage so next following calls will return yet another incremented number.
//
// Returns `Err` if `MarketId` addition overflows.
pub fn next_market_id() -> Result<T::MarketId, DispatchError> {
let id = MarketCounter::<T>::get();
let new_counter = id.checked_add(&1u8.into()).ok_or(ArithmeticError::Overflow)?;
<MarketCounter<T>>::put(new_counter);
Ok(id)
}
}
impl<T> MarketCommonsPalletApi for Pallet<T>
where
T: Config,
<T as Config>::Timestamp: Time,
{
type AccountId = T::AccountId;
type BlockNumber = T::BlockNumber;
type Currency = T::Currency;
type MarketId = T::MarketId;
type Moment = MomentOf<T>;
// Market
fn latest_market_id() -> Result<Self::MarketId, DispatchError> {
match <MarketCounter<T>>::try_get() {
Ok(market_id) => {
Ok(market_id.saturating_sub(1u8.into())) // Note: market_id > 0!
}
_ => Err(Error::<T>::NoMarketHasBeenCreated.into()),
}
}
fn market_iter() -> PrefixIterator<(Self::MarketId, MarketOf<T>)> {
<Markets<T>>::iter()
}
fn market(market_id: &Self::MarketId) -> Result<MarketOf<T>, DispatchError> {
<Markets<T>>::try_get(market_id).map_err(|_err| Error::<T>::MarketDoesNotExist.into())
}
fn mutate_market<F>(market_id: &Self::MarketId, cb: F) -> DispatchResult
where
F: FnOnce(&mut MarketOf<T>) -> DispatchResult,
{
<Markets<T>>::try_mutate(market_id, |opt| {
if let Some(market) = opt {
cb(market)?;
return Ok(());
}
Err(Error::<T>::MarketDoesNotExist.into())
})
}
fn push_market(market: MarketOf<T>) -> Result<Self::MarketId, DispatchError> {
let market_id = Self::next_market_id()?;
<Markets<T>>::insert(market_id, market);
Ok(market_id)
}
fn remove_market(market_id: &Self::MarketId) -> DispatchResult {
if !<Markets<T>>::contains_key(market_id) {
return Err(Error::<T>::MarketDoesNotExist.into());
}
<Markets<T>>::remove(market_id);
Ok(())
}
// TODO(#837): Remove when on-chain arbitrage is removed!
#[inline]
fn market_account(market_id: Self::MarketId) -> Self::AccountId {
T::PredictionMarketsPalletId::get()
.into_sub_account_truncating(market_id.saturated_into::<u128>())
}
// MarketPool
fn insert_market_pool(market_id: Self::MarketId, pool_id: PoolId) -> DispatchResult {
ensure!(!<MarketPool<T>>::contains_key(market_id), Error::<T>::PoolAlreadyExists);
ensure!(<Markets<T>>::contains_key(market_id), Error::<T>::MarketDoesNotExist);
<MarketPool<T>>::insert(market_id, pool_id);
Ok(())
}
fn remove_market_pool(market_id: &Self::MarketId) -> DispatchResult {
if !<MarketPool<T>>::contains_key(market_id) {
return Err(Error::<T>::MarketPoolDoesNotExist.into());
}
<MarketPool<T>>::remove(market_id);
Ok(())
}
fn market_pool(market_id: &Self::MarketId) -> Result<PoolId, DispatchError> {
<MarketPool<T>>::try_get(market_id)
.map_err(|_err| Error::<T>::MarketPoolDoesNotExist.into())
}
// Etc
fn now() -> Self::Moment {
T::Timestamp::now()
}
}
/// Holds all markets
#[pallet::storage]
pub type Markets<T: Config> = StorageMap<_, Blake2_128Concat, T::MarketId, MarketOf<T>>;
/// The number of markets that have been created (including removed markets) and the next
/// identifier for a created market.
#[pallet::storage]
pub type MarketCounter<T: Config> = StorageValue<_, T::MarketId, ValueQuery>;
/// Maps a market id to a related pool id. It is up to the caller to keep and sync valid
/// existent markets with valid existent pools.
#[pallet::storage]
pub type MarketPool<T: Config> = StorageMap<_, Blake2_128Concat, T::MarketId, PoolId>;
}