Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mantle int. #701

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions blockchain/known_networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
WeMixClientImplementation ClientImplementation = "WeMix"
KromaClientImplementation ClientImplementation = "Kroma"
GnosisClientImplementation ClientImplementation = "Gnosis"
MantleClientImplementation ClientImplementation = "Mantle"
)

// wrapSingleClient Wraps a single EVM client in its appropriate implementation, based on the chain ID
Expand Down Expand Up @@ -66,6 +67,8 @@ func wrapSingleClient(networkSettings EVMNetwork, client *EthereumClient) EVMCli
wrappedEc = &KromaClient{client}
case GnosisClientImplementation:
wrappedEc = &GnosisClient{client}
case MantleClientImplementation:
wrappedEc = &MantleClient{client}
default:
wrappedEc = client
}
Expand Down Expand Up @@ -128,6 +131,9 @@ func wrapMultiClient(networkSettings EVMNetwork, client *EthereumMultinodeClient
case GnosisClientImplementation:
logMsg.Msg("Using Gnosis Client")
wrappedEc = &GnosisMultinodeClient{client}
case MantleClientImplementation:
logMsg.Msg("Using Mantle Client")
wrappedEc = &MantleMultinodeClient{client}
default:
log.Warn().Str("Network", networkSettings.Name).Msg("Unknown client implementation, defaulting to standard Ethereum client")
wrappedEc = client
Expand Down
115 changes: 115 additions & 0 deletions blockchain/mantle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package blockchain

import (
"context"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum"
"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/smartcontractkit/chainlink-testing-framework/utils/conversions"
)

var multiplier *big.Int = big.NewInt(1000)

// MantleMultinodeClient represents a multi-node, EVM compatible client for the Mantle network
type MantleMultinodeClient struct {
*EthereumMultinodeClient
}

// MantleClient represents a single node, EVM compatible client for the Mantle network
type MantleClient struct {
*EthereumClient
}

func (b *MantleClient) EstimateGas(callData ethereum.CallMsg) (GasEstimations, error) {
gasEstimations, err := b.EthereumClient.EstimateGas(callData)
if err != nil {
return GasEstimations{}, err
}
gasEstimations.GasPrice.Mul(gasEstimations.GasPrice, multiplier)

return gasEstimations, err
}

// DeployContract acts as a general contract deployment tool to an ethereum chain
func (b *MantleClient) DeployContract(
contractName string,
deployer ContractDeployer,
) (*common.Address, *types.Transaction, interface{}, error) {
opts, err := b.TransactionOpts(b.DefaultWallet)
if err != nil {
return nil, nil, nil, err
}
fmt.Printf("%+v\n", opts)
opts.GasPrice, err = b.EstimateGasPrice()
fmt.Printf("Gas Price: %s\n", opts.GasPrice.String())
if err != nil {
return nil, nil, nil, err
}
opts.GasPrice.Mul(opts.GasPrice, multiplier)
fmt.Printf("Gas Price: %s\n", opts.GasPrice.String())
fmt.Printf("Gas Limit: %d\n", opts.GasLimit)
// opts.GasPrice.Add(opts.GasPrice, big.NewInt(1))
// fmt.Printf("Gas Price: %s\n", opts.GasPrice.String())
contractAddress, transaction, contractInstance, err := deployer(opts, b.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 = b.ProcessTransaction(transaction); err != nil {
return nil, nil, nil, err
}

b.l.Info().
Str("Contract Address", contractAddress.Hex()).
Str("Contract Name", contractName).
Str("From", b.DefaultWallet.Address()).
Str("Total Gas Cost", conversions.WeiToEther(transaction.Cost()).String()).
Str("Network Name", b.NetworkConfig.Name).
Msg("Deployed contract")
return &contractAddress, transaction, contractInstance, err
}

// TransactionOpts returns the base Tx options for 'transactions' that interact with a smart contract. Since most
// contract interactions in this framework are designed to happen through abigen calls, it's intentionally quite bare.
func (b *MantleClient) 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(b.NetworkConfig.ChainID))
if err != nil {
return nil, err
}
opts.From = common.HexToAddress(from.Address())
opts.Context = context.Background()

nonce, err := b.GetNonce(context.Background(), common.HexToAddress(from.Address()))
if err != nil {
return nil, err
}
opts.Nonce = big.NewInt(int64(nonce))

if b.NetworkConfig.MinimumConfirmations <= 0 { // Wait for your turn to send on an L2 chain
<-b.NonceSettings.registerInstantTransaction(from.Address(), nonce)
}
// if the gas limit is less than the default gas limit, use the default
if b.NetworkConfig.DefaultGasLimit > opts.GasLimit {
opts.GasLimit = b.NetworkConfig.DefaultGasLimit
}
opts.GasLimit = 10_000_000
opts.GasPrice, err = b.EstimateGasPrice()
if err != nil {
return nil, err
}
opts.GasPrice.Mul(opts.GasPrice, multiplier)
return opts, nil
}
26 changes: 26 additions & 0 deletions networks/known_networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,30 @@ var (
DefaultGasLimit: 6000000,
}

MantleMainnet blockchain.EVMNetwork = blockchain.EVMNetwork{
Name: "Mantle Mainnet",
SupportsEIP1559: false,
ClientImplementation: blockchain.MantleClientImplementation,
ChainID: 5000,
Simulated: false,
ChainlinkTransactionLimit: 5000,
Timeout: blockchain.StrDuration{Duration: 3 * time.Minute},
MinimumConfirmations: 0,
GasEstimationBuffer: 1000,
}

MantleGoerli blockchain.EVMNetwork = blockchain.EVMNetwork{
Name: "Mantle Goerli",
SupportsEIP1559: false,
ClientImplementation: blockchain.MantleClientImplementation,
ChainID: 5001,
Simulated: false,
ChainlinkTransactionLimit: 5000,
Timeout: blockchain.StrDuration{Duration: 3 * time.Minute},
MinimumConfirmations: 0,
GasEstimationBuffer: 10000,
}

MappedNetworks = map[string]blockchain.EVMNetwork{
"SIMULATED": SimulatedEVM,
"SIMULATED_1": SimulatedEVMNonDev1,
Expand Down Expand Up @@ -812,6 +836,8 @@ var (
"NEXON_STAGE": NexonStage,
"GNOSIS_CHIADO": GnosisChiado,
"GNOSIS_MAINNET": GnosisMainnet,
"MANTLE_GOERLI": MantleGoerli,
"MANTLE_MAINNET": MantleMainnet,
}
)

Expand Down