github.com/prysmaticlabs/prysm@v1.4.4/contracts/deposit-contract/testutils.go (about)

     1  package depositcontract
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"fmt"
     6  	"math/big"
     7  
     8  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
     9  	"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/ethereum/go-ethereum/core"
    12  	"github.com/ethereum/go-ethereum/crypto"
    13  )
    14  
    15  var (
    16  	amount32Eth        = "32000000000000000000"
    17  	amountLessThan1Eth = "500000000000000000"
    18  )
    19  
    20  // TestAccount represents a test account in the simulated backend,
    21  // through which we can perform actions on the eth1.0 chain.
    22  type TestAccount struct {
    23  	Addr         common.Address
    24  	ContractAddr common.Address
    25  	Contract     *DepositContract
    26  	Backend      *backends.SimulatedBackend
    27  	TxOpts       *bind.TransactOpts
    28  }
    29  
    30  // Setup creates the simulated backend with the deposit contract deployed
    31  func Setup() (*TestAccount, error) {
    32  	genesis := make(core.GenesisAlloc)
    33  	privKey, err := crypto.GenerateKey()
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	pubKeyECDSA, ok := privKey.Public().(*ecdsa.PublicKey)
    38  	if !ok {
    39  		return nil, fmt.Errorf("error casting public key to ECDSA")
    40  	}
    41  
    42  	// strip off the 0x and the first 2 characters 04 which is always the EC prefix and is not required.
    43  	publicKeyBytes := crypto.FromECDSAPub(pubKeyECDSA)[4:]
    44  	var pubKey = make([]byte, 48)
    45  	copy(pubKey, publicKeyBytes)
    46  
    47  	addr := crypto.PubkeyToAddress(privKey.PublicKey)
    48  	txOpts, err := bind.NewKeyedTransactorWithChainID(privKey, big.NewInt(1337))
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	startingBalance, _ := new(big.Int).SetString("100000000000000000000000000000000000000", 10)
    53  	genesis[addr] = core.GenesisAccount{Balance: startingBalance}
    54  	backend := backends.NewSimulatedBackend(genesis, 210000000000)
    55  
    56  	contractAddr, _, contract, err := DeployDepositContract(txOpts, backend, addr)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  	backend.Commit()
    61  
    62  	return &TestAccount{addr, contractAddr, contract, backend, txOpts}, nil
    63  }
    64  
    65  // Amount32Eth returns 32Eth(in wei) in terms of the big.Int type.
    66  func Amount32Eth() *big.Int {
    67  	amount, _ := new(big.Int).SetString(amount32Eth, 10)
    68  	return amount
    69  }
    70  
    71  // LessThan1Eth returns less than 1 Eth(in wei) in terms of the big.Int type.
    72  func LessThan1Eth() *big.Int {
    73  	amount, _ := new(big.Int).SetString(amountLessThan1Eth, 10)
    74  	return amount
    75  }