github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/vm/wasmapi/handler/read_tx.go (about)

     1  package handler
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  
     7  	"github.com/blocto/solana-go-sdk/client"
     8  	"github.com/gin-gonic/gin"
     9  	"github.com/gin-gonic/gin/binding"
    10  	"github.com/pkg/errors"
    11  
    12  	"github.com/machinefi/w3bstream/pkg/depends/kit/logr"
    13  	"github.com/machinefi/w3bstream/pkg/enums"
    14  	"github.com/machinefi/w3bstream/pkg/types/wasm"
    15  )
    16  
    17  type readTxReq struct {
    18  	ChainName enums.ChainName `json:"chainName"   binding:"required"`
    19  	Hash      string          `json:"hash"        binding:"required"`
    20  }
    21  
    22  type readEthTxResp struct {
    23  	Transaction any `json:"transaction,omitempty"`
    24  }
    25  
    26  type readSolanaTxResp struct {
    27  	Transaction *client.Transaction `json:"result,omitempty"`
    28  }
    29  
    30  func (h *Handler) ReadTx(c *gin.Context) {
    31  	// l := types.MustLoggerFromContext(c.Request.Context())
    32  	_, l := logr.Start(c, "vm.Handler.ReadTx")
    33  	defer l.End()
    34  
    35  	var req readTxReq
    36  	if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
    37  		l.Error(errors.Wrap(err, "decode http request failed"))
    38  		c.JSON(http.StatusBadRequest, newErrResp(err))
    39  		return
    40  	}
    41  
    42  	l = l.WithValues("chain_name", req.ChainName)
    43  
    44  	_, ok := h.chainConf.Chains[req.ChainName]
    45  	if !ok {
    46  		err := errors.New("blockchain not exist")
    47  		l.Error(err)
    48  		c.JSON(http.StatusBadRequest, newErrResp(err))
    49  		return
    50  	}
    51  
    52  	if err := h.setAsync(c); err != nil {
    53  		l.Error(err)
    54  		c.JSON(http.StatusInternalServerError, newErrResp(err))
    55  		return
    56  	}
    57  
    58  	c.Status(http.StatusOK)
    59  }
    60  
    61  func (h *Handler) ReadTxAsync(c *gin.Context) {
    62  	// l := types.MustLoggerFromContext(c.Request.Context())
    63  	_, l := logr.Start(c, "vm.Handler.ReadTxAsync")
    64  	defer l.End()
    65  
    66  	var req readTxReq
    67  	c.ShouldBindJSON(&req)
    68  
    69  	l = l.WithValues("chain_name", req.ChainName)
    70  
    71  	chain := h.chainConf.Chains[req.ChainName]
    72  
    73  	var resp any
    74  
    75  	switch {
    76  	case chain.IsEth():
    77  		client := wasm.NewEthClient(chain)
    78  		tx, err := client.TransactionByHash(context.Background(), req.Hash)
    79  		if err != nil {
    80  			l.Error(err)
    81  			c.JSON(http.StatusInternalServerError, newErrResp(err))
    82  			return
    83  		}
    84  		resp = &readEthTxResp{Transaction: tx}
    85  
    86  	case chain.IsSolana():
    87  		cli := client.NewClient(chain.Endpoint)
    88  		tx, err := cli.GetTransaction(context.Background(), req.Hash)
    89  		if err != nil {
    90  			l.Error(errors.Wrap(err, "query transaction failed"))
    91  			c.JSON(http.StatusInternalServerError, newErrResp(err))
    92  			return
    93  		}
    94  		resp = &readSolanaTxResp{Transaction: tx}
    95  
    96  	default:
    97  		err := errors.New("server error")
    98  		l.Error(err)
    99  		c.JSON(http.StatusInternalServerError, newErrResp(err))
   100  		return
   101  	}
   102  
   103  	c.JSON(http.StatusOK, resp)
   104  }