Copy
Ask AI
package main
import (
"encoding/hex"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
)
func main() {
to := common.HexToAddress("0x...")
txData := &types.DynamicFeeTx{
ChainID: big.NewInt(1),
Nonce: 0,
GasTipCap: big.NewInt(12344),
GasFeeCap: big.NewInt(10010000000),
Gas: 100000,
To: &to,
Value: big.NewInt(1),
Data: hexDecode("<tx_data>"),
AccessList: types.AccessList{}, // Optional
}
// RLP-encode only the fields included in the unsigned tx
unsignedRLP := encodeUnsignedDynamicFeeTx(txData)
// Prepend EIP-1559 type byte (0x02)
serializedUnsigned := append([]byte{types.DynamicFeeTxType}, unsignedRLP...)
fmt.Printf("Unsigned serialized tx: 0x%x\n", serializedUnsigned)
}
func encodeUnsignedDynamicFeeTx(tx *types.DynamicFeeTx) []byte {
rlpInput := []interface{}{
tx.ChainID,
tx.Nonce,
tx.GasTipCap,
tx.GasFeeCap,
tx.Gas,
tx.To,
tx.Value,
tx.Data,
tx.AccessList,
}
out, err := rlp.EncodeToBytes(rlpInput)
if err != nil {
log.Fatalf("failed to encode RLP: %v", err)
}
return out
}
func hexDecode(input string) []byte {
b, err := hex.DecodeString(input)
if err != nil {
log.Fatal(err)
}
return b
}