erigon-pulse/cmd/devnet/contracts/util.go
Mark Holt 751f62615d
Devnet sync events (#7911)
This request implements an end to end Polygon state sync in the devnet.

It does this by deploying smart contracts ont the L2 & L2 chain which
follow the polygon fx portal model with security checks removed to
simplify the code. The sync events generated are routed through a local
mock heimdal - to avoid the consensus process for testing purposes.

The commit also includes support code to help the delivery of additional
contract based scenratios.
2023-07-20 23:10:18 +01:00

77 lines
2.2 KiB
Go

package contracts
import (
"context"
"math/big"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/accounts/abi/bind"
"github.com/ledgerwatch/erigon/cmd/devnet/accounts"
"github.com/ledgerwatch/erigon/cmd/devnet/devnet"
"github.com/ledgerwatch/erigon/cmd/devnet/requests"
"github.com/ledgerwatch/erigon/core/types"
)
func TransactOpts(ctx context.Context, sender libcommon.Address) (*bind.TransactOpts, error) {
node := devnet.SelectNode(ctx)
transactOpts, err := bind.NewKeyedTransactorWithChainID(accounts.SigKey(sender), node.ChainID())
if err != nil {
return nil, err
}
count, err := node.GetTransactionCount(sender, requests.BlockNumbers.Pending)
if err != nil {
return nil, err
}
transactOpts.GasLimit = uint64(200_000)
transactOpts.GasPrice = big.NewInt(880_000_000)
transactOpts.Nonce = count
return transactOpts, nil
}
func DeploymentTransactor(ctx context.Context, deployer libcommon.Address) (*bind.TransactOpts, bind.ContractBackend, error) {
node := devnet.SelectNode(ctx)
transactOpts, err := TransactOpts(ctx, deployer)
if err != nil {
return nil, nil, err
}
return transactOpts, NewBackend(node), nil
}
func Deploy[C any](ctx context.Context, deployer libcommon.Address, deploy func(auth *bind.TransactOpts, backend bind.ContractBackend) (libcommon.Address, types.Transaction, *C, error)) (libcommon.Address, *C, error) {
transactOpts, err := bind.NewKeyedTransactorWithChainID(accounts.SigKey(deployer), devnet.CurrentChainID(ctx))
if err != nil {
return libcommon.Address{}, nil, err
}
return DeployWithOps[C](ctx, transactOpts, deploy)
}
func DeployWithOps[C any](ctx context.Context, auth *bind.TransactOpts, deploy func(auth *bind.TransactOpts, backend bind.ContractBackend) (libcommon.Address, types.Transaction, *C, error)) (libcommon.Address, *C, error) {
node := devnet.SelectNode(ctx)
count, err := node.GetTransactionCount(auth.From, requests.BlockNumbers.Pending)
if err != nil {
return libcommon.Address{}, nil, err
}
auth.GasLimit = uint64(200_000)
auth.GasPrice = big.NewInt(880_000_000)
auth.Nonce = count
// deploy the contract and get the contract handler
address, _, contract, err := deploy(auth, NewBackend(node))
return address, contract, err
}