From 87a1dc9b1e1f340cfe30f4198ce4df0b417026e1 Mon Sep 17 00:00:00 2001 From: Tomasz Nowak Date: Mon, 2 Oct 2023 09:09:14 +0200 Subject: [PATCH] fix: Use name instead id for chain name --- config.yaml | 55 +++++++++++++++++++++++++------------- pkg/account/account.go | 37 ------------------------- pkg/collector/collector.go | 16 +++++------ pkg/config/config.go | 48 ++++++++++++++++++++++++++------- pkg/config/config_test.go | 26 ++++++++++++------ pkg/ibc/ibc.go | 11 ++++---- 6 files changed, 107 insertions(+), 86 deletions(-) delete mode 100644 pkg/account/account.go diff --git a/config.yaml b/config.yaml index 7217136..193e8b0 100644 --- a/config.yaml +++ b/config.yaml @@ -1,38 +1,55 @@ --- rpc: - - chainId: archway-1 + - chainName: archway + chainId: archway-1 url: https://rpc.mainnet.archway.io:443 - - chainId: agoric-3 + - chainName: agoric + chainId: agoric-3 url: https://main.rpc.agoric.net:443 - - chainId: axelar-dojo-1 + - chainName: axelar + chainId: axelar-dojo-1 url: https://axelar-rpc.polkachu.com:443 - - chainId: axelar-testnet-lisbon-3 + - chainName: axelartestnet + chainId: axelar-testnet-lisbon-3 url: https://rpc-axelar-testnet.imperator.co:443 - - chainId: constantine-3 + - chainName: archwaytestnet + chainId: constantine-3 url: https://rpc.constantine.archway.tech:443 - - chainId: cosmoshub-4 + - chainName: bitcanna + chainId: bitcanna-1 + url: https://rpc.bitcanna.io:443 + - chainName: cosmoshub + chainId: cosmoshub-4 url: https://cosmoshub-rpc.stakely.io:443 - - chainId: jackal-1 + - chainName: jackal + chainId: jackal-1 url: https://jackal-rpc.polkachu.com:443 - - chainId: juno-1 + - chainName: juno + chainId: juno-1 url: https://juno-rpc.polkachu.com:443 - - chainId: kaiyo-1 + - chainName: kujira + chainId: kaiyo-1 url: https://kujira-rpc.polkachu.com:443 - - chainId: noble-1 + - chainName: noble + chainId: noble-1 url: https://noble-rpc.polkachu.com:443 - - chainId: nois-1 + - chainName: nois + chainId: nois-1 url: https://nois-rpc.polkachu.com:443 - - chainId: osmo-test-5 + - chainName: osmosistestnet + chainId: osmo-test-5 url: https://rpc.osmotest5.osmosis.zone:443 - - chainId: osmosis-1 + - chainName: osmosis + chainId: osmosis-1 url: https://osmosis-rpc.stakely.io:443 - - chainId: quicksilver-2 + - chainName: quicksilver + chainId: quicksilver-2 url: https://rpc.quicksilver.zone:443 - - chainId: sandbox-01 + - chainName: akashtestnet + chainId: sandbox-01 url: https://rpc.sandbox-01.aksh.pw:443 - - chainId: theta-testnet-001 - url: https://rpc.sentry-01.theta-testnet.polypore.xyz:443 - - chainId: umee-1 + - chainName: umee + chainId: umee-1 url: https://rpc-umee.mzonder.com:443 github: @@ -42,5 +59,5 @@ github: accounts: - address: archway1l2al7y78500h5akvgt8exwnkpmf2zmk8ky9ht3 - chainId: constantine-3 + chainName: archwaytestnet denom: aconst diff --git a/pkg/account/account.go b/pkg/account/account.go deleted file mode 100644 index 4c9518f..0000000 --- a/pkg/account/account.go +++ /dev/null @@ -1,37 +0,0 @@ -package account - -import ( - "context" - - "cosmossdk.io/math" - - "github.com/archway-network/relayer_exporter/pkg/chain" -) - -type Account struct { - Address string `yaml:"address"` - Denom string `yaml:"denom"` - ChainID string `yaml:"chainId"` - Balance math.Int -} - -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 { - return err - } - - a.Balance = coins.AmountOf(a.Denom) - - return nil -} diff --git a/pkg/collector/collector.go b/pkg/collector/collector.go index 85793b1..3c1f6a6 100644 --- a/pkg/collector/collector.go +++ b/pkg/collector/collector.go @@ -4,7 +4,7 @@ import ( "math/big" "sync" - "github.com/archway-network/relayer_exporter/pkg/account" + "github.com/archway-network/relayer_exporter/pkg/config" "github.com/archway-network/relayer_exporter/pkg/ibc" log "github.com/archway-network/relayer_exporter/pkg/logger" "github.com/cosmos/relayer/v2/relayer" @@ -33,13 +33,13 @@ var ( ) type IBCClientsCollector struct { - RPCs map[string]string + RPCs *map[string]config.RPC Paths []*relayer.IBCdata } type WalletBalanceCollector struct { - RPCs map[string]string - Accounts []account.Account + RPCs *map[string]config.RPC + Accounts []config.Account } func (cc IBCClientsCollector) Describe(ch chan<- *prometheus.Desc) { @@ -70,14 +70,14 @@ func (cc IBCClientsCollector) Collect(ch chan<- prometheus.Metric) { clientExpiry, prometheus.GaugeValue, float64(ci.ChainAClientExpiration.Unix()), - []string{path.Chain1.ChainName, path.Chain1.ClientID, path.Chain2.ChainName, status}..., + []string{(*cc.RPCs)[path.Chain1.ChainName].ChainID, path.Chain1.ClientID, (*cc.RPCs)[path.Chain2.ChainName].ChainID, status}..., ) ch <- prometheus.MustNewConstMetric( clientExpiry, prometheus.GaugeValue, float64(ci.ChainBClientExpiration.Unix()), - []string{path.Chain2.ChainName, path.Chain2.ClientID, path.Chain1.ChainName, status}..., + []string{(*cc.RPCs)[path.Chain2.ChainName].ChainID, path.Chain2.ClientID, (*cc.RPCs)[path.Chain1.ChainName].ChainID, status}..., ) }(p) } @@ -99,7 +99,7 @@ func (wb WalletBalanceCollector) Collect(ch chan<- prometheus.Metric) { for _, a := range wb.Accounts { wg.Add(1) - go func(account account.Account) { + go func(account config.Account) { defer wg.Done() balance := 0.0 @@ -119,7 +119,7 @@ func (wb WalletBalanceCollector) Collect(ch chan<- prometheus.Metric) { walletBalance, prometheus.GaugeValue, balance, - []string{account.Address, account.ChainID, account.Denom, status}..., + []string{account.Address, (*wb.RPCs)[account.ChainName].ChainID, account.Denom, status}..., ) }(a) } diff --git a/pkg/config/config.go b/pkg/config/config.go index 093a5a7..da0faa4 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -6,25 +6,34 @@ import ( "os" "strings" + "cosmossdk.io/math" "github.com/caarlos0/env/v9" "github.com/cosmos/relayer/v2/relayer" "github.com/google/go-github/v55/github" "gopkg.in/yaml.v3" - "github.com/archway-network/relayer_exporter/pkg/account" + "github.com/archway-network/relayer_exporter/pkg/chain" log "github.com/archway-network/relayer_exporter/pkg/logger" ) const ibcPathSuffix = ".json" +type Account struct { + Address string `yaml:"address"` + Denom string `yaml:"denom"` + ChainName string `yaml:"chainName"` + Balance math.Int +} + type RPC struct { - ChainID string `yaml:"chainId"` - URL string `yaml:"url"` + ChainName string `yaml:"chainName"` + ChainID string `yaml:"chainId"` + URL string `yaml:"url"` } type Config struct { - Accounts []account.Account `yaml:"accounts"` - RPCs []RPC `yaml:"rpc"` + Accounts []Account `yaml:"accounts"` + RPCs []RPC `yaml:"rpc"` GitHub struct { Org string `yaml:"org"` Repo string `yaml:"repo"` @@ -33,14 +42,35 @@ type Config struct { } `yaml:"github"` } -func (c *Config) GetRPCsMap() map[string]string { - rpcs := map[string]string{} +func (a *Account) GetBalance(rpcs *map[string]RPC) error { + chain, err := chain.PrepChain(chain.Info{ + ChainID: (*rpcs)[a.ChainName].ChainID, + RPCAddr: (*rpcs)[a.ChainName].URL, + }) + if err != nil { + return err + } + + ctx := context.Background() + + coins, err := chain.ChainProvider.QueryBalanceWithAddress(ctx, a.Address) + if err != nil { + return err + } + + a.Balance = coins.AmountOf(a.Denom) + + return nil +} + +func (c *Config) GetRPCsMap() *map[string]RPC { + rpcs := map[string]RPC{} for _, rpc := range c.RPCs { - rpcs[rpc.ChainID] = rpc.URL + rpcs[rpc.ChainName] = rpc } - return rpcs + return &rpcs } func (c *Config) IBCPaths() ([]*relayer.IBCdata, error) { diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 4e946d6..e2375fd 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -9,23 +9,33 @@ import ( func TestGetRPCsMap(t *testing.T) { rpcs := []RPC{ { - ChainID: "archway-1", - URL: "https://rpc.mainnet.archway.io:443", + ChainName: "archway", + ChainID: "archway-1", + URL: "https://rpc.mainnet.archway.io:443", }, { - ChainID: "constantine-3", - URL: "https://rpc.constantine.archway.tech:443", + ChainName: "archwaytestnet", + ChainID: "constantine-3", + URL: "https://rpc.constantine.archway.tech:443", }, } cfg := Config{RPCs: rpcs} - exp := map[string]string{ - "archway-1": "https://rpc.mainnet.archway.io:443", - "constantine-3": "https://rpc.constantine.archway.tech:443", + exp := map[string]RPC{ + "archway": { + ChainName: "archway", + ChainID: "archway-1", + URL: "https://rpc.mainnet.archway.io:443", + }, + "archwaytestnet": { + ChainName: "archwaytestnet", + ChainID: "constantine-3", + URL: "https://rpc.constantine.archway.tech:443", + }, } res := cfg.GetRPCsMap() - assert.Equal(t, exp, res) + assert.Equal(t, &exp, res) } diff --git a/pkg/ibc/ibc.go b/pkg/ibc/ibc.go index 51ca875..196b8ff 100644 --- a/pkg/ibc/ibc.go +++ b/pkg/ibc/ibc.go @@ -6,6 +6,7 @@ import ( "time" "github.com/archway-network/relayer_exporter/pkg/chain" + "github.com/archway-network/relayer_exporter/pkg/config" "github.com/cosmos/relayer/v2/relayer" ) @@ -18,12 +19,12 @@ type ClientsInfo struct { ChainBClientExpiration time.Time } -func GetClientsInfo(ibc *relayer.IBCdata, rpcs map[string]string) (ClientsInfo, error) { +func GetClientsInfo(ibc *relayer.IBCdata, rpcs *map[string]config.RPC) (ClientsInfo, error) { clientsInfo := ClientsInfo{} cdA := chain.Info{ - ChainID: ibc.Chain1.ChainName, - RPCAddr: rpcs[ibc.Chain1.ChainName], + ChainID: (*rpcs)[ibc.Chain1.ChainName].ChainID, + RPCAddr: (*rpcs)[ibc.Chain1.ChainName].URL, ClientID: ibc.Chain1.ClientID, } @@ -35,8 +36,8 @@ func GetClientsInfo(ibc *relayer.IBCdata, rpcs map[string]string) (ClientsInfo, clientsInfo.ChainA = chainA cdB := chain.Info{ - ChainID: ibc.Chain2.ChainName, - RPCAddr: rpcs[ibc.Chain2.ChainName], + ChainID: (*rpcs)[ibc.Chain2.ChainName].ChainID, + RPCAddr: (*rpcs)[ibc.Chain2.ChainName].URL, ClientID: ibc.Chain2.ClientID, }