-
Notifications
You must be signed in to change notification settings - Fork 108
/
grpc_query_tss.go
106 lines (96 loc) · 3.19 KB
/
grpc_query_tss.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
package keeper
import (
"context"
"sort"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/zeta-chain/zetacore/pkg/chains"
"github.com/zeta-chain/zetacore/pkg/crypto"
"github.com/zeta-chain/zetacore/x/observer/types"
)
// TSS returns the tss address for the current tss only
func (k Keeper) TSS(c context.Context, req *types.QueryGetTSSRequest) (*types.QueryGetTSSResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
val, found := k.GetTSS(ctx)
if !found {
return nil, status.Error(codes.InvalidArgument, "not found")
}
return &types.QueryGetTSSResponse{TSS: val}, nil
}
// TssHistory Query historical list of TSS information
func (k Keeper) TssHistory(c context.Context, _ *types.QueryTssHistoryRequest) (*types.QueryTssHistoryResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
tssList := k.GetAllTSS(ctx)
sort.SliceStable(tssList, func(i, j int) bool {
return tssList[i].FinalizedZetaHeight < tssList[j].FinalizedZetaHeight
})
return &types.QueryTssHistoryResponse{TssList: tssList}, nil
}
func (k Keeper) GetTssAddress(
goCtx context.Context,
req *types.QueryGetTssAddressRequest,
) (*types.QueryGetTssAddressResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
tss, found := k.GetTSS(ctx)
if !found {
return nil, status.Error(codes.NotFound, "current tss not set")
}
ethAddress, err := crypto.GetTssAddrEVM(tss.TssPubkey)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
bitcoinParams := chains.BitcoinRegnetParams
if req.BitcoinChainId != 0 {
bitcoinParams, err = chains.BitcoinNetParamsFromChainID(req.BitcoinChainId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
}
btcAddress, err := crypto.GetTssAddrBTC(tss.TssPubkey, bitcoinParams)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryGetTssAddressResponse{
Eth: ethAddress.String(),
Btc: btcAddress,
}, nil
}
func (k Keeper) GetTssAddressByFinalizedHeight(
goCtx context.Context,
req *types.QueryGetTssAddressByFinalizedHeightRequest,
) (*types.QueryGetTssAddressByFinalizedHeightResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
tss, found := k.GetHistoricalTssByFinalizedHeight(ctx, req.FinalizedZetaHeight)
if !found {
return nil, status.Error(codes.NotFound, "tss not found")
}
ethAddress, err := crypto.GetTssAddrEVM(tss.TssPubkey)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
bitcoinParams := chains.BitcoinRegnetParams
if req.BitcoinChainId != 0 {
bitcoinParams, err = chains.BitcoinNetParamsFromChainID(req.BitcoinChainId)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
}
btcAddress, err := crypto.GetTssAddrBTC(tss.TssPubkey, bitcoinParams)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryGetTssAddressByFinalizedHeightResponse{
Eth: ethAddress.String(),
Btc: btcAddress,
}, nil
}