forked from infrared-dao/protocols
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulla.go
More file actions
275 lines (229 loc) · 7.54 KB
/
bulla.go
File metadata and controls
275 lines (229 loc) · 7.54 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package protocols
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/infrared-dao/protocols/internal/sc"
"github.com/rs/zerolog"
"github.com/shopspring/decimal"
)
var _ Protocol = &BullaLPPriceProvider{}
// The bulla abi given is a contract type called hypervisor which manages automated pools
// For other non-automated pools we would need a slightly different adapter because the function
// getPoolBalances wouldn't exist on those contract types, we could generalize to work across
// all pools by looking at the reserves instead which has a helper call on the base Algrabra Pool
// BullaConfig defines the configuration for Bulla Exchange adapter
type BullaConfig struct {
Token0 string `json:"token0"`
Token1 string `json:"token1"`
LPTDecimals uint `json:"lpt_decimals"`
// Add any other Bulla-specific configuration fields here
}
// BullaLPPriceProvider defines the provider for Bulla Exchange LP price and TVL.
type BullaLPPriceProvider struct {
address common.Address
block *big.Int
priceMap map[string]Price
logger zerolog.Logger
configBytes []byte
config *BullaConfig
contract *sc.Bulla
// Add any Bulla-specific contract interfaces here
}
// NewBullaLPPriceProvider creates a new instance of the BullaLPPriceProvider.
func NewBullaLPPriceProvider(
address common.Address,
block *big.Int,
prices map[string]Price,
logger zerolog.Logger,
config []byte,
) *BullaLPPriceProvider {
b := &BullaLPPriceProvider{
address: address,
block: block,
priceMap: prices,
logger: logger,
configBytes: config,
}
return b
}
// Initialize checks the configuration/data provided and instantiates the Bulla smart contract.
func (b *BullaLPPriceProvider) Initialize(ctx context.Context, client *ethclient.Client) error {
var err error
b.config = &BullaConfig{}
err = json.Unmarshal(b.configBytes, b.config)
if err != nil {
b.logger.Error().Err(err).Msg("failed to deserialize config")
return err
}
// Validate that we have price data for the tokens
_, ok := b.priceMap[b.config.Token0]
if !ok {
err = fmt.Errorf("no price data found for token0 (%s)", b.config.Token0)
b.logger.Error().Msg(err.Error())
return err
}
_, ok = b.priceMap[b.config.Token1]
if !ok {
err = fmt.Errorf("no price data found for token1 (%s)", b.config.Token1)
b.logger.Error().Msg(err.Error())
return err
}
// Initialize Bulla contract
b.contract, err = sc.NewBulla(b.address, client)
if err != nil {
b.logger.Error().Err(err).Msg("failed to instantiate Bulla smart contract")
return err
}
return nil
}
// LPTokenPrice returns the current price of the protocol's LP token in USD.
func (b *BullaLPPriceProvider) LPTokenPrice(ctx context.Context) (string, error) {
// Fetch total supply
totalSupply, err := b.getTotalSupply(ctx)
if err != nil {
return "", err
}
// Avoid division by zero
if totalSupply.Sign() == 0 {
err := errors.New("totalSupply is zero, cannot calculate LP token price")
b.logger.Error().Err(err).Msg("Invalid totalSupply")
return "", err
}
totalValue, err := b.totalValue(ctx)
if err != nil {
return "", err
}
totalSupplyDecimal := NormalizeAmount(totalSupply, b.config.LPTDecimals)
pricePerToken := totalValue.Div(totalSupplyDecimal)
b.logger.Debug().
Str("totalValue", totalValue.String()).
Str("totalSupply", totalSupplyDecimal.String()).
Str("pricePerToken", pricePerToken.String()).
Msg("LP token price calculated successfully")
return pricePerToken.StringFixed(roundingDecimals), nil
}
// TVL returns the Total Value Locked in the protocol in USD.
func (b *BullaLPPriceProvider) TVL(ctx context.Context) (string, error) {
totalValue, err := b.totalValue(ctx)
if err != nil {
return "", err
}
b.logger.Debug().
Str("totalValue", totalValue.String()).
Msg("TVL calculated successfully")
return totalValue.StringFixed(roundingDecimals), nil
}
// GetConfig returns the configuration for the Bulla Exchange adapter.
func (b *BullaLPPriceProvider) GetConfig(ctx context.Context, address string, client *ethclient.Client) ([]byte, error) {
var err error
if !common.IsHexAddress(address) {
err = fmt.Errorf("invalid smart contract address, '%s'", address)
return nil, err
}
contract, err := sc.NewBulla(common.HexToAddress(address), client)
if err != nil {
err = fmt.Errorf("failed to instantiate Bulla smart contract, %v", err)
return nil, err
}
bc := BullaConfig{}
opts := &bind.CallOpts{
Context: ctx,
}
// Token0
addr, err := contract.Token0(opts)
if err != nil {
err = fmt.Errorf("failed to obtain token0 address for Bulla pool %s, %v", address, err)
return nil, err
}
bc.Token0 = strings.ToLower(addr.Hex())
// Token1
addr, err = contract.Token1(opts)
if err != nil {
err = fmt.Errorf("failed to obtain token1 address for Bulla pool %s, %v", address, err)
return nil, err
}
bc.Token1 = strings.ToLower(addr.Hex())
// Decimals
decimals, err := contract.Decimals(opts)
if err != nil {
err = fmt.Errorf("failed to obtain number of decimals for LP token %s, %v", address, err)
return nil, err
}
bc.LPTDecimals = uint(decimals)
body, err := json.Marshal(bc)
if err != nil {
return nil, err
}
return body, nil
}
func (b *BullaLPPriceProvider) UpdateBlock(block *big.Int, prices map[string]Price) {
b.block = block
if prices != nil {
b.priceMap = prices
}
}
// Internal Helper methods not able to be called except in this file
// totalValue calculates the total value of assets in the pool
func (b *BullaLPPriceProvider) totalValue(ctx context.Context) (decimal.Decimal, error) {
total0, total1, err := b.getPoolBalances(ctx)
if err != nil {
return decimal.Zero, err
}
price0, err := b.getPrice(b.config.Token0)
if err != nil {
return decimal.Zero, err
}
price1, err := b.getPrice(b.config.Token1)
if err != nil {
return decimal.Zero, err
}
amount0Decimal := NormalizeAmount(total0, price0.Decimals)
amount1Decimal := NormalizeAmount(total1, price1.Decimals)
value0 := amount0Decimal.Mul(price0.Price)
value1 := amount1Decimal.Mul(price1.Price)
totalValue := value0.Add(value1)
return totalValue, nil
}
// getPrice retrieves the price for a given token
func (b *BullaLPPriceProvider) getPrice(tokenKey string) (*Price, error) {
price, ok := b.priceMap[tokenKey]
if !ok {
err := fmt.Errorf("no price data found for token (%s)", tokenKey)
b.logger.Error().Msg(err.Error())
return nil, err
}
return &price, nil
}
// getTotalSupply fetches the total supply of the LP token
func (b *BullaLPPriceProvider) getTotalSupply(ctx context.Context) (*big.Int, error) {
opts := &bind.CallOpts{
Context: ctx,
BlockNumber: b.block,
}
totalSupply, err := b.contract.TotalSupply(opts)
if err != nil {
b.logger.Error().Msgf("failed to obtain total supply for Bulla pool %s, %v", b.address.String(), err)
return nil, fmt.Errorf("failed to get Bulla total supply, err: %w", err)
}
return totalSupply, nil
}
// getPoolBalances fetches the base and quote token balances in the pool
func (b *BullaLPPriceProvider) getPoolBalances(ctx context.Context) (*big.Int, *big.Int, error) {
opts := &bind.CallOpts{
Context: ctx,
BlockNumber: b.block,
}
totalAmounts, err := b.contract.GetTotalAmounts(opts)
if err != nil {
b.logger.Error().Msgf("failed to obtain total amounts for Bulla pool %s, %v", b.address.String(), err)
return nil, nil, fmt.Errorf("failed to get Bulla total amounts, err: %w", err)
}
return totalAmounts.Total0, totalAmounts.Total1, nil
}