Skip to content

Commit

Permalink
Allow to create a new wallet with the provided funds (#17). (#291)
Browse files Browse the repository at this point in the history
* Allow to create a new wallet with the provided funds (#17).

* remove useless import
  • Loading branch information
ak3n committed Feb 2, 2022
1 parent e4d852f commit 779a81d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions plutus-pab/src/Cardano/Wallet/Mock/API.hs
Expand Up @@ -9,7 +9,7 @@ import Cardano.Wallet.Mock.Types (WalletInfo)
import Ledger (Value)
import Ledger.Constraints.OffChain (UnbalancedTx)
import Ledger.Tx (Tx)
import Servant.API (Capture, Get, JSON, NoContent, Post, ReqBody, (:<|>), (:>))
import Servant.API (Capture, Get, JSON, NoContent, Post, QueryParam, ReqBody, (:<|>), (:>))
import Wallet.Emulator.Error (WalletAPIError)

{- Note [WalletID type in wallet API]
Expand All @@ -34,7 +34,7 @@ PSGenerator we specialise it to 'Text'.
-}

type API walletId -- see note [WalletID type in wallet API]
= "create" :> Post '[JSON] WalletInfo
= "create" :> QueryParam "funds" Integer :> Post '[JSON] WalletInfo
:<|> Capture "walletId" walletId :> "submit-txn" :> ReqBody '[JSON] Tx :> Post '[JSON] NoContent
:<|> Capture "walletId" walletId :> "own-payment-public-key" :> Get '[JSON] WalletInfo
:<|> Capture "walletId" walletId :> "balance-tx" :> ReqBody '[JSON] UnbalancedTx :> Post '[JSON] (Either WalletAPIError Tx)
Expand Down
2 changes: 1 addition & 1 deletion plutus-pab/src/Cardano/Wallet/Mock/Client.hs
Expand Up @@ -24,7 +24,7 @@ import Wallet.Effects (WalletEffect (BalanceTx, OwnPaymentPubKeyHash, SubmitTxn,
import Wallet.Emulator.Error (WalletAPIError)
import Wallet.Emulator.Wallet (Wallet (Wallet, getWalletId), WalletId)

createWallet :: ClientM WalletInfo
createWallet :: Maybe Integer -> ClientM WalletInfo
submitTxn :: Wallet -> Tx -> ClientM ()
ownPaymentPublicKey :: Wallet -> ClientM WalletInfo
balanceTx :: Wallet -> UnbalancedTx -> ClientM (Either WalletAPIError Tx)
Expand Down
10 changes: 6 additions & 4 deletions plutus-pab/src/Cardano/Wallet/Mock/Handlers.hs
Expand Up @@ -89,9 +89,11 @@ distributeNewWalletFunds :: forall effs.
, Member (Error WalletAPIError) effs
, Member (LogMsg Text) effs
)
=> PaymentPubKeyHash
=> Maybe Ada.Ada
-> PaymentPubKeyHash
-> Eff effs CardanoTx
distributeNewWalletFunds = WAPI.payToPaymentPublicKeyHash WAPI.defaultSlotRange (Ada.adaValueOf 10_000)
distributeNewWalletFunds funds = WAPI.payToPaymentPublicKeyHash WAPI.defaultSlotRange
(maybe (Ada.adaValueOf 10_000) Ada.toValue funds)

newWallet :: forall m effs. (LastMember m effs, MonadIO m) => Eff effs MockWallet
newWallet = do
Expand Down Expand Up @@ -125,7 +127,7 @@ handleMultiWallet feeCfg = \case
put @Wallets (wallets & at wallet ?~ newState)
pure x
Nothing -> throwError $ WAPI.OtherError "Wallet not found"
CreateWallet -> do
CreateWallet funds -> do
wallets <- get @Wallets
mockWallet <- newWallet
let walletId = Wallet.Wallet $ Wallet.WalletId $ CW.mwWalletId mockWallet
Expand All @@ -139,7 +141,7 @@ handleMultiWallet feeCfg = \case
_ <- evalState sourceWallet $
interpret (mapLog @TxBalanceMsg @WalletMsg Balancing)
$ interpret (Wallet.handleWallet feeCfg)
$ distributeNewWalletFunds pkh
$ distributeNewWalletFunds funds pkh
return $ WalletInfo{wiWallet = walletId, wiPaymentPubKeyHash = pkh}
GetWalletInfo wllt -> do
wallets <- get @Wallets
Expand Down
3 changes: 2 additions & 1 deletion plutus-pab/src/Cardano/Wallet/Mock/Server.hs
Expand Up @@ -32,6 +32,7 @@ import Data.Either (fromRight)
import Data.Function ((&))
import Data.Map.Strict qualified as Map
import Data.Proxy (Proxy (Proxy))
import Ledger.Ada qualified as Ada
import Ledger.CardanoWallet qualified as CW
import Ledger.Fee (FeeConfig)
import Ledger.TimeSlot (SlotConfig)
Expand All @@ -58,7 +59,7 @@ app trace txSendHandle chainSyncHandle chainIndexEnv mVarState feeCfg slotCfg =
hoistServer
(Proxy @(API WalletId))
(processWalletEffects trace txSendHandle chainSyncHandle chainIndexEnv mVarState feeCfg slotCfg) $
createWallet :<|>
(\funds -> createWallet (Ada.lovelaceOf <$> funds)) :<|>
(\w tx -> multiWallet (Wallet w) (submitTxn $ Right tx) >>= const (pure NoContent)) :<|>
(getWalletInfo >=> maybe (throwError err404) pure ) :<|>
(\w -> fmap (fmap (fromRight (error "Cardano.Wallet.Mock.Server: Expecting a mock tx, not an Alonzo tx when submitting it.")))
Expand Down
3 changes: 2 additions & 1 deletion plutus-pab/src/Cardano/Wallet/Mock/Types.hs
Expand Up @@ -47,6 +47,7 @@ import Data.Map.Strict (Map)
import Data.Text (Text)
import GHC.Generics (Generic)
import Ledger (PaymentPubKeyHash)
import Ledger.Ada (Ada)
import Plutus.ChainIndex (ChainIndexQueryEffect)
import Plutus.PAB.Arbitrary ()
import Plutus.PAB.Types (PABError)
Expand Down Expand Up @@ -77,7 +78,7 @@ fromWalletState WalletState{_mockWallet} = WalletInfo{wiWallet, wiPaymentPubKeyH
wiPaymentPubKeyHash = mockWalletPaymentPubKeyHash wiWallet

data MultiWalletEffect r where
CreateWallet :: MultiWalletEffect WalletInfo
CreateWallet :: Maybe Ada -> MultiWalletEffect WalletInfo
MultiWallet :: Wallet -> Eff '[WalletEffect] a -> MultiWalletEffect a
GetWalletInfo :: WalletId -> MultiWalletEffect (Maybe WalletInfo)
makeEffect ''MultiWalletEffect
Expand Down
11 changes: 8 additions & 3 deletions plutus-pab/src/Plutus/PAB/Simulator.hs
Expand Up @@ -28,6 +28,7 @@ module Plutus.PAB.Simulator(
, SimulatorEffectHandlers
, mkSimulatorHandlers
, addWallet
, addWalletWith
-- * Logging
, logString
-- ** Agent actions
Expand Down Expand Up @@ -739,7 +740,12 @@ instanceActivity = Core.instanceActivity
-- | Create a new wallet with a random key, give it some funds
-- and add it to the list of simulated wallets.
addWallet :: forall t. Simulation t (Wallet, PaymentPubKeyHash)
addWallet = do
addWallet = addWalletWith Nothing

-- | Create a new wallet with a random key, give it provided funds
-- and add it to the list of simulated wallets.
addWalletWith :: forall t. Maybe Ada.Ada -> Simulation t (Wallet, PaymentPubKeyHash)
addWalletWith funds = do
SimulatorState{_agentStates} <- Core.askUserEnv @t @(SimulatorState t)
mockWallet <- MockWallet.newWallet
void $ liftIO $ STM.atomically $ do
Expand All @@ -748,10 +754,9 @@ addWallet = do
STM.writeTVar _agentStates newWallets
_ <- handleAgentThread (knownWallet 2) Nothing
$ Modify.wrapError WalletError
$ MockWallet.distributeNewWalletFunds (CW.paymentPubKeyHash mockWallet)
$ MockWallet.distributeNewWalletFunds funds (CW.paymentPubKeyHash mockWallet)
pure (Wallet.toMockWallet mockWallet, CW.paymentPubKeyHash mockWallet)


-- | Retrieve the balances of all the entities in the simulator.
currentBalances :: forall t. Simulation t (Map.Map Wallet.Entity Value)
currentBalances = do
Expand Down

0 comments on commit 779a81d

Please sign in to comment.