Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
[Cosmos] Add APR reward information
Browse files Browse the repository at this point in the history
  • Loading branch information
vikmeup committed Aug 11, 2019
1 parent 8747bdd commit 4e15409
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 14 deletions.
47 changes: 43 additions & 4 deletions platform/cosmos/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,23 @@ func (p *Platform) GetTxsByAddress(address string) (blockatlas.TxPage, error) {
}

func (p *Platform) GetValidators() (blockatlas.ValidatorPage, error) {
results := blockatlas.ValidatorPage{}
validators, _ := p.client.GetValidators()
results := make(blockatlas.ValidatorPage, 0)
validators, err := p.client.GetValidators()
if err != nil {
return results, nil
}
pool, err := p.client.GetPool()
if err != nil {
return results, nil
}

inflation, err := p.client.GetInflation()
if err != nil {
return results, nil
}

for _, validator := range validators {
results = append(results, normalizeValidator(validator, p.Coin()))
results = append(results, normalizeValidator(validator, pool, inflation, p.Coin()))
}

return results, nil
Expand Down Expand Up @@ -88,10 +100,37 @@ func Normalize(srcTx *Tx) (tx blockatlas.Tx) {
}
}

func normalizeValidator(v CosmosValidator, c coin.Coin) (validator blockatlas.Validator) {
func normalizeValidator(v CosmosValidator, p StakingPool, inflation float64, c coin.Coin) (validator blockatlas.Validator) {

reward := CalculateAnnualReward(p, inflation, v)

return blockatlas.Validator{
Coin: c,
Status: bool(v.Status == 2),
ID: v.Operator_Address,
Reward: blockatlas.StakingReward{Annual: reward},
}
}

func CalculateAnnualReward(p StakingPool, inflation float64, validator CosmosValidator) float64 {

notBondedTokens, err := strconv.ParseFloat(string(p.NotBondedTokens), 32)

if err != nil {
return 0
}

bondedTokens, err := strconv.ParseFloat(string(p.BondedTokens), 32)
if err != nil {
return 0
}

commission, err := strconv.ParseFloat(string(validator.Commission.Rate), 32)
if err != nil {
return 0
}

result := (notBondedTokens + bondedTokens) / bondedTokens * inflation

return (result - (result * commission)) * 100
}
16 changes: 15 additions & 1 deletion platform/cosmos/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ var basicDst = blockatlas.Tx{
},
}

var stakingPool = StakingPool{"1222", "200"}

var cosmosValidator = CosmosValidator{Commission: CosmosCommission{Rate: "0.4"}}

var inflation = 0.7

func TestNormalize(t *testing.T) {
var srcTx Tx
err := json.Unmarshal([]byte(basicSrc), &srcTx)
Expand Down Expand Up @@ -152,9 +158,17 @@ func TestNormalizeValidator(t *testing.T) {
expected := blockatlas.Validator{
Status: true,
ID: v.Operator_Address,
Reward: blockatlas.StakingReward{Annual: 435.48749999999995},
}

result := normalizeValidator(v, coin)
result := normalizeValidator(v, stakingPool, inflation, coin)

assert.Equal(t, result, expected)
}

func TestCalculateAnnualReward(t *testing.T) {

result := CalculateAnnualReward(StakingPool{"1222", "200"}, inflation, CosmosValidator{Commission: CosmosCommission{Rate: "0.4"}})

assert.Equal(t, result, 298.61999703347686)
}
15 changes: 15 additions & 0 deletions platform/cosmos/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/trustwallet/blockatlas"
"github.com/trustwallet/blockatlas/client"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -73,3 +74,17 @@ func (c *Client) GetValidators() (validators []CosmosValidator, err error) {

return validators, err
}

func (c *Client) GetPool() (result StakingPool, err error) {
return result, client.Request(c.HTTPClient, c.BaseURL, "staking/pool", url.Values{}, &result)
}

func (c *Client) GetInflation() (float64, error) {
var result string

err := client.Request(c.HTTPClient, c.BaseURL, "minting/inflation", url.Values{}, &result)

s, err := strconv.ParseFloat(result, 32)

return s, err
}
14 changes: 12 additions & 2 deletions platform/cosmos/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ type Amount struct {

// # Staking

type CosmosCommission struct {
Rate string `json:"rate"`
}

type CosmosValidator struct {
Status int `json:"status"`
Operator_Address string `json:"operator_address"`
Status int `json:"status"`
Operator_Address string `json:"operator_address"`
Commission CosmosCommission `json:"commission"`
}

type StakingPool struct {
NotBondedTokens string `json:"not_bonded_tokens"`
BondedTokens string `json:"bonded_tokens"`
}
3 changes: 2 additions & 1 deletion services/assets/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GetValidators(coin coin.Coin) ([]AssetValidator, error) {
}

func NormalizeValidators(validators []blockatlas.Validator, assets []AssetValidator) []blockatlas.StakeValidator {
var results []blockatlas.StakeValidator
results := make([]blockatlas.StakeValidator, 0)

for _, v := range validators {
for _, v2 := range assets {
Expand All @@ -47,6 +47,7 @@ func NormalizeValidator(plainValidator blockatlas.Validator, validator AssetVali
Image: GetImage(plainValidator.Coin, plainValidator.ID),
Website: validator.Website,
},
Reward: plainValidator.Reward,
}
}

Expand Down
18 changes: 12 additions & 6 deletions staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ type DocsResponse struct {

const ValidatorsPerPage = 100

type StakingReward struct {
Annual float64 `json:"annual"`
}

type Validator struct {
Coin coin.Coin
ID string `json:"id"`
Status bool `json:"status"`
Reward StakingReward `json:"reward"`
}

type StakeValidatorInfo struct {
Name string `json:"name"`
Description string `json:"description"`
Image string `json:"image"`
Website string `json:"website"`
}

type Validator struct {
Coin coin.Coin
ID string `json:"id"`
Status bool `json:"status"`
}

type StakeValidator struct {
ID string `json:"id"`
Status bool `json:"status"`
Info StakeValidatorInfo `json:"info"`
Reward StakingReward `json:"reward"`
}

0 comments on commit 4e15409

Please sign in to comment.