forked from infrared-dao/protocols
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasabi.go
More file actions
223 lines (189 loc) · 5.56 KB
/
wasabi.go
File metadata and controls
223 lines (189 loc) · 5.56 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
package protocols
import (
"context"
"encoding/json"
"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 = &WasabiLPPriceProvider{}
type WasabiConfig struct {
Token0 string `json:"token0"`
LPTDecimals uint `json:"lpt_decimals"`
}
// WasabiLPPriceProvider defines the provider for Wasabi Token price and TVL.
type WasabiLPPriceProvider struct {
address common.Address
block *big.Int
priceMap map[string]Price
logger zerolog.Logger
configBytes []byte
config *WasabiConfig
contract *sc.ERC4626
}
// NewWasabiLPPriceProvider creates a new instance of the WasabiLPPriceProvider.
func NewWasabiLPPriceProvider(
address common.Address,
block *big.Int,
prices map[string]Price,
logger zerolog.Logger,
config []byte,
) *WasabiLPPriceProvider {
w := &WasabiLPPriceProvider{
address: address,
block: block,
priceMap: prices,
logger: logger,
configBytes: config,
}
return w
}
// Initialize checks the configuration/data provided and instantiates the Wasabi smart contract.
func (w *WasabiLPPriceProvider) Initialize(ctx context.Context, client *ethclient.Client) error {
var err error
w.config = &WasabiConfig{}
err = json.Unmarshal(w.configBytes, w.config)
if err != nil {
w.logger.Error().Err(err).Msg("failed to deserialize config")
return err
}
_, ok := w.priceMap[w.config.Token0]
if !ok {
err = fmt.Errorf("no price data found for token0 (%s)", w.config.Token0)
w.logger.Error().Msg(err.Error())
return err
}
w.contract, err = sc.NewERC4626(w.address, client)
if err != nil {
w.logger.Error().Err(err).Msg("failed to instantiate Wasabi smart contract")
return err
}
return nil
}
func (w *WasabiLPPriceProvider) LPTokenPrice(ctx context.Context) (string, error) {
ts, err := w.getTotalSupply(ctx)
if err != nil {
return "", err
}
if ts.Cmp(big.NewInt(0)) == 0 {
err = fmt.Errorf("total supply is zero")
w.logger.Error().Err(err).Msg("failed to fetch total supply")
return "", err
}
tvl, err := w.tvl(ctx)
if err != nil {
return "", err
}
tsd := NormalizeAmount(ts, w.config.LPTDecimals)
price := tvl.Div(tsd)
w.logger.Debug().
Str("totalValue", tvl.String()).
Str("totalSupply", ts.String()).
Str("pricePerToken", price.String()).
Msg("LP token price calculated successfully")
return price.StringFixed(roundingDecimals), nil
}
func (w *WasabiLPPriceProvider) TVL(ctx context.Context) (string, error) {
totalValue, err := w.tvl(ctx)
if err != nil {
return "", err
}
w.logger.Debug().Str("tvl", totalValue.String()).Msg("successfully fetched TVL")
return totalValue.StringFixed(roundingDecimals), nil
}
func (w *WasabiLPPriceProvider) GetConfig(ctx context.Context, address string, ethClient *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.NewERC4626(common.HexToAddress(address), ethClient)
if err != nil {
err = fmt.Errorf("failed to instantiate Wasabi smart contract, %v", err)
return nil, err
}
wc := &WasabiConfig{}
opts := &bind.CallOpts{
Context: ctx,
}
addr, err := contract.Asset(opts)
if err != nil {
err = fmt.Errorf("failed to fetch asset address, %v", err)
return nil, err
}
wc.Token0 = strings.ToLower(addr.Hex())
decimals, err := contract.Decimals(opts)
if err != nil {
err = fmt.Errorf("failed to fetch decimals, %v", err)
return nil, err
}
wc.LPTDecimals = uint(decimals)
body, err := json.Marshal(wc)
if err != nil {
return nil, err
}
return body, nil
}
func (w *WasabiLPPriceProvider) UpdateBlock(block *big.Int, prices map[string]Price) {
w.block = block
if prices != nil {
w.priceMap = prices
}
}
// Internal Helper methods not able to be called except in this file
// tvl fetches the TVL from the Wasabi smart contract.
func (w *WasabiLPPriceProvider) tvl(ctx context.Context) (decimal.Decimal, error) {
wTokenAmount, err := w.getUnderlyingBalances(ctx)
if err != nil {
w.logger.Error().Err(err).Msg("failed to fetch wToken amount")
return decimal.Zero, err
}
wTokenPrice, err := w.getPrice(w.config.Token0)
if err != nil {
return decimal.Zero, err
}
wTokenAmountDecimal := NormalizeAmount(wTokenAmount, wTokenPrice.Decimals)
tvl := wTokenAmountDecimal.Mul(wTokenPrice.Price)
return tvl, nil
}
// getPrice fetches the price of the token from the price map.
func (w *WasabiLPPriceProvider) getPrice(tokenKey string) (*Price, error) {
price, ok := w.priceMap[tokenKey]
if !ok {
err := fmt.Errorf("no price data found for token (%s)", tokenKey)
w.logger.Error().Msg(err.Error())
return nil, err
}
return &price, nil
}
// getTotalSupply fetches the total supply of the LP token.
func (w *WasabiLPPriceProvider) getTotalSupply(ctx context.Context) (*big.Int, error) {
opts := &bind.CallOpts{
Context: ctx,
BlockNumber: w.block,
}
totalSupply, err := w.contract.TotalSupply(opts)
if err != nil {
w.logger.Error().Err(err).Msg("failed to fetch total supply")
return nil, err
}
return totalSupply, nil
}
func (w *WasabiLPPriceProvider) getUnderlyingBalances(ctx context.Context) (*big.Int, error) {
opts := &bind.CallOpts{
Context: ctx,
BlockNumber: w.block,
}
amount0, err := w.contract.TotalAssets(opts)
if err != nil {
w.logger.Error().Err(err).Msg("failed to fetch total assets")
return nil, err
}
return amount0, nil
}