forked from watsonwanda/oracle-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
composable_balancer_v2.go
162 lines (139 loc) · 4.74 KB
/
composable_balancer_v2.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
153
154
155
156
157
158
159
160
161
162
// Copyright (C) 2021-2023 Chronicle Labs, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package origin
import (
"context"
"fmt"
"sort"
"time"
"github.com/defiweb/go-eth/rpc"
"github.com/defiweb/go-eth/types"
"github.com/orcfax/oracle-suite/pkg/datapoint"
"github.com/orcfax/oracle-suite/pkg/datapoint/value"
"github.com/orcfax/oracle-suite/pkg/log"
"github.com/orcfax/oracle-suite/pkg/log/null"
"github.com/orcfax/oracle-suite/pkg/util/bn"
)
const ComposableBalancerV2LoggerTag = "COMPOSABLE_BALANCERV2_ORIGIN"
type ComposableBalancerV2Config struct {
Client rpc.RPC
ContractAddresses ContractAddresses
Logger log.Logger
Blocks []int64
}
type ComposableBalancerV2 struct {
client rpc.RPC
contractAddresses ContractAddresses
erc20 *ERC20
blocks []int64
logger log.Logger
}
// NewComposableBalancerV2 create instance for ComposableStableBalancer
// `ComposableStableBalancer` is just a notable name, it is balancer v2 origin specialized for ComposableStablePool implementation
// https://docs.balancer.fi/concepts/pools/composable-stable.html
// WeightedPool or MetaStablePool was implemented in BalancerV2
func NewComposableBalancerV2(config ComposableBalancerV2Config) (*ComposableBalancerV2, error) {
if config.Client == nil {
return nil, fmt.Errorf("ethereum client not set")
}
if config.Logger == nil {
config.Logger = null.New()
}
erc20, err := NewERC20(config.Client)
if err != nil {
return nil, err
}
return &ComposableBalancerV2{
client: config.Client,
contractAddresses: config.ContractAddresses,
erc20: erc20,
blocks: config.Blocks,
logger: config.Logger.WithField("composableBalancerV2", ComposableBalancerV2LoggerTag),
}, nil
}
func (b *ComposableBalancerV2) FetchDataPoints(ctx context.Context, query []any) (map[any]datapoint.Point, error) {
pairs, ok := queryToPairs(query)
if !ok {
return nil, fmt.Errorf("invalid query type: %T, expected []Pair", query)
}
sort.Slice(pairs, func(i, j int) bool {
return pairs[i].String() < pairs[j].String()
})
block, err := b.client.BlockNumber(ctx)
if err != nil {
return nil, fmt.Errorf("cannot get block number, %w", err)
}
points := make(map[any]datapoint.Point)
totals := make(map[value.Pair]*bn.DecFloatPointNumber)
var poolConfigs []ComposableStablePoolConfig
for _, pair := range pairs {
contract, _, _, err := b.contractAddresses.ByPair(pair)
if err != nil {
points[pair] = datapoint.Point{Error: err}
continue
}
poolConfigs = append(poolConfigs, ComposableStablePoolConfig{
Pair: pair,
Address: contract,
})
totals[pair] = bn.DecFloatPoint(0)
}
for _, pair := range pairs {
if points[pair].Error != nil {
continue
}
for _, blockDelta := range b.blocks {
blockNumber := types.BlockNumberFromUint64(uint64(block.Int64() - blockDelta))
pools, err := NewComposableStablePools(poolConfigs, b.client)
if err != nil {
points[pair] = datapoint.Point{Error: err}
break
}
err = pools.InitializePools(ctx, blockNumber)
if err != nil {
points[pair] = datapoint.Point{Error: err}
break
}
pool := pools.FindPoolByPair(pair)
if pool == nil {
points[pair] = datapoint.Point{Error: fmt.Errorf(
"not found balancer's weighted pool: %s", pair.String())}
break
}
baseToken := pools.tokenDetails[pair.Base]
quoteToken := pools.tokenDetails[pair.Quote]
// amountIn = 10 ^ baseDecimals
amountIn := _powX(10, int64(baseToken.decimals))
amountOut, _, err := pool.CalcAmountOut(baseToken.address, quoteToken.address, amountIn)
if err != nil {
points[pair] = datapoint.Point{Error: err}
break
}
// price = amountOut / 10 ^ quoteDecimals
price := bn.DecFloatPoint(amountOut).Div(bn.DecFloatPoint(_powX(10, int64(quoteToken.decimals))))
totals[pair] = totals[pair].Add(price)
}
if points[pair].Error != nil {
continue
}
avgPrice := totals[pair].Div(bn.DecFloatPoint(len(b.blocks)))
tick := value.NewTick(pair, avgPrice, nil)
points[pair] = datapoint.Point{
Value: tick,
Time: time.Now(),
}
}
return points, nil
}