Skip to content

Commit

Permalink
client,app: Transaction History UI
Browse files Browse the repository at this point in the history
This diff adds a UI to the wallets page displaying the transaction
history of a wallet.
  • Loading branch information
martonp committed Jan 8, 2024
1 parent 71de25d commit 3e1aa66
Show file tree
Hide file tree
Showing 16 changed files with 825 additions and 110 deletions.
87 changes: 46 additions & 41 deletions client/asset/btc/btc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2419,7 +2419,7 @@ func (btc *baseWallet) submitMultiSplitTx(fundingCoins asset.Coins, spents []*Ou
for _, txOut := range tx.TxOut {
totalOut += uint64(txOut.Value)
}
btc.addTxToHistory(asset.Split, txHash[:], 0, totalIn-totalOut, true)
btc.addTxToHistory(asset.Split, txHash[:], 0, totalIn-totalOut, nil, nil, true)

success = true
return coins, totalIn - totalOut, nil
Expand Down Expand Up @@ -2675,7 +2675,7 @@ func (btc *baseWallet) split(value uint64, lots uint64, outputs []*Output, input
totalOut += uint64(msgTx.TxOut[i].Value)
}

btc.addTxToHistory(asset.Split, txHash[:], 0, coinSum-totalOut, true)
btc.addTxToHistory(asset.Split, txHash[:], 0, coinSum-totalOut, nil, nil, true)

fundingCoins = map[OutPoint]*UTxO{op.Pt: {
TxHash: op.txHash(),
Expand Down Expand Up @@ -3110,7 +3110,7 @@ func accelerateOrder(btc *baseWallet, swapCoins, accelerationCoins []dex.Bytes,
}

txHash := btc.hashTx(signedTx)
btc.addTxToHistory(asset.Acceleration, txHash[:], 0, fees, true)
btc.addTxToHistory(asset.Acceleration, txHash[:], 0, fees, nil, nil, true)

// Delete the old change from the cache
btc.cm.ReturnOutPoint(NewOutPoint(changeTxHash, changeVout))
Expand Down Expand Up @@ -3436,6 +3436,8 @@ func (btc *baseWallet) markTxAsSubmitted(id dex.Bytes) {
if err != nil {
btc.log.Errorf("failed to mark tx as submitted in tx history db: %v", err)
}

btc.emit.TransactionNote(wt.WalletTransaction, true)
}

func (btc *baseWallet) removeTxFromHistory(id dex.Bytes) {
Expand All @@ -3450,18 +3452,21 @@ func (btc *baseWallet) removeTxFromHistory(id dex.Bytes) {
}
}

func (btc *baseWallet) addTxToHistory(txType asset.TransactionType, id dex.Bytes, balanceDelta int64, fees uint64, submitted bool) {
func (btc *baseWallet) addTxToHistory(txType asset.TransactionType, id dex.Bytes, amount uint64, fees uint64,
bondInfo *asset.BondTxInfo, recipient *string, submitted bool) {
txHistoryDB := btc.txDB()
if txHistoryDB == nil {
return
}

wt := &extendedWalletTx{
WalletTransaction: &asset.WalletTransaction{
Type: txType,
ID: id,
BalanceDelta: balanceDelta,
Fees: fees,
Type: txType,
ID: id,
Amount: amount,
Fees: fees,
BondInfo: bondInfo,
Recipient: recipient,
},
Submitted: submitted,
}
Expand All @@ -3477,6 +3482,10 @@ func (btc *baseWallet) addTxToHistory(txType asset.TransactionType, id dex.Bytes
if err != nil {
btc.log.Errorf("failed to store tx in tx history db: %v", err)
}

if submitted {
btc.emit.TransactionNote(wt.WalletTransaction, true)
}
}

// Swap sends the swaps in a single transaction and prepares the receipts. The
Expand Down Expand Up @@ -3607,7 +3616,7 @@ func (btc *baseWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, ui
return nil, nil, 0, err
}

btc.addTxToHistory(asset.Swap, txHash[:], -int64(totalOut), fees, true)
btc.addTxToHistory(asset.Swap, txHash[:], totalOut, fees, nil, nil, true)

// If change is nil, return a nil asset.Coin.
var changeCoin asset.Coin
Expand Down Expand Up @@ -3771,7 +3780,7 @@ func (btc *baseWallet) Redeem(form *asset.RedeemForm) ([]dex.Bytes, asset.Coin,
return nil, nil, 0, err
}

btc.addTxToHistory(asset.Redeem, txHash[:], int64(totalIn), fee, true)
btc.addTxToHistory(asset.Redeem, txHash[:], totalIn, fee, nil, nil, true)

// Log the change output.
coinIDs := make([]dex.Bytes, 0, len(form.Redemptions))
Expand Down Expand Up @@ -4028,7 +4037,7 @@ func (btc *baseWallet) Refund(coinID, contract dex.Bytes, feeRate uint64) (dex.B
if len(msgTx.TxOut) > 0 { // something went very wrong if not true
fee = uint64(utxo.Value - msgTx.TxOut[0].Value)
}
btc.addTxToHistory(asset.Refund, txHash[:], utxo.Value, fee, true)
btc.addTxToHistory(asset.Refund, txHash[:], uint64(utxo.Value), fee, nil, nil, true)

return ToCoinID(refundHash, 0), nil
}
Expand Down Expand Up @@ -4264,11 +4273,6 @@ func (btc *baseWallet) send(address string, val uint64, feeRate uint64, subtract
return nil, 0, 0, fmt.Errorf("PayToAddrScript error: %w", err)
}

selfSend, err := btc.OwnsDepositAddress(address)
if err != nil {
return nil, 0, 0, fmt.Errorf("error checking address ownership: %w", err)
}

baseSize := dexbtc.MinimumTxOverhead
if btc.segwit {
baseSize += dexbtc.P2WPKHOutputSize * 2
Expand Down Expand Up @@ -4314,12 +4318,16 @@ func (btc *baseWallet) send(address string, val uint64, feeRate uint64, subtract
totalOut += uint64(txOut.Value)
}

var amtSent int64
if !selfSend {
amtSent = -int64(toSend)
selfSend, err := btc.OwnsDepositAddress(address)
if err != nil {
return nil, 0, 0, fmt.Errorf("error checking address ownership: %w", err)
}
txType := asset.Send
if selfSend {
txType = asset.SelfSend
}

btc.addTxToHistory(asset.Send, txHash[:], amtSent, totalIn-totalOut, true)
btc.addTxToHistory(txType, txHash[:], toSend, totalIn-totalOut, nil, &address, true)

return txHash, 0, toSend, nil
}
Expand Down Expand Up @@ -4911,7 +4919,12 @@ func (btc *baseWallet) MakeBondTx(ver uint16, amt, feeRate uint64, lockTime time
}
success = true

btc.addTxToHistory(asset.CreateBond, txid[:], -int64(amt), fee, false)
bondInfo := &asset.BondTxInfo{
AccountID: acctID,
LockTime: uint64(lockTimeSec),
BondID: pkh,
}
btc.addTxToHistory(asset.CreateBond, txid[:], amt, fee, bondInfo, nil, false)
txIDToRemoveFromHistory = txid

return bond, abandon, nil
Expand Down Expand Up @@ -5000,6 +5013,10 @@ func (btc *baseWallet) RefundBond(ctx context.Context, ver uint16, coinID, scrip
if ver != 0 {
return nil, errors.New("only version 0 bonds supported")
}
lockTime, pkhPush, err := dexbtc.ExtractBondDetailsV0(0, script)
if err != nil {
return nil, err
}
txHash, vout, err := decodeCoinID(coinID)
if err != nil {
return nil, err
Expand All @@ -5021,8 +5038,11 @@ func (btc *baseWallet) RefundBond(ctx context.Context, ver uint16, coinID, scrip
if len(msgTx.TxOut) == 1 {
fees = amt - uint64(msgTx.TxOut[0].Value)
}
btc.addTxToHistory(asset.RedeemBond, txID[:], int64(amt), fees, true)

bondInfo := &asset.BondTxInfo{
LockTime: uint64(lockTime),
BondID: pkhPush,
}
btc.addTxToHistory(asset.RedeemBond, txID[:], amt, fees, bondInfo, nil, true)
return NewOutput(txHash, 0, uint64(msgTx.TxOut[0].Value)), nil
}

Expand Down Expand Up @@ -5199,25 +5219,8 @@ func (btc *intermediaryWallet) checkPendingTxs(tip uint64) {
fee = toSatoshi(*tx.Fee)
}
}
wt := &extendedWalletTx{
WalletTransaction: &asset.WalletTransaction{
Type: asset.Receive,
ID: txID,
BalanceDelta: int64(toSatoshi(tx.Amount)),
Fees: fee,
},
Submitted: true,
}

err = txHistoryDB.storeTx(wt)
if err != nil {
btc.log.Errorf("Error storing tx %s: %v", tx.TxID, err)
}
if !wt.Confirmed || err != nil {
btc.pendingTxsMtx.Lock()
btc.pendingTxs[*txHash] = wt
btc.pendingTxsMtx.Unlock()
}
btc.addTxToHistory(asset.Receive, txID, toSatoshi(tx.Amount), fee, nil, nil, true)
}
}
}
Expand Down Expand Up @@ -5267,6 +5270,7 @@ func (btc *intermediaryWallet) checkPendingTxs(tip uint64) {
btc.log.Errorf("Error getting block height for %s: %v", blockHash, err)
return
}
tx.Timestamp = gtr.BlockTime
if tx.BlockNumber != uint64(blockHeight) {
tx.BlockNumber = uint64(blockHeight)
updated = true
Expand Down Expand Up @@ -5296,6 +5300,7 @@ func (btc *intermediaryWallet) checkPendingTxs(tip uint64) {
delete(btc.pendingTxs, hash)
btc.pendingTxsMtx.Unlock()
}
btc.emit.TransactionNote(tx.WalletTransaction, false)
}
}

Expand Down
Loading

0 comments on commit 3e1aa66

Please sign in to comment.