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

feat: merge version v1.0.0-stargate #329

Merged
merged 22 commits into from
Feb 15, 2022
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
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