@@ -24,11 +24,12 @@ use sp_runtime::RuntimeDebug;
2424
2525/// Types
2626///
27- /// * `AI`: Account Id
28- /// * `BN`: Block Number
29- /// * `M`: Moment (Time moment)
27+ /// * `AI`: Account id
28+ /// * `BA`: Balance type for bonds
29+ /// * `BN`: Block number
30+ /// * `M`: Moment (time moment)
3031#[ derive( Clone , Decode , Encode , Eq , PartialEq , RuntimeDebug , TypeInfo ) ]
31- pub struct Market < AI , BN , M > {
32+ pub struct Market < AI , BA , BN , M > {
3233 /// Creator of this market.
3334 pub creator : AI ,
3435 /// Creation type.
@@ -56,9 +57,53 @@ pub struct Market<AI, BN, M> {
5657 pub resolved_outcome : Option < OutcomeReport > ,
5758 /// See [`MarketDisputeMechanism`].
5859 pub dispute_mechanism : MarketDisputeMechanism ,
60+ /// The bonds reserved for this market.
61+ pub bonds : MarketBonds < AI , BA > ,
62+ }
63+
64+ /// Tracks the status of a bond.
65+ #[ derive( Clone , Decode , Encode , MaxEncodedLen , PartialEq , Eq , RuntimeDebug , TypeInfo ) ]
66+ pub struct Bond < AI , BA > {
67+ /// The account that reserved the bond.
68+ pub who : AI ,
69+ /// The amount reserved.
70+ pub value : BA ,
71+ /// `true` if and only if the bond is unreserved and/or (partially) slashed.
72+ pub is_settled : bool ,
73+ }
74+
75+ impl < AI , BA > Bond < AI , BA > {
76+ pub fn new ( who : AI , value : BA ) -> Bond < AI , BA > {
77+ Bond { who, value, is_settled : false }
78+ }
79+ }
80+
81+ /// Tracks bonds associated with a prediction market.
82+ #[ derive( Clone , Decode , Encode , MaxEncodedLen , PartialEq , Eq , RuntimeDebug , TypeInfo ) ]
83+ pub struct MarketBonds < AI , BA > {
84+ pub creation : Option < Bond < AI , BA > > ,
85+ pub oracle : Option < Bond < AI , BA > > ,
5986}
6087
61- impl < AI , BN , M > Market < AI , BN , M > {
88+ impl < AI : Ord , BA : frame_support:: traits:: tokens:: Balance > MarketBonds < AI , BA > {
89+ /// Return the combined value of the open bonds for `who`.
90+ pub fn total_amount_bonded ( & self , who : & AI ) -> BA {
91+ let value_or_default = |bond : & Option < Bond < AI , BA > > | match bond {
92+ Some ( bond) if bond. who == * who => bond. value ,
93+ _ => BA :: zero ( ) ,
94+ } ;
95+ value_or_default ( & self . creation ) . saturating_add ( value_or_default ( & self . oracle ) )
96+ }
97+ }
98+
99+ // Used primarily for testing purposes.
100+ impl < AI , BA > Default for MarketBonds < AI , BA > {
101+ fn default ( ) -> Self {
102+ MarketBonds { creation : None , oracle : None }
103+ }
104+ }
105+
106+ impl < AI , BA , BN , M > Market < AI , BA , BN , M > {
62107 // Returns the number of outcomes for a market.
63108 pub fn outcomes ( & self ) -> u16 {
64109 match self . market_type {
@@ -84,9 +129,10 @@ impl<AI, BN, M> Market<AI, BN, M> {
84129 }
85130}
86131
87- impl < AI , BN , M > MaxEncodedLen for Market < AI , BN , M >
132+ impl < AI , BA , BN , M > MaxEncodedLen for Market < AI , BA , BN , M >
88133where
89134 AI : MaxEncodedLen ,
135+ BA : MaxEncodedLen ,
90136 BN : MaxEncodedLen ,
91137 M : MaxEncodedLen ,
92138{
@@ -105,6 +151,7 @@ where
105151 . saturating_add ( <Option < Report < AI , BN > > >:: max_encoded_len ( ) )
106152 . saturating_add ( <Option < OutcomeReport > >:: max_encoded_len ( ) )
107153 . saturating_add ( <MarketDisputeMechanism >:: max_encoded_len ( ) )
154+ . saturating_add ( <MarketBonds < AI , BA > >:: max_encoded_len ( ) )
108155 }
109156}
110157
@@ -160,7 +207,9 @@ impl<BN: MaxEncodedLen, M: MaxEncodedLen> MaxEncodedLen for MarketPeriod<BN, M>
160207}
161208
162209/// Defines deadlines for market.
163- #[ derive( Clone , Copy , Decode , Encode , Eq , MaxEncodedLen , PartialEq , RuntimeDebug , TypeInfo ) ]
210+ #[ derive(
211+ Clone , Copy , Decode , Default , Encode , Eq , MaxEncodedLen , PartialEq , RuntimeDebug , TypeInfo ,
212+ ) ]
164213pub struct Deadlines < BN > {
165214 pub grace_period : BN ,
166215 pub oracle_duration : BN ,
@@ -232,7 +281,7 @@ pub struct SubsidyUntil<BN, MO, MI> {
232281mod tests {
233282 use crate :: market:: * ;
234283 use test_case:: test_case;
235- type Market = crate :: market:: Market < u32 , u32 , u32 > ;
284+ type Market = crate :: market:: Market < u32 , u32 , u32 , u32 > ;
236285
237286 #[ test_case(
238287 MarketType :: Categorical ( 6 ) ,
@@ -305,16 +354,19 @@ mod tests {
305354 report : None ,
306355 resolved_outcome : None ,
307356 dispute_mechanism : MarketDisputeMechanism :: Authorized ,
357+ bonds : MarketBonds :: default ( ) ,
308358 } ;
309359 assert_eq ! ( market. matches_outcome_report( & outcome_report) , expected) ;
310360 }
361+
311362 #[ test]
312363 fn max_encoded_len_market_type ( ) {
313364 // `MarketType::Scalar` is the largest enum variant.
314365 let market_type = MarketType :: Scalar ( 1u128 ..=2 ) ;
315366 let len = parity_scale_codec:: Encode :: encode ( & market_type) . len ( ) ;
316367 assert_eq ! ( MarketType :: max_encoded_len( ) , len) ;
317368 }
369+
318370 #[ test]
319371 fn max_encoded_len_market_period ( ) {
320372 let market_period: MarketPeriod < u32 , u32 > = MarketPeriod :: Block ( Default :: default ( ) ) ;
0 commit comments