-
Notifications
You must be signed in to change notification settings - Fork 16
/
query.go
118 lines (96 loc) · 3.28 KB
/
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package e2e
import (
"errors"
"fmt"
"strings"
sdkmath "cosmossdk.io/math"
tenderminttypes "github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
evmtypes "github.com/settlus/chain/evmos/x/evm/types"
settlementtypes "github.com/settlus/chain/x/settlement/types"
)
// if coin is zero, return empty coin.
func getSpecificBalance(endpoint, addr, denom string) (amt sdk.Coin, err error) {
balances, err := querySettlusAllBalances(endpoint, addr)
if err != nil {
return amt, err
}
for _, c := range balances {
if strings.Contains(c.Denom, denom) {
amt = c
break
}
}
return amt, nil
}
func getEthBalance(endpoint, addr string) (uint64, error) {
body, err := httpGet(fmt.Sprintf("%s/evmos/evm/v1/balances/%s", endpoint, addr))
if err != nil {
return 0, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var res evmtypes.QueryBalanceResponse
if err := cdc.UnmarshalJSON(body, &res); err != nil {
return 0, err
}
val, ok := sdkmath.NewIntFromString(res.Balance)
if !ok {
return 0, errors.New("invalid balance")
}
return val.Uint64(), nil
}
func queryTenants(endpoint string) ([]settlementtypes.TenantWithTreasury, error) {
body, err := httpGet(fmt.Sprintf("%s/settlus/settlement/tenants", endpoint))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var tenantsResp settlementtypes.QueryTenantsResponse
if err := cdc.UnmarshalJSON(body, &tenantsResp); err != nil {
return nil, err
}
return tenantsResp.Tenants, nil
}
func queryTenant(endpoint string, tenantId uint64) (*settlementtypes.TenantWithTreasury, error) {
body, err := httpGet(fmt.Sprintf("%s/settlus/settlement/tenant/%d", endpoint, tenantId))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var tenantResp settlementtypes.QueryTenantResponse
if err := cdc.UnmarshalJSON(body, &tenantResp); err != nil {
return nil, err
}
return &tenantResp.Tenant, nil
}
func queryUtxr(endpoint string, tenantId uint64, requestId string) (*settlementtypes.UTXR, error) {
body, err := httpGet(fmt.Sprintf("%s/settlus/settlement/utxr/%d/%s", endpoint, tenantId, requestId))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var utxrResp settlementtypes.QueryUTXRResponse
if err := cdc.UnmarshalJSON(body, &utxrResp); err != nil {
return nil, err
}
return &utxrResp.Utxr, nil
}
func querySettlusAllBalances(endpoint, addr string) (sdk.Coins, error) {
body, err := httpGet(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", endpoint, addr))
if err != nil {
return nil, fmt.Errorf("failed to execute HTTP request: %w", err)
}
var balancesResp banktypes.QueryAllBalancesResponse
if err := cdc.UnmarshalJSON(body, &balancesResp); err != nil {
return nil, err
}
return balancesResp.Balances, nil
}
func queryLatestBlockId(endpoint string) (string, error) {
body, err := httpGet(fmt.Sprintf("%s/cosmos/base/tendermint/v1beta1/blocks/latest", endpoint))
if err != nil {
return "", fmt.Errorf("failed to execute HTTP request: %w", err)
}
var blockResp tenderminttypes.GetLatestBlockResponse
if err := cdc.UnmarshalJSON(body, &blockResp); err != nil {
return "", err
}
return blockResp.BlockId.String(), nil
}