@@ -47,7 +47,12 @@ pub mod pallet {
47
47
#[ pallet:: hooks]
48
48
impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
49
49
fn offchain_worker ( _n : BlockNumberFor < T > ) {
50
- // TODO: call `fetch_btc_price_and_send_unsigned_transaction` and log any error
50
+ match Self :: fetch_btc_price_and_send_unsigned_transaction ( ) {
51
+ Ok ( _) => { } ,
52
+ Err ( e) => {
53
+ log:: error!( "Failed to fetch and set BTC price: {e}" ) ;
54
+ } ,
55
+ } ;
51
56
}
52
57
}
53
58
@@ -58,10 +63,11 @@ pub mod pallet {
58
63
impl < T : Config > Pallet < T > {
59
64
#[ pallet:: weight( 0 ) ]
60
65
pub fn set_btc_price ( origin : OriginFor < T > , btc_price : FixedI64 ) -> DispatchResult {
61
- // TODO:
62
- // - ensure origin is none
63
- // - set BTCPrice storage
64
- // - emit `BtcPriceSet` event
66
+ ensure_none ( origin) ?;
67
+
68
+ BTCPrice :: < T > :: set ( Some ( btc_price) ) ;
69
+
70
+ Self :: deposit_event ( Event :: < T > :: BtcPriceSet ( btc_price) ) ;
65
71
66
72
Ok ( ( ) )
67
73
}
@@ -77,10 +83,24 @@ pub mod pallet {
77
83
/// here we make sure that some particular calls (the ones produced by offchain worker)
78
84
/// are being whitelisted and marked as valid.
79
85
fn validate_unsigned ( source : TransactionSource , call : & Self :: Call ) -> TransactionValidity {
80
- // TODO: implemente some kind of validation
81
- // It should accept calls to `set_btc_price` and refuse any other
82
-
83
- InvalidTransaction :: Call . into ( )
86
+ if source == TransactionSource :: External {
87
+ return InvalidTransaction :: Call . into ( )
88
+ }
89
+
90
+ const UNSIGNED_TXS_PRIORITY : TransactionPriority = u64:: MAX ;
91
+ let valid_tx = |provide| {
92
+ ValidTransaction :: with_tag_prefix ( "price-oracle" )
93
+ . priority ( UNSIGNED_TXS_PRIORITY )
94
+ . and_provides ( [ & provide] )
95
+ . longevity ( 1 ) // Only valid for this block
96
+ . propagate ( false ) // Should not be gossiped
97
+ . build ( )
98
+ } ;
99
+
100
+ match call {
101
+ Call :: set_btc_price { .. } => valid_tx ( b"set_btc_price" . to_vec ( ) ) ,
102
+ _ => InvalidTransaction :: Call . into ( ) ,
103
+ }
84
104
}
85
105
}
86
106
}
0 commit comments