-
Notifications
You must be signed in to change notification settings - Fork 4
/
grpc_query.go
98 lines (75 loc) · 2.94 KB
/
grpc_query.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
package keeper
import (
"context"
"github.com/MinterTeam/mhub2/module/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
const gweiInEth = 1e9
var _ types.QueryServer = Keeper{}
func (k Keeper) Holders(context context.Context, _ *types.QueryHoldersRequest) (*types.QueryHoldersResponse, error) {
ctx := sdk.UnwrapSDKContext(context)
return &types.QueryHoldersResponse{Holders: k.GetHolders(ctx)}, nil
}
func (k Keeper) Prices(context context.Context, _ *types.QueryPricesRequest) (*types.QueryPricesResponse, error) {
ctx := sdk.UnwrapSDKContext(context)
return &types.QueryPricesResponse{Prices: k.GetPrices(ctx)}, nil
}
func (k Keeper) CurrentEpoch(context context.Context, _ *types.QueryCurrentEpochRequest) (*types.QueryCurrentEpochResponse, error) {
ctx := sdk.UnwrapSDKContext(context)
currentEpoch := types.Epoch{
Nonce: k.GetCurrentEpoch(ctx),
}
att := k.GetAttestation(ctx, currentEpoch.Nonce, &types.MsgPriceClaim{})
votes := att.GetVotes()
for _, valaddr := range votes {
validator, _ := sdk.ValAddressFromBech32(valaddr)
oracle := sdk.AccAddress(validator).String()
priceClaim := k.GetPriceClaim(ctx, oracle, currentEpoch.Nonce)
var priceClaimResponse *types.MsgPriceClaim
if priceClaim != nil {
priceClaimResponse = priceClaim.(*types.GenericClaim).GetPriceClaim()
}
holdersClaim := k.GetHoldersClaim(ctx, oracle, currentEpoch.Nonce)
var holdersClaimResponse *types.MsgHoldersClaim
if holdersClaim != nil {
holdersClaimResponse = holdersClaim.(*types.GenericClaim).GetHoldersClaim()
}
currentEpoch.Votes = append(currentEpoch.Votes, &types.Vote{
Oracle: oracle,
PriceClaim: priceClaimResponse,
HoldersClaim: holdersClaimResponse,
})
}
return &types.QueryCurrentEpochResponse{Epoch: ¤tEpoch}, nil
}
func (k Keeper) EthFee(context context.Context, _ *types.QueryEthFeeRequest) (*types.QueryEthFeeResponse, error) {
ctx := sdk.UnwrapSDKContext(context)
gasPrice, err := k.GetTokenPrice(ctx, "ethereum/gas")
if err != nil {
return nil, sdkerrors.Wrap(err, "gas price")
}
ethPrice, err := k.GetTokenPrice(ctx, "eth")
if err != nil {
return nil, sdkerrors.Wrap(err, "eth price")
}
return &types.QueryEthFeeResponse{
Min: gasPrice.Mul(ethPrice).MulInt64(150000).QuoInt64(gweiInEth),
Fast: gasPrice.Mul(ethPrice).MulInt64(300000).QuoInt64(gweiInEth),
}, nil
}
func (k Keeper) BscFee(context context.Context, _ *types.QueryBscFeeRequest) (*types.QueryBscFeeResponse, error) {
ctx := sdk.UnwrapSDKContext(context)
gasPrice, err := k.GetTokenPrice(ctx, "bsc/gas")
if err != nil {
return nil, sdkerrors.Wrap(err, "gas price")
}
bnbPrice, err := k.GetTokenPrice(ctx, "bnb")
if err != nil {
return nil, sdkerrors.Wrap(err, "bnb price")
}
return &types.QueryBscFeeResponse{
Min: gasPrice.Mul(bnbPrice).MulInt64(100000).QuoInt64(gweiInEth),
Fast: gasPrice.Mul(bnbPrice).MulInt64(200000).QuoInt64(gweiInEth),
}, nil
}