-
Notifications
You must be signed in to change notification settings - Fork 0
/
celo.go
99 lines (84 loc) · 3.03 KB
/
celo.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package blockchain
import (
"context"
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/rs/zerolog/log"
"github.com/goplugin/plugin-testing-framework/lib/utils/conversions"
)
// Handles specific issues with the Celo EVM chain: https://docs.celo.org/
// CeloMultinodeClient represents a multi-node, EVM compatible client for the Celo network
type CeloMultinodeClient struct {
*EthereumMultinodeClient
}
// CeloClient represents a single node, EVM compatible client for the Celo network
type CeloClient struct {
*EthereumClient
}
// DeployContract uses legacy txs for Celo to bypass Geth checking Celo headers which do not have a required
// sha3Uncles field
func (e *CeloClient) DeployContract(
contractName string,
deployer ContractDeployer,
) (*common.Address, *types.Transaction, interface{}, error) {
suggestedPrice, err := e.Client.SuggestGasPrice(context.Background())
if err != nil {
return nil, nil, nil, err
}
gasPriceBuffer := big.NewInt(0).SetUint64(e.NetworkConfig.GasEstimationBuffer)
opts, err := e.TransactionOpts(e.DefaultWallet)
if err != nil {
return nil, nil, nil, err
}
opts.GasPrice = suggestedPrice.Add(gasPriceBuffer, suggestedPrice)
contractAddress, transaction, contractInstance, err := deployer(opts, e.Client)
if err != nil {
if strings.Contains(err.Error(), "nonce") {
err = fmt.Errorf("using nonce %d err: %w", opts.Nonce.Uint64(), err)
}
return nil, nil, nil, err
}
if err = e.ProcessTransaction(transaction); err != nil {
return nil, nil, nil, err
}
log.Info().
Str("Contract Address", contractAddress.Hex()).
Str("Contract Name", contractName).
Str("From", e.DefaultWallet.Address()).
Str("Total Gas Cost (CELO)", conversions.WeiToEther(transaction.Cost()).String()).
Str("Network Name", e.NetworkConfig.Name).
Msg("Deployed contract")
return &contractAddress, transaction, contractInstance, err
}
func (e *CeloClient) TransactionOpts(from *EthereumWallet) (*bind.TransactOpts, error) {
privateKey, err := crypto.HexToECDSA(from.PrivateKey())
if err != nil {
return nil, fmt.Errorf("invalid private key: %w", err)
}
opts, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(e.NetworkConfig.ChainID))
if err != nil {
return nil, err
}
opts.From = common.HexToAddress(from.Address())
opts.Context = context.Background()
nonce, err := e.GetNonce(context.Background(), common.HexToAddress(from.Address()))
if err != nil {
return nil, err
}
opts.Nonce = big.NewInt(int64(nonce))
gasPrice, err := e.Client.SuggestGasPrice(context.Background())
if err != nil {
return nil, err
}
gasPriceBuffer := big.NewInt(0).SetUint64(e.NetworkConfig.GasEstimationBuffer)
opts.GasPrice = gasPrice.Add(gasPriceBuffer, gasPrice)
if e.NetworkConfig.MinimumConfirmations <= 0 { // Wait for your turn to send on an L2 chain
<-e.NonceSettings.registerInstantTransaction(from.Address(), nonce)
}
return opts, nil
}