forked from dcb9/janus
-
Notifications
You must be signed in to change notification settings - Fork 27
/
eth_sendRawTransaction.go
64 lines (54 loc) · 2.01 KB
/
eth_sendRawTransaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package transformer
import (
"context"
"github.com/labstack/echo"
"github.com/qtumproject/janus/pkg/eth"
"github.com/qtumproject/janus/pkg/qtum"
"github.com/qtumproject/janus/pkg/utils"
)
// ProxyETHSendRawTransaction implements ETHProxy
type ProxyETHSendRawTransaction struct {
*qtum.Qtum
}
var _ ETHProxy = (*ProxyETHSendRawTransaction)(nil)
func (p *ProxyETHSendRawTransaction) Method() string {
return "eth_sendRawTransaction"
}
func (p *ProxyETHSendRawTransaction) Request(req *eth.JSONRPCRequest, c echo.Context) (interface{}, eth.JSONRPCError) {
var params eth.SendRawTransactionRequest
if err := unmarshalRequest(req.Params, ¶ms); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
if params[0] == "" {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError("invalid parameter: raw transaction hexed string is empty")
}
return p.request(c.Request().Context(), params)
}
func (p *ProxyETHSendRawTransaction) request(ctx context.Context, params eth.SendRawTransactionRequest) (eth.SendRawTransactionResponse, eth.JSONRPCError) {
var (
qtumHexedRawTx = utils.RemoveHexPrefix(params[0])
req = qtum.SendRawTransactionRequest([1]string{qtumHexedRawTx})
)
qtumresp, err := p.Qtum.SendRawTransaction(ctx, &req)
if err != nil {
if err == qtum.ErrVerifyAlreadyInChain {
// already committed
// we need to send back the tx hash
rawTx, err := p.Qtum.DecodeRawTransaction(ctx, qtumHexedRawTx)
if err != nil {
p.GetErrorLogger().Log("msg", "Error decoding raw transaction for duplicate raw transaction", "err", err)
return eth.SendRawTransactionResponse(""), eth.NewCallbackError(err.Error())
}
qtumresp = &qtum.SendRawTransactionResponse{Result: rawTx.Hash}
} else {
return eth.SendRawTransactionResponse(""), eth.NewCallbackError(err.Error())
}
} else {
p.GenerateIfPossible()
}
resp := *qtumresp
ethHexedTxHash := utils.AddHexPrefix(resp.Result)
return eth.SendRawTransactionResponse(ethHexedTxHash), nil
}