1

i am kinda new to golang and i would appreciate any help.

My main purpose is offline signing EIP1559 transactions based on a json using go-ethereum. I have managed to sign legacy transactions following docs and got success on eth transfers but my erc20 transactions were reverted. I have used types.NewTransaction() method and saw that it was deprecated.

I have been digging around the web to find some examples of the new EIP1559 transaction creation using types.NewTx() but i have been unsuccessful.

1

I have managed to create and sign transactions:

tx := types.NewTx(&types.DynamicFeeTx{
    ChainID: chainID,
    Nonce: nonce,
    GasFeeCap: feeCap,
    GasTipCap: tip,
    Gas: gas,
    To: &toAddress,
    Value: value,
    Data: txData,
})
1
  • Can you please post how you calculated feeCap and gas. This would really help me! Been struggling past two days.
    – Maddy
    Nov 4 at 6:06
0

I am glad you managed to figure out the answer. To anyone else who is struggling, I managed to do the same but with LegacyTx instead of DynamicFeeTx.

Seeing as I've been struggling on this the past day, I thought I would paste the full code to give a better picture for anyone else.

pendingNonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
    log.Fatal(err)
}
gasPrice := big.NewInt(1000000)
gas := uint64(6721975)
toAddress := common.HexToAddress(pubAddress)
value := big.NewInt(1000000000000000000) // in wei (1eth)

tx := types.NewTx(&types.LegacyTx{
  Nonce: pendingNonce,
  GasPrice: gasPrice,
  Gas: gas,
  To: &toAddress,
  Value: value,
})

chainID, err := client.NetworkID(context.Background())
if err != nil {log.Fatal(err)}

signedtx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
    log.Fatal(err)
}
fmt.Println(signedtx)

t := client.SendTransaction(context.Background(), signedtx)
fmt.Println(t)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.