Skip to content
This repository was archived by the owner on Nov 30, 2021. It is now read-only.

Commit 2ca42cc

Browse files
authored
Implement eth_sendRawTransaction (#101)
* Implement sendRawTransaction (tx not being broadcasted to node from server) * Add broadcast type flag to rpc API and fixed amount validation * Add documentation
1 parent 69567e2 commit 2ca42cc

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

rpc/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rpc
33
import (
44
"fmt"
55

6+
"github.com/cosmos/cosmos-sdk/client/flags"
67
"github.com/cosmos/cosmos-sdk/client/lcd"
78
"github.com/cosmos/cosmos-sdk/codec"
89
emintcrypto "github.com/cosmos/ethermint/crypto"
@@ -35,8 +36,8 @@ type Config struct {
3536
// Web3RpcCmd creates a CLI command to start RPC server
3637
func Web3RpcCmd(cdc *codec.Codec) *cobra.Command {
3738
cmd := lcd.ServeCommand(cdc, registerRoutes)
38-
// Attach flag to cmd output to be handled in registerRoutes
3939
cmd.Flags().String(flagUnlockKey, "", "Select a key to unlock on the RPC server")
40+
cmd.Flags().StringP(flags.FlagBroadcastMode, "b", flags.BroadcastSync, "Transaction broadcasting mode (sync|async|block)")
4041
return cmd
4142
}
4243

rpc/eth_api.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"math/big"
77

88
"github.com/cosmos/cosmos-sdk/client/context"
9+
authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
910
emintcrypto "github.com/cosmos/ethermint/crypto"
1011
emintkeys "github.com/cosmos/ethermint/keys"
1112
"github.com/cosmos/ethermint/version"
@@ -14,6 +15,7 @@ import (
1415
"github.com/ethereum/go-ethereum/accounts/keystore"
1516
"github.com/ethereum/go-ethereum/common"
1617
"github.com/ethereum/go-ethereum/common/hexutil"
18+
"github.com/ethereum/go-ethereum/rlp"
1719
"github.com/ethereum/go-ethereum/rpc"
1820
"github.com/ethereum/go-ethereum/signer/core"
1921
)
@@ -204,9 +206,31 @@ func (e *PublicEthAPI) SendTransaction(args core.SendTxArgs) common.Hash {
204206
}
205207

206208
// SendRawTransaction send a raw Ethereum transaction.
207-
func (e *PublicEthAPI) SendRawTransaction(data hexutil.Bytes) common.Hash {
208-
var h common.Hash
209-
return h
209+
func (e *PublicEthAPI) SendRawTransaction(data hexutil.Bytes) (common.Hash, error) {
210+
tx := new(types.EthereumTxMsg)
211+
212+
// RLP decode raw transaction bytes
213+
if err := rlp.DecodeBytes(data, tx); err != nil {
214+
// Return nil is for when gasLimit overflows uint64
215+
return common.Hash{}, nil
216+
}
217+
218+
// Encode transaction by default Tx encoder
219+
txEncoder := authutils.GetTxEncoder(e.cliCtx.Codec)
220+
txBytes, err := txEncoder(tx)
221+
if err != nil {
222+
return common.Hash{}, err
223+
}
224+
225+
// TODO: Possibly log the contract creation address (if recipient address is nil) or tx data
226+
res, err := e.cliCtx.BroadcastTx(txBytes)
227+
// If error is encountered on the node, the broadcast will not return an error
228+
fmt.Println(res.RawLog)
229+
if err != nil {
230+
return common.Hash{}, err
231+
}
232+
233+
return common.HexToHash(res.TxHash), nil
210234
}
211235

212236
// CallArgs represents arguments to a smart contract call as provided by RPC clients.

x/evm/types/msg.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ func (msg EthereumTxMsg) ValidateBasic() sdk.Error {
131131
return types.ErrInvalidValue(fmt.Sprintf("Price must be positive: %x", msg.Data.Price))
132132
}
133133

134-
if msg.Data.Amount.Sign() != 1 {
134+
// Amount can be 0
135+
if msg.Data.Amount.Sign() == -1 {
135136
return types.ErrInvalidValue(fmt.Sprintf("amount must be positive: %x", msg.Data.Amount))
136137
}
137138

0 commit comments

Comments
 (0)