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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
pub mod state {
use crate::{
objects::{core::OsId, time_weighted_average::TimeWeightedAverage},
AbstractResult,
};
use cosmwasm_std::{Addr, Api, Decimal, StdError, StdResult, Uint128, Uint64};
use cw_asset::{AssetInfo, AssetInfoUnchecked};
use cw_storage_plus::{Item, Map};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::ops::Sub;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub enum UncheckedEmissionType {
None,
BlockShared(Decimal, AssetInfoUnchecked),
BlockPerUser(Decimal, AssetInfoUnchecked),
IncomeBased(AssetInfoUnchecked),
}
impl UncheckedEmissionType {
pub fn check(self, api: &dyn Api) -> AbstractResult<EmissionType> {
match self {
UncheckedEmissionType::None => Ok(EmissionType::None),
UncheckedEmissionType::BlockShared(d, a) => {
Ok(EmissionType::BlockShared(d, a.check(api, None)?))
}
UncheckedEmissionType::BlockPerUser(d, a) => {
Ok(EmissionType::BlockPerUser(d, a.check(api, None)?))
}
UncheckedEmissionType::IncomeBased(a) => {
Ok(EmissionType::IncomeBased(a.check(api, None)?))
}
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub enum EmissionType {
None,
BlockShared(Decimal, AssetInfo),
BlockPerUser(Decimal, AssetInfo),
IncomeBased(AssetInfo),
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct SubscriptionConfig {
pub version_control_address: Addr,
pub factory_address: Addr,
pub payment_asset: AssetInfo,
pub subscription_cost_per_block: Decimal,
pub subscription_per_block_emissions: EmissionType,
}
#[cosmwasm_schema::cw_serde]
pub struct SubscriptionState {
pub active_subs: u32,
}
#[cosmwasm_schema::cw_serde]
pub struct Subscriber {
pub expiration_block: u64,
pub last_emission_claim_block: u64,
pub manager_addr: Addr,
}
pub const INCOME_TWA: TimeWeightedAverage = TimeWeightedAverage::new("\u{0}{7}sub_twa");
pub const SUBSCRIPTION_CONFIG: Item<SubscriptionConfig> = Item::new("\u{0}{10}sub_config");
pub const SUBSCRIPTION_STATE: Item<SubscriptionState> = Item::new("\u{0}{9}sub_state");
pub const SUBSCRIBERS: Map<OsId, Subscriber> = Map::new("subscribed");
pub const DORMANT_SUBSCRIBERS: Map<OsId, Subscriber> = Map::new("un-subscribed");
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct ContributionConfig {
pub protocol_income_share: Decimal,
pub emission_user_share: Decimal,
pub max_emissions_multiple: Decimal,
pub emissions_amp_factor: Uint128,
pub emissions_offset: Uint128,
pub token_info: AssetInfo,
}
impl ContributionConfig {
pub fn verify(self) -> StdResult<Self> {
if !(decimal_is_percentage(&self.protocol_income_share)
|| decimal_is_percentage(&self.emission_user_share))
{
Err(StdError::generic_err(
"Some config fields should not be >1.",
))
} else {
Ok(self)
}
}
}
fn decimal_is_percentage(decimal: &Decimal) -> bool {
decimal <= &Decimal::one()
}
#[cosmwasm_schema::cw_serde]
pub struct ContributionState {
pub income_target: Decimal,
pub expense: Decimal,
pub total_weight: Uint128,
pub emissions: Decimal,
}
pub const CONTRIBUTORS: Map<&Addr, Compensation> = Map::new("contributors");
pub const CONTRIBUTION_CONFIG: Item<ContributionConfig> = Item::new("\u{0}{10}con_config");
pub const CACHED_CONTRIBUTION_STATE: Item<ContributionState> =
Item::new("\u{0}{15}cache_con_state");
pub const CONTRIBUTION_STATE: Item<ContributionState> = Item::new("\u{0}{9}con_state");
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, Default)]
pub struct Compensation {
pub base_per_block: Decimal,
pub weight: u32,
pub last_claim_block: Uint64,
pub expiration_block: Uint64,
}
impl Compensation {
pub fn overwrite(
mut self,
base_per_block: Option<Decimal>,
weight: Option<u32>,
expiration_block: Option<u64>,
) -> Self {
if let Some(base_per_block) = base_per_block {
self.base_per_block = base_per_block;
}
if let Some(weight) = weight {
self.weight = weight;
}
if let Some(expiration_block) = expiration_block {
self.expiration_block = expiration_block.into();
}
self
}
}
impl Sub for Compensation {
type Output = (Decimal, i32);
fn sub(self, other: Self) -> (Decimal, i32) {
(
self.base_per_block - other.base_per_block,
self.weight as i32 - other.weight as i32,
)
}
}
}
use self::state::UncheckedEmissionType;
use crate::{
app::{self},
objects::core::OsId,
};
use cosmwasm_schema::QueryResponses;
use cosmwasm_std::{Decimal, Uint128, Uint64};
use cw_asset::{Asset, AssetInfoUnchecked};
use state::{
Compensation, ContributionConfig, ContributionState, Subscriber, SubscriptionConfig,
SubscriptionState,
};
pub type ExecuteMsg = app::ExecuteMsg<SubscriptionExecuteMsg>;
pub type QueryMsg = app::QueryMsg<SubscriptionQueryMsg>;
impl app::AppExecuteMsg for SubscriptionExecuteMsg {}
impl app::AppQueryMsg for SubscriptionQueryMsg {}
#[cosmwasm_schema::cw_serde]
pub struct MigrateMsg {}
#[cosmwasm_schema::cw_serde]
pub struct InstantiateMsg {
pub subscription: SubscriptionInstantiateMsg,
pub contribution: Option<ContributionInstantiateMsg>,
}
#[cosmwasm_schema::cw_serde]
pub struct SubscriptionInstantiateMsg {
pub payment_asset: AssetInfoUnchecked,
pub subscription_cost_per_block: Decimal,
pub version_control_addr: String,
pub factory_addr: String,
pub subscription_per_block_emissions: UncheckedEmissionType,
}
#[cosmwasm_schema::cw_serde]
pub struct ContributionInstantiateMsg {
pub protocol_income_share: Decimal,
pub emission_user_share: Decimal,
pub max_emissions_multiple: Decimal,
pub token_info: AssetInfoUnchecked,
pub emissions_amp_factor: Uint128,
pub emissions_offset: Uint128,
pub income_averaging_period: Uint64,
}
#[cosmwasm_schema::cw_serde]
#[cfg_attr(feature = "boot", derive(boot_core::ExecuteFns))]
#[cfg_attr(feature = "boot", impl_into(ExecuteMsg))]
pub enum SubscriptionExecuteMsg {
Pay {
os_id: OsId,
},
Unsubscribe {
os_ids: Vec<u32>,
},
ClaimCompensation {
os_id: OsId,
},
ClaimEmissions {
os_id: OsId,
},
UpdateContributor {
contributor_os_id: OsId,
base_per_block: Option<Decimal>,
weight: Option<Uint64>,
expiration_block: Option<Uint64>,
},
RemoveContributor {
os_id: OsId,
},
UpdateSubscriptionConfig {
payment_asset: Option<AssetInfoUnchecked>,
version_control_address: Option<String>,
factory_address: Option<String>,
subscription_cost: Option<Decimal>,
},
UpdateContributionConfig {
protocol_income_share: Option<Decimal>,
emission_user_share: Option<Decimal>,
max_emissions_multiple: Option<Decimal>,
project_token_info: Option<AssetInfoUnchecked>,
emissions_amp_factor: Option<Uint128>,
emissions_offset: Option<Uint128>,
},
}
#[cosmwasm_schema::cw_serde]
#[cfg_attr(feature = "boot", derive(boot_core::QueryFns))]
#[cfg_attr(feature = "boot", impl_into(QueryMsg))]
#[derive(QueryResponses)]
pub enum SubscriptionQueryMsg {
#[returns(StateResponse)]
State {},
#[returns(ConfigResponse)]
Config {},
#[returns(SubscriptionFeeResponse)]
Fee {},
#[returns(SubscriberStateResponse)]
SubscriberState { os_id: OsId },
#[returns(ContributorStateResponse)]
ContributorState { os_id: OsId },
}
#[cosmwasm_schema::cw_serde]
pub enum DepositHookMsg {
Pay { os_id: OsId },
}
#[cosmwasm_schema::cw_serde]
pub struct ConfigResponse {
pub contribution: ContributionConfig,
pub subscription: SubscriptionConfig,
}
#[cosmwasm_schema::cw_serde]
pub struct StateResponse {
pub contribution: ContributionState,
pub subscription: SubscriptionState,
}
#[cosmwasm_schema::cw_serde]
pub struct SubscriptionFeeResponse {
pub fee: Asset,
}
#[cosmwasm_schema::cw_serde]
pub struct SubscriberStateResponse {
pub currently_subscribed: bool,
pub subscriber_details: Subscriber,
}
#[cosmwasm_schema::cw_serde]
pub struct ContributorStateResponse {
pub compensation: Compensation,
}