|
| 1 | +namespace GWallet.Backend.UtxoCoin |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Net |
| 5 | + |
| 6 | +open GWallet.Backend |
| 7 | +open GWallet.Backend.FSharpUtil |
| 8 | +open GWallet.Backend.FSharpUtil.UwpHacks |
| 9 | + |
| 10 | + |
| 11 | +module BtcTransactionPrinting = |
| 12 | + let GetBitcoinPriceForDate (date: DateTime) : Async<Result<decimal, Exception>> = |
| 13 | + async { |
| 14 | + try |
| 15 | + let baseUrl = |
| 16 | + let dateFormated = date.ToString("dd-MM-yyyy") |
| 17 | + SPrintF1 "https://api.coingecko.com/api/v3/coins/bitcoin/history?date=%s&localization=false" dateFormated |
| 18 | + let uri = Uri baseUrl |
| 19 | + use webClient = new WebClient() |
| 20 | + let task = webClient.DownloadStringTaskAsync uri |
| 21 | + let! result = Async.AwaitTask task |
| 22 | + let json = Newtonsoft.Json.Linq.JObject.Parse result |
| 23 | + return Ok(json.["market_data"].["current_price"].["usd"].ToObject<decimal>()) |
| 24 | + with |
| 25 | + | ex -> |
| 26 | + return Error ex |
| 27 | + } |
| 28 | + |
| 29 | + let PrintTransactions (maxTransactionsCount: uint32) (btcAddress: string) = |
| 30 | + let address = NBitcoin.BitcoinAddress.Create(btcAddress, NBitcoin.Network.Main) |
| 31 | + async { |
| 32 | + let scriptHash = Account.GetElectrumScriptHashFromPublicAddress Currency.BTC btcAddress |
| 33 | + let! history = |
| 34 | + Server.Query |
| 35 | + Currency.BTC |
| 36 | + (QuerySettings.Default ServerSelectionMode.Fast) |
| 37 | + (ElectrumClient.GetBlockchainScriptHashHistory scriptHash) |
| 38 | + None |
| 39 | + |
| 40 | + let sortedHistory = history |> List.sortByDescending (fun entry -> entry.Height) |
| 41 | + |
| 42 | + let rec processHistory history (maxTransactionsToPrint: uint32) = |
| 43 | + match history with |
| 44 | + | nextEntry :: rest when maxTransactionsToPrint > 0u -> |
| 45 | + async { |
| 46 | + let! transaction = |
| 47 | + Server.Query |
| 48 | + Currency.BTC |
| 49 | + (QuerySettings.Default ServerSelectionMode.Fast) |
| 50 | + (ElectrumClient.GetBlockchainTransactionVerbose nextEntry.TxHash) |
| 51 | + None |
| 52 | + let transactionInfo = NBitcoin.Transaction.Parse(transaction.Hex, NBitcoin.Network.Main) |
| 53 | + |
| 54 | + let incomingOutputs = |
| 55 | + transactionInfo.Outputs |
| 56 | + |> Seq.filter (fun output -> output.IsTo address) |
| 57 | + |
| 58 | + if not (Seq.isEmpty incomingOutputs) then |
| 59 | + let amount = incomingOutputs |> Seq.sumBy (fun txOut -> txOut.Value) |
| 60 | + let dateTime = |
| 61 | + let startOfUnixEpoch = DateTime(1970, 1, 1) |
| 62 | + startOfUnixEpoch + TimeSpan.FromSeconds(float transaction.Time) |
| 63 | + let! bitcoinPrice = GetBitcoinPriceForDate dateTime |
| 64 | + Console.WriteLine(SPrintF1 "BTC amount: %A" amount) |
| 65 | + match bitcoinPrice with |
| 66 | + | Ok price -> |
| 67 | + let bitcoinAmount = amount.ToDecimal(NBitcoin.MoneyUnit.BTC) |
| 68 | + Console.WriteLine(SPrintF1 "~USD amount: %s" ((bitcoinAmount * price).ToString("F2"))) |
| 69 | + | Error exn -> |
| 70 | + Console.WriteLine("Could not get bitcoin price for the date. An error has occured:\n" + exn.ToString()) |
| 71 | + Console.WriteLine(SPrintF1 "date: %A UTC" dateTime) |
| 72 | + Console.WriteLine() |
| 73 | + return! processHistory rest (maxTransactionsToPrint - 1u) |
| 74 | + else |
| 75 | + return! processHistory rest maxTransactionsToPrint |
| 76 | + } |
| 77 | + | _ -> async { return () } |
| 78 | + |
| 79 | + do! processHistory sortedHistory maxTransactionsCount |
| 80 | + Console.WriteLine("End of results") |
| 81 | + Console.ReadKey() |> ignore |
| 82 | + return 0 |
| 83 | + } |
| 84 | + |> Async.RunSynchronously |
0 commit comments