@@ -110,7 +110,9 @@ impl Baker {
110
110
111
111
async fn baker_id ( & self ) -> BakerId { self . id }
112
112
113
- async fn state < ' a > ( & ' a self ) -> ApiResult < BakerState < ' a > > {
113
+ async fn state < ' a > ( & ' a self , ctx : & Context < ' a > ) -> ApiResult < BakerState < ' a > > {
114
+ let pool = get_pool ( ctx) ?;
115
+
114
116
let transaction_commission = self
115
117
. transaction_commission
116
118
. map ( u32:: try_from)
@@ -127,17 +129,53 @@ impl Baker {
127
129
. transpose ( ) ?
128
130
. map ( |c| AmountFraction :: new_unchecked ( c) . into ( ) ) ;
129
131
132
+ let total_stake: i64 =
133
+ sqlx:: query_scalar!( "SELECT total_staked FROM blocks ORDER BY height DESC LIMIT 1" )
134
+ . fetch_one ( pool)
135
+ . await ?;
136
+
137
+ let row = sqlx:: query!(
138
+ "
139
+ SELECT
140
+ COUNT(*) AS delegator_count,
141
+ SUM(delegated_stake)::BIGINT AS delegated_stake
142
+ FROM accounts
143
+ WHERE delegated_target_baker_id = $1
144
+ " ,
145
+ self . id. 0
146
+ )
147
+ . fetch_one ( pool)
148
+ . await ?;
149
+
150
+ let delegated_stake = row. delegated_stake . unwrap_or ( 0 ) ;
151
+
152
+ // The total amount staked in this baker pool includes the baker stake
153
+ // and the delegated stake.
154
+ let total_pool_stake = self . staked + delegated_stake;
155
+
156
+ // Division by 0 is not possible because `total_staked` is always a positive
157
+ // number.
158
+ let total_stake_percentage = ( rust_decimal:: Decimal :: from ( total_pool_stake)
159
+ * rust_decimal:: Decimal :: from ( 100 ) )
160
+ . checked_div ( rust_decimal:: Decimal :: from ( total_stake) )
161
+ . ok_or_else ( || ApiError :: InternalError ( "Division by zero" . to_string ( ) ) ) ?
162
+ . into ( ) ;
163
+
130
164
let out = BakerState :: ActiveBakerState ( ActiveBakerState {
131
165
staked_amount : Amount :: try_from ( self . staked ) ?,
132
166
restake_earnings : self . restake_earnings ,
133
167
pool : BakerPool {
134
- open_status : self . open_status ,
168
+ open_status : self . open_status ,
135
169
commission_rates : CommissionRates {
136
170
transaction_commission,
137
171
baking_commission,
138
172
finalization_commission,
139
173
} ,
140
- metadata_url : self . metadata_url . as_deref ( ) ,
174
+ metadata_url : self . metadata_url . as_deref ( ) ,
175
+ total_stake_percentage,
176
+ total_stake : total_pool_stake. try_into ( ) ?,
177
+ delegated_stake : delegated_stake. try_into ( ) ?,
178
+ delegator_count : row. delegator_count . unwrap_or ( 0 ) ,
141
179
} ,
142
180
pending_change : None , // This is not used starting from P7.
143
181
} ) ;
@@ -338,29 +376,28 @@ enum BakerSort {
338
376
339
377
#[ derive( SimpleObject ) ]
340
378
struct BakerPool < ' a > {
341
- // /// Total stake of the baker pool as a percentage of all CCDs in existence.
342
- // /// Value may be null for brand new bakers where statistics have not
343
- // /// been calculated yet. This should be rare and only a temporary
344
- // /// condition.
345
- // total_stake_percentage: Decimal,
379
+ /// Total stake of the baker pool as a percentage of all CCDs in existence.
380
+ /// Includes both baker stake and delegated stake.
381
+ total_stake_percentage : Decimal ,
382
+ /// The total amount staked in this baker pool. Includes both baker stake
383
+ /// and delegated stake.
384
+ total_stake : Amount ,
385
+ /// The total amount staked by delegators to this baker pool.
386
+ delegated_stake : Amount ,
387
+ /// The number of delegators that delegate to this baker pool.
388
+ delegator_count : i64 ,
346
389
// lottery_power: Decimal,
347
390
// payday_commission_rates: CommissionRates,
348
- open_status : Option < BakerPoolOpenStatus > ,
349
- commission_rates : CommissionRates ,
350
- metadata_url : Option < & ' a str > ,
351
- // /// The total amount staked by delegation to this baker pool.
352
- // delegated_stake: Amount,
353
- // /// The maximum amount that may be delegated to the pool, accounting for
354
- // /// leverage and stake limits.
355
- // delegated_stake_cap: Amount,
356
- // /// The total amount staked in this baker pool. Includes both baker stake
357
- // /// and delegated stake.
358
- // total_stake: Amount,
359
- // delegator_count: i32,
360
391
// /// Ranking of the baker pool by total staked amount. Value may be null for
361
392
// /// brand new bakers where statistics have not been calculated yet. This
362
393
// /// should be rare and only a temporary condition.
363
394
// ranking_by_total_stake: Ranking,
395
+ // /// The maximum amount that may be delegated to the pool, accounting for
396
+ // /// leverage and stake limits.
397
+ // delegated_stake_cap: Amount,
398
+ open_status : Option < BakerPoolOpenStatus > ,
399
+ commission_rates : CommissionRates ,
400
+ metadata_url : Option < & ' a str > ,
364
401
// TODO: apy(period: ApyPeriod!): PoolApy!
365
402
// TODO: delegators("Returns the first _n_ elements from the list." first: Int "Returns the
366
403
// elements in the list that come after the specified cursor." after: String "Returns the last
0 commit comments