-
Notifications
You must be signed in to change notification settings - Fork 41
/
estimate_price.go
65 lines (57 loc) · 2.09 KB
/
estimate_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
package keeper
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
)
// Estimate the price : eg, 1 Eden -> x usdc
func (k Keeper) EstimatePrice(ctx sdk.Context, tokenInDenom, baseCurrency string) math.LegacyDec {
// Find a pool that can convert tokenIn to usdc
pool, found := k.GetBestPoolWithDenoms(ctx, []string{tokenInDenom, baseCurrency})
if !found {
return sdk.ZeroDec()
}
// Executes the swap in the pool and stores the output. Updates pool assets but
// does not actually transfer any tokens to or from the pool.
snapshot := k.GetPoolSnapshotOrSet(ctx, pool)
rate, err := pool.GetTokenARate(ctx, k.oracleKeeper, &snapshot, tokenInDenom, baseCurrency, k.accountedPoolKeeper)
if err != nil {
return sdk.ZeroDec()
}
return rate
}
func (k Keeper) GetEdenDenomPrice(ctx sdk.Context, baseCurrency string) math.LegacyDec {
// Calc ueden / uusdc rate
edenUsdcRate := k.EstimatePrice(ctx, ptypes.Elys, baseCurrency)
if edenUsdcRate.IsZero() {
edenUsdcRate = sdk.OneDec()
}
usdcDenomPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, baseCurrency)
if usdcDenomPrice.IsZero() {
usdcDecimal := int64(6)
usdcEntry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency)
if found {
usdcDecimal = int64(usdcEntry.Decimals)
}
usdcDenomPrice = sdk.NewDecWithPrec(1, usdcDecimal)
}
return edenUsdcRate.Mul(usdcDenomPrice)
}
func (k Keeper) GetTokenPrice(ctx sdk.Context, tokenInDenom, baseCurrency string) math.LegacyDec {
oraclePrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, tokenInDenom)
if !oraclePrice.IsZero() {
return oraclePrice
}
// Calc tokenIn / uusdc rate
tokenUsdcRate := k.EstimatePrice(ctx, tokenInDenom, baseCurrency)
usdcDenomPrice := k.oracleKeeper.GetAssetPriceFromDenom(ctx, baseCurrency)
if usdcDenomPrice.IsZero() {
usdcDecimal := int64(6)
usdcEntry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency)
if found {
usdcDecimal = int64(usdcEntry.Decimals)
}
usdcDenomPrice = sdk.NewDecWithPrec(1, usdcDecimal)
}
return tokenUsdcRate.Mul(usdcDenomPrice)
}