2022-01-26 14:48:20 +00:00
|
|
|
package mock
|
2021-09-22 17:27:13 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/contracts/deposit"
|
2021-09-22 17:27:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
amount32Eth = "32000000000000000000"
|
|
|
|
amountLessThan1Eth = "500000000000000000"
|
|
|
|
depositContractBin = "0x608060405234801561001057600080fd5b5060005b601f8110156101025760026021826020811061002c57fe5b01546021836020811061003b57fe5b015460405160200180838152602001828152602001925050506040516020818303038152906040526040518082805190602001908083835b602083106100925780518252601f199092019160209182019101610073565b51815160209384036101000a60001901801990921691161790526040519190930194509192505080830381855afa1580156100d1573d6000803e3d6000fd5b5050506040513d60208110156100e657600080fd5b5051602160018301602081106100f857fe5b0155600101610014565b506118d680620001136000396000f3fe60806040526004361061003f5760003560e01c806301ffc9a71461004457806322895118146100a4578063621fd130146101ba578063c5f2892f14610244575b600080fd5b34801561005057600080fd5b506100906004803603602081101561006757600080fd5b50357fffffffff000000000000000000000000000000000000000000000000000000001661026b565b604080519115158252519081900360200190f35b6101b8600480360360808110156100ba57600080fd5b8101906020810181356401000000008111156100d557600080fd5b8201836020820111156100e757600080fd5b8035906020019184600183028401116401000000008311171561010957600080fd5b91939092909160208101903564010000000081111561012757600080fd5b82018360208201111561013957600080fd5b8035906020019184600183028401116401000000008311171561015b57600080fd5b91939092909160208101903564010000000081111561017957600080fd5b82018360208201111561018b57600080fd5b803590602001918460018302840111640100000000831117156101ad57600080fd5b919350915035610304565b005b3480156101c657600080fd5b506101cf6110b5565b6040805160208082528351818301528351919283929083019185019080838360005b838110156102095781810151838201526020016101f1565b50505050905090810190601f1680156102365780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561025057600080fd5b506102596110c7565b60408051918252519081900360200190f35b60007fffffffff0000000000000000000000000000000000000000000000000000000082167f01ffc9a70000000000000000000000000000000000000000000000000000000014806102fe57507fffffffff0000000000000000000000000000000000000000000000000000000082167f8564090700000000000000000000000000000000000000000000000000000000145b92915050565b6030861461035d576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260268152602001806118056026913960400191505060405180910390fd5b602084146103b6576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252603681526020018061179c6036913960400191505060405180910390fd5b6060821461040f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260298152602001806118786029913960400191505060405180910390fd5b670de0b6b3a7640000341015610470576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260268152602001806118526026913960400191505060405180910390fd5b633b9aca003406156104cd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260338152602001806117d26033913960400191505060405180910390fd5b633b9aca00340467ffffffffffffffff811115610535576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602781526020018061182b6027913960400191505060405180910390fd5b6060610540826114ba565b90507f649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c589898989858a8a6105756020546114ba565b6040805160a0808252810189905290819060208201908201606083016080840160c085018e8e80828437600083820152601f017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01690910187810386528c815260200190508c8c808284376000838201819052601f9091017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01690920188810386528c5181528c51602091820193918e019250908190849084905b83811015610648578181015183820152602001610630565b50505050905090810190601f1680156106755780820380516001836020036101000a031916815260200191505b5086810383528881526020018989808284376000838201819052601f9091017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0169092018881038452895181528951602091820193918b019250908190849084905b838110156106ef57
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestAccount represents a test account in the simulated backend,
|
|
|
|
// through which we can perform actions on the eth1.0 chain.
|
|
|
|
type TestAccount struct {
|
|
|
|
Addr common.Address
|
|
|
|
ContractAddr common.Address
|
2022-01-26 14:48:20 +00:00
|
|
|
Contract *deposit.DepositContract
|
2021-09-22 17:27:13 +00:00
|
|
|
Backend *backends.SimulatedBackend
|
|
|
|
TxOpts *bind.TransactOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup creates the simulated backend with the deposit contract deployed
|
|
|
|
func Setup() (*TestAccount, error) {
|
|
|
|
genesis := make(core.GenesisAlloc)
|
|
|
|
privKey, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pubKeyECDSA, ok := privKey.Public().(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error casting public key to ECDSA")
|
|
|
|
}
|
|
|
|
|
|
|
|
// strip off the 0x and the first 2 characters 04 which is always the EC prefix and is not required.
|
|
|
|
publicKeyBytes := crypto.FromECDSAPub(pubKeyECDSA)[4:]
|
|
|
|
var pubKey = make([]byte, 48)
|
|
|
|
copy(pubKey, publicKeyBytes)
|
|
|
|
|
|
|
|
addr := crypto.PubkeyToAddress(privKey.PublicKey)
|
|
|
|
txOpts, err := bind.NewKeyedTransactorWithChainID(privKey, big.NewInt(1337))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
startingBalance, _ := new(big.Int).SetString("100000000000000000000000000000000000000", 10)
|
|
|
|
genesis[addr] = core.GenesisAccount{Balance: startingBalance}
|
|
|
|
backend := backends.NewSimulatedBackend(genesis, 210000000000)
|
|
|
|
|
|
|
|
contractAddr, _, contract, err := DeployDepositContract(txOpts, backend)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
backend.Commit()
|
|
|
|
|
|
|
|
return &TestAccount{addr, contractAddr, contract, backend, txOpts}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Amount32Eth returns 32Eth(in wei) in terms of the big.Int type.
|
|
|
|
func Amount32Eth() *big.Int {
|
|
|
|
amount, _ := new(big.Int).SetString(amount32Eth, 10)
|
|
|
|
return amount
|
|
|
|
}
|
|
|
|
|
|
|
|
// LessThan1Eth returns less than 1 Eth(in wei) in terms of the big.Int type.
|
|
|
|
func LessThan1Eth() *big.Int {
|
|
|
|
amount, _ := new(big.Int).SetString(amountLessThan1Eth, 10)
|
|
|
|
return amount
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeployDepositContract deploys a new Ethereum contract, binding an instance of DepositContract to it.
|
2022-01-26 14:48:20 +00:00
|
|
|
func DeployDepositContract(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *deposit.DepositContract, error) {
|
|
|
|
parsed, err := abi.JSON(strings.NewReader(deposit.DepositContractABI))
|
2021-09-22 17:27:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(depositContractBin), backend)
|
|
|
|
if err != nil {
|
|
|
|
return common.Address{}, nil, nil, err
|
|
|
|
}
|
2022-01-26 14:48:20 +00:00
|
|
|
return address, tx, &deposit.DepositContract{
|
|
|
|
DepositContractCaller: deposit.NewDepositContractCallerFromBoundContract(contract),
|
|
|
|
DepositContractTransactor: deposit.NewDepositContractTransactorFromBoundContract(contract),
|
|
|
|
DepositContractFilterer: deposit.NewDepositContractFiltererFromBoundContract(contract),
|
|
|
|
}, nil
|
2021-09-22 17:27:13 +00:00
|
|
|
}
|