Skip to content

Commit 0278cd2

Browse files
committed
Integrate fixed fee average into CLI. Add option for number of blocks to collect for fee average.
1 parent 92fd181 commit 0278cd2

File tree

4 files changed

+47
-27
lines changed

4 files changed

+47
-27
lines changed

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog for check_cgminer
22

3+
## 0.7.10.0
4+
5+
- Fix block fee calculation for profitability calculation
6+
- Allow option to specify the number of past blocks to average when considering average block fee
7+
38
## 0.7.9.0
49

510
- Allow for dynamic power strategy power rate override via the command line.

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: check-cgminer
2-
version: 0.7.9.0
2+
version: 0.7.10.0
33
github: "dmp1ce/check_cgminer"
44
license: PublicDomain
55
author: "David Parrish"

src/CheckCgminer.hs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ data CliOptions = CliOptions
6262
, electricity_rate :: Double
6363
, profitability_warning :: Double
6464
, profitability_critical :: Double
65+
, block_fee_average_num :: Integer
6566
, block_reward :: Maybe Double
6667
, mining_fee_reward :: Maybe Double
6768
, pool_fee :: Double
@@ -105,6 +106,8 @@ defaultProfitabilityCriticalThreshold :: Double
105106
defaultProfitabilityCriticalThreshold = 0
106107
defaultPoolFee :: Double
107108
defaultPoolFee = 0
109+
defaultBlockFeeAverageNum :: Integer
110+
defaultBlockFeeAverageNum = 10
108111

109112
cliOptions :: Parser CliOptions
110113
cliOptions = CliOptions
@@ -277,6 +280,13 @@ cliOptions = CliOptions
277280
<> help "Critical profitability threshold in USD/day"
278281
<> showDefault
279282
)
283+
<*> option auto
284+
( long "fee_block_average_num"
285+
<> metavar "NUMBER"
286+
<> help "Number of past blocks to use for fee average"
287+
<> value defaultBlockFeeAverageNum
288+
<> showDefault
289+
)
280290
<*> optional (option auto
281291
( long "block_reward"
282292
<> metavar "NUMBER"
@@ -464,7 +474,7 @@ instance ToPerfData PerfDataWorkMode where
464474

465475
-- | Try to parse stats from miner. Return error `T.Text` if for failure.
466476
tryCommand :: T.Text -> (ReplyApi -> Either String Stats) -> CliOptions -> IO (Either T.Text Stats)
467-
tryCommand c f (CliOptions h p _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) = do
477+
tryCommand c f (CliOptions h p _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) = do
468478
r <- catches (sendCGMinerCommand h p c)
469479
[ Handler (\(e :: IOException) ->
470480
error $ "IOException while sending '" ++ T.unpack c ++ "' command: " ++ show e
@@ -498,7 +508,7 @@ trySummary = tryCommand "summary" getSummary
498508

499509
execCheck :: CliOptions -> IO ()
500510
execCheck opts@(CliOptions _ _ tw tc hw hc hmax hirw hirc hu flw flc fhw fhc vhw vhc freqhw
501-
freqhc ps mpc erd profw profc mbr mmfr pfp) = do
511+
freqhc ps mpc erd profw profc bfan mbr mmfr pfp) = do
502512
-- Try to get "stats" command first
503513
eStats <- tryStats opts
504514
processStats eStats
@@ -525,7 +535,7 @@ execCheck opts@(CliOptions _ _ tw tc hw hc hmax hirw hirc hu flw flc fhw fhc vhw
525535
getProfitabilityFactors :: IO (Maybe ProfitabilityFactors)
526536
getProfitabilityFactors = do
527537
dNr <- cacheIO "difficultyAndRewardCache" nominalDay getBitcoinDifficultyAndReward
528-
mr <- cacheIO "minerFeeRewardCache" nominalDay getBitcoinAverageMiningFeeReward
538+
mr <- cacheIO "minerFeeRewardCache" nominalDay $ getBitcoinAverageMiningFeeReward bfan
529539
p <- cacheIO "priceCache" nominalDay getBitcoinPrice
530540
let er = EnergyRate USD KiloWattHour (toRational erd)
531541
return $ ProfitabilityFactors er <$> (fst <$> dNr) <*> p

src/Helper.hs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,34 @@ getBitcoinDifficultyAndRewardFromBitcoinChain = do
164164
_ -> return Nothing
165165
return $ (,) <$> d <*> reward
166166

167-
-- Average mining fee per block
168-
getBitcoinAverageMiningFeeReward :: IO (Maybe Bitcoins)
169-
getBitcoinAverageMiningFeeReward = do
167+
-- | Average mining fee per block
168+
getBitcoinAverageMiningFeeReward :: Integer -> IO (Maybe Bitcoins)
169+
getBitcoinAverageMiningFeeReward = getBitcoinAverageBlockFeesBlockchainInfo
170+
171+
-- | Get the average fee for past blocks
172+
-- Can take up to about 1 second wait per block.
173+
getBitcoinAverageBlockFeesBlockchainInfo :: Integer -- ^ How many blocks back to average
174+
-> IO (Maybe Bitcoins)
175+
getBitcoinAverageBlockFeesBlockchainInfo h
176+
| h <= 0 = return $ Just $ Bitcoins Bitcoin 0
177+
| otherwise = do
178+
l <- getLatestBitcoinBlockCached
179+
t <- f h l
180+
return $ Just $ Bitcoins Bitcoin $ (t / toRational h) / 100000000
181+
where
182+
f :: Integer -> Maybe RawBlock -> IO Rational
183+
f 0 _ = return 0
184+
f h' b =
185+
case b of
186+
Just b' -> do
187+
p <- getBitcoinRawBlockCached $ _previousBlock b'
188+
pf <- f (h' -1) p
189+
return $ _fee b' + pf
190+
Nothing -> return 0
191+
192+
-- Archived and original Fee average from blockchain.com
193+
getBitcoinAverageBlockFeesBitcoinchain :: IO (Maybe Bitcoins)
194+
getBitcoinAverageBlockFeesBitcoinchain = do
170195
r <- W.get "https://api-r.bitcoinchain.com/v1/blocks/100"
171196
let rats = rational <$> r ^.. W.responseBody . values . key "fee" . _String
172197
case (/ (toRational . length) rats) <$> (sum . (fst <$>) <$> sequenceA rats) of
@@ -204,26 +229,6 @@ getBitcoinRawBlock h = do
204229
getBitcoinRawBlockCached :: B.ByteString -> IO (Maybe RawBlock)
205230
getBitcoinRawBlockCached h = cacheIO ("block_" <> cs h) (C.nominalDay * 10000) $ getBitcoinRawBlock h
206231

207-
-- | Get the average fee for past blocks
208-
-- Can take up to about 1 second wait per block.
209-
getBitcoinAverageBlockFees :: Integer -- ^ How many blocks back to average
210-
-> IO (Maybe Rational)
211-
getBitcoinAverageBlockFees h
212-
| h <= 0 = return $ Just 0
213-
| otherwise = do
214-
l <- getLatestBitcoinBlockCached
215-
t <- f h l
216-
return $ Just $ t / toRational h
217-
where
218-
f :: Integer -> Maybe RawBlock -> IO Rational
219-
f 0 _ = return 0
220-
f h' b =
221-
case b of
222-
Just b' -> do
223-
p <- getBitcoinRawBlockCached $ _previousBlock b'
224-
pf <- f (h' -1) p
225-
return $ _fee b' + pf
226-
Nothing -> return 0
227232

228233
-- Caching
229234
newtype CacheUTCTime = CacheUTCTime C.UTCTime deriving (Eq, Show)

0 commit comments

Comments
 (0)