-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw_transaction.go
59 lines (51 loc) · 1.51 KB
/
raw_transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"context"
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"goweb3/config"
"goweb3/connect"
"log"
"math/big"
)
func main() {
client := connect.Wss_eth()
pv := config.Get_pv()
fromAddress := config.Get_fromaddr()
toaddr := config.Get_toaddr()
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
chainID, err := client.NetworkID(context.Background())
value := big.NewInt(1000000000000000) // in wei (1 eth)
gasLimit := uint64(21000) // in units
gasFeeCap, err := client.SuggestGasPrice(context.Background())
gasTipCap, err := client.SuggestGasTipCap(context.Background())
var data []byte
tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
GasFeeCap: gasFeeCap,
GasTipCap: gasTipCap,
Gas: gasLimit,
To: &toaddr,
Value: value,
Data: data,
})
signedTx, err := types.SignTx(tx, types.NewLondonSigner(chainID), pv)
ts := types.Transactions{signedTx}
rawTxBytes, _ := rlp.EncodeToBytes(ts[0]) //<-- ts.GetRlp undefined
rawTxHex := hex.EncodeToString(rawTxBytes)
fmt.Printf("%s\n", rawTxHex) // f86...772
send_rawTxBytes, err := hex.DecodeString(rawTxHex)
sendtx := new(types.Transaction)
rlp.DecodeBytes(send_rawTxBytes, &sendtx)
err = client.SendTransaction(context.Background(), sendtx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("https://sepolia.etherscan.io/tx/%s \n", sendtx.Hash().Hex())
}