@@ -94,90 +94,27 @@ func (handlers *Handlers) Uninit() {
94
94
handlers .account = nil
95
95
}
96
96
97
- // FormattedAmount with unit and conversions.
98
- type FormattedAmount struct {
99
- Amount string `json:"amount"`
100
- Unit string `json:"unit"`
101
- Conversions map [string ]string `json:"conversions"`
102
- // Estimated flag is enabled if the Conversions map was expected to
103
- // be calculated using historical rates, but latest rates have been used instead.
104
- Estimated bool `json:"estimated"`
105
- }
106
-
107
- func (handlers * Handlers ) formatAmountAsJSON (amount coin.Amount , isFee bool ) FormattedAmount {
108
- accountCoin := handlers .account .Coin ()
109
- return FormattedAmount {
110
- Amount : accountCoin .FormatAmount (amount , isFee ),
111
- Unit : accountCoin .GetFormatUnit (isFee ),
112
- Conversions : coin .Conversions (
113
- amount ,
114
- accountCoin ,
115
- isFee ,
116
- handlers .account .Config ().RateUpdater ,
117
- util .FormatBtcAsSat (handlers .account .Config ().BtcCurrencyUnit ),
118
- ),
119
- }
120
- }
121
-
122
- func (handlers * Handlers ) formatAmountAtTimeAsJSON (amount coin.Amount , timeStamp * time.Time ) FormattedAmount {
123
- accountCoin := handlers .account .Coin ()
124
- rateUpdater := handlers .account .Config ().RateUpdater
125
- formatBtcAsSat := util .FormatBtcAsSat (handlers .account .Config ().BtcCurrencyUnit )
126
- var conversions map [string ]string
127
- var estimated bool
128
-
129
- if timeStamp == nil {
130
- conversions = coin .Conversions (
131
- amount ,
132
- accountCoin ,
133
- false ,
134
- rateUpdater ,
135
- formatBtcAsSat ,
136
- )
137
- estimated = true
138
- } else {
139
- conversions , estimated = coin .ConversionsAtTime (
140
- amount ,
141
- accountCoin ,
142
- false ,
143
- rateUpdater ,
144
- formatBtcAsSat ,
145
- timeStamp ,
146
- )
147
- }
148
- return FormattedAmount {
149
- Amount : accountCoin .FormatAmount (amount , false ),
150
- Unit : accountCoin .GetFormatUnit (false ),
151
- Conversions : conversions ,
152
- Estimated : estimated ,
153
- }
154
- }
155
-
156
- func (handlers * Handlers ) formatBTCAmountAsJSON (amount btcutil.Amount , isFee bool ) FormattedAmount {
157
- return handlers .formatAmountAsJSON (coin .NewAmountFromInt64 (int64 (amount )), isFee )
158
- }
159
-
160
97
// Transaction is the info returned per transaction by the /transactions and /transaction endpoint.
161
98
type Transaction struct {
162
- TxID string `json:"txID"`
163
- InternalID string `json:"internalID"`
164
- NumConfirmations int `json:"numConfirmations"`
165
- NumConfirmationsComplete int `json:"numConfirmationsComplete"`
166
- Type string `json:"type"`
167
- Status accounts.TxStatus `json:"status"`
168
- Amount FormattedAmount `json:"amount"`
169
- AmountAtTime FormattedAmount `json:"amountAtTime"`
170
- DeductedAmountAtTime FormattedAmount `json:"deductedAmountAtTime"`
171
- Fee FormattedAmount `json:"fee"`
172
- Time * string `json:"time"`
173
- Addresses []string `json:"addresses"`
174
- Note string `json:"note"`
99
+ TxID string `json:"txID"`
100
+ InternalID string `json:"internalID"`
101
+ NumConfirmations int `json:"numConfirmations"`
102
+ NumConfirmationsComplete int `json:"numConfirmationsComplete"`
103
+ Type string `json:"type"`
104
+ Status accounts.TxStatus `json:"status"`
105
+ Amount coin. FormattedAmountWithConversions `json:"amount"`
106
+ AmountAtTime coin. FormattedAmountWithConversions `json:"amountAtTime"`
107
+ DeductedAmountAtTime coin. FormattedAmountWithConversions `json:"deductedAmountAtTime"`
108
+ Fee coin. FormattedAmountWithConversions `json:"fee"`
109
+ Time * string `json:"time"`
110
+ Addresses []string `json:"addresses"`
111
+ Note string `json:"note"`
175
112
176
113
// BTC specific fields.
177
- VSize int64 `json:"vsize"`
178
- Size int64 `json:"size"`
179
- Weight int64 `json:"weight"`
180
- FeeRatePerKb FormattedAmount `json:"feeRatePerKb"`
114
+ VSize int64 `json:"vsize"`
115
+ Size int64 `json:"size"`
116
+ Weight int64 `json:"weight"`
117
+ FeeRatePerKb coin. FormattedAmountWithConversions `json:"feeRatePerKb"`
181
118
182
119
// ETH specific fields
183
120
Gas uint64 `json:"gas"`
@@ -196,19 +133,20 @@ func (handlers *Handlers) ensureAccountInitialized(h func(*http.Request) (interf
196
133
// getTxInfoJSON encodes a given transaction in JSON.
197
134
// If `detail` is false, Coin related details, fees and historical fiat amount won't be included.
198
135
func (handlers * Handlers ) getTxInfoJSON (txInfo * accounts.TransactionData , detail bool ) Transaction {
199
- var feeString FormattedAmount
136
+ accountConfig := handlers .account .Config ()
137
+ var feeString coin.FormattedAmountWithConversions
200
138
if txInfo .Fee != nil {
201
- feeString = handlers . formatAmountAsJSON ( * txInfo .Fee , true )
139
+ feeString = txInfo .Fee . FormatWithConversions ( handlers . account . Coin () , true , accountConfig . RateUpdater )
202
140
}
203
- amount := handlers . formatAmountAsJSON ( txInfo .Amount , false )
141
+ amount := txInfo .Amount . FormatWithConversions ( handlers . account . Coin () , false , accountConfig . RateUpdater )
204
142
var formattedTime * string
205
143
timestamp := txInfo .Timestamp
206
144
if timestamp == nil {
207
145
timestamp = txInfo .CreatedTimestamp
208
146
}
209
147
210
- deductedAmountAtTime := handlers . formatAmountAtTimeAsJSON ( txInfo .DeductedAmount , timestamp )
211
- amountAtTime := handlers . formatAmountAtTimeAsJSON ( txInfo .Amount , timestamp )
148
+ deductedAmountAtTime := txInfo .DeductedAmount . FormatWithConversionsAtTime ( handlers . account . Coin () , timestamp , accountConfig . RateUpdater )
149
+ amountAtTime := txInfo .Amount . FormatWithConversionsAtTime ( handlers . account . Coin () , timestamp , accountConfig . RateUpdater )
212
150
213
151
if timestamp != nil {
214
152
t := timestamp .Format (time .RFC3339 )
@@ -247,7 +185,7 @@ func (handlers *Handlers) getTxInfoJSON(txInfo *accounts.TransactionData, detail
247
185
txInfoJSON .Weight = txInfo .Weight
248
186
feeRatePerKb := txInfo .FeeRatePerKb
249
187
if feeRatePerKb != nil {
250
- txInfoJSON .FeeRatePerKb = handlers .formatBTCAmountAsJSON ( * feeRatePerKb , true )
188
+ txInfoJSON .FeeRatePerKb = coin . ConvertBTCAmount ( handlers .account . Coin (), * feeRatePerKb , true , accountConfig . RateUpdater )
251
189
}
252
190
case * eth.Coin :
253
191
txInfoJSON .Gas = txInfo .Gas
@@ -344,6 +282,7 @@ func (handlers *Handlers) getAccountInfo(*http.Request) (interface{}, error) {
344
282
}
345
283
346
284
func (handlers * Handlers ) getUTXOs (* http.Request ) (interface {}, error ) {
285
+ accountConfig := handlers .account .Config ()
347
286
result := []map [string ]interface {}{}
348
287
349
288
t , ok := handlers .account .(* btc.Account )
@@ -378,7 +317,7 @@ func (handlers *Handlers) getUTXOs(*http.Request) (interface{}, error) {
378
317
"outPoint" : output .OutPoint .String (),
379
318
"txId" : output .OutPoint .Hash .String (),
380
319
"txOutput" : output .OutPoint .Index ,
381
- "amount" : handlers .formatBTCAmountAsJSON ( btcutil .Amount (output .TxOut .Value ), false ),
320
+ "amount" : coin . ConvertBTCAmount ( handlers .account . Coin (), btcutil .Amount (output .TxOut .Value ), false , accountConfig . RateUpdater ),
382
321
"address" : address ,
383
322
"scriptType" : output .Address .AccountConfiguration .ScriptType (),
384
323
"note" : handlers .account .TxNote (output .OutPoint .Hash .String ()),
@@ -391,11 +330,12 @@ func (handlers *Handlers) getUTXOs(*http.Request) (interface{}, error) {
391
330
}
392
331
393
332
func (handlers * Handlers ) getAccountBalance (* http.Request ) (interface {}, error ) {
333
+ accountConfig := handlers .account .Config ()
394
334
type balance struct {
395
- HasAvailable bool `json:"hasAvailable"`
396
- Available FormattedAmount `json:"available"`
397
- HasIncoming bool `json:"hasIncoming"`
398
- Incoming FormattedAmount `json:"incoming"`
335
+ HasAvailable bool `json:"hasAvailable"`
336
+ Available coin. FormattedAmountWithConversions `json:"available"`
337
+ HasIncoming bool `json:"hasIncoming"`
338
+ Incoming coin. FormattedAmountWithConversions `json:"incoming"`
399
339
}
400
340
401
341
type result struct {
@@ -410,9 +350,9 @@ func (handlers *Handlers) getAccountBalance(*http.Request) (interface{}, error)
410
350
Success : true ,
411
351
Balance : balance {
412
352
HasAvailable : accountBalance .Available ().BigInt ().Sign () > 0 ,
413
- Available : handlers . formatAmountAsJSON ( accountBalance .Available (), false ),
353
+ Available : accountBalance .Available (). FormatWithConversions ( handlers . account . Coin () , false , accountConfig . RateUpdater ),
414
354
HasIncoming : accountBalance .Incoming ().BigInt ().Sign () > 0 ,
415
- Incoming : handlers . formatAmountAsJSON ( accountBalance .Incoming (), false ),
355
+ Incoming : accountBalance .Incoming (). FormatWithConversions ( handlers . account . Coin () , false , accountConfig . RateUpdater ),
416
356
},
417
357
}, nil
418
358
}
@@ -551,6 +491,7 @@ func txProposalError(err error) (interface{}, error) {
551
491
}
552
492
553
493
func (handlers * Handlers ) postAccountTxProposal (r * http.Request ) (interface {}, error ) {
494
+ accountConfig := handlers .account .Config ()
554
495
var input sendTxInput
555
496
if err := json .NewDecoder (r .Body ).Decode (& input ); err != nil {
556
497
return txProposalError (errp .WithStack (err ))
@@ -561,9 +502,9 @@ func (handlers *Handlers) postAccountTxProposal(r *http.Request) (interface{}, e
561
502
}
562
503
return map [string ]interface {}{
563
504
"success" : true ,
564
- "amount" : handlers .formatAmountAsJSON ( outputAmount , false ),
565
- "fee" : handlers .formatAmountAsJSON ( fee , true ),
566
- "total" : handlers .formatAmountAsJSON ( total , false ),
505
+ "amount" : outputAmount . FormatWithConversions ( handlers .account . Coin () , false , accountConfig . RateUpdater ),
506
+ "fee" : fee . FormatWithConversions ( handlers .account . Coin () , true , accountConfig . RateUpdater ),
507
+ "total" : total . FormatWithConversions ( handlers .account . Coin () , false , accountConfig . RateUpdater ),
567
508
}, nil
568
509
}
569
510
0 commit comments