Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Sep 20, 2024
1 parent 61bad48 commit 0145c4e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 48 deletions.
51 changes: 3 additions & 48 deletions action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type (
setNonce(uint64)
setGas(uint64)
setChainID(uint32)
toEthTx(EthCompatibleAction) (*types.Transaction, error)
}

TxDynamicGas interface {
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions action/tx_access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions action/tx_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 21 additions & 0 deletions action/tx_dynamic_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions action/tx_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0145c4e

Please sign in to comment.