Skip to content

Commit 30ae0d3

Browse files
committed
Update transaction history if new block received
1 parent d282877 commit 30ae0d3

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

internal/view/account.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package view
22

33
import (
4+
"github.com/dyng/ramen/internal/common"
45
"github.com/dyng/ramen/internal/common/conv"
6+
"github.com/dyng/ramen/internal/service"
57
serv "github.com/dyng/ramen/internal/service"
68
"github.com/dyng/ramen/internal/view/format"
79
"github.com/dyng/ramen/internal/view/style"
@@ -41,6 +43,9 @@ func NewAccount(app *App) *Account {
4143
// setup keymap
4244
account.initKeymap()
4345

46+
// subscribe to new blocks
47+
app.eventBus.Subscribe(service.TopicNewBlock, account.onNewBlock)
48+
4449
return account
4550
}
4651

@@ -178,6 +183,18 @@ func (a *Account) HideImportABIDialog() {
178183
a.app.SetFocus(a)
179184
}
180185

186+
func (a *Account) onNewBlock(block *common.Block) {
187+
txns, err := a.app.service.GetTransactionsByBlock(block)
188+
if err != nil {
189+
log.Error("cannot extract transactions from block", "blockHash", block.Hash(), "error", err)
190+
return
191+
}
192+
193+
a.app.QueueUpdateDraw(func() {
194+
a.transactionList.FilterAndPrependTransactions(txns)
195+
})
196+
}
197+
181198
func (a *Account) refresh() {
182199
addr := a.account.GetAddress()
183200
a.accountInfo.address.SetText(addr.Hex())

internal/view/helper.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ func StyledTxnDirection(base *common.Address, txn common.Transaction) string {
8383

8484
if txn.From().String() == base.String() {
8585
return "[sandybrown]OUT[-]"
86-
} else {
86+
}
87+
88+
if txn.To() != nil && txn.To().String() == base.String() {
8789
return "[lightgreen]IN[-]"
8890
}
91+
92+
return ""
8993
}

internal/view/transactions.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ func (t *TransactionList) SetBaseAccount(account *common.Address) {
7979
t.base = account
8080
}
8181

82+
func (t *TransactionList) FilterAndPrependTransactions(txns common.Transactions) {
83+
if t.base == nil {
84+
t.PrependTransactions(txns)
85+
return
86+
}
87+
88+
toAdd := make(common.Transactions, 0)
89+
for _, tx := range txns {
90+
if tx.From().String() == t.base.String() {
91+
toAdd = append(toAdd, tx)
92+
}
93+
if tx.To() != nil && tx.To().String() == t.base.String() {
94+
toAdd = append(toAdd, tx)
95+
}
96+
}
97+
t.PrependTransactions(toAdd)
98+
}
99+
82100
func (t *TransactionList) PrependTransactions(txns common.Transactions) {
83101
prepended := append(txns, t.txns...)
84102
t.SetTransactions(prepended)

0 commit comments

Comments
 (0)