-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract_stub.go
70 lines (59 loc) · 2.15 KB
/
contract_stub.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
60
61
62
63
64
65
66
67
68
69
70
package swan
import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"math/big"
"strings"
)
func privateKeyToPublicKey(privateKey string) (common.Address, error) {
if len(strings.TrimSpace(privateKey)) == 0 {
return common.Address{}, fmt.Errorf("wallet address private key must be not empty")
}
privateK, err := crypto.HexToECDSA(privateKey)
if err != nil {
return common.Address{}, fmt.Errorf("parses private key error: %+v", err)
}
publicKey := privateK.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return common.Address{}, fmt.Errorf("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
}
return crypto.PubkeyToAddress(*publicKeyECDSA), nil
}
func CreateTransactOpts(client *ethclient.Client, privateKey string) (*bind.TransactOpts, error) {
if privateKey == "" {
return nil, fmt.Errorf("required privateKey field")
}
publicAddress, err := privateKeyToPublicKey(privateKey)
nonce, err := client.PendingNonceAt(context.Background(), publicAddress)
if err != nil {
return nil, fmt.Errorf("failed to get nonce error: %v", err)
}
suggestGasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to retrieves the currently suggested gas price, error: %v", err)
}
chainId, err := client.ChainID(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get networkId, error: %v", err)
}
privateK, err := crypto.HexToECDSA(privateKey)
if err != nil {
return nil, fmt.Errorf("parses private key error: %+v", err)
}
txOptions, err := bind.NewKeyedTransactorWithChainID(privateK, chainId)
if err != nil {
return nil, fmt.Errorf("failed to create transaction opts, error: %v", err)
}
txOptions.Nonce = big.NewInt(int64(nonce))
suggestGasPrice = suggestGasPrice.Mul(suggestGasPrice, big.NewInt(3))
suggestGasPrice = suggestGasPrice.Div(suggestGasPrice, big.NewInt(2))
txOptions.GasFeeCap = suggestGasPrice
txOptions.Context = context.Background()
return txOptions, nil
}