2021-01-11 13:31:30 +00:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2021-04-17 10:00:07 +00:00
|
|
|
"context"
|
2021-01-11 13:31:30 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2022-01-14 09:37:33 +00:00
|
|
|
"errors"
|
2021-10-06 11:22:32 +00:00
|
|
|
"math/big"
|
2021-01-11 13:31:30 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
clientrest "github.com/cosmos/cosmos-sdk/client/rest"
|
2021-01-11 13:31:30 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/rest"
|
|
|
|
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2022-05-02 06:26:24 +00:00
|
|
|
rpctypes "github.com/tharsis/ethermint/rpc/types"
|
2021-10-06 11:22:32 +00:00
|
|
|
feemarkettypes "github.com/tharsis/ethermint/x/feemarket/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-01-11 13:31:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// RegisterTxRoutes - Central function to define routes that get registered by the main application
|
|
|
|
func RegisterTxRoutes(clientCtx client.Context, rtr *mux.Router) {
|
|
|
|
r := clientrest.WithHTTPDeprecationHeaders(rtr)
|
|
|
|
r.HandleFunc("/txs/{hash}", QueryTxRequestHandlerFn(clientCtx)).Methods("GET")
|
|
|
|
r.HandleFunc("/txs", authrest.QueryTxsRequestHandlerFn(clientCtx)).Methods("GET")
|
|
|
|
r.HandleFunc("/txs/decode", authrest.DecodeTxRequestHandlerFn(clientCtx)).Methods("POST")
|
2021-01-11 13:31:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// QueryTxRequestHandlerFn implements a REST handler that queries a transaction
|
|
|
|
// by hash in a committed block.
|
|
|
|
func QueryTxRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
|
2021-01-11 13:31:30 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
hashHexStr := vars["hash"]
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
clientCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, clientCtx, r)
|
2021-01-11 13:31:30 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ethHashPrefix := "0x"
|
2021-04-17 10:00:07 +00:00
|
|
|
if !strings.HasPrefix(hashHexStr, ethHashPrefix) {
|
|
|
|
authrest.QueryTxRequestHandlerFn(clientCtx)
|
2021-01-11 13:31:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// eth Tx
|
|
|
|
ethHashPrefixLength := len(ethHashPrefix)
|
|
|
|
output, err := getEthTransactionByHash(clientCtx, hashHexStr[ethHashPrefixLength:])
|
2021-01-11 13:31:30 +00:00
|
|
|
if err != nil {
|
|
|
|
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
rest.PostProcessResponseBare(w, clientCtx, output)
|
|
|
|
}
|
2021-01-11 13:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTransactionByHash returns the transaction identified by hash.
|
2021-04-17 10:00:07 +00:00
|
|
|
func getEthTransactionByHash(clientCtx client.Context, hashHex string) ([]byte, error) {
|
2021-01-11 13:31:30 +00:00
|
|
|
hash, err := hex.DecodeString(hashHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
node, err := clientCtx.GetNode()
|
2021-01-11 13:31:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
tx, err := node.Tx(context.Background(), hash, false)
|
2021-01-11 13:31:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can either cache or just leave this out if not necessary
|
2021-04-17 10:00:07 +00:00
|
|
|
block, err := node.Block(context.Background(), &tx.Height)
|
2021-01-11 13:31:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-06 11:22:32 +00:00
|
|
|
client := feemarkettypes.NewQueryClient(clientCtx)
|
|
|
|
res, err := client.BaseFee(context.Background(), &feemarkettypes.QueryBaseFeeRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var baseFee *big.Int
|
|
|
|
if res.BaseFee != nil {
|
|
|
|
baseFee = res.BaseFee.BigInt()
|
|
|
|
}
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
blockHash := common.BytesToHash(block.Block.Header.Hash())
|
2021-01-11 13:31:30 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
ethTxs, err := rpctypes.RawTxToEthTx(clientCtx, tx.Tx)
|
2021-01-11 13:31:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
height := uint64(tx.Height)
|
2021-10-06 11:22:32 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
for _, ethTx := range ethTxs {
|
|
|
|
if common.HexToHash(ethTx.Hash) == common.BytesToHash(hash) {
|
|
|
|
rpcTx, err := rpctypes.NewRPCTransaction(ethTx.AsTransaction(), blockHash, height, uint64(tx.Index), baseFee)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return json.Marshal(rpcTx)
|
|
|
|
}
|
2021-10-06 11:22:32 +00:00
|
|
|
}
|
2021-05-10 16:34:00 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
return nil, errors.New("eth tx not found")
|
2021-01-11 13:31:30 +00:00
|
|
|
}
|