diff --git a/api/types.go b/api/types.go index ecb42a96..0d21a284 100644 --- a/api/types.go +++ b/api/types.go @@ -76,6 +76,16 @@ func (a *Amount) AsBigInt() big.Int { return big.Int(*a) } +// AsInt64 returns Amount as int64 (0 if Amount is nil). +// It is used only for legacy interfaces (socket.io) +// and generally not recommended to use for possible loss of precision. +func (a *Amount) AsInt64() int64 { + if a == nil { + return 0 + } + return (*big.Int)(a).Int64() +} + // Vin contains information about single transaction input type Vin struct { Txid string `json:"txid,omitempty"` diff --git a/api/worker.go b/api/worker.go index 372c3202..f538683c 100644 --- a/api/worker.go +++ b/api/worker.go @@ -191,7 +191,9 @@ func (w *Worker) GetTransactionFromBchainTx(bchainTx *bchain.Tx, height uint32, } } } - valInSat.Add(&valInSat, (*big.Int)(vin.ValueSat)) + if vin.ValueSat != nil { + valInSat.Add(&valInSat, (*big.Int)(vin.ValueSat)) + } } } else if w.chainType == bchain.ChainEthereumType { if len(bchainVin.Addresses) > 0 { diff --git a/server/socketio.go b/server/socketio.go index 2a43586f..e742f3cb 100644 --- a/server/socketio.go +++ b/server/socketio.go @@ -15,7 +15,7 @@ import ( "github.com/golang/glog" "github.com/juju/errors" - "github.com/martinboehm/golang-socketio" + gosocketio "github.com/martinboehm/golang-socketio" "github.com/martinboehm/golang-socketio/transport" ) @@ -312,7 +312,7 @@ func txToResTx(tx *api.Tx) resTx { Script: &script, Sequence: int64(vin.Sequence), OutputIndex: int(vin.Vout), - Satoshis: (*big.Int)(vin.ValueSat).Int64(), + Satoshis: vin.ValueSat.AsInt64(), } if len(vin.Addresses) > 0 { a := vin.Addresses[0] @@ -325,7 +325,7 @@ func txToResTx(tx *api.Tx) resTx { vout := &tx.Vout[i] script := vout.Hex output := txOutputs{ - Satoshis: (*big.Int)(vout.ValueSat).Int64(), + Satoshis: vout.ValueSat.AsInt64(), Script: &script, } if len(vout.Addresses) > 0 { @@ -342,15 +342,15 @@ func txToResTx(tx *api.Tx) resTx { } return resTx{ BlockTimestamp: tx.Blocktime, - FeeSatoshis: (*big.Int)(tx.FeesSat).Int64(), + FeeSatoshis: tx.FeesSat.AsInt64(), Hash: tx.Txid, Height: h, Hex: tx.Hex, Inputs: inputs, - InputSatoshis: (*big.Int)(tx.ValueInSat).Int64(), + InputSatoshis: tx.ValueInSat.AsInt64(), Locktime: int(tx.Locktime), Outputs: outputs, - OutputSatoshis: (*big.Int)(tx.ValueOutSat).Int64(), + OutputSatoshis: tx.ValueOutSat.AsInt64(), Version: int(tx.Version), } } @@ -407,7 +407,9 @@ func (s *SocketIoServer) getAddressHistory(addr []string, opts *addrOpts) (res r ads[a] = hi } hi.InputIndexes = append(hi.InputIndexes, int(vin.N)) - totalSat.Sub(&totalSat, (*big.Int)(vin.ValueSat)) + if vin.ValueSat != nil { + totalSat.Sub(&totalSat, (*big.Int)(vin.ValueSat)) + } } } for i := range tx.Vout { @@ -420,7 +422,9 @@ func (s *SocketIoServer) getAddressHistory(addr []string, opts *addrOpts) (res r ads[a] = hi } hi.OutputIndexes = append(hi.OutputIndexes, int(vout.N)) - totalSat.Add(&totalSat, (*big.Int)(vout.ValueSat)) + if vout.ValueSat != nil { + totalSat.Add(&totalSat, (*big.Int)(vout.ValueSat)) + } } } ahi := addressHistoryItem{}