Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: refactor/cleanup transfer flow #716

Draft
wants to merge 1 commit into
base: jam@fungible-support
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 41 additions & 29 deletions frontend/src/Frontend/UI/Transfer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -228,40 +228,38 @@ uiGenericTransfer
-> TransferCfg t
-> m mConf
uiGenericTransfer model cfg = do
let attrs = do
visible <- _transferCfg_isVisible cfg
pure $ if visible
then ("class" =: "main transfer transfer__expanded")
else ("class" =: "main transfer transfer__collapsed")
elDynAttr "main" attrs $ mdo
transferInfo <- divClass "transfer-fields" $ do
(fromAcct,amount) <- divClass "transfer__left-pane" $ do
eTransInfoAndType <- gTransferWidget
let transferWithNet = flip push eTransInfoAndType $ \(mTInfo, tType) ->
sampleNetInfo model >>= pure . fmap (mTInfo, tType, )
mkModal (Nothing, _, _) = Nothing
mkModal (Just ti, ty, ni) = Just $ lookupAndTransfer model ni ti ty
pure $ mempty & modalCfg_setModal .~ (fmap mkModal transferWithNet)
where
-- From
fromAccountWidget eClearForm =
divClass "transfer__left-pane" $ do
el "h4" $ text "From"
fca <- labeledChainAccount model $ mkCfg Nothing
& initialAttributes .~ "placeholder" =: "Account Name"
& setValue .~ (Just $ Nothing <$ clear)
& setValue .~ (Just $ Nothing <$ eClearForm)
rec
amt <- amountFormWithMaxButton model fca $ mkCfg (Left "", False)
& setValue .~ (Just $ (Left "", False) <$ leftmost
[ clear
[ eClearForm

-- If Max is checked, clear the amount and max checkbox when the
-- ChainAccount is updated. Otherwise leave it alone.
, () <$ gate (fmap snd <$> current $ value amt) (updated (value fca))
])
return (fca,amt)
(toAcct,ks) <- divClass "transfer__right-pane" $ do
el "h4" $ text "To"
toFormWidget model $ mkCfg (Nothing, Nothing)
& setValue .~ (Just $ (Nothing, Nothing) <$ clear)
return $ runMaybeT $ TransferInfo <$>
MaybeT (value fromAcct) <*>
MaybeT (Just <$> model ^. wallet_fungible ) <*>
MaybeT (hush . fst <$> value amount) <*>
lift (snd <$> value amount) <*>
MaybeT (value toAcct) <*>
lift ks
(clear, signTransfer) <- divClass "transfer-fields submit" $ do

-- Destination
toAccountWidget = divClass "transfer__right-pane" $ do
el "h4" $ text "To"
toFormWidget model $ mkCfg (Nothing, Nothing)

-- Submit
submitOrClearWidget transferInfo = divClass "transfer-fields submit" $ do
clr <- el "div" $ uiButton btnCfgTertiary $ text "Clear"
normal <- confirmButton (def { _uiButtonCfg_disabled = (isNothing <$> transferInfo) }) "Sign & Transfer"
let safeDisabled Nothing = True
Expand All @@ -279,18 +277,32 @@ uiGenericTransfer model cfg = do
, _uiButtonCfg_title = constDyn $ Just "Safe transfers make it impossible to lose coins by sending to the wrong public key when transferring to the same chain. They require a little extra work because the receiving account also has to sign the transaction."
}
safe <- confirmButton safeBtnCfg "Safe Transfer"
-- _ <- confirmButton (def { _uiButtonCfg_disabled = (isNothing <$> transferInfo) }) "Quick Transfer"
let txEvt = leftmost
[ NormalTransfer <$ normal
, SafeTransfer <$ safe
]
return (clr, txEvt)
let netInfo = flip push signTransfer $ \ty -> do
ni <- sampleNetInfo model
return ((ty, ) <$> ni)
let mkModal (Just ti) (ty, ni) = Just $ lookupAndTransfer model ni ti ty
mkModal Nothing _ = Nothing
pure $ mempty & modalCfg_setModal .~ (attachWith mkModal (current transferInfo) netInfo)

attrs = do
visible <- _transferCfg_isVisible cfg
pure $ if visible
then ("class" =: "main transfer transfer__expanded")
else ("class" =: "main transfer transfer__collapsed")

-- Put it all together
gTransferWidget = elDynAttr "main" attrs $ mdo
transferInfo <- divClass "transfer-fields" $ do
(fromAcct,amount) <- fromAccountWidget eClearTransfer
(toAcct,ks) <- toAccountWidget
return $ runMaybeT $ TransferInfo <$>
MaybeT (value fromAcct) <*>
MaybeT (Just <$> model ^. wallet_fungible ) <*>
MaybeT (hush . fst <$> value amount) <*>
lift (snd <$> value amount) <*>
MaybeT (value toAcct) <*>
lift ks
(eClearTransfer, signTransfer) <- submitOrClearWidget transferInfo
pure $ flip attach signTransfer $ current transferInfo

amountFormWithMaxButton
:: ( DomBuilder t m, MonadFix m
Expand Down