-
Notifications
You must be signed in to change notification settings - Fork 338
/
priceoracle.go
152 lines (129 loc) · 4.13 KB
/
priceoracle.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package priceoracle
import (
"context"
"errors"
"io"
"math/big"
"time"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/log"
"github.com/ethersphere/bee/pkg/transaction"
"github.com/ethersphere/go-price-oracle-abi/priceoracleabi"
)
// loggerName is the tree path name of the logger for this package.
const loggerName = "priceoracle"
var (
errDecodeABI = errors.New("could not decode abi data")
)
type service struct {
logger log.Logger
priceOracleAddress common.Address
transactionService transaction.Service
exchangeRate *big.Int
deduction *big.Int
timeDivisor int64
quitC chan struct{}
}
type Service interface {
io.Closer
// CurrentRates returns the current value of exchange rate and deduction
// according to the latest information from oracle
CurrentRates() (exchangeRate *big.Int, deduction *big.Int, err error)
// GetPrice retrieves latest available information from oracle
GetPrice(ctx context.Context) (*big.Int, *big.Int, error)
Start()
}
var (
priceOracleABI = transaction.ParseABIUnchecked(priceoracleabi.PriceOracleABIv0_1_0)
)
func New(logger log.Logger, priceOracleAddress common.Address, transactionService transaction.Service, timeDivisor int64) Service {
return &service{
logger: logger.WithName(loggerName).Register(),
priceOracleAddress: priceOracleAddress,
transactionService: transactionService,
exchangeRate: big.NewInt(0),
deduction: nil,
quitC: make(chan struct{}),
timeDivisor: timeDivisor,
}
}
func (s *service) Start() {
loggerV1 := s.logger.V(1).Register()
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer cancel()
<-s.quitC
}()
go func() {
defer cancel()
for {
exchangeRate, deduction, err := s.GetPrice(ctx)
if err != nil {
s.logger.Error(err, "could not get price")
} else {
loggerV1.Debug("updated exchange rate and deduction", "new_exchange_rate", exchangeRate, "new_deduction", deduction)
s.exchangeRate = exchangeRate
s.deduction = deduction
}
ts := time.Now().Unix()
// We poll the oracle in every timestamp divisible by constant 300 (timeDivisor)
// in order to get latest version approximately at the same time on all nodes
// and to minimize polling frequency
// If the node gets newer information than what was applicable at last polling point at startup
// this minimizes the negative scenario to less than 5 minutes
// during which cheques can not be sent / accepted because of the asymmetric information
timeUntilNextPoll := time.Duration(s.timeDivisor-ts%s.timeDivisor) * time.Second
select {
case <-s.quitC:
return
case <-time.After(timeUntilNextPoll):
}
}
}()
}
func (s *service) GetPrice(ctx context.Context) (*big.Int, *big.Int, error) {
callData, err := priceOracleABI.Pack("getPrice")
if err != nil {
return nil, nil, err
}
result, err := s.transactionService.Call(ctx, &transaction.TxRequest{
To: &s.priceOracleAddress,
Data: callData,
})
if err != nil {
return nil, nil, err
}
results, err := priceOracleABI.Unpack("getPrice", result)
if err != nil {
return nil, nil, err
}
if len(results) != 2 {
return nil, nil, errDecodeABI
}
exchangeRate, ok := abi.ConvertType(results[0], new(big.Int)).(*big.Int)
if !ok || exchangeRate == nil {
return nil, nil, errDecodeABI
}
deduction, ok := abi.ConvertType(results[1], new(big.Int)).(*big.Int)
if !ok || deduction == nil {
return nil, nil, errDecodeABI
}
return exchangeRate, deduction, nil
}
func (s *service) CurrentRates() (exchangeRate, deduction *big.Int, err error) {
if s.exchangeRate.Cmp(big.NewInt(0)) == 0 {
return nil, nil, errors.New("exchange rate not yet available")
}
if s.deduction == nil {
return nil, nil, errors.New("deduction amount not yet available")
}
return s.exchangeRate, s.deduction, nil
}
func (s *service) Close() error {
close(s.quitC)
return nil
}