-
Notifications
You must be signed in to change notification settings - Fork 26
/
query.go
51 lines (42 loc) · 1.09 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
package helpers
import (
"context"
cosmossdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
"github.com/hyperledger-labs/yui-relayer/core"
)
// QueryBalance is a helper function for query balance
func QueryBalance(chain *core.ProvableChain, height ibcexported.Height, address sdk.AccAddress, showDenoms bool) (sdk.Coins, error) {
ctx := core.NewQueryContext(context.TODO(), height)
coins, err := chain.QueryBalance(ctx, address)
if err != nil {
return nil, err
}
if showDenoms {
return coins, nil
}
dts, err := chain.QueryDenomTraces(ctx, 0, 1000)
if err != nil {
return nil, err
}
if len(dts.DenomTraces) == 0 {
return coins, nil
}
var out sdk.Coins
for _, c := range coins {
if c.Amount.Equal(cosmossdkmath.NewInt(0)) {
continue
}
for i, d := range dts.DenomTraces {
if c.Denom == d.IBCDenom() {
out = append(out, sdk.Coin{Denom: d.GetFullDenomPath(), Amount: c.Amount})
break
}
if i == len(dts.DenomTraces)-1 {
out = append(out, c)
}
}
}
return out, nil
}