|
| 1 | +package tx |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "math/big" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/crypto" |
| 13 | + |
| 14 | + "github.com/bcdevtools/devd/v2/cmd/utils" |
| 15 | + ethtypes "github.com/ethereum/go-ethereum/core/types" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +func GetDeployContractEvmTxCommand() *cobra.Command { |
| 20 | + cmd := &cobra.Command{ |
| 21 | + Use: "deploy-contract [bytecode]", |
| 22 | + Short: `Deploy an EVM contract using bytecode.`, |
| 23 | + Long: `Deploy an EVM contract. |
| 24 | +Predefined bytecode: erc20`, |
| 25 | + Args: cobra.ExactArgs(1), |
| 26 | + Run: func(cmd *cobra.Command, args []string) { |
| 27 | + bytecode := strings.ToLower(args[0]) |
| 28 | + if bytecode == "erc20" { |
| 29 | + bytecode = BytecodeErc20Contract |
| 30 | + } |
| 31 | + deployEvmContract(bytecode, cmd) |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + cmd.Flags().String(flagRpc, "", flagEvmRpcDesc) |
| 36 | + cmd.Flags().String(flagSecretKey, "", flagSecretKeyDesc) |
| 37 | + |
| 38 | + return cmd |
| 39 | +} |
| 40 | + |
| 41 | +func deployEvmContract(bytecode string, cmd *cobra.Command) { |
| 42 | + ethClient8545, _ := mustGetEthClient(cmd) |
| 43 | + |
| 44 | + _, ecdsaPrivateKey, _, from := mustSecretEvmAccount(cmd) |
| 45 | + |
| 46 | + nonce, err := ethClient8545.NonceAt(context.Background(), *from, nil) |
| 47 | + utils.ExitOnErr(err, "failed to get nonce of sender") |
| 48 | + |
| 49 | + chainId, err := ethClient8545.ChainID(context.Background()) |
| 50 | + utils.ExitOnErr(err, "failed to get chain ID") |
| 51 | + |
| 52 | + if strings.HasPrefix(bytecode, "0x") { |
| 53 | + bytecode = bytecode[2:] |
| 54 | + } |
| 55 | + deploymentBytes, err := hex.DecodeString(bytecode) |
| 56 | + utils.ExitOnErr(err, "failed to parse deployment bytecode") |
| 57 | + |
| 58 | + txData := ethtypes.LegacyTx{ |
| 59 | + Nonce: nonce, |
| 60 | + GasPrice: big.NewInt(20_000_000_000), |
| 61 | + Gas: 2_000_000, |
| 62 | + To: nil, |
| 63 | + Data: deploymentBytes, |
| 64 | + Value: common.Big0, |
| 65 | + } |
| 66 | + tx := ethtypes.NewTx(&txData) |
| 67 | + |
| 68 | + newContractAddress := crypto.CreateAddress(*from, nonce) |
| 69 | + |
| 70 | + fmt.Println("Deploying new contract using account", from) |
| 71 | + |
| 72 | + signedTx, err := ethtypes.SignTx(tx, ethtypes.LatestSignerForChainID(chainId), ecdsaPrivateKey) |
| 73 | + utils.ExitOnErr(err, "failed to sign tx") |
| 74 | + |
| 75 | + var buf bytes.Buffer |
| 76 | + err = signedTx.EncodeRLP(&buf) |
| 77 | + utils.ExitOnErr(err, "failed to encode tx") |
| 78 | + |
| 79 | + rawTxRLPHex := hex.EncodeToString(buf.Bytes()) |
| 80 | + fmt.Printf("RawTx: 0x%s\n", rawTxRLPHex) |
| 81 | + |
| 82 | + err = ethClient8545.SendTransaction(context.Background(), signedTx) |
| 83 | + utils.ExitOnErr(err, "failed to send tx") |
| 84 | + |
| 85 | + fmt.Println("New contract deployed at") |
| 86 | + fmt.Println(newContractAddress) |
| 87 | +} |
0 commit comments