forked from forbole/callisto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use a single gRPC and RPC connection for all Hasura actions (for…
…bole#338) ## Description This PR improves the overall architecture of Hasura actions by having a single `HasuraActionsWorker` that keeps the various connections to the node so that all handlers can share them without having to re-create it every time they are called. It should also make it easier to define new handlers in the future if there's the need to. --- ### 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... - [x] 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
Showing
24 changed files
with
365 additions
and
702 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,23 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
actionstypes "github.com/forbole/bdjuno/v2/cmd/actions/types" | ||
dbtypes "github.com/forbole/bdjuno/v2/database/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) | ||
func AccountBalanceHandler(ctx *actionstypes.Context, payload *actionstypes.Payload) (interface{}, error) { | ||
height, err := ctx.GetHeight(payload) | ||
if err != nil { | ||
return response, fmt.Errorf("error while getting height: %s", err) | ||
return nil, err | ||
} | ||
|
||
balance, err := sources.BankSource.GetAccountBalance(input.Address, height) | ||
balance, err := ctx.Sources.BankSource.GetAccountBalance(payload.GetAddress(), height) | ||
if err != nil { | ||
return response, err | ||
return nil, fmt.Errorf("error while getting account balance: %s", err) | ||
} | ||
|
||
return actionstypes.Balance{ | ||
Coins: dbtypes.NewDbCoins(balance), | ||
Coins: actionstypes.ConvertCoins(balance), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.