Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
fix zero fee coins
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jan 26, 2022
1 parent 5926529 commit 4b62961
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
30 changes: 4 additions & 26 deletions rpc/ethereum/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import (
"time"

"github.com/cosmos/cosmos-sdk/client/flags"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -802,43 +800,23 @@ func (e *EVMBackend) SendTransaction(args evmtypes.TransactionArgs) (common.Hash
return common.Hash{}, err
}

// Assemble transaction from fields
builder, ok := e.clientCtx.TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder)
if !ok {
e.logger.Error("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error())
}

option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{})
if err != nil {
e.logger.Error("codectypes.NewAnyWithValue failed to pack an obvious value", "error", err.Error())
return common.Hash{}, err
}

builder.SetExtensionOptions(option)
if err = builder.SetMsgs(msg); err != nil {
e.logger.Error("builder.SetMsgs failed", "error", err.Error())
}

// Query params to use the EVM denomination
res, err := e.queryClient.QueryClient.Params(e.ctx, &evmtypes.QueryParamsRequest{})
if err != nil {
e.logger.Error("failed to query evm params", "error", err.Error())
return common.Hash{}, err
}

txData, err := evmtypes.UnpackTxData(msg.Data)
// Assemble transaction from fields
tx, err := msg.BuildTx(e.clientCtx.TxConfig.NewTxBuilder(), res.Params.EvmDenom)
if err != nil {
e.logger.Error("failed to unpack tx data", "error", err.Error())
e.logger.Error("build cosmos tx failed", "error", err.Error())
return common.Hash{}, err
}

fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))}
builder.SetFeeAmount(fees)
builder.SetGasLimit(msg.GetGas())

// Encode transaction by default Tx encoder
txEncoder := e.clientCtx.TxConfig.TxEncoder()
txBytes, err := txEncoder(builder.GetTx())
txBytes, err := txEncoder(tx)
if err != nil {
e.logger.Error("failed to encode eth tx using default encoder", "error", err.Error())
return common.Hash{}, err
Expand Down
5 changes: 5 additions & 0 deletions x/evm/keeper/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func (k Keeper) DeductTxCostsFromUserBalance(
feeAmt = txData.Fee()
}

if feeAmt.Sign() == 0 {
// zero fee, no need to deduct
return sdk.NewCoins(), nil
}

fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(feeAmt))}

// deduct the full gas cost from the user balance
Expand Down
9 changes: 4 additions & 5 deletions x/evm/types/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,10 @@ func (msg *MsgEthereumTx) BuildTx(b client.TxBuilder, evmDenom string) (signing.
if err != nil {
return nil, err
}
fees := sdk.Coins{
{
Denom: evmDenom,
Amount: sdk.NewIntFromBigInt(txData.Fee()),
},
fees := make(sdk.Coins, 0)
feeAmt := sdk.NewIntFromBigInt(txData.Fee())
if feeAmt.Sign() > 0 {
fees = append(fees, sdk.NewCoin(evmDenom, feeAmt))
}

builder.SetExtensionOptions(option)
Expand Down

0 comments on commit 4b62961

Please sign in to comment.