-
Notifications
You must be signed in to change notification settings - Fork 18
/
server.go
86 lines (65 loc) · 1.75 KB
/
server.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
// Copyright 2022 Fluidity Money. All rights reserved. Use of this
// source code is governed by a GPL-style license that can be found in the
// LICENSE.md file.
package chainlink
import (
"math/big"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/fluidity-money/fluidity-app/common/ethereum"
"github.com/fluidity-money/fluidity-app/lib/log"
"github.com/ethereum/go-ethereum/ethclient"
)
type request struct {
client *ethclient.Client
priceFeedAddress ethCommon.Address
resp chan *big.Rat
}
// decimalsServer to respond to requests with cached (or not, looked up)
// responses
var decimalsServer = make(chan request)
func startDecimalsServer() {
decimalsCache := make(map[ethCommon.Address]*big.Rat)
for request := range decimalsServer {
var (
client = request.client
priceFeedAddress = request.priceFeedAddress
respChan = request.resp
)
decimals, exists := decimalsCache[priceFeedAddress]
if exists {
respChan <- decimals
continue
}
decimalsRes, err := ethereum.StaticCall(
client,
priceFeedAddress,
priceFeedAbi,
"decimals",
)
if err != nil {
log.Fatal(func(k *log.Log) {
k.Context = Context
k.Format(
"Failed to static call feed address %v",
priceFeedAddress,
)
k.Payload = err
})
}
decimals_, err := ethereum.CoerceBoundContractResultsToUint8(decimalsRes)
if err != nil {
log.Fatal(func(k *log.Log) {
k.Context = Context
k.Format(
"Failed to read decimals result for price feed address %v",
priceFeedAddress,
)
k.Payload = err
})
}
ten := big.NewRat(10, 1)
decimals = ethereum.BigPow(ten, int(decimals_))
decimalsCache[priceFeedAddress] = decimals
respChan <- decimals
}
}