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

vesting accounts and bug fixes #49

Merged
merged 5 commits into from
Sep 1, 2023
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
43 changes: 43 additions & 0 deletions client/cosmos/api/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package api

import (
"encoding/json"
"fmt"
"io"
"net/http"

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

func GetAuth(account *model.Account) (model.AuthResponse, error) {
var authResponse model.AuthResponse

url := account.Chain.LCD + "/cosmos/auth/v1beta1/accounts/" + account.Address
method := "GET"

client := &http.Client{}
req, err := http.NewRequest(method, url, nil)

if err != nil {
fmt.Println(err)
return authResponse, err
}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return authResponse, err
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return authResponse, err
}
err = json.Unmarshal(body, &authResponse)
if err != nil {
fmt.Println(err)
return authResponse, err
}
return authResponse, nil
}
36 changes: 6 additions & 30 deletions client/cosmos/api/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,8 @@ import (
"strings"
)

type BalancesResponse 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 GetBalances(account *model.Account) (BalancesResponse, error) {
var balanceResponse BalancesResponse
func GetBalances(account *model.Account) (model.BalancesResponse, error) {
var balanceResponse model.BalancesResponse

url := account.Chain.LCD + "/cosmos/bank/v1beta1/balances/" + account.Address
method := "GET"
Expand Down Expand Up @@ -66,8 +42,8 @@ func GetBalances(account *model.Account) (BalancesResponse, error) {
return balanceResponse, nil
}

func GetDenomMetadata(account *model.Account, denom string) (DenomMetadataResponse, error) {
var denomMetadata DenomMetadataResponse
func GetDenomMetadata(account *model.Account, denom string) (model.DenomMetadataResponse, error) {
var denomMetadata model.DenomMetadataResponse

url := account.Chain.LCD + "/cosmos/bank/v1beta1/denoms_metadata/" + denom
method := "GET"
Expand Down Expand Up @@ -100,12 +76,12 @@ func GetDenomMetadata(account *model.Account, denom string) (DenomMetadataRespon
return denomMetadata, nil
}

func (metadata *DenomMetadataResponse) GetExponent() int {
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
}
}
return exponent
}
}
2 changes: 1 addition & 1 deletion client/cosmos/api/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,4 @@ func GetCommissions(account *model.Account, validator string) (CommissionRespons
return response, err
}
return response, nil
}
}
128 changes: 14 additions & 114 deletions client/cosmos/api/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,114 +3,14 @@ package api
import (
"encoding/json"
"fmt"
"github.com/informalsystems/stakooler/client/cosmos/model"
"io"
"net/http"
"time"
)

type Delegations struct {
DelegationResponses []struct {
Delegation struct {
DelegatorAddress string `json:"delegator_address"`
ValidatorAddress string `json:"validator_address"`
Shares string `json:"shares"`
} `json:"delegation"`
Balance struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
} `json:"balance"`
} `json:"delegation_responses"`
Pagination struct {
NextKey interface{} `json:"next_key"`
Total string `json:"total"`
} `json:"pagination"`
}

type Unbondings struct {
UnbondingResponses []struct {
DelegatorAddress string `json:"delegator_address"`
ValidatorAddress string `json:"validator_address"`
Entries []struct {
CreationHeight string `json:"creation_height"`
CompletionTime time.Time `json:"completion_time"`
InitialBalance string `json:"initial_balance"`
Balance string `json:"balance"`
} `json:"entries"`
} `json:"unbonding_responses"`
Pagination struct {
NextKey interface{} `json:"next_key"`
Total string `json:"total"`
} `json:"pagination"`
}

type Params struct {
ParamsResponse struct {
UnbondingTime string `json:"unbonding_time"`
MaxValidators int `json:"max_validators"`
MaxEntries int `json:"max_entries"`
HistoricalEntries int `json:"historical_entries"`
BondDenom string `json:"bond_denom"`
MinCommissionRate string `json:"min_commission_rate"`
} `json:"params"`
}

type ValidatorSet struct {
BlockHeight string `json:"block_height"`
Validators []struct {
Address string `json:"address"`
PubKey struct {
Type string `json:"@type"`
Key string `json:"key"`
} `json:"pub_key"`
VotingPower string `json:"voting_power"`
ProposerPriority string `json:"proposer_priority"`
} `json:"validators"`
Pagination struct {
NextKey interface{} `json:"next_key"`
Total string `json:"total"`
} `json:"pagination"`
}

type Validators struct {
BlockHeight string `json:"block_height,omitempty"`
ValidatorsResponse []struct {
OperatorAddress string `json:"operator_address"`
ConsensusPubkey struct {
Type string `json:"@type"`
Key string `json:"key"`
} `json:"consensus_pubkey"`
Jailed bool `json:"jailed"`
Status string `json:"status"`
Tokens string `json:"tokens"`
DelegatorShares string `json:"delegator_shares"`
Description struct {
Moniker string `json:"moniker"`
Identity string `json:"identity"`
Website string `json:"website"`
SecurityContact string `json:"security_contact"`
Details string `json:"details"`
} `json:"description"`
UnbondingHeight string `json:"unbonding_height"`
UnbondingTime time.Time `json:"unbonding_time"`
Commission struct {
CommissionRates struct {
Rate string `json:"rate"`
MaxRate string `json:"max_rate"`
MaxChangeRate string `json:"max_change_rate"`
} `json:"commission_rates"`
UpdateTime time.Time `json:"update_time"`
} `json:"commission"`
MinSelfDelegation string `json:"min_self_delegation"`
} `json:"validators"`
Pagination struct {
NextKey interface{} `json:"next_key"`
Total string `json:"total"`
} `json:"pagination"`
}
"github.com/informalsystems/stakooler/client/cosmos/model"
)

func GetDelegations(account *model.Account) (Delegations, error) {
var delegations Delegations
func GetDelegations(account *model.Account) (model.Delegations, error) {
var delegations model.Delegations

url := account.Chain.LCD + "/cosmos/staking/v1beta1/delegations/" + account.Address
method := "GET"
Expand Down Expand Up @@ -142,8 +42,8 @@ func GetDelegations(account *model.Account) (Delegations, error) {
return delegations, nil
}

func GetUnbondings(account *model.Account) (Unbondings, error) {
var unbondings Unbondings
func GetUnbondings(account *model.Account) (model.Unbondings, error) {
var unbondings model.Unbondings

url := account.Chain.LCD + "/cosmos/staking/v1beta1/delegators/" + account.Address + "/unbonding_delegations"
method := "GET"
Expand Down Expand Up @@ -175,8 +75,8 @@ func GetUnbondings(account *model.Account) (Unbondings, error) {
return unbondings, nil
}

func GetStakingParams(chainEndpoint string) (Params, error) {
var params Params
func GetStakingParams(chainEndpoint string) (model.Params, error) {
var params model.Params

url := chainEndpoint + "/cosmos/staking/v1beta1/params"
method := "GET"
Expand Down Expand Up @@ -239,8 +139,8 @@ func GetStakingParams(chainEndpoint string) (Params, error) {
// return valset, nil
//}

func GetChainValidators(validator *model.Validator) (Validators, error) {
var validators Validators
func GetChainValidators(validator *model.Validator) (model.Validators, error) {
var validators model.Validators

url := validator.Chain.LCD + "/cosmos/staking/v1beta1/validators?pagination.limit=1000&pagination.count_total=true&status=BOND_STATUS_BONDED"
method := "GET"
Expand Down Expand Up @@ -274,8 +174,8 @@ func GetChainValidators(validator *model.Validator) (Validators, error) {
return validators, nil
}

func GetValidatorUnbondings(validator *model.Validator) (Unbondings, error) {
var unbondings Unbondings
func GetValidatorUnbondings(validator *model.Validator) (model.Unbondings, error) {
var unbondings model.Unbondings
url := validator.Chain.LCD + "/cosmos/staking/v1beta1/validators/" + validator.ValoperAddress + "/unbonding_delegations"
method := "GET"

Expand Down Expand Up @@ -306,8 +206,8 @@ func GetValidatorUnbondings(validator *model.Validator) (Unbondings, error) {
return unbondings, nil
}

func GetValidatorDelegations(validator *model.Validator) (Delegations, error) {
var delegations Delegations
func GetValidatorDelegations(validator *model.Validator) (model.Delegations, error) {
var delegations model.Delegations

url := validator.Chain.LCD + "/cosmos/staking/v1beta1/validators/" + validator.ValoperAddress + "/delegations?pagination.limit=15000&pagination.count_total=true"
method := "GET"
Expand Down
59 changes: 49 additions & 10 deletions client/cosmos/model/account.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package model

import "time"
import (
"time"
)

type Accounts struct {
Entries []*Account
Expand All @@ -10,17 +12,54 @@ type Account struct {
Name string
Address string
Chain Chain
BlockTime time.Time
BlockHeight string
TokensEntry []TokenEntry
}

type TokenEntry struct {
DisplayName string
Denom string
BlockTime time.Time
BlockHeight string
Balance float64
Reward float64
Delegation float64
Unbonding float64
Commission float64
DisplayName string
Denom string
Balance float64
Reward float64
Delegation float64
Unbonding float64
Commission float64
Vesting float64
DelegatedVesting float64
}

type AuthResponse 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"`
}
25 changes: 25 additions & 0 deletions client/cosmos/model/bank.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package model

type BalancesResponse 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"`
}
2 changes: 1 addition & 1 deletion client/cosmos/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type ZabbixConfig struct {

type Config struct {
Accounts Accounts
Validators Validators
Validators ValidatorList
Chains Chains
Zabbix ZabbixConfig
}
Loading
Loading