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

Commit

Permalink
add new rest endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Benzbeeb committed Jun 8, 2020
1 parent 58413e7 commit ed684af
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Chain

- (feat) [\#1905](https://github.com/bandprotocol/bandchain/pull/1905) Add request + response packet to request rest endpoint.
- (impv) [\#1901](https://github.com/bandprotocol/bandchain/pull/1901) Added `moniker` field to `delegations_view` table.
- (feat) [\#1879](https://github.com/bandprotocol/bandchain/pull/1879) Keep `Request` and `Report` around. We avoid premature optimization at the moment.
- (feat) [\#1873](https://github.com/bandprotocol/bandchain/pull/1873) Add `in-before-resolve` field in `Report` data structure.
Expand Down
1 change: 1 addition & 0 deletions chain/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/bandchain/evm-validators", GetEVMValidators(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/bandchain/proof/{%s}", proof.RequestIDTag), proof.GetProofHandlerFn(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/bandchain/getfile/{%s}", Filename), GetFile()).Methods("GET")
r.HandleFunc(fmt.Sprintf("/bandchain/getresult/{%s}", proof.RequestIDTag), GetResult(cliCtx)).Methods("GET")
}
70 changes: 70 additions & 0 deletions chain/client/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package rpc

import (
"net/http"
"strconv"

"github.com/bandprotocol/bandchain/chain/client/proof"
"github.com/bandprotocol/bandchain/chain/pkg/obi"
"github.com/bandprotocol/bandchain/chain/x/oracle"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
rpcclient "github.com/tendermint/tendermint/rpc/client"

otypes "github.com/bandprotocol/bandchain/chain/x/oracle/types"
)

type Result struct {
ReqPacket oracle.OracleRequestPacketData `json:"oracleRequestPacketData"`
ResPacket oracle.OracleResponsePacketData `json:"oracleResponsePacketData"`
}

func GetResult(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
intRequestID, err := strconv.ParseUint(vars[proof.RequestIDTag], 10, 64)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
requestID := otypes.RequestID(intRequestID)

commit, err := cliCtx.Client.Commit(nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

resp, err := cliCtx.Client.ABCIQueryWithOptions(
"/store/oracle/key",
otypes.ResultStoreKey(requestID),
rpcclient.ABCIQueryOptions{Height: commit.Height - 1, Prove: true},
)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

resValue := resp.Response.GetValue()

type result struct {
oracle.OracleRequestPacketData
oracle.OracleResponsePacketData
}
var rs result
obi.MustDecode(resValue, &rs)
reqPacket := otypes.NewOracleRequestPacketData(
rs.OracleRequestPacketData.ClientID, rs.OracleRequestPacketData.OracleScriptID, rs.OracleRequestPacketData.Calldata,
rs.OracleRequestPacketData.AskCount, rs.OracleRequestPacketData.MinCount)
resPacket := otypes.NewOracleResponsePacketData(
rs.OracleResponsePacketData.ClientID, rs.OracleResponsePacketData.RequestID, rs.OracleResponsePacketData.AnsCount,
rs.OracleResponsePacketData.RequestTime, rs.OracleResponsePacketData.ResolveTime, rs.OracleResponsePacketData.ResolveStatus,
rs.OracleResponsePacketData.Result)

rest.PostProcessResponse(w, cliCtx, Result{
ReqPacket: reqPacket,
ResPacket: resPacket,
})
}
}

0 comments on commit ed684af

Please sign in to comment.