-
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 (#12)
* feat: Add cosmos_wallet_balance metric * refactor: Rename variable name * refactor: Rename function name * refactor: make GetBalance a method for Account struct * chore: Simplify code after review * fix: Use accounts instead account for collection
- Loading branch information
Showing
9 changed files
with
191 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,46 @@ | ||
package account | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/math" | ||
|
||
"github.com/archway-network/relayer_exporter/pkg/chain" | ||
) | ||
|
||
const ( | ||
successStatus = "success" | ||
errorStatus = "error" | ||
) | ||
|
||
type Account struct { | ||
Address string `yaml:"address"` | ||
Denom string `yaml:"denom"` | ||
ChainID string `yaml:"chainId"` | ||
Balance math.Int | ||
Status string | ||
} | ||
|
||
func (a *Account) GetBalance(rpcs map[string]string) error { | ||
chain, err := chain.PrepChain(chain.Info{ | ||
ChainID: a.ChainID, | ||
RPCAddr: rpcs[a.ChainID], | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
coins, err := chain.ChainProvider.QueryBalanceWithAddress(ctx, a.Address) | ||
if err != nil { | ||
a.Status = errorStatus | ||
|
||
return err | ||
} | ||
|
||
a.Balance = coins.AmountOf(a.Denom) | ||
a.Status = successStatus | ||
|
||
return 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(info Info) (*relayer.Chain, error) { | ||
chain := relayer.Chain{} | ||
providerConfig := cosmos.CosmosProviderConfig{ | ||
ChainID: info.ChainID, | ||
Timeout: rpcTimeout, | ||
KeyringBackend: keyringBackend, | ||
RPCAddr: info.RPCAddr, | ||
} | ||
|
||
provider, err := providerConfig.NewProvider(nil, "", false, info.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: info.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