Skip to content

Commit

Permalink
Backend,Frontend.XF: refactor CryptoSubUnit
Browse files Browse the repository at this point in the history
Move it to Backend in case we use it in Frontend.Console,
and respect DRY (for the caption).
  • Loading branch information
knocte committed Feb 8, 2024
1 parent e9b862b commit f96bdc6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
16 changes: 16 additions & 0 deletions src/GWallet.Backend/UtxoCoin/TransactionTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ open GWallet.Backend

open NBitcoin

type SubUnit =
{
Multiplier: int
Caption: string
}
static member Bits =
{
Multiplier = 1000000
Caption = "bits"
}
static member Sats =
{
Multiplier = 100000000
Caption = "sats"
}

type TransactionInputOutpointInfo =
{
TransactionHash: string;
Expand Down
27 changes: 9 additions & 18 deletions src/GWallet.Frontend.XF/FrontendHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ type internal CurrencyImageSize =

module FrontendHelpers =

type CryptoSubUnit =
| No
| Specific of multiplier:int * caption:string

let Sats =
CryptoSubUnit.Specific(100_000_000, "sats")
let Bits =
CryptoSubUnit.Specific(1_000_000, "bits")

type IGlobalAppState =
[<CLIEvent>]
abstract member Resumed: IEvent<unit> with get
Expand Down Expand Up @@ -118,7 +109,7 @@ module FrontendHelpers =

let UpdateBalance (balance: MaybeCached<decimal>) currency usdRate
(maybeFrame: Option<Frame>) (balanceLabel: Label) (fiatBalanceLabel: Label)
(cryptoSubUnit: CryptoSubUnit)
(cryptoSubUnit: Option<UtxoCoin.SubUnit>)
: MaybeCached<decimal> =
let maybeBalanceAmount =
match balance with
Expand All @@ -141,16 +132,16 @@ module FrontendHelpers =
| Some balanceAmount ->
let adjustedBalance =
match cryptoSubUnit with
| No -> balanceAmount
| Specific (multiplier, _caption) ->
balanceAmount * decimal multiplier
| None -> balanceAmount
| Some unit ->
balanceAmount * decimal unit.Multiplier
let cryptoAmount = Formatting.DecimalAmountRounding CurrencyType.Crypto adjustedBalance
let cryptoAmountStr =
match cryptoSubUnit with
| No ->
| None ->
SPrintF2 "%A %s" currency cryptoAmount
| Specific (_multiplier, caption) ->
SPrintF2 "%s %A" cryptoAmount caption
| Some unit ->
SPrintF2 "%s %A" cryptoAmount unit.Caption
let fiatAmount,fiatAmountStr = BalanceInUsdString balanceAmount usdRate
cryptoAmountStr,fiatAmount,fiatAmountStr
MainThread.BeginInvokeOnMainThread(fun _ ->
Expand Down Expand Up @@ -179,7 +170,7 @@ module FrontendHelpers =
(Some balanceSet.Widgets.Frame)
balanceSet.Widgets.CryptoLabel
balanceSet.Widgets.FiatLabel
CryptoSubUnit.No
None
return {
BalanceSet = balanceSet
FiatAmount = fiatAmount
Expand Down Expand Up @@ -208,7 +199,7 @@ module FrontendHelpers =
(Some balanceSet.Widgets.Frame)
balanceSet.Widgets.CryptoLabel
balanceSet.Widgets.FiatLabel
CryptoSubUnit.No
None
return {
BalanceSet = balanceSet
FiatAmount = fiatAmount
Expand Down
14 changes: 7 additions & 7 deletions src/GWallet.Frontend.XF/ReceivePage.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ type ReceivePage(account: IAccount,

let tapCryptoLabel accountBalance =
let cryptoSubUnit =
if balanceLabel.Text.StartsWith "BTC" then
FrontendHelpers.Bits
elif balanceLabel.Text.EndsWith "bits" then
FrontendHelpers.Sats
if balanceLabel.Text.Contains UtxoCoin.SubUnit.Bits.Caption then
Some UtxoCoin.SubUnit.Sats
elif balanceLabel.Text.Contains UtxoCoin.SubUnit.Sats.Caption then
None
else
FrontendHelpers.CryptoSubUnit.No
Some UtxoCoin.SubUnit.Bits
FrontendHelpers.UpdateBalance
(NotFresh accountBalance)
account.Currency
Expand Down Expand Up @@ -75,7 +75,7 @@ type ReceivePage(account: IAccount,
None
balanceLabel
fiatBalanceLabel
FrontendHelpers.CryptoSubUnit.No
None
|> ignore

// this below is for the case when a new ReceivePage() instance is suddenly created after sending a transaction
Expand All @@ -86,7 +86,7 @@ type ReceivePage(account: IAccount,
(Some balanceWidgetsFromBalancePage.Frame)
balanceWidgetsFromBalancePage.CryptoLabel
balanceWidgetsFromBalancePage.FiatLabel
FrontendHelpers.CryptoSubUnit.No
None
|> ignore

balanceLabel.FontSize <- FrontendHelpers.BigFontSize
Expand Down

0 comments on commit f96bdc6

Please sign in to comment.