Repository navigation
Expand file tree
/
Copy pathBitcoinRpc.fs
More file actions
154 lines (128 loc) · 4.27 KB
/
Copy pathBitcoinRpc.fs
File metadata and controls
154 lines (128 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
module BitcoinRpc
open System
open Microsoft.FSharp.Core
open Thoth.Fetch
open Thoth.Json
type ScriptPubKey = {
asm : string
address: string option
scriptType: string
}
type Output = {
value: float
scriptPubKey: ScriptPubKey
}
type Input = {
txid: string
vout: int
prevOut: Output
}
type ConfirmedTransaction = {
txid: string
version: int
size: int
vsize: int
weight: int
locktime: int
vin: Input[]
vout: Output[]
fee: float
blockhash: string
confirmations: int
time: DateTimeOffset
blocktime: DateTimeOffset
}
type UnconfirmedTransaction = {
txid: string
version: int
size: int
vsize: int
weight: int
locktime: int
vout: Output[]
}
type Transaction =
| ConfirmedTransaction of ConfirmedTransaction
| UnconfirmedTransaction of UnconfirmedTransaction
let unixDateTimeDecoder : Decoder<DateTimeOffset> =
Decode.int64 |> Decode.map (DateTimeOffset.FromUnixTimeSeconds)
let scriptPubKeyDecoder =
Decode.object( fun get -> {
asm = get.Required.Field "asm" Decode.string
address = get.Optional.Field "address" Decode.string
scriptType = get.Required.Field "type" Decode.string
});
let OutputDecoder =
Decode.object(fun get -> {
value = get.Required.Field "value" Decode.float
scriptPubKey = get.Required.Field "scriptPubKey" scriptPubKeyDecoder
})
let InputDecoder =
Decode.object(fun get -> {
txid = get.Required.Field "txid" Decode.string
vout = get.Required.Field "vout" Decode.int
prevOut = get.Required.Field "prevout" OutputDecoder
})
let ConfirmedTransactionDecoder : Decoder<ConfirmedTransaction> =
Decode.object (fun get -> {
txid = get.Required.Field "txid" Decode.string
version = get.Required.Field "version" Decode.int
size = get.Required.Field "size" Decode.int
vsize = get.Required.Field "vsize" Decode.int
weight = get.Required.Field "weight" Decode.int
locktime = get.Required.Field "locktime" Decode.int
vin = get.Required.Field "vin" (Decode.array InputDecoder)
vout = get.Required.Field "vout" (Decode.array OutputDecoder)
blockhash = get.Required.Field "blockhash" Decode.string
fee = get.Required.Field "fee" Decode.float
time = get.Required.Field "time" unixDateTimeDecoder
confirmations = get.Required.Field "confirmations" Decode.int
blocktime = get.Required.Field "blocktime" unixDateTimeDecoder
})
let UnconfirmedTransactionDecoder : Decoder<UnconfirmedTransaction> =
Decode.object (fun get -> {
txid = get.Required.Field "txid" Decode.string
version = get.Required.Field "version" Decode.int
size = get.Required.Field "size" Decode.int
vsize = get.Required.Field "vsize" Decode.int
weight = get.Required.Field "weight" Decode.int
locktime = get.Required.Field "locktime" Decode.int
vout = get.Required.Field "vout" (Decode.array OutputDecoder)
})
let TransactionDecoder : Decoder<Transaction> =
Decode.optional "confirmations" Decode.int
|> Decode.andThen(function
| Some _ -> ConfirmedTransactionDecoder |> Decode.map ConfirmedTransaction
| None -> UnconfirmedTransactionDecoder |> Decode.map UnconfirmedTransaction)
type RpcResponse<'T> = {
result: 'T
error: string option
id: string
}
let RpcErrorDecoder =
Decode.field "error" Decode.string |> Decode.andThen (fun e -> Decode.fail e)
let RpcSuccessDecoder =
Decode.field "result" TransactionDecoder
let RpcResponseDecoder<'T> =
Decode.oneOf [
RpcErrorDecoder
RpcSuccessDecoder
]
// Bitcoin RPC functions
let rpcUrl = "https://rpc.ankr.com/btc"
//let rpcUser = "bitcoinuser"
//let rpcPassword = "bitcoinpassword"
let makeRpcCall (method: string) (parameters: obj[]) =
let headers = [
Fetch.Types.ContentType "application/json"
//Fetch.Types.Authorization (sprintf "Basic %s" (window.btoa(sprintf "%s:%s" rpcUser rpcPassword)))
]
let request = {|
jsonrpc = "1.0"
id = "1"
method = method
params = parameters
|}
promise {
return! Fetch.post(rpcUrl, request, headers = headers, decoder = RpcResponseDecoder)
}