Skip to content

Commit

Permalink
Merge pull request #199 from threefoldtech/development_address_monbot
Browse files Browse the repository at this point in the history
  • Loading branch information
maayarosama authored Jun 18, 2023
2 parents 6423f8d + 8df098b commit aa5b623
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
45 changes: 42 additions & 3 deletions monitoring-bot/internal/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ type Monitor struct {
substrate map[network]client.Manager
}

func getAddressBalance(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{}
Expand All @@ -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 := getAddressBalance(mon.substrate[network], wallet.Address)
if err != nil {
return mon, err
}

}
case testNetwork:
for _, wallet := range addresses.Testnet {
_, err := getAddressBalance(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) {
Expand Down
8 changes: 4 additions & 4 deletions monitoring-bot/internal/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit aa5b623

Please sign in to comment.