forked from compound-finance/gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
706 lines (581 loc) · 28.8 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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#![allow(incomplete_features)]
#![feature(array_methods)]
#![feature(associated_type_defaults)]
#![feature(const_fn_floating_point_arithmetic)]
#![feature(const_panic)]
#![feature(destructuring_assignment)]
#![feature(str_split_once)]
#[macro_use]
extern crate alloc;
extern crate ethereum_client;
extern crate trx_request;
use crate::{
chains::{
ChainAccount, ChainAccountSignature, ChainAsset, ChainBlock, ChainHash, ChainId,
ChainSignature, ChainSignatureList,
},
events::{ChainLogEvent, ChainLogId, EventState},
notices::{Notice, NoticeId, NoticeState},
types::{
AssetAmount, AssetBalance, AssetIndex, AssetInfo, Bips, CashIndex, CashPrincipal,
CashPrincipalAmount, CodeHash, EncodedNotice, GovernanceResult, InterestRateModel,
LiquidityFactor, Nonce, Reason, SessionIndex, Timestamp, ValidatorKeys, ValidatorSig, APR,
},
};
use codec::alloc::string::String;
use frame_support::{
decl_event, decl_module, decl_storage, dispatch,
sp_runtime::traits::Convert,
traits::{OnRuntimeUpgrade, StoredMap, UnfilteredDispatchable},
weights::{DispatchClass, GetDispatchInfo, Pays, Weight},
Parameter,
};
use frame_system;
use frame_system::{ensure_none, ensure_root, offchain::CreateSignedTransaction};
use our_std::{collections::btree_set::BTreeSet, error, log, str, vec::Vec, Debuggable};
use sp_core::crypto::AccountId32;
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity,
};
use pallet_oracle;
use pallet_oracle::ticker::Ticker;
use pallet_session;
use pallet_timestamp;
#[macro_use]
extern crate lazy_static;
pub mod chains;
pub mod converters;
pub mod core;
pub mod events;
pub mod factor;
pub mod internal;
pub mod notices;
pub mod params;
pub mod portfolio;
pub mod rates;
pub mod reason;
pub mod serdes;
pub mod symbol;
pub mod trx_req;
pub mod types;
#[cfg(test)]
mod tests;
/// Type for linking sessions to validators.
pub type SubstrateId = AccountId32;
/// Configure the pallet by specifying the parameters and types on which it depends.
pub trait Config:
frame_system::Config
+ CreateSignedTransaction<Call<Self>>
+ pallet_timestamp::Config
+ pallet_oracle::Config
{
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>;
/// The overarching dispatch call type.
type Call: From<Call<Self>>
+ Parameter
+ UnfilteredDispatchable<Origin = Self::Origin>
+ GetDispatchInfo;
/// Convert implementation for Moment -> Timestamp.
type TimeConverter: Convert<<Self as pallet_timestamp::Config>::Moment, Timestamp>;
/// Placate substrate's `HandleLifetime` trait.
type AccountStore: StoredMap<SubstrateId, ()>;
type SessionInterface: self::SessionInterface<SubstrateId>;
}
decl_storage! {
trait Store for Module<T: Config> as Cash {
/// The timestamp of the previous block (or initialized to yield start defined in genesis).
LastYieldTimestamp get(fn last_yield_timestamp) config(): Timestamp;
/// A possible next code hash which is used to accept code provided to SetNextCodeViaHash.
AllowedNextCodeHash get(fn allowed_next_code_hash): Option<CodeHash>;
/// The upcoming session at which to tell the sessions pallet to rotate the validators.
NextSessionIndex get(fn next_session_index): SessionIndex;
/// The upcoming set of allowed validators, and their associated keys (or none).
NextValidators get(fn next_validators) : map hasher(blake2_128_concat) SubstrateId => Option<ValidatorKeys>;
/// The current set of allowed validators, and their associated keys.
Validators get(fn validators) : map hasher(blake2_128_concat) SubstrateId => Option<ValidatorKeys>;
/// An index to track interest earned by CASH holders and owed by CASH borrowers.
/// Note - the implementation of Default for CashIndex returns ONE. This also provides
/// the initial value as it is currently implemented.
GlobalCashIndex get(fn cash_index): CashIndex;
/// The upcoming base rate change for CASH and when, if any.
CashYieldNext get(fn cash_yield_next): Option<(APR, Timestamp)>;
/// The current APR on CASH held, and the base rate paid by borrowers.
CashYield get(fn cash_yield) config(): APR;
/// The liquidation incentive on seized collateral (e.g. 8% = 800 bips).
GlobalLiquidationIncentive get(fn liquidation_incentive): Bips;
/// The fraction of borrower interest that is paid to the protocol (e.g. 1/10th = 1000 bips).
Spreads get(fn spread): map hasher(blake2_128_concat) ChainAsset => Bips;
/// The mapping of indices to track interest owed by asset borrowers, by asset.
BorrowIndices get(fn borrow_index): map hasher(blake2_128_concat) ChainAsset => AssetIndex;
/// The mapping of indices to track interest earned by asset suppliers, by asset.
SupplyIndices get(fn supply_index): map hasher(blake2_128_concat) ChainAsset => AssetIndex;
/// The total CASH principal held per chain.
ChainCashPrincipals get(fn chain_cash_principal): map hasher(blake2_128_concat) ChainId => CashPrincipalAmount;
/// The total CASH principal in existence.
TotalCashPrincipal get(fn total_cash_principal): CashPrincipalAmount;
/// The total amount supplied per collateral asset.
TotalSupplyAssets get(fn total_supply_asset): map hasher(blake2_128_concat) ChainAsset => AssetAmount;
/// The total amount borrowed per collateral asset.
TotalBorrowAssets get(fn total_borrow_asset): map hasher(blake2_128_concat) ChainAsset => AssetAmount;
/// The mapping of CASH principal, by account.
CashPrincipals get(fn cash_principal): map hasher(blake2_128_concat) ChainAccount => CashPrincipal;
/// The mapping of asset balances, by asset and account.
AssetBalances get(fn asset_balance): double_map hasher(blake2_128_concat) ChainAsset, hasher(blake2_128_concat) ChainAccount => AssetBalance;
/// The index of assets with non-zero balance for each account.
AssetsWithNonZeroBalance get(fn assets_with_non_zero_balance): double_map hasher(blake2_128_concat) ChainAccount, hasher(blake2_128_concat) ChainAsset => ();
/// The mapping of asset indices, by asset and account.
LastIndices get(fn last_index): double_map hasher(blake2_128_concat) ChainAsset, hasher(blake2_128_concat) ChainAccount => AssetIndex;
/// The mapping of (status of) events witnessed on a given chain, by event id.
EventStates get(fn event_state): map hasher(blake2_128_concat) ChainLogId => EventState;
/// The mapping of notice id to notice.
Notices get(fn notice): double_map hasher(blake2_128_concat) ChainId, hasher(blake2_128_concat) NoticeId => Option<Notice>;
/// Notice IDs, indexed by the hash of the notice itself.
NoticeHashes get(fn notice_hash): map hasher(blake2_128_concat) ChainHash => Option<NoticeId>;
/// The state of a notice in regards to signing and execution, as tracked by the chain.
NoticeStates get(fn notice_state): double_map hasher(blake2_128_concat) ChainId, hasher(blake2_128_concat) NoticeId => NoticeState;
/// The most recent notice emitted for a given chain.
LatestNotice get(fn latest_notice_id): map hasher(blake2_128_concat) ChainId => Option<(NoticeId, ChainHash)>;
/// The change authority notices which must be fully signed before we allow notice signing to continue
NoticeHolds get(fn notice_hold): map hasher(blake2_128_concat) ChainId => Option<NoticeId>;
/// Index of notices by chain account
AccountNotices get(fn account_notices): map hasher(blake2_128_concat) ChainAccount => Vec<NoticeId>;
/// The last used nonce for each account, initialized at zero.
Nonces get(fn nonce): map hasher(blake2_128_concat) ChainAccount => Nonce;
/// The asset metadata for each supported asset, which will also be synced with the starports.
SupportedAssets get(fn asset): map hasher(blake2_128_concat) ChainAsset => Option<AssetInfo>;
/// Mapping of strings to tickers (valid tickers indexed by ticker string).
Tickers get(fn ticker): map hasher(blake2_128_concat) String => Option<Ticker>;
/// Miner of the current block.
Miner get(fn miner): Option<ChainAccount>;
/// Validator spread due to miner of last block.
LastMinerSharePrincipal get(fn last_miner_share_principal): CashPrincipalAmount;
/// The timestamp of the previous block or defaults to timestamp at genesis.
LastBlockTimestamp get(fn last_block_timestamp): Timestamp;
/// The cash index of the previous yield accrual point or defaults to initial cash index.
LastYieldCashIndex get(fn last_yield_cash_index): CashIndex;
/// The mapping of last block number for which validators added events to the ingression queue, by chain.
LastProcessedBlock get(fn last_processed_block): map hasher(blake2_128_concat) ChainId => Option<ChainBlock>;
/// Mapping of chain to the relevant Starport address
Starports get(fn chain_starports): map hasher(blake2_128_concat) ChainId => Option<ChainAccount>;
}
add_extra_genesis {
config(assets): Vec<AssetInfo>;
config(validators): Vec<ValidatorKeys>;
build(|config| {
Module::<T>::initialize_assets(config.assets.clone());
Module::<T>::initialize_validators(config.validators.clone());
})
}
}
/* ::EVENTS:: */
decl_event!(
pub enum Event {
/// An account has locked an asset. [asset, sender, recipient, amount]
Locked(ChainAsset, ChainAccount, ChainAccount, AssetAmount),
/// An account has locked CASH. [sender, recipient, principal, index]
LockedCash(ChainAccount, ChainAccount, CashPrincipalAmount, CashIndex),
/// An account has extracted an asset. [asset, sender, recipient, amount]
Extract(ChainAsset, ChainAccount, ChainAccount, AssetAmount),
/// An account has extracted CASH. [sender, recipient, principal, index]
ExtractCash(ChainAccount, ChainAccount, CashPrincipalAmount, CashIndex),
/// An account has transferred an asset. [asset, sender, recipient, amount]
Transfer(ChainAsset, ChainAccount, ChainAccount, AssetAmount),
/// An account has transferred CASH. [sender, recipient, principal, index]
TransferCash(ChainAccount, ChainAccount, CashPrincipalAmount, CashIndex),
/// An account has been liquidated. [asset, collateral_asset, liquidator, borrower, amount]
Liquidate(
ChainAsset,
ChainAsset,
ChainAccount,
ChainAccount,
AssetAmount,
),
/// An account borrowing CASH has been liquidated. [collateral_asset, liquidator, borrower, principal, index]
LiquidateCash(
ChainAsset,
ChainAccount,
ChainAccount,
CashPrincipalAmount,
CashIndex,
),
/// An account using CASH as collateral has been liquidated. [asset, liquidator, borrower, amount]
LiquidateCashCollateral(ChainAsset, ChainAccount, ChainAccount, AssetAmount),
/// The next code hash has been allowed. [hash]
AllowedNextCodeHash(CodeHash),
/// An attempt to set code via hash was made. [hash, result]
AttemptedSetCodeByHash(CodeHash, dispatch::DispatchResult),
/// An Ethereum event was successfully processed. [event_id]
ProcessedChainEvent(ChainLogId),
/// An Ethereum event failed during processing. [event_id, reason]
FailedProcessingChainEvent(ChainLogId, Reason),
/// A new notice is generated by the chain. [notice_id, notice, encoded_notice]
Notice(NoticeId, Notice, EncodedNotice),
/// A notice has been signe. [chain_id, notice_id, message, signatures]
SignedNotice(ChainId, NoticeId, EncodedNotice, ChainSignatureList),
/// A sequence of governance actions has been executed. [actions]
ExecutedGovernance(Vec<(Vec<u8>, GovernanceResult)>),
/// A new supply cap has been set. [asset, cap]
SetSupplyCap(ChainAsset, AssetAmount),
/// A new validator set has been chosen
ChangeValidators(Vec<ValidatorKeys>),
/// A new yield rate has been chosen
SetYieldNext(APR, Timestamp),
/// Failed to process a given extrinsic. [reason]
Failure(Reason),
}
);
/* ::ERRORS:: */
fn check_failure<T: Config>(res: Result<(), Reason>) -> Result<(), Reason> {
if let Err(err) = res {
<Module<T>>::deposit_event(Event::Failure(err));
log!("Cash Failure {:#?}", err);
}
res
}
pub trait SessionInterface<AccountId>: frame_system::Config {
fn is_valid_keys(x: AccountId) -> bool;
fn rotate_session();
}
impl<T: Config> SessionInterface<SubstrateId> for T
where
T: pallet_session::Config<ValidatorId = SubstrateId>,
{
fn is_valid_keys(x: SubstrateId) -> bool {
match <pallet_session::Module<T>>::next_keys(x as T::ValidatorId) {
Some(_keys) => true,
None => false,
}
}
fn rotate_session() {
<pallet_session::Module<T>>::rotate_session();
}
}
impl<T: Config> pallet_session::SessionManager<SubstrateId> for Module<T> {
// return validator set to use in the next session (aura and grandpa also stage new auths associated w these accountIds)
fn new_session(session_index: SessionIndex) -> Option<Vec<SubstrateId>> {
if NextValidators::iter().count() != 0 {
NextSessionIndex::put(session_index);
Some(NextValidators::iter().map(|x| x.0).collect::<Vec<_>>())
} else {
Some(Validators::iter().map(|x| x.0).collect::<Vec<_>>())
}
}
fn start_session(index: SessionIndex) {
// if changes have been queued
// if starting the queued session
if NextSessionIndex::get() == index && NextValidators::iter().count() != 0 {
// delete existing validators
for kv in <Validators>::iter() {
<Validators>::take(&kv.0);
}
// push next validators into current validators
for (id, chain_keys) in <NextValidators>::iter() {
<NextValidators>::take(&id);
<Validators>::insert(&id, chain_keys);
}
} else {
()
}
}
fn end_session(_: SessionIndex) {
()
}
}
/*
-- Block N --
changeAuth extrinsic, nextValidators set, hold is set, rotate_session is called
* new_session returns the nextValidators
-- Afterwards --
"ShouldEndSession" returns true when notice era notices were signed
* when it does, start_session sets Validators = NextValidators
*/
fn intersection_count<T: Ord + Debuggable>(a: Vec<T>, b: Vec<T>) -> usize {
let mut a_set = BTreeSet::<T>::new();
for v in a {
a_set.insert(v);
}
let mut b_set = BTreeSet::<T>::new();
for v in b {
b_set.insert(v);
}
a_set.intersection(&b_set).into_iter().count()
}
fn has_requisite_signatures(notice_state: NoticeState, validators: &Vec<ValidatorKeys>) -> bool {
let validator_count = validators.iter().count();
let quorum_count = validator_count; // TODO: Add a real quorum count
match notice_state {
NoticeState::Pending { signature_pairs } => match signature_pairs {
ChainSignatureList::Eth(signature_pairs) => {
intersection_count(
signature_pairs.iter().map(|p| p.0).collect(),
validators.iter().map(|v| v.eth_address).collect(),
) >= quorum_count
}
_ => false,
},
_ => false,
}
}
// XXX should this have all these printlns?
// periodic except when new authorities are pending and when an era notice has just been completed
impl<T: Config> pallet_session::ShouldEndSession<T::BlockNumber> for Module<T> {
fn should_end_session(now: T::BlockNumber) -> bool {
if NextValidators::iter().count() > 0 {
// Check if we should end the hold
let validators: Vec<_> = Validators::iter().map(|v| v.1).collect();
let every_notice_hold_executed = NoticeHolds::iter().all(|(chain_id, notice_id)| {
has_requisite_signatures(NoticeStates::get(chain_id, notice_id), &validators)
});
if every_notice_hold_executed {
for (chain_id, _) in NoticeHolds::iter() {
NoticeHolds::take(chain_id);
}
println!("should_end_session=true[next_validators]");
true
} else {
println!("should_end_session=false[pending_notice_held]");
false
}
} else {
// no era changes pending, periodic
let period: T::BlockNumber = <T>::BlockNumber::from(params::SESSION_PERIOD as u32);
let is_new_period = (now % period) == <T>::BlockNumber::from(0 as u32);
if is_new_period {
println!(
"should_end_session={}[periodic {:?}%{:?}]",
is_new_period, now, period
);
}
is_new_period
}
}
}
impl<T: Config> frame_support::traits::EstimateNextSessionRotation<T::BlockNumber> for Module<T> {
fn estimate_next_session_rotation(now: T::BlockNumber) -> Option<T::BlockNumber> {
let period: T::BlockNumber = <T>::BlockNumber::from(params::SESSION_PERIOD as u32);
Some(now + period - now % period)
}
// The validity of this weight depends on the implementation of `estimate_next_session_rotation`
fn weight(_now: T::BlockNumber) -> Weight {
0
}
}
/* ::MODULE:: */
/* ::EXTRINSICS:: */
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
// Events must be initialized if they are used by the pallet.
fn deposit_event() = default;
/// Called by substrate on block initialization.
/// Our initialization function is fallible, but that's not allowed.
fn on_initialize(block: T::BlockNumber) -> frame_support::weights::Weight {
match core::on_initialize::<T>() {
Ok(weight) => weight,
Err(err) => {
// This should never happen...
error!("Could not initialize block!!! {:#?} {:#?}", block, err);
0
}
}
}
/// Sets the miner of the this block via inherent
#[weight = (
0,
DispatchClass::Operational
)]
fn set_miner(origin, miner: ChainAccount) {
ensure_none(origin)?;
log!("set_miner({:?})", miner);
Miner::put(miner);
}
/// Sets the keys for the next set of validators beginning at the next session. [Root]
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn change_validators(origin, validators: Vec<ValidatorKeys>) -> dispatch::DispatchResult {
ensure_root(origin)?;
Ok(check_failure::<T>(internal::change_validators::change_validators::<T>(validators))?)
}
/// Sets the allowed next code hash to the given hash. [Root]
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn allow_next_code_with_hash(origin, hash: CodeHash) -> dispatch::DispatchResult {
ensure_root(origin)?;
Ok(check_failure::<T>(internal::next_code::allow_next_code_with_hash::<T>(hash))?)
}
/// Sets the allowed next code hash to the given hash. [User] [Free]
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn set_next_code_via_hash(origin, code: Vec<u8>) -> dispatch::DispatchResult {
ensure_none(origin)?;
let res = check_failure::<T>(internal::next_code::set_next_code_via_hash::<T>(code));
log!("Set next code via hash result: {:?}", res);
Ok(res?)
}
#[weight = (0, DispatchClass::Operational, Pays::No)]
pub fn set_starport(origin, chain_account: ChainAccount) -> dispatch::DispatchResult {
ensure_root(origin)?;
log!("Setting Starport to {:?}", chain_account);
Starports::insert(chain_account.chain_id(), chain_account);
Ok(())
}
#[weight = (0, DispatchClass::Operational, Pays::No)]
pub fn set_genesis_block(origin, chain_id: ChainId, chain_block: ChainBlock) -> dispatch::DispatchResult {
ensure_root(origin)?;
log!("Setting last processed block to {:?} {:?}", chain_id, chain_block);
LastProcessedBlock::insert(chain_id, chain_block);
Ok(())
}
/// Sets the supply cap for a given chain asset [Root]
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn set_supply_cap(origin, asset: ChainAsset, amount: AssetAmount) -> dispatch::DispatchResult {
ensure_root(origin)?;
Ok(check_failure::<T>(internal::supply_cap::set_supply_cap::<T>(asset, amount))?)
}
/// Set the liquidity factor for an asset [Root]
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn set_liquidity_factor(origin, asset: ChainAsset, factor: LiquidityFactor) -> dispatch::DispatchResult {
ensure_root(origin)?;
Ok(check_failure::<T>(internal::assets::set_liquidity_factor::<T>(asset, factor))?)
}
/// Update the interest rate model for a given asset. [Root]
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn set_rate_model(origin, asset: ChainAsset, model: InterestRateModel) -> dispatch::DispatchResult {
ensure_root(origin)?;
Ok(check_failure::<T>(internal::assets::set_rate_model::<T>(asset, model))?)
}
/// Set the cash yield rate at some point in the future. [Root]
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn set_yield_next(origin, next_apr: APR, next_apr_start: Timestamp) -> dispatch::DispatchResult {
ensure_root(origin)?;
Ok(check_failure::<T>(internal::set_yield_next::set_yield_next::<T>(next_apr, next_apr_start))?)
}
/// Adds the asset to the runtime by defining it as a supported asset. [Root]
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn support_asset(origin, asset: ChainAsset, asset_info: AssetInfo) -> dispatch::DispatchResult {
ensure_root(origin)?;
Ok(check_failure::<T>(internal::assets::support_asset::<T>(asset, asset_info))?)
}
// TODO: Do we need to sign the event id, too?
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn receive_event(origin, event_id: ChainLogId, event: ChainLogEvent, signature: ValidatorSig) -> dispatch::DispatchResult { // XXX sig
log!("receive_event(origin,event_id,event,signature): {:?} {:?} {}", event_id, &event, hex::encode(&signature)); // XXX ?
ensure_none(origin)?;
Ok(check_failure::<T>(internal::events::receive_event::<T>(event_id, event, signature))?)
}
#[weight = (1, DispatchClass::Operational, Pays::No)] // XXX
pub fn publish_signature(origin, chain_id: ChainId, notice_id: NoticeId, signature: ChainSignature) -> dispatch::DispatchResult {
ensure_none(origin)?;
Ok(check_failure::<T>(internal::notices::publish_signature(chain_id, notice_id, signature))?)
}
/// Offchain Worker entry point.
fn offchain_worker(block_number: T::BlockNumber) {
if let Err(e) = internal::events::fetch_events::<T>() {
log!("offchain_worker error during fetch events: {:?}", e);
}
let (succ, skip, failures) = internal::notices::process_notices::<T>(block_number);
let fail: usize = failures.iter().count();
if succ > 0 || skip > 0 || fail > 0 {
log!("offchain_worker process_notices: {} succ, {} skip, {} fail", succ, skip, fail);
}
if fail > 0 {
log!("offchain_worker error(s) during process notices: {:?}", failures);
}
}
/// Execute a transaction request on behalf of a user
#[weight = (1, DispatchClass::Normal, Pays::No)] // XXX
pub fn exec_trx_request(origin, request: Vec<u8>, signature: ChainAccountSignature, nonce: Nonce) -> dispatch::DispatchResult {
ensure_none(origin)?;
Ok(check_failure::<T>(internal::exec_trx_request::exec::<T>(request, signature, nonce))?)
}
// Remove any notice holds if they have been executed
#[weight = (1, DispatchClass::Normal, Pays::No)] // XXX
pub fn cull_notices(origin) -> dispatch::DispatchResult {
log!("Culling executed notices");
NoticeHolds::iter().for_each(|(chain_id, notice_id)| {
match NoticeStates::get(chain_id, notice_id) {
NoticeState::Executed => {
NoticeHolds::take(chain_id);
log!("Removed notice hold {:?}:{:?} as it was already executed", chain_id, notice_id);
},
_ => ()
}
});
Ok(())
}
}
}
/// Reading error messages inside `decl_module!` can be difficult, so we move them here.
impl<T: Config> Module<T> {
/// Initializes the set of supported assets from a config value.
fn initialize_assets(assets: Vec<AssetInfo>) {
for asset in assets {
SupportedAssets::insert(&asset.asset, asset);
}
}
/// Set the initial set of validators from the genesis config.
/// NextValidators will become current Validators upon first session start.
fn initialize_validators(validators: Vec<ValidatorKeys>) {
assert!(
!validators.is_empty(),
"Validators must be set in the genesis config"
);
for validator_keys in validators {
log!("Adding validator with keys: {:?}", validator_keys);
assert!(
<Validators>::get(&validator_keys.substrate_id) == None,
"Duplicate validator keys in genesis config"
);
// XXX for Substrate 3
// assert!(
// T::AccountStore::insert(&validator_keys.substrate_id, ()).is_ok(),
// "Could not placate the substrate account existence thing"
// );
<Validators>::insert(&validator_keys.substrate_id, validator_keys.clone());
}
}
// ** API / View Functions ** //
/// Get the asset balance for the given account.
pub fn get_account_balance(
account: ChainAccount,
asset: ChainAsset,
) -> Result<AssetBalance, Reason> {
Ok(core::get_account_balance::<T>(account, asset)?)
}
/// Get the asset info for the given asset.
pub fn get_asset(asset: ChainAsset) -> Result<AssetInfo, Reason> {
Ok(core::get_asset::<T>(asset)?)
}
/// Get the cash yield.
pub fn get_cash_yield() -> Result<APR, Reason> {
Ok(core::get_cash_yield::<T>()?)
}
/// Get the full cash balance for the given account.
pub fn get_full_cash_balance(account: ChainAccount) -> Result<AssetBalance, Reason> {
Ok(core::get_cash_balance_with_asset_interest::<T>(account)?.value)
}
/// Get the liquidity for the given account.
pub fn get_liquidity(account: ChainAccount) -> Result<AssetBalance, Reason> {
Ok(core::get_liquidity::<T>(account)?.value)
}
/// Get the total supply for the given asset.
pub fn get_market_totals(asset: ChainAsset) -> Result<(AssetAmount, AssetAmount), Reason> {
Ok(core::get_market_totals::<T>(asset)?)
}
/// Get the rates for the given asset.
pub fn get_rates(asset: ChainAsset) -> Result<(APR, APR), Reason> {
Ok(core::get_rates::<T>(asset)?)
}
}
impl<T: Config> frame_support::unsigned::ValidateUnsigned for Module<T> {
type Call = Call<T>;
/// Validate unsigned call to this module.
///
/// By default unsigned transactions are disallowed, but implementing the validator
/// here we make sure that some particular calls (the ones produced by offchain worker)
/// are being whitelisted and marked as valid.
fn validate_unsigned(source: TransactionSource, call: &Self::Call) -> TransactionValidity {
internal::validate_trx::validate_unsigned::<T>(source, call)
.unwrap_or(InvalidTransaction::Call.into())
}
}