Skip to content

Commit

Permalink
feat: merge version v1.0.0-stargate (forbole#329)
Browse files Browse the repository at this point in the history
## Description

Merge bdjuno version [v1.0.0-stargate](https://github.com/forbole/bdjuno/releases/tag/v1.0.0-stargate)



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch
- [ ] provided a link to the relevant issue or specification
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
MonikaCat authored Feb 15, 2022
1 parent 82245e9 commit 37bbc91
Show file tree
Hide file tree
Showing 103 changed files with 1,851 additions and 4,089 deletions.
52 changes: 52 additions & 0 deletions cmd/actions/actionscmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package actions

import (
"log"
"net/http"

"github.com/forbole/juno/v2/cmd/parse"
"github.com/spf13/cobra"

"github.com/forbole/bdjuno/v2/cmd/actions/handlers"
)

// NewActionsCmd returns the Cobra command allowing to activate hasura actions
func NewActionsCmd(parseCfg *parse.Config) *cobra.Command {
return &cobra.Command{
Use: "hasura-actions",
Short: "Activate hasura actions",
PreRunE: parse.ReadConfig(parseCfg),
RunE: func(cmd *cobra.Command, args []string) error {

// HTTP server for the handlers
mux := http.NewServeMux()

// End points:

// -- Bank --
mux.HandleFunc("/account_balance", handlers.AccountBalance)

// -- Distribution --
mux.HandleFunc("/delegation_reward", handlers.DelegationReward)
mux.HandleFunc("/delegator_withdraw_address", handlers.DelegatorWithdrawAddress)
mux.HandleFunc("/validator_commission_amount", handlers.ValidatorCommissionAmount)

// -- Staking Delegator --
mux.HandleFunc("/delegation", handlers.Delegation)
mux.HandleFunc("/delegation_total", handlers.TotalDelegationAmount)
mux.HandleFunc("/unbonding_delegation", handlers.UnbondingDelegations)
mux.HandleFunc("/unbonding_delegation_total", handlers.UnbondingDelegationsTotal)
mux.HandleFunc("/redelegation", handlers.Redelegation)

// -- Staking Validator --
mux.HandleFunc("/validator_delegations", handlers.ValidatorDelegation)
mux.HandleFunc("/validator_redelegations_from", handlers.ValidatorRedelegationsFrom)
mux.HandleFunc("/validator_unbonding_delegations", handlers.ValidatorUnbondingDelegations)

err := http.ListenAndServe(":3000", mux)
log.Fatal(err)

return nil
},
}
}
59 changes: 59 additions & 0 deletions cmd/actions/handlers/account_balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package handlers

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

actionstypes "github.com/forbole/bdjuno/v2/cmd/actions/types"
"github.com/forbole/bdjuno/v2/utils"
)

func AccountBalance(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "invalid payload", http.StatusBadRequest)
return
}

var actionPayload actionstypes.Payload
err = json.Unmarshal(reqBody, &actionPayload)
if err != nil {
http.Error(w, "invalid payload: failed to unmarshal json", http.StatusInternalServerError)
return
}

result, err := getAccountBalance(actionPayload.Input)
if err != nil {
errorHandler(w, err)
return
}

data, _ := json.Marshal(result)
w.Write(data)
}

func getAccountBalance(input actionstypes.PayloadArgs) (response actionstypes.Balance, err error) {
parseCtx, sources, err := getCtxAndSources()
if err != nil {
return response, err
}

height, err := utils.GetHeight(parseCtx, input.Height)
if err != nil {
return response, fmt.Errorf("error while getting height: %s", err)
}

balance, err := sources.BankSource.GetAccountBalance(input.Address, height)
if err != nil {
return response, err
}

return actionstypes.Balance{
Coins: balance,
}, nil
}
77 changes: 77 additions & 0 deletions cmd/actions/handlers/delegation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package handlers

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

"github.com/cosmos/cosmos-sdk/types/query"
actionstypes "github.com/forbole/bdjuno/v2/cmd/actions/types"
"github.com/forbole/bdjuno/v2/utils"
)

func Delegation(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "invalid payload", http.StatusBadRequest)
return
}

var actionPayload actionstypes.Payload
err = json.Unmarshal(reqBody, &actionPayload)
if err != nil {
http.Error(w, "invalid payload: failed to unmarshal json", http.StatusInternalServerError)
return
}

result, err := getDelegation(actionPayload.Input)
if err != nil {
errorHandler(w, err)
return
}

data, _ := json.Marshal(result)
w.Write(data)
}

func getDelegation(input actionstypes.PayloadArgs) (actionstypes.DelegationResponse, error) {
parseCtx, sources, err := getCtxAndSources()
if err != nil {
return actionstypes.DelegationResponse{}, err
}

height, err := utils.GetHeight(parseCtx, input.Height)
if err != nil {
return actionstypes.DelegationResponse{}, fmt.Errorf("error while getting height: %s", err)
}

pagination := &query.PageRequest{
Offset: input.Offset,
Limit: input.Limit,
CountTotal: input.CountTotal,
}

// Get delegator's total rewards
res, err := sources.StakingSource.GetDelegationsWithPagination(height, input.Address, pagination)
if err != nil {
return actionstypes.DelegationResponse{}, fmt.Errorf("error while getting delegator delegations: %s", err)
}

delegations := make([]actionstypes.Delegation, len(res.DelegationResponses))
for index, del := range res.DelegationResponses {
delegations[index] = actionstypes.Delegation{
DelegatorAddress: del.Delegation.DelegatorAddress,
ValidatorAddress: del.Delegation.ValidatorAddress,
Coins: del.Balance,
}
}

return actionstypes.DelegationResponse{
Delegations: delegations,
Pagination: res.Pagination,
}, nil
}
78 changes: 78 additions & 0 deletions cmd/actions/handlers/delegation_total.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package handlers

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

sdk "github.com/cosmos/cosmos-sdk/types"
actionstypes "github.com/forbole/bdjuno/v2/cmd/actions/types"
"github.com/forbole/bdjuno/v2/utils"
)

func TotalDelegationAmount(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "invalid payload", http.StatusBadRequest)
return
}

var actionPayload actionstypes.Payload
err = json.Unmarshal(reqBody, &actionPayload)
if err != nil {
http.Error(w, "invalid payload: failed to unmarshal json", http.StatusInternalServerError)
return
}

result, err := getTotalDelegationAmount(actionPayload.Input)
if err != nil {
errorHandler(w, err)
return
}

data, _ := json.Marshal(result)
w.Write(data)
}

func getTotalDelegationAmount(input actionstypes.PayloadArgs) (actionstypes.Balance, error) {
parseCtx, sources, err := getCtxAndSources()
if err != nil {
return actionstypes.Balance{}, err
}

height, err := utils.GetHeight(parseCtx, input.Height)
if err != nil {
return actionstypes.Balance{}, fmt.Errorf("error while getting height: %s", err)
}

// Get all delegations for given delegator address
delegationList, err := sources.StakingSource.GetDelegationsWithPagination(height, input.Address, nil)
if err != nil {
return actionstypes.Balance{}, fmt.Errorf("error while getting delegator delegations: %s", err)
}

var coinObject sdk.Coins

// Add up total value of delegations
for _, eachDelegation := range delegationList.DelegationResponses {
for index, eachCoin := range coinObject {
if eachCoin.Denom == eachDelegation.Balance.Denom {
coinObject[index].Amount = coinObject[index].Amount.Add(eachDelegation.Balance.Amount)
}
if eachCoin.Denom != eachDelegation.Balance.Denom {
coinObject = append(coinObject, sdk.NewCoin(eachDelegation.Balance.Denom, eachDelegation.Balance.Amount))
}
}
if coinObject == nil {
coinObject = append(coinObject, sdk.NewCoin(eachDelegation.Balance.Denom, eachDelegation.Balance.Amount))
}
}

return actionstypes.Balance{
Coins: coinObject,
}, nil
}
66 changes: 66 additions & 0 deletions cmd/actions/handlers/delegator_reward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package handlers

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

actionstypes "github.com/forbole/bdjuno/v2/cmd/actions/types"
"github.com/forbole/bdjuno/v2/utils"
)

func DelegationReward(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "invalid payload", http.StatusBadRequest)
return
}

var actionPayload actionstypes.Payload
err = json.Unmarshal(reqBody, &actionPayload)
if err != nil {
http.Error(w, "invalid payload: failed to unmarshal json", http.StatusInternalServerError)
return
}

result, err := getDelegationReward(actionPayload.Input)
if err != nil {
errorHandler(w, err)
return
}

data, _ := json.Marshal(result)
w.Write(data)
}

func getDelegationReward(input actionstypes.PayloadArgs) (response []actionstypes.DelegationReward, err error) {
parseCtx, sources, err := getCtxAndSources()
if err != nil {
return response, err
}

height, err := utils.GetHeight(parseCtx, input.Height)
if err != nil {
return response, fmt.Errorf("error while getting chain latest block height: %s", err)
}

// Get delegator's total rewards
rewards, err := sources.DistrSource.DelegatorTotalRewards(input.Address, height)
if err != nil {
return response, err
}

delegationRewards := make([]actionstypes.DelegationReward, len(rewards))
for index, rew := range rewards {
delegationRewards[index] = actionstypes.DelegationReward{
Coins: rew.Reward,
ValidatorAddress: rew.ValidatorAddress,
}
}

return delegationRewards, nil
}
62 changes: 62 additions & 0 deletions cmd/actions/handlers/delegator_withdraw_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package handlers

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

actionstypes "github.com/forbole/bdjuno/v2/cmd/actions/types"
)

func DelegatorWithdrawAddress(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "invalid payload", http.StatusBadRequest)
return
}

var actionPayload actionstypes.Payload
err = json.Unmarshal(reqBody, &actionPayload)
if err != nil {
http.Error(w, "invalid payload: failed to unmarshal json", http.StatusInternalServerError)
return
}

result, err := getDelegatorWithdrawAddress(actionPayload.Input.Address)
if err != nil {
errorHandler(w, err)
return
}

data, _ := json.Marshal(result)
w.Write(data)
}

func getDelegatorWithdrawAddress(address string) (response actionstypes.Address, err error) {
parseCtx, sources, err := getCtxAndSources()
if err != nil {
return response, err
}

// Get latest node height
height, err := parseCtx.Node.LatestHeight()
if err != nil {
return response, fmt.Errorf("error while getting chain latest block height: %s", err)
}

// Get delegator's total rewards
withdrawAddress, err := sources.DistrSource.DelegatorWithdrawAddress(address, height)
if err != nil {
return response, err
}

response = actionstypes.Address{
Address: withdrawAddress,
}

return response, nil
}
Loading

0 comments on commit 37bbc91

Please sign in to comment.