-
Notifications
You must be signed in to change notification settings - Fork 721
/
ListBufferedSelector.hs
36 lines (32 loc) · 1.02 KB
/
ListBufferedSelector.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{-# LANGUAGE LambdaCase #-}
module Cardano.Benchmarking.ListBufferedSelector
(
mkBufferedSource
)
where
import Prelude
import Control.Concurrent.MVar
import Cardano.Api
import Cardano.Benchmarking.FundSet as FundSet
import Cardano.Benchmarking.Wallet as Wallet
mkBufferedSource ::
WalletRef
-> Int
-> Lovelace
-> Maybe Variant
-> Int
-> IO (Either String (FundSource IO))
mkBufferedSource walletRef count minValue variant munch
= mkWalletFundSource walletRef (selectToBuffer count minValue variant) >>= \case
Left err -> return $ Left err
Right funds -> do
buffer <- newMVar funds
return $ Right $ listSource buffer munch
listSource :: MVar [Fund] -> Int -> IO (Either String [Fund])
listSource mvar count = modifyMVarMasked mvar popFunds
where
popFunds funds
= if length sel == count
then return (rest, Right sel)
else return (funds, Left "ListSource: out of funds")
where (sel, rest) = splitAt count funds