-
Notifications
You must be signed in to change notification settings - Fork 23
/
rebalance.go
213 lines (192 loc) · 7.51 KB
/
rebalance.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package inventory
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/synapsecns/sanguine/services/rfq/relayer/relconfig"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// RebalanceData contains metadata for a rebalance action.
type RebalanceData struct {
OriginMetadata *TokenMetadata
DestMetadata *TokenMetadata
Amount *big.Int
Method relconfig.RebalanceMethod
}
// RebalanceManager is the interface for the rebalance manager.
type RebalanceManager interface {
// Start starts the rebalance manager.
Start(ctx context.Context) (err error)
// Execute executes a rebalance action.
Execute(ctx context.Context, rebalance *RebalanceData) error
}
// getRebalance builds a rebalance action based on current token balances and configured thresholds.
// Note that only the given chain/token pair is considered for rebalance (as the destination chain).
//
//nolint:cyclop,nilnil
func getRebalance(span trace.Span, cfg relconfig.Config, tokens map[int]map[common.Address]*TokenMetadata, chainID int, token common.Address) (rebalance *RebalanceData, err error) {
// get rebalance method
method, err := cfg.GetRebalanceMethod(chainID, token.Hex())
if err != nil {
return nil, fmt.Errorf("could not get rebalance method: %w", err)
}
if method == relconfig.RebalanceMethodNone {
return nil, nil
}
// get token metadata
var rebalanceTokenData *TokenMetadata
for address, tokenData := range tokens[chainID] {
if address == token {
rebalanceTokenData = tokenData
break
}
}
// evaluate the origin and dest of the rebalance based on min/max token balances
originTokenData, destTokenData := getRebalanceMetadatas(cfg, tokens, rebalanceTokenData.Name, method)
if originTokenData == nil {
if span != nil {
span.SetAttributes(attribute.Bool("no_rebalance_origin", true))
}
return nil, nil
}
if destTokenData == nil {
if span != nil {
span.SetAttributes(attribute.Bool("no_rebalance_dest", true))
}
return nil, nil
}
// if the given chain is not the destination of the rebalance, no need to do anything
if destTokenData.ChainID != chainID {
if span != nil {
span.SetAttributes(attribute.Int("rebalance_dest", destTokenData.ChainID))
}
return nil, nil
}
amount, err := getRebalanceAmount(span, cfg, tokens, originTokenData, destTokenData)
if err != nil {
return nil, fmt.Errorf("could not get rebalance amount: %w", err)
}
if amount == nil {
if span != nil {
span.SetAttributes(attribute.Bool("no_rebalance_amount", true))
}
return nil, nil
}
rebalance = &RebalanceData{
OriginMetadata: originTokenData,
DestMetadata: destTokenData,
Amount: amount,
Method: method,
}
return rebalance, nil
}
// getRebalanceMetadatas finds the origin and dest token metadata based on the configured rebalance method.
func getRebalanceMetadatas(cfg relconfig.Config, tokens map[int]map[common.Address]*TokenMetadata, tokenName string, method relconfig.RebalanceMethod) (originTokenData, destTokenData *TokenMetadata) {
for _, tokenMap := range tokens {
for _, tokenData := range tokenMap {
if tokenData.Name == tokenName {
// make sure that the token is compatible with our rebalance method
tokenMethod, tokenErr := cfg.GetRebalanceMethod(tokenData.ChainID, tokenData.Addr.Hex())
if tokenErr != nil {
logger.Errorf("could not get token rebalance method: %v", tokenErr)
continue
}
if tokenMethod != method {
continue
}
// assign origin / dest metadata based on min / max balances
if originTokenData == nil || tokenData.Balance.Cmp(originTokenData.Balance) > 0 {
originTokenData = tokenData
}
if destTokenData == nil || tokenData.Balance.Cmp(destTokenData.Balance) < 0 {
destTokenData = tokenData
}
}
}
}
return originTokenData, destTokenData
}
// getRebalanceAmount calculates the amount to rebalance based on the configured thresholds.
//
//nolint:cyclop,nilnil
func getRebalanceAmount(span trace.Span, cfg relconfig.Config, tokens map[int]map[common.Address]*TokenMetadata, originTokenData, destTokenData *TokenMetadata) (amount *big.Int, err error) {
// get the maintenance and initial values for the destination chain
maintenancePct, err := cfg.GetMaintenanceBalancePct(destTokenData.ChainID, destTokenData.Addr.Hex())
if err != nil {
return nil, fmt.Errorf("could not get maintenance pct: %w", err)
}
initialPct, err := cfg.GetInitialBalancePct(destTokenData.ChainID, destTokenData.Addr.Hex())
if err != nil {
return nil, fmt.Errorf("could not get initial pct: %w", err)
}
// calculate maintenance threshold relative to total balance
tokenName := originTokenData.Name
totalBalance := big.NewInt(0)
for _, tokenMap := range tokens {
for _, tokenData := range tokenMap {
if tokenData.Name == tokenName {
totalBalance.Add(totalBalance, tokenData.Balance)
}
}
}
maintenanceThresh, _ := new(big.Float).Mul(new(big.Float).SetInt(totalBalance), big.NewFloat(maintenancePct/100)).Int(nil)
if span != nil {
span.SetAttributes(attribute.Float64("maintenance_pct", maintenancePct))
span.SetAttributes(attribute.Float64("initial_pct", initialPct))
span.SetAttributes(attribute.String("max_token_balance", originTokenData.Balance.String()))
span.SetAttributes(attribute.String("min_token_balance", destTokenData.Balance.String()))
span.SetAttributes(attribute.String("total_balance", totalBalance.String()))
span.SetAttributes(attribute.String("maintenance_thresh", maintenanceThresh.String()))
}
// no need to rebalance if we are not below maintenance threshold on destination
if destTokenData.Balance.Cmp(maintenanceThresh) > 0 {
return nil, nil
}
// calculate the amount to rebalance vs the initial threshold on destination
initialThresh, _ := new(big.Float).Mul(new(big.Float).SetInt(totalBalance), big.NewFloat(initialPct/100)).Int(nil)
amount = new(big.Int).Sub(originTokenData.Balance, initialThresh)
// no need to rebalance since amount would not be positive
if amount.Cmp(big.NewInt(0)) <= 0 {
//nolint:nilnil
return nil, nil
}
// filter the rebalance amount by the configured min
minAmount := cfg.GetMinRebalanceAmount(originTokenData.ChainID, originTokenData.Addr)
if amount.Cmp(minAmount) < 0 {
// no need to rebalance
//nolint:nilnil
return nil, nil
}
// clip the rebalance amount by the configured max
maxAmount := cfg.GetMaxRebalanceAmount(originTokenData.ChainID, originTokenData.Addr)
if amount.Cmp(maxAmount) > 0 {
amount = maxAmount
}
if span != nil {
span.SetAttributes(
attribute.String("initial_thresh", initialThresh.String()),
attribute.String("rebalance_amount", amount.String()),
attribute.String("max_rebalance_amount", maxAmount.String()),
)
}
// make sure that the rebalance amount does not take origin below maintenance threshold
maintenancePctOrigin, err := cfg.GetMaintenanceBalancePct(originTokenData.ChainID, originTokenData.Addr.Hex())
if err != nil {
return nil, fmt.Errorf("could not get maintenance pct: %w", err)
}
maintenanceThreshOrigin, _ := new(big.Float).Mul(new(big.Float).SetInt(totalBalance), big.NewFloat(maintenancePctOrigin/100)).Int(nil)
newBalanceOrigin := new(big.Int).Sub(originTokenData.Balance, amount)
if newBalanceOrigin.Cmp(maintenanceThreshOrigin) < 0 {
if span != nil {
span.SetAttributes(
attribute.Float64("maintenance_pct_origin", maintenancePctOrigin),
attribute.String("maintenance_thresh_origin", maintenanceThreshOrigin.String()),
attribute.String("new_balance_origin", newBalanceOrigin.String()),
)
}
return nil, nil
}
return amount, nil
}