From 6ea019a66b45053bc91f91c6373ee91857a84c46 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 12 Mar 2020 23:22:39 -0400 Subject: [PATCH 1/5] Save and load unconfirmed txs from db --- .../OpenBazaar/go-ethwallet/wallet/wallet.go | 59 +++++++++++-------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go index 09e5029a04..cc08125ab2 100644 --- a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go +++ b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go @@ -519,6 +519,15 @@ func (wallet *EthereumWallet) Balance() (confirmed, unconfirmed wi.CurrencyValue // TransactionsFromBlock - Returns a list of transactions for this wallet begining from the specified block func (wallet *EthereumWallet) TransactionsFromBlock(startBlock *int) ([]wi.Txn, error) { + ret := []wi.Txn{} + unconf, err := wallet.db.Txns().GetAll(false) + if err == nil { + for _, u := range unconf { + u.Status = wi.StatusUnconfirmed + ret = append(ret, u) + } + } + txns, err := wallet.client.eClient.NormalTxByAddress(util.EnsureCorrectPrefix(wallet.account.Address().String()), startBlock, nil, 1, 0, false) if err != nil { @@ -526,7 +535,6 @@ func (wallet *EthereumWallet) TransactionsFromBlock(startBlock *int) ([]wi.Txn, return []wi.Txn{}, nil } - ret := []wi.Txn{} for _, t := range txns { status := wi.StatusConfirmed prefix := "" @@ -653,15 +661,20 @@ func (wallet *EthereumWallet) GetFeePerByte(feeLevel wi.FeeLevel) big.Int { // Spend - Send ether to an external wallet func (wallet *EthereumWallet) Spend(amount big.Int, addr btcutil.Address, feeLevel wi.FeeLevel, referenceID string, spendAll bool) (*chainhash.Hash, error) { - var hash common.Hash - var h *chainhash.Hash - var err error + var ( + hash common.Hash + h *chainhash.Hash + watchOnly bool + nonce int32 + err error + ) actualRecipient := addr if referenceID == "" { // no referenceID means this is a direct transfer hash, err = wallet.Transfer(util.EnsureCorrectPrefix(addr.String()), &amount, spendAll, wallet.GetFeePerByte(feeLevel)) } else { + watchOnly = true // this is a spend which means it has to be linked to an order // specified using the referenceID @@ -710,33 +723,33 @@ func (wallet *EthereumWallet) Spend(amount big.Int, addr btcutil.Address, feeLev } // txn is pending - nonce, err := wallet.client.GetTxnNonce(util.EnsureCorrectPrefix(hash.Hex())) - if err == nil { - data, err := SerializePendingTxn(PendingTxn{ - TxnID: hash, - Amount: amount.String(), - OrderID: referenceID, - Nonce: nonce, - From: wallet.address.EncodeAddress(), - To: actualRecipient.EncodeAddress(), - WithInput: false, - }) - if err == nil { - err0 := wallet.db.Txns().Put(data, ut.NormalizeAddress(hash.Hex()), "0", 0, time.Now(), true) - if err0 != nil { - log.Error(err0.Error()) - } - } + nonce, err = wallet.client.GetTxnNonce(util.EnsureCorrectPrefix(hash.Hex())) + if err != nil { + return nil, err } } - if err == nil { h, err = util.CreateChainHash(hash.Hex()) if err == nil { wallet.invokeTxnCB(h.String(), &amount) } } - return h, err + data, err := SerializePendingTxn(PendingTxn{ + TxnID: hash, + Amount: amount.String(), + OrderID: referenceID, + Nonce: nonce, + From: wallet.address.EncodeAddress(), + To: actualRecipient.EncodeAddress(), + WithInput: false, + }) + if err == nil { + err0 := wallet.db.Txns().Put(data, ut.NormalizeAddress(hash.Hex()), "0", 0, time.Now(), watchOnly) + if err0 != nil { + log.Error(err0.Error()) + } + } + return h, nil } func (wallet *EthereumWallet) createTxnCallback(txID, orderID string, toAddress btcutil.Address, value big.Int, bTime time.Time, withInput bool) wi.TransactionCallback { From dcf144bdbc2877b465d6b29bd41f4cd536766413 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 13 Mar 2020 16:59:55 -0400 Subject: [PATCH 2/5] Update eth pending confirmations --- .../OpenBazaar/go-ethwallet/wallet/wallet.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go index cc08125ab2..bc669641ec 100644 --- a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go +++ b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go @@ -537,6 +537,9 @@ func (wallet *EthereumWallet) TransactionsFromBlock(startBlock *int) ([]wi.Txn, for _, t := range txns { status := wi.StatusConfirmed + if t.Confirmations > 1 && t.Confirmations <= 7 { + status = wi.StatusPending + } prefix := "" if t.IsError != 0 { status = wi.StatusError @@ -752,7 +755,7 @@ func (wallet *EthereumWallet) Spend(amount big.Int, addr btcutil.Address, feeLev return h, nil } -func (wallet *EthereumWallet) createTxnCallback(txID, orderID string, toAddress btcutil.Address, value big.Int, bTime time.Time, withInput bool) wi.TransactionCallback { +func (wallet *EthereumWallet) createTxnCallback(txID, orderID string, toAddress btcutil.Address, value big.Int, bTime time.Time, withInput bool, height int64) wi.TransactionCallback { output := wi.TransactionOutput{ Address: toAddress, Value: value, @@ -772,12 +775,11 @@ func (wallet *EthereumWallet) createTxnCallback(txID, orderID string, toAddress } } - return wi.TransactionCallback{ Txid: util.EnsureCorrectPrefix(txID), Outputs: []wi.TransactionOutput{output}, Inputs: []wi.TransactionInput{input}, - Height: 1, + Height: int32(height), Timestamp: time.Now(), Value: value, WatchOnly: false, @@ -825,9 +827,10 @@ func (wallet *EthereumWallet) checkTxnRcpt(hash *common.Hash, data []byte) (*com toAddr = common.HexToAddress(util.EnsureCorrectPrefix(pTxn.To)) withInput = pTxn.WithInput } + height := rcpt.BlockNumber.Int64() go wallet.AssociateTransactionWithOrder( wallet.createTxnCallback(util.EnsureCorrectPrefix(hash.Hex()), pTxn.OrderID, EthAddress{&toAddr}, - *n, time.Now(), withInput)) + *n, time.Now(), withInput, height)) } } return hash, nil From 3f25b69eb1060332f6e9bc07a8500b3082804e9d Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 17 Mar 2020 09:01:58 -0400 Subject: [PATCH 3/5] Put eth unconfirmed txs at begining of list return --- .../OpenBazaar/go-ethwallet/wallet/wallet.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go index bc669641ec..3c4780f476 100644 --- a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go +++ b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go @@ -520,17 +520,12 @@ func (wallet *EthereumWallet) Balance() (confirmed, unconfirmed wi.CurrencyValue // TransactionsFromBlock - Returns a list of transactions for this wallet begining from the specified block func (wallet *EthereumWallet) TransactionsFromBlock(startBlock *int) ([]wi.Txn, error) { ret := []wi.Txn{} - unconf, err := wallet.db.Txns().GetAll(false) - if err == nil { - for _, u := range unconf { - u.Status = wi.StatusUnconfirmed - ret = append(ret, u) - } - } + + unconf, uerr := wallet.db.Txns().GetAll(false) txns, err := wallet.client.eClient.NormalTxByAddress(util.EnsureCorrectPrefix(wallet.account.Address().String()), startBlock, nil, 1, 0, false) - if err != nil { + if err != nil && len(unconf) == 0 { log.Error("err fetching transactions : ", err) return []wi.Txn{}, nil } @@ -560,6 +555,11 @@ func (wallet *EthereumWallet) TransactionsFromBlock(startBlock *int) ([]wi.Txn, ret = append(ret, tnew) } + for _, u := range unconf { + u.Status = wi.StatusUnconfirmed + ret = append(ret, u) + } + return ret, nil } From 01a24c1d523ed619016bd2107795abbed5852b38 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 17 Mar 2020 10:57:33 -0400 Subject: [PATCH 4/5] Ignore eth error fetching txs --- vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go index 3c4780f476..4bceb0b095 100644 --- a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go +++ b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go @@ -521,7 +521,7 @@ func (wallet *EthereumWallet) Balance() (confirmed, unconfirmed wi.CurrencyValue func (wallet *EthereumWallet) TransactionsFromBlock(startBlock *int) ([]wi.Txn, error) { ret := []wi.Txn{} - unconf, uerr := wallet.db.Txns().GetAll(false) + unconf, _ := wallet.db.Txns().GetAll(false) txns, err := wallet.client.eClient.NormalTxByAddress(util.EnsureCorrectPrefix(wallet.account.Address().String()), startBlock, nil, 1, 0, false) From fa5e79b0abc4cc4d85b61cdc72adec9180d74444 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 17 Mar 2020 11:07:05 -0400 Subject: [PATCH 5/5] Fix zero amount on unconfirmed tx --- vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go index 4bceb0b095..a95db5d5d3 100644 --- a/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go +++ b/vendor/github.com/OpenBazaar/go-ethwallet/wallet/wallet.go @@ -747,7 +747,7 @@ func (wallet *EthereumWallet) Spend(amount big.Int, addr btcutil.Address, feeLev WithInput: false, }) if err == nil { - err0 := wallet.db.Txns().Put(data, ut.NormalizeAddress(hash.Hex()), "0", 0, time.Now(), watchOnly) + err0 := wallet.db.Txns().Put(data, ut.NormalizeAddress(hash.Hex()), amount.String(), 0, time.Now(), watchOnly) if err0 != nil { log.Error(err0.Error()) }