Skip to content

Commit c56728b

Browse files
committed
WIP: check for confirmation of Eth txs when showing balances
1 parent 8b87f5b commit c56728b

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/GWallet.Backend/Account.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ module Account =
248248
txId
249249
amountTransferredPlusFeeIfCurrencyFeeMatches
250250
fee.FeeValue
251-
251+
match fee with
252+
| :? Ether.TransactionMetadata as etherTxMetadata ->
253+
Caching.Instance.StoreUnconfirmedTransaction fee.Currency txId etherTxMetadata.Fee.GasLimit
254+
| _ -> ()
255+
252256
// FIXME: broadcasting shouldn't just get N consistent replies from FaultTolerantClient,
253257
// but send it to as many as possible, otherwise it could happen that some server doesn't
254258
// broadcast it even if you sent it

src/GWallet.Backend/Caching.fs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type CachedNetworkData =
1414
UsdPrice: Map<Currency,CachedValue<decimal>>;
1515
Balances: Map<Currency,Map<PublicAddress,CachedValue<decimal>>>;
1616
OutgoingTransactions: Map<Currency,Map<PublicAddress,Map<string,CachedValue<decimal>>>>;
17+
UnconfirmedTransactions: Map<Currency,list<string * int64>>
1718
}
1819
member self.GetLeastOldDate() =
1920
let allDates =
@@ -35,6 +36,7 @@ type CachedNetworkData =
3536
UsdPrice = Map.empty
3637
Balances = Map.empty
3738
OutgoingTransactions = Map.empty
39+
UnconfirmedTransactions = Map.empty
3840
}
3941

4042
static member FromDietCache (dietCache: DietCache): CachedNetworkData =
@@ -52,7 +54,8 @@ type CachedNetworkData =
5254
yield (Currency.Parse currencyStr),Map.empty.Add(address,(balance,now))
5355
} |> Map.ofSeq
5456
{ UsdPrice = fiatPrices; Balances = balances
55-
OutgoingTransactions = Map.empty; }
57+
OutgoingTransactions = Map.empty;
58+
UnconfirmedTransactions = Map.empty }
5659

5760
member self.ToDietCache(readOnlyAccounts: seq<ReadOnlyAccount>) =
5861
let rec extractAddressesFromAccounts (acc: Map<PublicAddress,List<DietCurrency>>) (accounts: List<IAccount>)
@@ -647,4 +650,34 @@ module Caching =
647650
member __.FirstRun
648651
with get() = firstRun
649652

653+
member self.StoreUnconfirmedTransaction (currency: Currency) (txHash: string) (gasLimit: int64) =
654+
lock cacheFiles.CachedNetworkData (fun _ ->
655+
let newTransactions =
656+
match sessionCachedNetworkData.UnconfirmedTransactions |> Map.tryFind currency with
657+
| Some transactionsForCurrency -> (txHash, gasLimit) :: transactionsForCurrency
658+
| None -> List.singleton (txHash, gasLimit)
659+
let newCachedData =
660+
{ sessionCachedNetworkData with
661+
UnconfirmedTransactions =
662+
sessionCachedNetworkData.UnconfirmedTransactions
663+
|> Map.add currency newTransactions }
664+
SaveNetworkDataToDisk newCachedData
665+
)
666+
667+
member self.RemoveUnconfirmedTransaction (currency: Currency) (txHash: string) =
668+
lock cacheFiles.CachedNetworkData (fun _ ->
669+
match sessionCachedNetworkData.UnconfirmedTransactions |> Map.tryFind currency with
670+
| Some transactionsForCurrency ->
671+
let newTransactionForCurrency =
672+
transactionsForCurrency
673+
|> List.filter (fun (hash, _) -> hash <> txHash)
674+
let newCachedData =
675+
{ sessionCachedNetworkData with
676+
UnconfirmedTransactions =
677+
sessionCachedNetworkData.UnconfirmedTransactions
678+
|> Map.add currency newTransactionForCurrency }
679+
SaveNetworkDataToDisk newCachedData
680+
| None -> ()
681+
)
682+
650683
let Instance = MainCache (None, TimeSpan.FromDays 1.0)

src/GWallet.Frontend.Console/UserInteraction.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,24 @@ module UserInteraction =
352352
// All balances are fetched and their currency names printed, put a dot at the end of "Retrieving balances... " line.
353353
Console.Write '.'
354354

355+
let unconfirmedStatuses = ResizeArray<string>()
356+
for account in accounts do
357+
if account.Currency.IsEtherBased() then
358+
let cache = Caching.Instance.GetLastCachedData()
359+
match cache.UnconfirmedTransactions |> Map.tryFind account.Currency with
360+
| Some unconfirmedTransactions ->
361+
for txHash, gasSpent in unconfirmedTransactions do
362+
try
363+
let! isOutOfGas = Ether.Server.IsOutOfGas account.Currency txHash gasSpent
364+
if isOutOfGas then
365+
unconfirmedStatuses.Add(sprintf "Transaction ran out of gas: 0x%s" txHash)
366+
else
367+
Caching.Instance.RemoveUnconfirmedTransaction account.Currency txHash
368+
with
369+
| :? TimeoutException ->
370+
unconfirmedStatuses.Add(sprintf "Timed out when checking transaction 0x%s" txHash)
371+
| None -> ()
372+
355373
let maybeTotalInUsd, totals = displayTotalAndSumFiatBalance currencyTotals
356374
return
357375
seq {
@@ -362,6 +380,7 @@ module UserInteraction =
362380
seq {
363381
yield! statuses
364382
yield! totals
383+
yield! unconfirmedStatuses
365384
yield String.Empty // this ends up being simply an Environment.NewLine
366385
yield sprintf "Total estimated value in USD: %s"
367386
(Formatting.DecimalAmountRounding CurrencyType.Fiat totalInUsd)

0 commit comments

Comments
 (0)