Skip to content

Commit

Permalink
Add partial payments page to deposit wallet UI
Browse files Browse the repository at this point in the history
Just transaction definition component and base 58 transaction copy component
  • Loading branch information
paolino committed Nov 11, 2024
1 parent 57aa4fb commit 95a43e2
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/ui/cardano-wallet-ui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ library
Cardano.Wallet.UI.Deposit.API.Addresses.Transactions
Cardano.Wallet.UI.Deposit.API.Common
Cardano.Wallet.UI.Deposit.API.Deposits.Deposits
Cardano.Wallet.UI.Deposit.API.Payments
Cardano.Wallet.UI.Deposit.Handlers.Addresses
Cardano.Wallet.UI.Deposit.Handlers.Addresses.Transactions
Cardano.Wallet.UI.Deposit.Handlers.Deposits.Customers
Cardano.Wallet.UI.Deposit.Handlers.Deposits.Times
Cardano.Wallet.UI.Deposit.Handlers.Deposits.TxIds
Cardano.Wallet.UI.Deposit.Handlers.Lib
Cardano.Wallet.UI.Deposit.Handlers.Payments.Transaction
Cardano.Wallet.UI.Deposit.Handlers.Wallet
Cardano.Wallet.UI.Deposit.Html.Common
Cardano.Wallet.UI.Deposit.Html.Pages.About
Expand All @@ -79,6 +81,7 @@ library
Cardano.Wallet.UI.Deposit.Html.Pages.Deposits.Times
Cardano.Wallet.UI.Deposit.Html.Pages.Deposits.TxIds
Cardano.Wallet.UI.Deposit.Html.Pages.Page
Cardano.Wallet.UI.Deposit.Html.Pages.Payments.Page
Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
Cardano.Wallet.UI.Deposit.Server
Cardano.Wallet.UI.Deposit.Server.Addresses
Expand All @@ -87,12 +90,14 @@ library
Cardano.Wallet.UI.Deposit.Server.Deposits.Times
Cardano.Wallet.UI.Deposit.Server.Deposits.TxIds
Cardano.Wallet.UI.Deposit.Server.Lib
Cardano.Wallet.UI.Deposit.Server.Payments.Page
Cardano.Wallet.UI.Deposit.Server.Wallet
Cardano.Wallet.UI.Deposit.Types.Payments
Cardano.Wallet.UI.Lib.Address
Cardano.Wallet.UI.Lib.Discretization
Cardano.Wallet.UI.Lib.ListOf
Cardano.Wallet.UI.Lib.Pagination.TimedSeq
Cardano.Wallet.UI.Lib.Pagination.Map
Cardano.Wallet.UI.Lib.Pagination.TimedSeq
Cardano.Wallet.UI.Lib.Pagination.Type
Cardano.Wallet.UI.Lib.Time.Direction
Cardano.Wallet.UI.Shelley.API
Expand All @@ -115,6 +120,7 @@ library
, aeson
, aeson-pretty
, base
, base58-bytestring
, bech32
, bech32-th
, bytestring
Expand Down
29 changes: 28 additions & 1 deletion lib/ui/src/Cardano/Wallet/UI/Deposit/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ import Cardano.Wallet.UI.Deposit.API.Common
import Cardano.Wallet.UI.Deposit.API.Deposits.Deposits
( DepositsParams
)
import Cardano.Wallet.UI.Deposit.API.Payments
( NewReceiver
, WithReceivers
)
import Control.Lens
( makePrisms
)
Expand Down Expand Up @@ -80,6 +84,7 @@ data Page
| Wallet
| Addresses
| Deposits
| Payments

makePrisms ''Page

Expand All @@ -90,6 +95,7 @@ instance ToHttpApiData Page where
toUrlPiece Wallet = "wallet"
toUrlPiece Addresses = "addresses"
toUrlPiece Deposits = "deposits"
toUrlPiece Payments = "payments"

instance FromHttpApiData Page where
parseUrlPiece "about" = Right About
Expand All @@ -98,6 +104,7 @@ instance FromHttpApiData Page where
parseUrlPiece "wallet" = Right Wallet
parseUrlPiece "addresses" = Right Addresses
parseUrlPiece "deposits" = Right Deposits
parseUrlPiece "payments" = Right Payments
parseUrlPiece _ = Left "Invalid page"

-- | Pages endpoints
Expand All @@ -108,6 +115,7 @@ type Pages =
:<|> "wallet" :> SessionedHtml Get
:<|> "addresses" :> SessionedHtml Get
:<|> "deposits" :> SessionedHtml Get
:<|> "payments" :> SessionedHtml Get

-- | Data endpoints
type Data =
Expand Down Expand Up @@ -187,6 +195,17 @@ type Data =
:> QueryParam "customer" Customer
:> QueryParam "tx-id" TxId
:> SessionedHtml Post
:<|> "payments" :> SessionedHtml Get
:<|> "payments"
:> "receiver"
:> ReqBody '[FormUrlEncoded] NewReceiver
:> SessionedHtml Post
:<|> "payments"
:> "receiver"
:> "delete"
:> ReqBody '[FormUrlEncoded] (WithReceivers ())
:> QueryParam "receiver-number" Int
:> SessionedHtml Post

type Home = SessionedHtml Get

Expand All @@ -205,6 +224,7 @@ settingsPageLink :: Link
walletPageLink :: Link
addressesPageLink :: Link
depositPageLink :: Link
paymentsPageLink :: Link
networkInfoLink :: Link
settingsGetLink :: Link
settingsSseToggleLink :: Link
Expand Down Expand Up @@ -232,13 +252,17 @@ depositsTxIdsLink
:: Maybe (WithOrigin UTCTime) -> Maybe Customer -> Maybe Expand -> Link
depositsTxIdsPaginatingLink
:: Maybe (WithOrigin UTCTime) -> Maybe Customer -> Maybe TxId -> Link
paymentsLink :: Link
paymentsNewReceiverLink :: Link
paymentsDeleteReceiverLink :: Maybe Int -> Link
homePageLink
:<|> aboutPageLink
:<|> networkPageLink
:<|> settingsPageLink
:<|> walletPageLink
:<|> addressesPageLink
:<|> depositPageLink
:<|> paymentsPageLink
:<|> networkInfoLink
:<|> settingsGetLink
:<|> settingsSseToggleLink
Expand All @@ -260,5 +284,8 @@ homePageLink
:<|> depositsCustomersLink
:<|> depositsCustomersPaginatingLink
:<|> depositsTxIdsLink
:<|> depositsTxIdsPaginatingLink =
:<|> depositsTxIdsPaginatingLink
:<|> paymentsLink
:<|> paymentsNewReceiverLink
:<|> paymentsDeleteReceiverLink =
allLinks (Proxy @UI)
47 changes: 47 additions & 0 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/API/Payments.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NumericUnderscores #-}

module Cardano.Wallet.UI.Deposit.API.Payments
( NewReceiver (..)
, WithReceivers (..)
)
where

import Prelude

import Cardano.Wallet.UI.Deposit.Types.Payments
( Receiver (..)
)
import Web.FormUrlEncoded
( FromForm (..)
, parseAll
, parseUnique
)

data WithReceivers a
= WithReceivers
{ receivers :: [Receiver]
, what :: a
}

newtype NewReceiver = NewReceiver (WithReceivers Receiver)

instance FromForm NewReceiver where
fromForm form = do
WithReceivers receivers () <- fromForm form
address <- parseUnique "new-receiver-address" form
amountDouble :: Double <- parseUnique "new-receiver-amount" form
let amount = round $ amountDouble * 1_000_000
pure
$ NewReceiver
$ WithReceivers{receivers, what = Receiver{address, amount}}

instance FromForm (WithReceivers ()) where
fromForm form = do
receivers <- parseAll "receiver-defined" form
pure
$ WithReceivers{receivers, what = ()}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{-# LANGUAGE NamedFieldPuns #-}

module Cardano.Wallet.UI.Deposit.Handlers.Payments.Transaction
where

import Prelude

import Cardano.Read.Ledger.Tx.CBOR
( serializeTx
)
import Cardano.Wallet.Deposit.REST
( WalletResource
, createPayment
)
import Cardano.Wallet.Read
( Coin (..)
, Value (..)
)
import Cardano.Wallet.UI.Common.Layer
( SessionLayer
)
import Cardano.Wallet.UI.Deposit.Handlers.Lib
( catchRunWalletResourceHtml
)
import Cardano.Wallet.UI.Deposit.Types.Payments
( Receiver (..)
)
import Data.Text
( Text
)
import Servant
( Handler
)

import qualified Data.ByteString.Base58 as Base58
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as BL8
import qualified Data.Text.Encoding as T

createPaymentHandler
:: SessionLayer WalletResource
-> (BL.ByteString -> html)
-- ^ Function to render the exception as HTML
-> ([Receiver] -> Maybe Text -> html)
-- ^ Function to render the transaction cbor
-> [Receiver]
-- ^ Current receivers
-> Handler html
createPaymentHandler layer alert render receivers = do
catchRunWalletResourceHtml layer alert id $ do
case receivers of
[] -> pure $ render receivers Nothing
_ -> do
r <- createPayment $ do
Receiver{address, amount} <- receivers
pure (address, ValueC (CoinC $ fromIntegral amount) mempty)
pure $ case r of
Left e -> alert $ "Error: " <> BL8.pack (show e)
Right tx ->
render receivers
$ Just
$ T.decodeUtf8
$ Base58.encodeBase58 Base58.bitcoinAlphabet
$ BL.toStrict
$ serializeTx tx
10 changes: 10 additions & 0 deletions lib/ui/src/Cardano/Wallet/UI/Deposit/Html/Pages/Page.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import Cardano.Wallet.UI.Deposit.API
, _Addresses
, _Deposits
, _Network
, _Payments
, _Settings
, _Wallet
, aboutPageLink
Expand All @@ -55,6 +56,8 @@ import Cardano.Wallet.UI.Deposit.API
, navigationLink
, networkInfoLink
, networkPageLink
, paymentsLink
, paymentsPageLink
, settingsGetLink
, settingsPageLink
, sseLink
Expand All @@ -69,6 +72,9 @@ import Cardano.Wallet.UI.Deposit.Html.Pages.Addresses
import Cardano.Wallet.UI.Deposit.Html.Pages.Deposits.Page
( depositsH
)
import Cardano.Wallet.UI.Deposit.Html.Pages.Payments.Page
( paymentsH
)
import Cardano.Wallet.UI.Deposit.Html.Pages.Wallet
( WalletPresent
, isPresent
Expand Down Expand Up @@ -111,6 +117,7 @@ page c p = RawHtml
Wallet -> walletH
Addresses -> addressesH
Deposits -> depositsH depositsLink
Payments -> paymentsH paymentsLink

headerH :: Monad m => Page -> HtmlT m ()
headerH p = sseH (navigationLink $ Just p) "header" ["wallet"]
Expand All @@ -126,6 +133,9 @@ headerElementH p wp =
<> [ (is' _Deposits, depositPageLink, "Deposits")
| isPresent wp
]
<> [ (is' _Payments, paymentsPageLink, "Payments")
| isPresent wp
]
<> [ (is' _Network, networkPageLink, "Network")
, (is' _Settings, settingsPageLink, "Settings")
, (is' _About, aboutPageLink, "About")
Expand Down
Loading

0 comments on commit 95a43e2

Please sign in to comment.