Skip to content

Commit 0d45c2d

Browse files
committed
Update account balance when new block is received
1 parent bd7e1de commit 0d45c2d

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

internal/service/syncer.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const (
1818
TopicNewBlock = "service:newBlock"
1919
// TopicChainData is the topic about latest chain data (ether price, gas price, etc.)
2020
TopicChainData = "service:chainData"
21+
// TopicTick is a topic that receives tick event periodically
22+
TopicTick = "service:tick"
2123

2224
// UpdatePeriod is the time duration between two updates
2325
UpdatePeriod = 5 * time.Second
@@ -92,11 +94,13 @@ func (s *Syncer) sync() {
9294
case tick := <-s.ticker.C:
9395
log.Debug("Process periodic synchronization", "tick", tick)
9496

97+
// update eth price
9598
price, err := s.service.GetEthPrice()
9699
if err != nil {
97100
log.Error("Failed to fetch ether's price", "error", err)
98101
}
99102

103+
// update gas price
100104
gasPrice, err := s.service.GetGasPrice()
101105
if err != nil {
102106
log.Error("Failed to fetch gas price", "error", err)
@@ -107,6 +111,10 @@ func (s *Syncer) sync() {
107111
GasPrice: gasPrice,
108112
}
109113
s.eventBus.Publish(TopicChainData, data)
114+
115+
go func() {
116+
s.eventBus.Publish(TopicTick, tick)
117+
}()
110118
}
111119
}
112120
}

internal/view/account.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,21 @@ func (a *Account) ShowImportABIDialog() {
163163
}
164164

165165
func (a *Account) onNewBlock(block *common.Block) {
166+
if a.account == nil {
167+
return
168+
}
169+
166170
txns, err := a.app.service.GetTransactionsByBlock(block)
167171
if err != nil {
168172
log.Error("cannot extract transactions from block", "blockHash", block.Hash(), "error", err)
169173
return
170174
}
171175

176+
// update current account
177+
a.account.UpdateBalance()
178+
172179
a.app.QueueUpdateDraw(func() {
180+
a.refresh()
173181
a.transactionList.FilterAndPrependTransactions(txns)
174182
})
175183
}

internal/view/signer.go

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

33
import (
4+
"github.com/dyng/ramen/internal/common"
45
"github.com/dyng/ramen/internal/common/conv"
56
"github.com/dyng/ramen/internal/service"
67
"github.com/dyng/ramen/internal/view/style"
@@ -30,6 +31,9 @@ func NewSigner(app *App) *Signer {
3031
// setup layout
3132
signer.initLayout()
3233

34+
// subscribe tick event
35+
app.eventBus.Subscribe(service.TopicNewBlock, signer.onNewBlock)
36+
3337
return signer
3438
}
3539

@@ -97,3 +101,10 @@ func (si *Signer) layoutSomeSigner() {
97101

98102
si.Primitive = flex
99103
}
104+
105+
func (si *Signer) onNewBlock(block *common.Block) {
106+
if si.signer != nil {
107+
si.signer.UpdateBalance()
108+
si.refresh()
109+
}
110+
}

0 commit comments

Comments
 (0)