-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtestdata.go
More file actions
61 lines (52 loc) · 1.79 KB
/
Copy pathtestdata.go
File metadata and controls
61 lines (52 loc) · 1.79 KB
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
60
61
package testdata
import (
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
)
func GenerateKeyPair() (*ecdsa.PrivateKey, common.Address, error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
return nil, common.Address{}, err
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return nil, common.Address{}, err
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
return privateKey, fromAddress, nil
}
func Tx(privateKey *ecdsa.PrivateKey, nonce uint64, chainID *big.Int) (string, *types.Transaction, error) {
return TxWithGas(privateKey, nonce, chainID, big.NewInt(2000000000), 21000)
}
func TxWithGasPrice(privateKey *ecdsa.PrivateKey, nonce uint64, chainID *big.Int, gasPrice *big.Int) (string, *types.Transaction, error) {
return TxWithGas(privateKey, nonce, chainID, gasPrice, 21000)
}
func TxWithGas(privateKey *ecdsa.PrivateKey, nonce uint64, chainID *big.Int, gasPrice *big.Int, gasLimit uint64) (string, *types.Transaction, error) {
toAddress := common.HexToAddress("0xC0058BdcC93EaA1afd468f06A26394E2d80c8f01")
value := big.NewInt(120000000000) // in wei (0.12 eth)
maxPriorityFeePerGas := big.NewInt(2000000000)
tx := types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
To: &toAddress,
Value: value,
Gas: gasLimit,
GasFeeCap: gasPrice,
GasTipCap: maxPriorityFeePerGas,
})
signer := types.NewLondonSigner(chainID)
signedTx, err := types.SignTx(tx, signer, privateKey)
if err != nil {
return "", nil, err
}
rawTxBytes, err := signedTx.MarshalBinary()
if err != nil {
return "", nil, err
}
rawTx := "0x" + common.Bytes2Hex(rawTxBytes)
return rawTx, signedTx, nil
}