@@ -389,6 +389,7 @@ func (i *Indexer) cmdEpoch(m *telebot.Message) {
389389 i .bot .Send (m .Chat , msg )
390390}
391391
392+
392393func (i * Indexer ) cmdLeaderlog (m * telebot.Message ) {
393394 if ! i .isAllowed (m ) {
394395 return
@@ -417,14 +418,11 @@ func (i *Indexer) cmdLeaderlog(m *telebot.Message) {
417418
418419 // Parse argument: "next", "current", or epoch number
419420 var targetEpoch int
420- var snapType SnapshotType
421421 switch {
422422 case args == "next" :
423423 targetEpoch = i .getCurrentEpoch () + 1
424- snapType = SnapshotMark
425424 case args == "current" :
426425 targetEpoch = i .getCurrentEpoch ()
427- snapType = SnapshotSet
428426 default :
429427 parsed , err := strconv .Atoi (args )
430428 if err != nil {
@@ -460,69 +458,9 @@ func (i *Indexer) cmdLeaderlog(m *telebot.Message) {
460458 return
461459 }
462460
463- // Try NtC first if epoch is within snapshot range (mark/set/go)
464- var poolStake , totalStake uint64
465- curEpoch := i .getCurrentEpoch ()
466- if i .nodeQuery != nil {
467- var snap SnapshotType
468- ntcAvailable := true
469- switch targetEpoch {
470- case curEpoch + 1 :
471- snap = SnapshotMark
472- case curEpoch :
473- snap = SnapshotSet
474- case curEpoch - 1 :
475- snap = SnapshotGo
476- default :
477- ntcAvailable = false
478- }
479- if ntcAvailable {
480- ntcCtx , ntcCancel := context .WithTimeout (ctx , 5 * time .Minute )
481- snapshots , snapErr := i .nodeQuery .QueryPoolStakeSnapshots (ntcCtx , i .bech32PoolId )
482- ntcCancel ()
483- if snapErr != nil {
484- log .Printf ("NtC stake query for epoch %d failed: %v" , targetEpoch , snapErr )
485- } else {
486- switch snap {
487- case SnapshotMark :
488- poolStake = snapshots .PoolStakeMark
489- totalStake = snapshots .TotalStakeMark
490- case SnapshotSet :
491- poolStake = snapshots .PoolStakeSet
492- totalStake = snapshots .TotalStakeSet
493- case SnapshotGo :
494- poolStake = snapshots .PoolStakeGo
495- totalStake = snapshots .TotalStakeGo
496- }
497- }
498- }
499- }
500-
501- // Koios fallback for epochs outside NtC range or NtC failure
502- if poolStake == 0 && i .koios != nil {
503- curEpoch := i .getCurrentEpoch ()
504- for _ , tryEpoch := range []int {targetEpoch , curEpoch , curEpoch - 1 , curEpoch - 2 } {
505- epochNo := koios .EpochNo (tryEpoch )
506- poolHist , histErr := i .koios .GetPoolHistory (ctx , koios .PoolID (i .bech32PoolId ), & epochNo , nil )
507- if histErr != nil || len (poolHist .Data ) == 0 {
508- continue
509- }
510- poolStake = uint64 (poolHist .Data [0 ].ActiveStake .IntPart ())
511- epochInfo , infoErr := i .koios .GetEpochInfo (ctx , & epochNo , nil )
512- if infoErr != nil || len (epochInfo .Data ) == 0 {
513- poolStake = 0
514- continue
515- }
516- totalStake = uint64 (epochInfo .Data [0 ].ActiveStake .IntPart ())
517- if tryEpoch != targetEpoch {
518- log .Printf ("Using Koios stake fallback for /leaderlog %d (from epoch %d)" , targetEpoch , tryEpoch )
519- }
520- break
521- }
522- }
523-
524- if poolStake == 0 || totalStake == 0 {
525- replyEpoch (fmt .Sprintf ("Failed to get stake for epoch %d from NtC or Koios" , targetEpoch ))
461+ poolStake , totalStake , stakeErr := i .queryStakeForLeaderlog (ctx , targetEpoch )
462+ if stakeErr != nil {
463+ replyEpoch (fmt .Sprintf ("Failed to get stake for epoch %d: %v" , targetEpoch , stakeErr ))
526464 return
527465 }
528466
@@ -576,51 +514,9 @@ func (i *Indexer) cmdLeaderlog(m *telebot.Message) {
576514 return
577515 }
578516
579- // Try NtC first for stake data
580- var poolStake , totalStake uint64
581- if i .nodeQuery != nil {
582- ntcCtx , ntcCancel := context .WithTimeout (ctx , 5 * time .Minute )
583- snapshots , snapErr := i .nodeQuery .QueryPoolStakeSnapshots (ntcCtx , i .bech32PoolId )
584- ntcCancel ()
585- if snapErr != nil {
586- log .Printf ("NtC stake query for epoch %d failed: %v" , targetEpoch , snapErr )
587- } else {
588- switch snapType {
589- case SnapshotSet :
590- poolStake = snapshots .PoolStakeSet
591- totalStake = snapshots .TotalStakeSet
592- default :
593- poolStake = snapshots .PoolStakeMark
594- totalStake = snapshots .TotalStakeMark
595- }
596- }
597- }
598-
599- // Koios fallback if NtC unavailable or failed (try recent epochs since Koios may lag)
600- if poolStake == 0 && i .koios != nil {
601- curEpoch := i .getCurrentEpoch ()
602- for _ , tryEpoch := range []int {curEpoch , curEpoch - 1 , curEpoch - 2 } {
603- epochNo := koios .EpochNo (tryEpoch )
604- poolHist , histErr := i .koios .GetPoolHistory (ctx , koios .PoolID (i .bech32PoolId ), & epochNo , nil )
605- if histErr != nil || len (poolHist .Data ) == 0 {
606- continue
607- }
608- poolStake = uint64 (poolHist .Data [0 ].ActiveStake .IntPart ())
609- epochInfo , infoErr := i .koios .GetEpochInfo (ctx , & epochNo , nil )
610- if infoErr != nil || len (epochInfo .Data ) == 0 {
611- poolStake = 0
612- continue
613- }
614- totalStake = uint64 (epochInfo .Data [0 ].ActiveStake .IntPart ())
615- if tryEpoch != targetEpoch {
616- log .Printf ("Using Koios stake fallback for /leaderlog (from epoch %d)" , tryEpoch )
617- }
618- break
619- }
620- }
621-
622- if poolStake == 0 || totalStake == 0 {
623- reply (fmt .Sprintf ("Failed to get stake for epoch %d from NtC or Koios" , targetEpoch ))
517+ poolStake , totalStake , stakeErr := i .queryStakeForLeaderlog (ctx , targetEpoch )
518+ if stakeErr != nil {
519+ reply (fmt .Sprintf ("Failed to get stake for epoch %d: %v" , targetEpoch , stakeErr ))
624520 return
625521 }
626522
@@ -994,41 +890,9 @@ func (i *Indexer) cmdNextBlock(m *telebot.Message) {
994890 return
995891 }
996892
997- var poolStake , totalStake uint64
998- if i .nodeQuery != nil {
999- ntcCtx , ntcCancel := context .WithTimeout (ctx , 5 * time .Minute )
1000- snapshots , snapErr := i .nodeQuery .QueryPoolStakeSnapshots (ntcCtx , i .bech32PoolId )
1001- ntcCancel ()
1002- if snapErr != nil {
1003- log .Printf ("NtC stake query failed, trying Koios fallback: %v" , snapErr )
1004- } else {
1005- poolStake = snapshots .PoolStakeSet
1006- totalStake = snapshots .TotalStakeSet
1007- }
1008- }
1009- if poolStake == 0 && i .koios != nil {
1010- // Try recent epochs (Koios may lag 1-2 epochs behind)
1011- for _ , tryEpoch := range []int {currentEpoch , currentEpoch - 1 , currentEpoch - 2 } {
1012- epochNo := koios .EpochNo (tryEpoch )
1013- poolHist , histErr := i .koios .GetPoolHistory (ctx , koios .PoolID (i .bech32PoolId ), & epochNo , nil )
1014- if histErr != nil || len (poolHist .Data ) == 0 {
1015- log .Printf ("Koios GetPoolHistory(%d) empty or error: %v" , tryEpoch , histErr )
1016- continue
1017- }
1018- poolStake = uint64 (poolHist .Data [0 ].ActiveStake .IntPart ())
1019- epochInfo , infoErr := i .koios .GetEpochInfo (ctx , & epochNo , nil )
1020- if infoErr != nil || len (epochInfo .Data ) == 0 {
1021- log .Printf ("Koios GetEpochInfo(%d) failed: %v" , tryEpoch , infoErr )
1022- poolStake = 0
1023- continue
1024- }
1025- totalStake = uint64 (epochInfo .Data [0 ].ActiveStake .IntPart ())
1026- log .Printf ("Using Koios stake fallback for /nextblock (from epoch %d)" , tryEpoch )
1027- break
1028- }
1029- }
1030- if poolStake == 0 || totalStake == 0 {
1031- reply ("No stake data available from NtC or Koios" )
893+ poolStake , totalStake , stakeErr := i .queryStakeForLeaderlog (ctx , currentEpoch )
894+ if stakeErr != nil {
895+ reply (fmt .Sprintf ("No stake data available: %v" , stakeErr ))
1032896 return
1033897 }
1034898
0 commit comments