github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/e2e/blockchain/deployer.go (about)

     1  /*
     2   * Copyright (C) 2021 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package main
    19  
    20  import (
    21  	"context"
    22  	"flag"
    23  	"fmt"
    24  	"math/big"
    25  	"os"
    26  	"time"
    27  
    28  	"github.com/ethereum/go-ethereum/accounts"
    29  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
    30  	"github.com/ethereum/go-ethereum/accounts/keystore"
    31  	"github.com/ethereum/go-ethereum/common"
    32  	"github.com/ethereum/go-ethereum/core/types"
    33  	"github.com/ethereum/go-ethereum/ethclient"
    34  	"github.com/mysteriumnetwork/payments/test"
    35  	"github.com/rs/zerolog/log"
    36  )
    37  
    38  var hermes2Address = common.HexToAddress("0x761f2bb3e7ad6385a4c7833c5a26a8ddfdabf9f3")
    39  var mystToMint, _ = big.NewInt(0).SetString("1250000000000000000000", 10)
    40  
    41  func main() {
    42  	keyStoreDir := flag.String("keystore.directory", "", "Directory of keystore")
    43  	etherAddress := flag.String("ether.address", "", "Account inside keystore to use for deployment")
    44  	etherPassword := flag.String("ether.passphrase", "", "key of the account")
    45  	ethRPC := flag.String("geth.url", "", "RPC url of ethereum client")
    46  	flag.Parse()
    47  
    48  	addr := common.HexToAddress(*etherAddress)
    49  	ks := keystore.NewKeyStore(*keyStoreDir, keystore.StandardScryptN, keystore.StandardScryptP)
    50  	acc, err := ks.Find(accounts.Account{Address: addr})
    51  	checkError("find account", err)
    52  
    53  	err = ks.Unlock(acc, *etherPassword)
    54  	checkError("unlock account", err)
    55  
    56  	client, err := ethclient.Dial(*ethRPC)
    57  	checkError("lookup backend", err)
    58  
    59  	chainID, err := client.NetworkID(context.Background())
    60  	checkError("lookup chainid", err)
    61  
    62  	transactor := &bind.TransactOpts{
    63  		From: addr,
    64  		Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
    65  			return ks.SignTx(acc, tx, chainID)
    66  		},
    67  		Context:  context.Background(),
    68  		GasLimit: 6721975,
    69  	}
    70  
    71  	err = deployPaymentsv2Contracts(transactor, client)
    72  	checkError("deploy contracts", err)
    73  }
    74  
    75  func deployPaymentsv2Contracts(transactor *bind.TransactOpts, client *ethclient.Client) error {
    76  	maxStake, ok := big.NewInt(0).SetString("62000000000000000000", 10)
    77  	if !ok {
    78  		return fmt.Errorf("failed to parse max stake")
    79  	}
    80  	stake, ok := big.NewInt(0).SetString("100000000000000000000", 10)
    81  	if !ok {
    82  		return fmt.Errorf("failed to parse stake")
    83  	}
    84  
    85  	addresses, err := test.DeployHermesWithDependencies(transactor, client, 10*time.Second, test.RegistryOpts{
    86  		DexAddress:         common.HexToAddress("0x1"),
    87  		MinimalHermesStake: big.NewInt(0),
    88  	}, test.RegisterHermesOpts{
    89  		Operator:        transactor.From,
    90  		HermesStake:     stake,
    91  		HermesFee:       400,
    92  		MinChannelStake: big.NewInt(0),
    93  		MaxChannelStake: maxStake,
    94  		Url:             "http://hermes:8889",
    95  	})
    96  	if err != nil {
    97  		return fmt.Errorf("failed to deploy hermes with dependencies: %w", err)
    98  	}
    99  	log.Debug().Interface("addresses", addresses.BaseContractAddresses).Msg("deployed base contracts")
   100  	log.Debug().Interface("address", addresses.HermesAddress).Msg("deployed hermes contract")
   101  
   102  	addresses2, err := test.DeployHermes(transactor, client, 10*time.Second, addresses.BaseContractAddresses, test.RegisterHermesOpts{
   103  		Operator:        hermes2Address,
   104  		HermesStake:     stake,
   105  		HermesFee:       400,
   106  		MinChannelStake: big.NewInt(0),
   107  		MaxChannelStake: maxStake,
   108  		Url:             "http://hermes2:8889",
   109  	})
   110  	if err != nil {
   111  		return fmt.Errorf("failed to deploy hermes 2 with dependencies: %w", err)
   112  	}
   113  	log.Debug().Interface("address", addresses2.HermesAddress).Msg("deployed hermes 2 contract")
   114  
   115  	transactorAddress := common.HexToAddress("0x3d2cdbab09d2c8d613556769f37b47c82a5e13bf")
   116  	topupsAddress := common.HexToAddress("0xa29fb77b25181df094908b027821a7492ca4245b")
   117  	value := big.NewInt(0).SetUint64(10000000000000000000)
   118  	chainID, err := client.ChainID(context.Background())
   119  	if err != nil {
   120  		return fmt.Errorf("failed to get chain id: %w", err)
   121  	}
   122  
   123  	for _, address := range []common.Address{transactorAddress, hermes2Address, topupsAddress} {
   124  		err = test.TransferEth(transactor, client, chainID, address, value, 10*time.Second)
   125  		if err != nil {
   126  			return fmt.Errorf("failed to transfer eth: %w", err)
   127  		}
   128  	}
   129  
   130  	for _, address := range []common.Address{transactorAddress, hermes2Address, addresses.HermesAddress, addresses2.HermesAddress, topupsAddress, transactor.From} {
   131  		err = test.MintTokens(transactor, addresses.TokenV2Address, client, 10*time.Second, address, mystToMint)
   132  		if err != nil {
   133  			return fmt.Errorf("failed to mint myst: %w", err)
   134  		}
   135  	}
   136  
   137  	log.Debug().Msg("sent funds to addresses")
   138  	return nil
   139  }
   140  
   141  func checkError(context string, err error) {
   142  	if err != nil {
   143  		fmt.Println("Error at:", context, "value:", err.Error())
   144  		os.Exit(1)
   145  	}
   146  }