-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add cosmos_wallet_balance metric
- Loading branch information
Showing
9 changed files
with
199 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package account | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/math" | ||
"go.uber.org/zap" | ||
|
||
"github.com/archway-network/relayer_exporter/pkg/chain" | ||
log "github.com/archway-network/relayer_exporter/pkg/logger" | ||
) | ||
|
||
type Account struct { | ||
Address string `yaml:"address"` | ||
Denom string `yaml:"denom"` | ||
ChainID string `yaml:"chainId"` | ||
Balance math.Int | ||
} | ||
|
||
func GetAccountsBalances(accounts []Account, rpcs map[string]string) []Account { | ||
num := len(accounts) | ||
|
||
out := make(chan Account, num) | ||
defer close(out) | ||
|
||
for i := 0; i < num; i++ { | ||
go func(i int) { | ||
account, err := GetAccountBalance(accounts[i], rpcs) | ||
if err != nil { | ||
out <- Account{} | ||
|
||
log.Error(err.Error(), zap.Any("account", accounts[i])) | ||
|
||
return | ||
} | ||
out <- account | ||
}(i) | ||
} | ||
|
||
accountsWithBalance := []Account{} | ||
|
||
for i := 0; i < num; i++ { | ||
account := <-out | ||
if account.Address != "" { | ||
accountsWithBalance = append(accountsWithBalance, account) | ||
} | ||
} | ||
|
||
return accountsWithBalance | ||
} | ||
|
||
func GetAccountBalance(account Account, rpcs map[string]string) (Account, error) { | ||
chain, err := chain.PrepChain(chain.Info{ | ||
ChainID: account.ChainID, | ||
RPCAddr: rpcs[account.ChainID], | ||
}) | ||
if err != nil { | ||
return Account{}, err | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
coins, err := chain.ChainProvider.QueryBalanceWithAddress(ctx, account.Address) | ||
if err != nil { | ||
return Account{}, err | ||
} | ||
|
||
account.Balance = coins.AmountOf(account.Denom) | ||
|
||
return account, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package chain | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cosmos/relayer/v2/relayer" | ||
"github.com/cosmos/relayer/v2/relayer/chains/cosmos" | ||
) | ||
|
||
const ( | ||
rpcTimeout = "10s" | ||
keyringBackend = "test" | ||
) | ||
|
||
type Info struct { | ||
ChainID string | ||
RPCAddr string | ||
ClientID string | ||
} | ||
|
||
func PrepChain(cd Info) (*relayer.Chain, error) { | ||
chain := relayer.Chain{} | ||
providerConfig := cosmos.CosmosProviderConfig{ | ||
ChainID: cd.ChainID, | ||
Timeout: rpcTimeout, | ||
KeyringBackend: keyringBackend, | ||
RPCAddr: cd.RPCAddr, | ||
} | ||
|
||
provider, err := providerConfig.NewProvider(nil, "", false, cd.ChainID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = provider.Init(context.Background()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
chain.ChainProvider = provider | ||
|
||
err = chain.SetPath(&relayer.PathEnd{ClientID: cd.ClientID}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &chain, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters