diff --git a/action/envelope.go b/action/envelope.go index 3db70fb11a..f79b60c4da 100644 --- a/action/envelope.go +++ b/action/envelope.go @@ -64,6 +64,7 @@ type ( setNonce(uint64) setGas(uint64) setChainID(uint32) + toEthTx(EthCompatibleAction) (*types.Transaction, error) } TxDynamicGas interface { @@ -217,58 +218,12 @@ func (elp *envelope) Action() Action { return elp.payload } // ToEthTx converts to Ethereum tx func (elp *envelope) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding) (*types.Transaction, error) { - tx, ok := elp.Action().(EthCompatibleAction) + act, ok := elp.Action().(EthCompatibleAction) if !ok { // action type not supported return nil, ErrInvalidAct } - to, err := tx.EthTo() - if err != nil { - return nil, err - } - data, err := tx.EthData() - if err != nil { - return nil, err - } - var ( - common = elp.common - txdata types.TxData - ) - switch common.Version() { - case LegacyTxType: - txdata = &types.LegacyTx{ - Nonce: common.Nonce(), - GasPrice: common.GasPrice(), - Gas: common.Gas(), - To: to, - Value: tx.Value(), - Data: data, - } - case AccessListTxType: - txdata = &types.AccessListTx{ - Nonce: common.Nonce(), - GasPrice: common.GasPrice(), - Gas: common.Gas(), - To: to, - Value: tx.Value(), - Data: data, - AccessList: common.AccessList(), - } - case DynamicFeeTxType: - txdata = &types.DynamicFeeTx{ - Nonce: common.Nonce(), - GasTipCap: common.GasTipCap(), - GasFeeCap: common.GasFeeCap(), - Gas: common.Gas(), - To: to, - Value: tx.Value(), - Data: data, - AccessList: common.AccessList(), - } - default: - return nil, errors.Wrapf(ErrInvalidAct, "unsupported type = %v", common.Version()) - } - return types.NewTx(txdata), nil + return elp.common.toEthTx(act) } // Proto convert Envelope to protobuf format. diff --git a/action/tx_access_list.go b/action/tx_access_list.go index 5c171bebd6..7df0e2e92e 100644 --- a/action/tx_access_list.go +++ b/action/tx_access_list.go @@ -161,3 +161,23 @@ func (tx *AccessListTx) setGas(gas uint64) { func (tx *AccessListTx) setChainID(n uint32) { tx.chainID = n } + +func (tx *AccessListTx) toEthTx(ec EthCompatibleAction) (*types.Transaction, error) { + to, err := ec.EthTo() + if err != nil { + return nil, err + } + data, err := ec.EthData() + if err != nil { + return nil, err + } + return types.NewTx(&types.AccessListTx{ + Nonce: tx.nonce, + GasPrice: tx.price(), + Gas: tx.gasLimit, + To: to, + Value: ec.Value(), + Data: data, + AccessList: tx.accessList, + }), nil +} diff --git a/action/tx_blob.go b/action/tx_blob.go index fb50428361..b1bf917fa3 100644 --- a/action/tx_blob.go +++ b/action/tx_blob.go @@ -170,3 +170,8 @@ func (tx *BlobTx) setGas(gas uint64) { func (tx *BlobTx) setChainID(n uint32) { tx.chainID = n } + +func (tx *BlobTx) toEthTx(ec EthCompatibleAction) (*types.Transaction, error) { + // TODO: enable blob tx + return nil, nil +} diff --git a/action/tx_dynamic_fee.go b/action/tx_dynamic_fee.go index 691f992081..55aa11326e 100644 --- a/action/tx_dynamic_fee.go +++ b/action/tx_dynamic_fee.go @@ -153,3 +153,24 @@ func (tx *DynamicFeeTx) setGas(gas uint64) { func (tx *DynamicFeeTx) setChainID(n uint32) { tx.chainID = n } + +func (tx *DynamicFeeTx) toEthTx(ec EthCompatibleAction) (*types.Transaction, error) { + to, err := ec.EthTo() + if err != nil { + return nil, err + } + data, err := ec.EthData() + if err != nil { + return nil, err + } + return types.NewTx(&types.DynamicFeeTx{ + Nonce: tx.nonce, + GasTipCap: tx.GasTipCap(), + GasFeeCap: tx.GasFeeCap(), + Gas: tx.gasLimit, + To: to, + Value: ec.Value(), + Data: data, + AccessList: tx.accessList, + }), nil +} diff --git a/action/tx_legacy.go b/action/tx_legacy.go index e2a22b1959..e4bc25a5b2 100644 --- a/action/tx_legacy.go +++ b/action/tx_legacy.go @@ -117,3 +117,22 @@ func (tx *LegacyTx) setGas(gas uint64) { func (tx *LegacyTx) setChainID(n uint32) { tx.chainID = n } + +func (tx *LegacyTx) toEthTx(ec EthCompatibleAction) (*types.Transaction, error) { + to, err := ec.EthTo() + if err != nil { + return nil, err + } + data, err := ec.EthData() + if err != nil { + return nil, err + } + return types.NewTx(&types.LegacyTx{ + Nonce: tx.nonce, + GasPrice: tx.price(), + Gas: tx.gasLimit, + To: to, + Value: ec.Value(), + Data: data, + }), nil +}