Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
erikd committed Apr 28, 2024
1 parent cd1f7c7 commit 95c0a6f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
3 changes: 2 additions & 1 deletion cardano-faucet/cardano-faucet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ library
, MissingH
, bytestring
, cardano-addresses
, cardano-api
, cardano-api ^>= 8.44
, cardano-cli ^>= 8.22
, cardano-prelude
, cardano-ledger-core
, containers
, either
, formatting
Expand Down
19 changes: 10 additions & 9 deletions cardano-faucet/src/Cardano/Faucet/Misc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

module Cardano.Faucet.Misc where

import Cardano.Api (ConsensusModeParams(CardanoModeParams), CardanoMode, EpochSlots(EpochSlots), AddressAny, parseAddressAny, TxOutValue(TxOutAdaOnly, TxOutValue), CardanoEra, EraInMode, toEraInMode, ConsensusMode(CardanoMode),AssetId(AdaAssetId), Quantity, valueToList)
import Cardano.Api.Shelley (Lovelace, selectLovelace, AssetId(AssetId))
import Cardano.Api (ConsensusModeParams(CardanoModeParams), EpochSlots(EpochSlots), AddressAny, parseAddressAny, CardanoEra, AssetId(AdaAssetId), Quantity, valueToList, TxOutValue (..))
import Cardano.Api.Shelley (selectLovelace, AssetId(AssetId))
import Cardano.Faucet.Types
import Cardano.Ledger.Coin (Coin)
import Cardano.Prelude
import Control.Monad.Trans.Except.Extra (left)
import Data.Text qualified as T
import Text.Parsec

getValue :: TxOutValue era -> FaucetValue
getValue (TxOutAdaOnly _ ll) = Ada ll
getValue (TxOutValue _ val) = convertRemaining remaining
getValue (TxOutValueByron ll) = Ada ll
getValue (TxOutValueShelleyBased val) = convertRemaining remaining
where
ll :: Lovelace
ll :: Coin
ll = selectLovelace val
isntAda :: (AssetId, Quantity) -> Bool
isntAda (AdaAssetId, _) = False
Expand All @@ -37,10 +38,10 @@ stripMintingTokens (FaucetValueMultiAsset ll (FaucetMintToken _)) = Ada ll
stripMintingTokens fv@(FaucetValueManyTokens _) = fv

-- returns just the lovelace component and ignores tokens
faucetValueToLovelace :: FaucetValue -> Lovelace
faucetValueToLovelace (Ada ll) = ll
faucetValueToLovelace (FaucetValueMultiAsset ll _token) = ll
faucetValueToLovelace (FaucetValueManyTokens ll) = ll
faucetValueToCoin :: FaucetValue -> Coin
faucetValueToCoin (Ada ll) = ll
faucetValueToCoin (FaucetValueMultiAsset ll _token) = ll
faucetValueToCoin (FaucetValueManyTokens ll) = ll

parseAddress :: Text -> ExceptT FaucetWebError IO AddressAny
parseAddress addr = case parse (parseAddressAny <* eof) "" (T.unpack addr) of
Expand Down
29 changes: 15 additions & 14 deletions cardano-faucet/src/Cardano/Faucet/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import Prelude (fail)

import Cardano.Address.Derivation (Depth(RootK, AccountK, PaymentK, PolicyK), XPrv, genMasterKeyFromMnemonic, indexFromWord32, deriveAccountPrivateKey, deriveAddressPrivateKey, Index, DerivationType(Hardened, Soft))
import Cardano.Address.Style.Shelley (Shelley, Role(UTxOExternal, Stake), derivePolicyPrivateKey)
import Cardano.Api (AnyCardanoEra, IsCardanoEra, TxIn, TxOut, CtxUTxO, TxInMode, CardanoMode, TxId, FileError, Lovelace, AddressAny, AssetId(AssetId, AdaAssetId), Quantity, SigningKey, PaymentExtendedKey, VerificationKey, HashableScriptData)
import Cardano.Api (AnyCardanoEra, IsCardanoEra, TxIn, TxOut, CtxUTxO, TxInMode, TxId, FileError, AddressAny, AssetId(AssetId, AdaAssetId), Quantity, SigningKey, PaymentExtendedKey, VerificationKey, HashableScriptData)
import Cardano.Api.Shelley (PoolId, StakeExtendedKey, StakeCredential, AssetName(..), NetworkId(Testnet, Mainnet), NetworkMagic(NetworkMagic), ShelleyWitnessSigningKey)
import Cardano.Api (InputDecodeError)
--import Cardano.CLI.Shelley.Run.Address (SomeAddressVerificationKey(AByronVerificationKey, APaymentVerificationKey, APaymentExtendedVerificationKey, AGenesisUTxOVerificationKey), ShelleyAddressCmdError, buildShelleyAddress)
--import Cardano.CLI.Shelley.Run.Address (SomeAddressVerificationKey(AByronVerificationKey, APaymentVerificationKey, APaymentExtendedVerificationKey, AGenesisUTxOVerificationKey), AddressCmdError, buildShelleyAddress)
--import Cardano.CLI.Shelley.Run.Transaction (ShelleyTxCmdError, renderShelleyTxCmdError)
import Cardano.Ledger.Coin (Coin)
import Cardano.Mnemonic (mkSomeMnemonic, getMkSomeMnemonicError)
import Cardano.Prelude
import Control.Concurrent.STM (TMVar, TQueue)
Expand All @@ -35,7 +36,7 @@ import Data.Time.Clock (UTCTime, NominalDiffTime)
import Prelude (String, error, read)
import Servant (FromHttpApiData(parseHeader, parseQueryParam, parseUrlPiece))
import Web.Internal.FormUrlEncoded (ToForm(toForm), fromEntriesByKey)
import Cardano.CLI.Types.Errors.ShelleyAddressCmdError (ShelleyAddressCmdError)
import Cardano.CLI.Types.Errors.AddressCmdError (AddressCmdError)

-- the sitekey, secretkey, and token from recaptcha
newtype SiteKey = SiteKey { unSiteKey :: Text } deriving Show
Expand Down Expand Up @@ -63,7 +64,7 @@ data FaucetError = FaucetErrorSocketNotFound
| FaucetErrorConfigFileNotSet
| FaucetErrorBadMnemonic Text
| FaucetErrorBadIdx
| FaucetErrorShelleyAddr ShelleyAddressCmdError
| FaucetErrorShelleyAddr AddressCmdError
| FaucetErrorTodo2 Text
deriving Generic

Expand Down Expand Up @@ -106,9 +107,9 @@ data ApiKey = Recaptcha Text | ApiKey Text deriving (Ord, Eq)
-- the state of the entire faucet
data IsCardanoEra era => FaucetState era = FaucetState
{ fsUtxoTMVar :: TMVar (Map TxIn (TxOut CtxUTxO era))
, fsStakeTMVar :: TMVar ([(Word32, SigningKey StakeExtendedKey, StakeCredential)], [(Word32, Lovelace, PoolId)])
, fsStakeTMVar :: TMVar ([(Word32, SigningKey StakeExtendedKey, StakeCredential)], [(Word32, Coin, PoolId)])
, fsNetwork :: NetworkId
, fsTxQueue :: TQueue (TxInMode CardanoMode, ByteString)
, fsTxQueue :: TQueue (TxInMode {- CardanoMode -}, ByteString)
, fsRootKey :: Shelley 'RootK XPrv
, fsPaymentSkey :: ShelleyWitnessSigningKey
, fsPaymentVkey :: VerificationKey PaymentExtendedKey
Expand Down Expand Up @@ -138,10 +139,10 @@ data SendMoneySent = SendMoneySent
, amount :: FaucetValue
}

data StakeKeyIntermediateState = StakeKeyIntermediateStateNotRegistered Word32 | StakeKeyIntermediateStateRegistered (Word32, SigningKey StakeExtendedKey, StakeCredential, Lovelace)
data StakeKeyIntermediateState = StakeKeyIntermediateStateNotRegistered Word32 | StakeKeyIntermediateStateRegistered (Word32, SigningKey StakeExtendedKey, StakeCredential, Coin)

data StakeKeyState = StakeKeyRegistered Word32 (SigningKey StakeExtendedKey) StakeCredential Lovelace
| StakeKeyDelegated Word32 Lovelace PoolId
data StakeKeyState = StakeKeyRegistered Word32 (SigningKey StakeExtendedKey) StakeCredential Coin
| StakeKeyDelegated Word32 Coin PoolId
| StakeKeyNotRegistered Word32 deriving Show

-- the full reply type for /send-money
Expand All @@ -163,7 +164,7 @@ instance Aeson.ToJSON DelegationReply where
-- a complete description of an api key
data ApiKeyValue = ApiKeyValue
{ akvApiKey :: Text
, akvLovelace :: Lovelace
, akvCoin :: Coin
, akvRateLimit :: NominalDiffTime
, akvTokens :: Maybe FaucetToken
, akvCanDelegate :: Bool
Expand All @@ -172,7 +173,7 @@ data ApiKeyValue = ApiKeyValue
instance Aeson.FromJSON ApiKeyValue where
parseJSON = Aeson.withObject "ApiKeyValue" $ \v -> do
akvApiKey <- v .: "api_key"
akvLovelace <- v .: "lovelace"
akvCoin <- v .: "lovelace"
akvRateLimit <- v .: "rate_limit"
akvTokens <- v .:? "tokens"
akvCanDelegate <- fromMaybe False <$> v .:? "delegate"
Expand Down Expand Up @@ -220,9 +221,9 @@ instance Aeson.FromJSON FaucetConfigFile where

-- a value with only ada, or a value containing a mix of assets
-- TODO, maybe replace with the cardano Value type?
data FaucetValue = Ada Lovelace
| FaucetValueMultiAsset Lovelace FaucetToken
| FaucetValueManyTokens Lovelace deriving (Show, Eq, Ord)
data FaucetValue = Ada Coin
| FaucetValueMultiAsset Coin FaucetToken
| FaucetValueManyTokens Coin deriving (Show, Eq, Ord)

--tokenToValue :: FaucetToken -> Value
--tokenToValue (FaucetToken (AssetId policyid token, q)) = object [ "policyid" .= policyid, "token" .= token, "quantity" .= q ]
Expand Down

0 comments on commit 95c0a6f

Please sign in to comment.