From c3ed557391ba34f12bf637346ecc95b5e2b3aa63 Mon Sep 17 00:00:00 2001 From: mayar osama Date: Thu, 15 Jun 2023 16:18:20 +0200 Subject: [PATCH 1/2] Getting balance of each address --- monitoring-bot/internal/monitor.go | 45 ++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/monitoring-bot/internal/monitor.go b/monitoring-bot/internal/monitor.go index 43c152721..ea4c70082 100644 --- a/monitoring-bot/internal/monitor.go +++ b/monitoring-bot/internal/monitor.go @@ -59,6 +59,26 @@ type Monitor struct { substrate map[network]client.Manager } +func getAddBalance(manager client.Manager, address address) (float64, error) { + con, err := manager.Substrate() + if err != nil { + return 0, err + } + defer con.Close() + + account, err := client.FromAddress(string(address)) + if err != nil { + return 0, err + } + + balance, err := con.GetBalance(account) + if err != nil { + return 0, err + } + + return float64(balance.Free.Int64()) / math.Pow(10, 7), nil +} + // NewMonitor creates a new instance of monitor func NewMonitor(envPath string, jsonPath string) (Monitor, error) { mon := Monitor{} @@ -83,15 +103,34 @@ func NewMonitor(envPath string, jsonPath string) (Monitor, error) { return mon, err } - mon.wallets = addresses - mon.env = env - mon.substrate = map[network]client.Manager{} // all needed for proxy for _, network := range networks { mon.substrate[network] = client.NewManager(SubstrateURLs[network]...) + switch network { + case mainNetwork: + for _, wallet := range addresses.Mainnet { + _, err := getAddBalance(mon.substrate[network], wallet.Address) + if err != nil { + return mon, err + } + + } + case testNetwork: + for _, wallet := range addresses.Testnet { + _, err := getAddBalance(mon.substrate[network], wallet.Address) + if err != nil { + return mon, err + } + + } + + } + } + mon.wallets = addresses + mon.env = env mon.mnemonics = map[network]string{} if !bip39.IsMnemonicValid(mon.env.devMnemonic) { From 8df098b254a17931c75fe4804e1e5b6522281c9e Mon Sep 17 00:00:00 2001 From: mayar osama Date: Sun, 18 Jun 2023 14:06:02 +0200 Subject: [PATCH 2/2] Fixing tests --- monitoring-bot/internal/monitor.go | 6 +++--- monitoring-bot/internal/monitor_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/monitoring-bot/internal/monitor.go b/monitoring-bot/internal/monitor.go index ea4c70082..73524196d 100644 --- a/monitoring-bot/internal/monitor.go +++ b/monitoring-bot/internal/monitor.go @@ -59,7 +59,7 @@ type Monitor struct { substrate map[network]client.Manager } -func getAddBalance(manager client.Manager, address address) (float64, error) { +func getAddressBalance(manager client.Manager, address address) (float64, error) { con, err := manager.Substrate() if err != nil { return 0, err @@ -111,7 +111,7 @@ func NewMonitor(envPath string, jsonPath string) (Monitor, error) { switch network { case mainNetwork: for _, wallet := range addresses.Mainnet { - _, err := getAddBalance(mon.substrate[network], wallet.Address) + _, err := getAddressBalance(mon.substrate[network], wallet.Address) if err != nil { return mon, err } @@ -119,7 +119,7 @@ func NewMonitor(envPath string, jsonPath string) (Monitor, error) { } case testNetwork: for _, wallet := range addresses.Testnet { - _, err := getAddBalance(mon.substrate[network], wallet.Address) + _, err := getAddressBalance(mon.substrate[network], wallet.Address) if err != nil { return mon, err } diff --git a/monitoring-bot/internal/monitor_test.go b/monitoring-bot/internal/monitor_test.go index c1037fa3f..38e586c36 100644 --- a/monitoring-bot/internal/monitor_test.go +++ b/monitoring-bot/internal/monitor_test.go @@ -25,8 +25,8 @@ func TestMonitor(t *testing.T) { defer os.Remove(jsonFile.Name()) data := []byte(`{ - "mainnet": [ { "name": "name", "address": "address", "threshold": 1} ], - "testnet": [ { "name": "name-test", "address": "address", "threshold": 1} ] + "mainnet": [ { "name": "name", "address": "5ECu6QxQ8eQmAjDtKQE6UVgzRFrdmYH1VvUiiK4UyxhkJ469", "threshold": 1} ], + "testnet": [ { "name": "name-test", "address": "5GLQdUZ3tyeashZteV2nYYiJ6TdXKxEPhiBtoyWcb8jFuwVq", "threshold": 1} ] }`) if _, err := jsonFile.Write(data); err != nil { t.Error(err) @@ -227,8 +227,8 @@ func TestZosVersion(t *testing.T) { defer os.Remove(jsonFile.Name()) data := []byte(`{ - "mainnet": [ { "name": "name", "address": "address", "threshold": 1} ], - "testnet": [ { "name": "name-test", "address": "address", "threshold": 1} ] + "mainnet": [ { "name": "name", "address": "5ECu6QxQ8eQmAjDtKQE6UVgzRFrdmYH1VvUiiK4UyxhkJ469", "threshold": 1} ], + "testnet": [ { "name": "name-test", "address": "5GLQdUZ3tyeashZteV2nYYiJ6TdXKxEPhiBtoyWcb8jFuwVq", "threshold": 1} ] }`) if _, err := jsonFile.Write(data); err != nil { t.Error(err)