Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stakooler refactor #65

Merged
merged 13 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.17
go-version: 1.22

- name: Build
run: go build -v ./...
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,6 @@ chain = "[a chain id matching one from the chains section]"
[[chains]]
id = "[chain id]" # chain-id
lcd = "[lcd address of the node]" # the REST endpoint of the node e.g. http://myosmonode.com:1317


########################
# Zabbix - enabled when running with the --zbx flag
########################

[zabbix]
server = "[IP/URL]" # Zabbix server IP or URL
host = "[zabbix host]" # Host defined in zabbix with a trapper item
port = "[zabbix trapper port]" # Port used by Zabbix server for trapper items. Default 10051
```

> the [chain id] has to match one that is available in the [Cosmos Directory](https://cosmos.directory). Select the chain, in the tab Chain look for the Chain ID property
Expand Down
25 changes: 0 additions & 25 deletions client/cosmos/api/account.go

This file was deleted.

92 changes: 92 additions & 0 deletions client/cosmos/api/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package api

import (
"encoding/json"
"net/http"
)

type Bech32PrefixResponse struct {
Bech32Prefix string `json:"bech32_prefix"`
}

type AcctResponse struct {
Account struct {
Type string `json:"@type"`
BaseVestingAccount struct {
BaseAccount struct {
Address string `json:"address,omitempty"`
PubKey string `json:"public_key,omitempty"`
AccountNumber string `json:"account_number,omitempty"`
Sequence string `json:"sequence,omitempty"`
}
OriginalVesting []struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
} `json:"original_vesting"`
DelegatedFree []struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
} `json:"delegated_free"`
DelegatedVesting []struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
} `json:"delegated_vesting"`
EndTime string `json:"end_time"`
} `json:"base_vesting_account"`
StartTime string `json:"start_time"`
VestingPeriods []struct {
Length string `json:"length"`
Amount []struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
} `json:"amount"`
} `json:"vesting_periods"`
} `json:"account"`
}

func (a *AcctResponse) GetBalances() map[int]map[string]string {
balances := make(map[int]map[string]string)
balances[OriginalVesting] = make(map[string]string)
balances[DelegatedVesting] = make(map[string]string)

for _, balance := range a.Account.BaseVestingAccount.OriginalVesting {
balances[OriginalVesting][balance.Denom] = balance.Amount
}

for _, balance := range a.Account.BaseVestingAccount.DelegatedVesting {
balances[DelegatedVesting][balance.Denom] = balance.Amount
}
return balances
}

func (p *Bech32PrefixResponse) GetPrefix(endpointURL string, client *http.Client) error {
var body []byte

url := endpointURL + "/cosmos/auth/v1beta1/bech32"
body, err := HttpGet(url, client)
if err != nil {
return err
}

err = json.Unmarshal(body, p)
if err != nil {
return err
}
return err
}

func (a *AcctResponse) QueryAuth(address string, endpoint string, client *http.Client) error {
var body []byte

url := endpoint + "/cosmos/auth/v1beta1/accounts/" + address
body, err := HttpGet(url, client)
if err != nil {
return err
}

err = json.Unmarshal(body, a)
if err != nil {
return err
}
return nil
}
81 changes: 59 additions & 22 deletions client/cosmos/api/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,83 @@ import (
"encoding/json"
"net/http"
"strings"

"github.com/informalsystems/stakooler/client/cosmos"
"github.com/informalsystems/stakooler/client/cosmos/model"
)

func GetBalances(account *model.Account, client *http.Client) (response model.BalancesResponse, err error) {
type BankResponse struct {
Balances []struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
} `json:"balances"`
Pagination struct {
NextKey interface{} `json:"next_key"`
Total string `json:"total"`
} `json:"pagination"`
}

type DenomMetadataResponse struct {
Metadata struct {
Description string `json:"description"`
DenomUnits []struct {
Denom string `json:"denom"`
Exponent int `json:"exponent"`
Aliases []string `json:"aliases"`
} `json:"denom_units"`
Base string `json:"base"`
Display string `json:"display"`
} `json:"metadata"`
}

func (b *BankResponse) GetBalances() map[int]map[string]string {
balances := make(map[int]map[string]string)
balances[Bank] = map[string]string{}

for _, balance := range b.Balances {
balances[Bank][balance.Denom] = balance.Amount
}
return balances
}

func (b *BankResponse) QueryBankBalances(address string, endpoint string, client *http.Client) error {
var body []byte

url := account.Chain.LCD + "/cosmos/bank/v1beta1/balances/" + account.Address
body, err = cosmos.HttpGet(url, client)
url := endpoint + "/cosmos/bank/v1beta1/balances/" + address
body, err := HttpGet(url, client)

err = json.Unmarshal(body, &response)
err = json.Unmarshal(body, b)
if err != nil {
return
return err
}
return
return nil
}

func GetDenomMetadata(account *model.Account, denom string, client *http.Client) (response model.DenomMetadataResponse, err error) {
func (d *DenomMetadataResponse) QueryMetadataFromBank(denom string, endpoint string, client *http.Client) error {
var body []byte
var url string

// This is here because of injective does not implement the original query
if strings.HasPrefix(denom, "factory/inj") {
url = endpoint + "/cosmos/bank/v1beta1/denoms_metadata_by_query_string?denom=" + denom
} else {
url = endpoint + "/cosmos/bank/v1beta1/denoms_metadata/" + denom
}

url := account.Chain.LCD + "/cosmos/bank/v1beta1/denoms_metadata/" + denom
body, err = cosmos.HttpGet(url, client)
body, err := HttpGet(url, client)
if err != nil {
return
return err
}

err = json.Unmarshal(body, &response)
err = json.Unmarshal(body, d)
if err != nil {
return
return err
}
return
return err
}

func GetExponent(metadata *model.DenomMetadataResponse) int {
exponent := 0
for _, d := range metadata.Metadata.DenomUnits {
if strings.ToUpper(d.Denom) == strings.ToUpper(metadata.Metadata.Display) {
return d.Exponent
func (d *DenomMetadataResponse) GetExponent() int {
for _, unit := range d.Metadata.DenomUnits {
if strings.ToUpper(unit.Denom) == strings.ToUpper(d.Metadata.Display) {
return unit.Exponent
}
}
return exponent
return 0
}
75 changes: 75 additions & 0 deletions client/cosmos/api/chain_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package api

import (
"encoding/json"
"net/http"
"strings"
)

type AssetList struct {
Assets []struct {
Description string `json:"description"`
DenomUnits []struct {
Denom string `json:"denom"`
Exponent int `json:"exponent"`
} `json:"denom_units"`
Base string `json:"base"`
Name string `json:"name"`
Display string `json:"display"`
Symbol string `json:"symbol"`
} `json:"assets"`
}

type ChainData struct {
Bech32Prefix string `json:"bech32_prefix"`
}

// SearchForAsset search for the symbol for a particular denom in the assets list
func (a *AssetList) SearchForAsset(denom string) (string, int) {
for i := range a.Assets {
if a.Assets[i].Base == denom {
for j := range a.Assets[i].DenomUnits {
if strings.ToUpper(a.Assets[i].DenomUnits[j].Denom) == strings.ToUpper(a.Assets[i].Display) {
// Some chains (*cough* Injective *cough*) have decided to differentiate between denom units
// using letter casing...
if a.Assets[i].DenomUnits[j].Exponent != 0 {
return a.Assets[i].Symbol, a.Assets[i].DenomUnits[j].Exponent
}
}
}
}
}
return denom, 0
}

func (a *AssetList) QueryAssetList(chain string, client *http.Client) error {
var body []byte

url := "https://chains.cosmos.directory/" + chain + "/assetlist"
body, err := HttpGet(url, client)
if err != nil {
return err
}

err = json.Unmarshal(body, a)
if err != nil {
return err
}
return nil
}

func (c *ChainData) QueryChainData(chain string, client *http.Client) error {
var body []byte

url := "https://chains.cosmos.directory/" + chain + "/chain"
body, err := HttpGet(url, client)
if err != nil {
return err
}

err = json.Unmarshal(body, c)
if err != nil {
return err
}
return nil
}
55 changes: 0 additions & 55 deletions client/cosmos/api/chain_registry/registry.go

This file was deleted.

Loading
Loading