/
queries.go
146 lines (120 loc) · 4.83 KB
/
queries.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
package wasmbinding
import (
"fmt"
"time"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/osmosis-labs/osmosis/v13/wasmbinding/bindings"
gammkeeper "github.com/osmosis-labs/osmosis/v13/x/gamm/keeper"
gammtypes "github.com/osmosis-labs/osmosis/v13/x/gamm/types"
tokenfactorykeeper "github.com/osmosis-labs/osmosis/v13/x/tokenfactory/keeper"
twapkeeper "github.com/osmosis-labs/osmosis/v13/x/twap"
)
type QueryPlugin struct {
gammKeeper *gammkeeper.Keeper
twapKeeper *twapkeeper.Keeper
tokenFactoryKeeper *tokenfactorykeeper.Keeper
}
// NewQueryPlugin returns a reference to a new QueryPlugin.
func NewQueryPlugin(gk *gammkeeper.Keeper, tk *twapkeeper.Keeper, tfk *tokenfactorykeeper.Keeper) *QueryPlugin {
return &QueryPlugin{
gammKeeper: gk,
twapKeeper: tk,
tokenFactoryKeeper: tfk,
}
}
// GetDenomAdmin is a query to get denom admin.
func (qp QueryPlugin) GetDenomAdmin(ctx sdk.Context, denom string) (*bindings.DenomAdminResponse, error) {
metadata, err := qp.tokenFactoryKeeper.GetAuthorityMetadata(ctx, denom)
if err != nil {
return nil, fmt.Errorf("failed to get admin for denom: %s", denom)
}
return &bindings.DenomAdminResponse{Admin: metadata.Admin}, nil
}
// GetPoolState is a query to get pool liquidity and amount of each denoms' pool shares.
func (qp QueryPlugin) GetPoolState(ctx sdk.Context, poolID uint64) (*bindings.PoolAssets, error) {
poolData, err := qp.gammKeeper.GetPoolAndPoke(ctx, poolID)
if err != nil {
return nil, sdkerrors.Wrap(err, "gamm get pool")
}
return &bindings.PoolAssets{
Assets: poolData.GetTotalPoolLiquidity(ctx),
Shares: sdk.Coin{
Denom: gammtypes.GetPoolShareDenom(poolID),
Amount: poolData.GetTotalShares(),
},
}, nil
}
// GetSpotPrice is a query to get spot price of denoms.
func (qp QueryPlugin) GetSpotPrice(ctx sdk.Context, spotPrice *bindings.SpotPrice) (*sdk.Dec, error) {
if spotPrice == nil {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm spot price null"}
}
poolId := spotPrice.Swap.PoolId
denomIn := spotPrice.Swap.DenomIn
denomOut := spotPrice.Swap.DenomOut
withSwapFee := spotPrice.WithSwapFee
price, err := qp.gammKeeper.CalculateSpotPrice(ctx, poolId, denomIn, denomOut)
if err != nil {
return nil, sdkerrors.Wrap(err, "gamm get spot price")
}
if withSwapFee {
poolData, err := qp.gammKeeper.GetPoolAndPoke(ctx, poolId)
if err != nil {
return nil, sdkerrors.Wrap(err, "gamm get pool")
}
price = price.Mul(sdk.OneDec().Sub(poolData.GetSwapFee(ctx)))
}
return &price, nil
}
// EstimateSwap validates each denom (in / out) and performs a swap.
func (qp QueryPlugin) EstimateSwap(ctx sdk.Context, estimateSwap *bindings.EstimateSwap) (*bindings.SwapAmount, error) {
if estimateSwap == nil {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm estimate swap null"}
}
if err := sdk.ValidateDenom(estimateSwap.First.DenomIn); err != nil {
return nil, sdkerrors.Wrap(err, "gamm estimate swap denom in")
}
if err := sdk.ValidateDenom(estimateSwap.First.DenomOut); err != nil {
return nil, sdkerrors.Wrap(err, "gamm estimate swap denom out")
}
senderAddr, err := sdk.AccAddressFromBech32(estimateSwap.Sender)
if err != nil {
return nil, sdkerrors.Wrap(err, "gamm estimate swap sender address")
}
if estimateSwap.Amount == (bindings.SwapAmount{}) {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm estimate swap empty swap"}
}
estimate, err := PerformSwap(qp.gammKeeper, ctx, senderAddr, estimateSwap.ToSwapMsg())
return estimate, err
}
func (qp QueryPlugin) ArithmeticTwap(ctx sdk.Context, arithmeticTwap *bindings.ArithmeticTwap) (*sdk.Dec, error) {
if arithmeticTwap == nil {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm arithmetic twap null"}
}
poolId := arithmeticTwap.PoolId
quoteAssetDenom := arithmeticTwap.QuoteAssetDenom
baseAssetDenom := arithmeticTwap.BaseAssetDenom
startTime := time.UnixMilli(arithmeticTwap.StartTime)
endTime := time.UnixMilli(arithmeticTwap.EndTime)
twap, err := qp.twapKeeper.GetArithmeticTwap(ctx, poolId, baseAssetDenom, quoteAssetDenom, startTime, endTime)
if err != nil {
return nil, sdkerrors.Wrap(err, "gamm arithmetic twap")
}
return &twap, nil
}
func (qp QueryPlugin) ArithmeticTwapToNow(ctx sdk.Context, arithmeticTwap *bindings.ArithmeticTwapToNow) (*sdk.Dec, error) {
if arithmeticTwap == nil {
return nil, wasmvmtypes.InvalidRequest{Err: "gamm arithmetic twap null"}
}
poolId := arithmeticTwap.PoolId
quoteAssetDenom := arithmeticTwap.QuoteAssetDenom
baseAssetDenom := arithmeticTwap.BaseAssetDenom
startTime := time.UnixMilli(arithmeticTwap.StartTime)
twap, err := qp.twapKeeper.GetArithmeticTwapToNow(ctx, poolId, baseAssetDenom, quoteAssetDenom, startTime)
if err != nil {
return nil, sdkerrors.Wrap(err, "gamm arithmetic twap")
}
return &twap, nil
}