From 17c908013597fa8876e3e672ea880c7ea8f834eb Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Thu, 30 Jul 2020 16:02:08 +0200 Subject: [PATCH] Include eth transactions in unknown status into balance history --- api/types.go | 13 +++++++------ api/worker.go | 8 ++++---- bchain/coins/eth/ethparser.go | 22 +++++++++++++--------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/api/types.go b/api/types.go index c797069e..f6743823 100644 --- a/api/types.go +++ b/api/types.go @@ -8,6 +8,7 @@ import ( "time" "github.com/trezor/blockbook/bchain" + "github.com/trezor/blockbook/bchain/coins/eth" "github.com/trezor/blockbook/common" "github.com/trezor/blockbook/db" ) @@ -170,12 +171,12 @@ type TokenTransfer struct { // EthereumSpecific contains ethereum specific transaction data type EthereumSpecific struct { - Status int `json:"status"` // 1 OK, 0 Fail, -1 pending - Nonce uint64 `json:"nonce"` - GasLimit *big.Int `json:"gasLimit"` - GasUsed *big.Int `json:"gasUsed"` - GasPrice *Amount `json:"gasPrice"` - Data string `json:"data,omitempty"` + Status eth.TxStatus `json:"status"` // 1 OK, 0 Fail, -1 pending + Nonce uint64 `json:"nonce"` + GasLimit *big.Int `json:"gasLimit"` + GasUsed *big.Int `json:"gasUsed"` + GasPrice *Amount `json:"gasPrice"` + Data string `json:"data,omitempty"` } // Tx holds information about a transaction diff --git a/api/worker.go b/api/worker.go index 6eed9add..39941e0a 100644 --- a/api/worker.go +++ b/api/worker.go @@ -1037,8 +1037,8 @@ func (w *Worker) balanceHistoryForTxid(addrDesc bchain.AddressDescriptor, txid s } else if w.chainType == bchain.ChainEthereumType { var value big.Int ethTxData := eth.GetEthereumTxData(bchainTx) - // add received amount only for OK transactions - if ethTxData.Status == 1 { + // add received amount only for OK or unknown status (old) transactions + if ethTxData.Status == eth.TxStatusOK || ethTxData.Status == eth.TxStatusUnknown { if len(bchainTx.Vout) > 0 { bchainVout := &bchainTx.Vout[0] value = bchainVout.ValueSat @@ -1064,8 +1064,8 @@ func (w *Worker) balanceHistoryForTxid(addrDesc bchain.AddressDescriptor, txid s return nil, err } if bytes.Equal(addrDesc, txAddrDesc) { - // add sent amount only for OK transactions, however fees always - if ethTxData.Status == 1 { + // add received amount only for OK or unknown status (old) transactions, fees always + if ethTxData.Status == eth.TxStatusOK || ethTxData.Status == eth.TxStatusUnknown { (*big.Int)(bh.SentSat).Add((*big.Int)(bh.SentSat), &value) if countSentToSelf { if _, found := selfAddrDesc[string(txAddrDesc)]; found { diff --git a/bchain/coins/eth/ethparser.go b/bchain/coins/eth/ethparser.go index b564ed14..f0134e8b 100644 --- a/bchain/coins/eth/ethparser.go +++ b/bchain/coins/eth/ethparser.go @@ -461,16 +461,20 @@ func (p *EthereumParser) EthereumTypeGetErc20FromTx(tx *bchain.Tx) ([]bchain.Erc return r, nil } +// TxStatus is status of transaction +type TxStatus int + +// statuses of transaction const ( - txStatusUnknown = iota - 2 - txStatusPending - txStatusFailure - txStatusOK + TxStatusUnknown = TxStatus(iota - 2) + TxStatusPending + TxStatusFailure + TxStatusOK ) // EthereumTxData contains ethereum specific transaction data type EthereumTxData struct { - Status int `json:"status"` // 1 OK, 0 Fail, -1 pending, -2 unknown + Status TxStatus `json:"status"` // 1 OK, 0 Fail, -1 pending, -2 unknown Nonce uint64 `json:"nonce"` GasLimit *big.Int `json:"gaslimit"` GasUsed *big.Int `json:"gasused"` @@ -485,7 +489,7 @@ func GetEthereumTxData(tx *bchain.Tx) *EthereumTxData { // GetEthereumTxDataFromSpecificData returns EthereumTxData from coinSpecificData func GetEthereumTxDataFromSpecificData(coinSpecificData interface{}) *EthereumTxData { - etd := EthereumTxData{Status: txStatusPending} + etd := EthereumTxData{Status: TxStatusPending} csd, ok := coinSpecificData.(completeTransaction) if ok { if csd.Tx != nil { @@ -497,11 +501,11 @@ func GetEthereumTxDataFromSpecificData(coinSpecificData interface{}) *EthereumTx if csd.Receipt != nil { switch csd.Receipt.Status { case "0x1": - etd.Status = txStatusOK + etd.Status = TxStatusOK case "": // old transactions did not set status - etd.Status = txStatusUnknown + etd.Status = TxStatusUnknown default: - etd.Status = txStatusFailure + etd.Status = TxStatusFailure } etd.GasUsed, _ = hexutil.DecodeBig(csd.Receipt.GasUsed) }