-
Notifications
You must be signed in to change notification settings - Fork 571
/
twap_price.go
90 lines (77 loc) · 2.83 KB
/
twap_price.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
package keeper
import (
"github.com/cosmos/gogoproto/proto"
"github.com/osmosis-labs/osmosis/osmomath"
gammtypes "github.com/osmosis-labs/osmosis/v22/x/gamm/types"
"github.com/osmosis-labs/osmosis/v22/x/superfluid/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// This function calculates the osmo equivalent worth of an LP share.
// It is intended to eventually use the TWAP of the worth of an LP share
// once that is exposed from the gamm module.
func (k Keeper) calculateOsmoBackingPerShare(pool gammtypes.CFMMPoolI, osmoInPool osmomath.Int) osmomath.Dec {
twap := osmoInPool.ToLegacyDec().Quo(pool.GetTotalShares().ToLegacyDec())
return twap
}
func (k Keeper) SetOsmoEquivalentMultiplier(ctx sdk.Context, epoch int64, denom string, multiplier osmomath.Dec) {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.KeyPrefixTokenMultiplier)
priceRecord := types.OsmoEquivalentMultiplierRecord{
EpochNumber: epoch,
Denom: denom,
Multiplier: multiplier,
}
bz, err := proto.Marshal(&priceRecord)
if err != nil {
panic(err)
}
prefixStore.Set([]byte(denom), bz)
}
func (k Keeper) GetSuperfluidOSMOTokens(ctx sdk.Context, denom string, amount osmomath.Int) (osmomath.Int, error) {
multiplier := k.GetOsmoEquivalentMultiplier(ctx, denom)
if multiplier.IsZero() {
return osmomath.ZeroInt(), nil
}
decAmt := multiplier.Mul(amount.ToLegacyDec())
_, err := k.GetSuperfluidAsset(ctx, denom)
if err != nil {
return osmomath.ZeroInt(), err
}
return k.GetRiskAdjustedOsmoValue(ctx, decAmt.RoundInt()), nil
}
func (k Keeper) DeleteOsmoEquivalentMultiplier(ctx sdk.Context, denom string) {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.KeyPrefixTokenMultiplier)
prefixStore.Delete([]byte(denom))
}
func (k Keeper) GetOsmoEquivalentMultiplier(ctx sdk.Context, denom string) osmomath.Dec {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.KeyPrefixTokenMultiplier)
bz := prefixStore.Get([]byte(denom))
if bz == nil {
return osmomath.ZeroDec()
}
priceRecord := types.OsmoEquivalentMultiplierRecord{}
err := proto.Unmarshal(bz, &priceRecord)
if err != nil {
panic(err)
}
return priceRecord.Multiplier
}
func (k Keeper) GetAllOsmoEquivalentMultipliers(ctx sdk.Context) []types.OsmoEquivalentMultiplierRecord {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, types.KeyPrefixTokenMultiplier)
iterator := prefixStore.Iterator(nil, nil)
defer iterator.Close()
priceRecords := []types.OsmoEquivalentMultiplierRecord{}
for ; iterator.Valid(); iterator.Next() {
priceRecord := types.OsmoEquivalentMultiplierRecord{}
err := proto.Unmarshal(iterator.Value(), &priceRecord)
if err != nil {
panic(err)
}
priceRecords = append(priceRecords, priceRecord)
}
return priceRecords
}