rpc: SendTransaction wrap MsgEthereumTx in cosmos tx (#161)

* `SendTransaction` wrap `MsgEthereumTx` in cosmos tx

Closes #160

* Update ethereum/rpc/eth_api.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
yihuang 2021-06-22 15:32:06 +08:00 committed by GitHub
parent 71572d7ce0
commit 62fe3d7e80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -349,7 +349,21 @@ func (e *PublicEthAPI) SendTransaction(args rpctypes.SendTxArgs) (common.Hash, e
return common.Hash{}, err return common.Hash{}, err
} }
tx := args.ToTransaction() msg := args.ToTransaction()
if err := msg.ValidateBasic(); err != nil {
e.logger.WithError(err).Debugln("tx failed basic validation")
return common.Hash{}, err
}
// creates a new EIP2929 signer
// TODO: support legacy txs
signer := ethtypes.LatestSignerForChainID(args.ChainID.ToInt())
// Sign transaction
if err := msg.Sign(signer, e.clientCtx.Keyring); err != nil {
e.logger.Debugln("failed to sign tx", "error", err)
return common.Hash{}, err
}
// Assemble transaction from fields // Assemble transaction from fields
builder, ok := e.clientCtx.TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder) builder, ok := e.clientCtx.TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder)
@ -364,33 +378,18 @@ func (e *PublicEthAPI) SendTransaction(args rpctypes.SendTxArgs) (common.Hash, e
} }
builder.SetExtensionOptions(option) builder.SetExtensionOptions(option)
err = builder.SetMsgs(tx.GetMsgs()...) err = builder.SetMsgs(msg)
if err != nil { if err != nil {
e.logger.WithError(err).Panicln("builder.SetMsgs failed") e.logger.WithError(err).Panicln("builder.SetMsgs failed")
} }
fees := sdk.NewCoins(ethermint.NewPhotonCoin(sdk.NewIntFromBigInt(tx.Fee()))) fees := sdk.NewCoins(ethermint.NewPhotonCoin(sdk.NewIntFromBigInt(msg.Fee())))
builder.SetFeeAmount(fees) builder.SetFeeAmount(fees)
builder.SetGasLimit(tx.GetGas()) builder.SetGasLimit(msg.GetGas())
if err := tx.ValidateBasic(); err != nil {
e.logger.Debugln("tx failed basic validation", "error", err)
return common.Hash{}, err
}
// creates a new EIP2929 signer
// TODO: support legacy txs
signer := ethtypes.LatestSignerForChainID(args.ChainID.ToInt())
// Sign transaction
if err := tx.Sign(signer, e.clientCtx.Keyring); err != nil {
e.logger.Debugln("failed to sign tx", "error", err)
return common.Hash{}, err
}
// Encode transaction by default Tx encoder // Encode transaction by default Tx encoder
txEncoder := e.clientCtx.TxConfig.TxEncoder() txEncoder := e.clientCtx.TxConfig.TxEncoder()
txBytes, err := txEncoder(tx) txBytes, err := txEncoder(builder.GetTx())
if err != nil { if err != nil {
e.logger.WithError(err).Errorln("failed to encode eth tx using default encoder") e.logger.WithError(err).Errorln("failed to encode eth tx using default encoder")
return common.Hash{}, err return common.Hash{}, err