Skip to content

Commit c18fdde

Browse files
authored
Staking API Division by 0
1 parent 060e7b0 commit c18fdde

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

staking-api/src/queries.ts

+38-19
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ WITH
416416
ce.epoch_id
417417
, case when fe.protocol_version='v4' then fe.pool
418418
else cebs.pool_id end
419-
as pool_id
419+
as pool_id
420420
, COUNT(*) AS num_fills
421421
, SUM(fe.protocol_fee_paid) / 1e18 AS protocol_fees
422422
FROM events.native_fills fe
@@ -439,7 +439,7 @@ WITH
439439
ELSE 0.00
440440
END) AS total_staked
441441
FROM current_epoch_beginning_status cebs
442-
LEFT JOIN current_epoch_fills_by_pool fbp ON fbp.epoch_id = cebs.epoch_id AND fbp.pool_id = cebs.pool_id
442+
LEFT JOIN current_epoch_fills_by_pool fbp ON fbp.epoch_id = cebs.epoch_id AND fbp.pool_id = cebs.pool_id
443443
)
444444
, total_fees AS (
445445
SELECT
@@ -452,24 +452,30 @@ WITH
452452
, cebs.maker_addresses AS maker_addresses
453453
, cebs.operator_share AS operator_share
454454
, cebs.zrx_delegated AS zrx_staked
455-
, ts.total_staked
456455
, cebs.operator_zrx_delegated AS operator_zrx_staked
457456
, cebs.member_zrx_delegated AS member_zrx_staked
458-
, cebs.zrx_delegated / NULLIF(ts.total_staked,0) AS share_of_stake
457+
, ts.total_staked
458+
, CASE
459+
WHEN ts.total_staked = 0 THEN
460+
NULL
461+
ELSE
462+
cebs.zrx_delegated / ts.total_staked
463+
END AS share_of_stake
459464
, fbp.protocol_fees AS total_protocol_fees_generated_in_eth
460465
, fbp.num_fills AS number_of_fills
461466
, CASE
462-
WHEN tf.total_protocol_fees = 0 THEN
463-
NULL
464-
ELSE fbp.protocol_fees / tf.total_protocol_fees
465-
END AS share_of_fees
467+
WHEN tf.total_protocol_fees = 0 THEN
468+
NULL
469+
ELSE
470+
fbp.protocol_fees / tf.total_protocol_fees
471+
END AS share_of_fees
466472
, fbp.num_fills::FLOAT / tf.total_fills::FLOAT AS share_of_fills
467473
, CASE
468-
WHEN tf.total_protocol_fees = 0 THEN
469-
NULL
470-
ELSE
471-
(cebs.zrx_delegated / NULLIF(ts.total_staked,0))
472-
/ NULLIF((COALESCE(fbp.protocol_fees,0) / tf.total_protocol_fees),0)
474+
WHEN tf.total_protocol_fees = 0 THEN
475+
NULL
476+
ELSE
477+
(cebs.zrx_delegated / ts.total_staked)
478+
/ (fbp.protocol_fees / tf.total_protocol_fees)
473479
END AS approximate_stake_ratio
474480
FROM events.staking_pool_created_events pce
475481
LEFT JOIN current_epoch_beginning_status cebs ON cebs.pool_id = pce.pool_id
@@ -530,14 +536,27 @@ export const currentEpochPoolStatsQuery = `
530536
, cebs.operator_zrx_delegated AS operator_zrx_staked
531537
, cebs.member_zrx_delegated AS member_zrx_staked
532538
, ts.total_staked
533-
, cebs.zrx_delegated / NULLIF(ts.total_staked,0) AS share_of_stake
539+
, CASE
540+
WHEN ts.total_staked = 0 THEN
541+
NULL
542+
ELSE
543+
cebs.zrx_delegated / ts.total_staked
544+
END AS share_of_stake
534545
, fbp.protocol_fees AS total_protocol_fees_generated_in_eth
535546
, fbp.num_fills AS number_of_fills
536-
, fbp.protocol_fees / tf.total_protocol_fees AS share_of_fees
537-
, fbp.num_fills::FLOAT / tf.total_fills::FLOAT AS share_of_fills
538-
, (cebs.zrx_delegated / NULLIF(ts.total_staked,0))
539-
/ (fbp.protocol_fees / tf.total_protocol_fees)
540-
AS approximate_stake_ratio
547+
, CASE
548+
WHEN tf.total_protocol_fees = 0 THEN
549+
NULL
550+
ELSE
551+
fbp.protocol_fees / tf.total_protocol_fees
552+
END AS share_of_fees
553+
, CASE
554+
WHEN tf.total_protocol_fees = 0 THEN
555+
NULL
556+
ELSE
557+
(cebs.zrx_delegated / ts.total_staked)
558+
/ (fbp.protocol_fees / tf.total_protocol_fees)
559+
END AS approximate_stake_ratio
541560
FROM events.staking_pool_created_events pce
542561
LEFT JOIN current_epoch_beginning_status cebs ON cebs.pool_id = pce.pool_id
543562
LEFT JOIN current_epoch_fills_by_pool fbp ON fbp.epoch_id = cebs.epoch_id AND fbp.pool_id = cebs.pool_id

0 commit comments

Comments
 (0)