-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas.go
64 lines (52 loc) · 1.14 KB
/
gas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package services
import (
"fmt"
"time"
"github.com/berkeleytrue/crypto-egg-go/internal/core/domain"
"github.com/berkeleytrue/crypto-egg-go/internal/core/ports"
"go.uber.org/zap"
)
type GasSrv struct {
repo ports.GasRepo
api ports.GasApi
}
func CreateGasSrv(repo ports.GasRepo, api ports.GasApi) *GasSrv {
return &GasSrv{repo, api}
}
func (srv *GasSrv) Get() domain.Gas {
return srv.repo.Get()
}
func (srv *GasSrv) Update(baseFee float32) domain.Gas {
return srv.repo.Update(domain.Gas{
ID: "ethereum",
Base: baseFee,
UpdatedAt: time.Now(),
})
}
func (srv *GasSrv) StartService() func() {
logger := zap.NewExample().Sugar()
defer logger.Sync()
ticker := time.NewTicker(6 * time.Second)
quitTickerChan := make(chan struct{}, 1)
go func() {
for {
select {
case <-ticker.C:
baseFee, err := srv.api.Get()
if err != nil {
fmt.Println(fmt.Errorf("GasSrv:api:Get: %w", err))
break
}
// log.Printf("baseFee: %f", baseFee)
srv.Update(baseFee)
case <-quitTickerChan:
ticker.Stop()
return
}
}
}()
return func() {
quitTickerChan <- struct{}{}
close(quitTickerChan)
}
}