From bee46c8e6e4b25e10406c7ee640f72ccef5d51fb Mon Sep 17 00:00:00 2001 From: Guillermo Paoletti Date: Thu, 22 Aug 2024 00:47:00 +0200 Subject: [PATCH] feat: deploy UniswapV2Multicall cmd (#36) * feat: deploy UniswapV2Multicall cmd * fix: linter issues --- .../tx/solidity/deployUniswapV2Multicall.go | 120 ++++++++++++++++++ .../tx/solidity/deployUniswapV2.mdx | 7 + 2 files changed, 127 insertions(+) create mode 100644 cmd/playground/tx/solidity/deployUniswapV2Multicall.go diff --git a/cmd/playground/tx/solidity/deployUniswapV2Multicall.go b/cmd/playground/tx/solidity/deployUniswapV2Multicall.go new file mode 100644 index 0000000..73eeaef --- /dev/null +++ b/cmd/playground/tx/solidity/deployUniswapV2Multicall.go @@ -0,0 +1,120 @@ +package solidity + +import ( + "encoding/hex" + "fmt" + "os" + + "github.com/hanchon/hanchond/playground/evmos" + "github.com/hanchon/hanchond/playground/filesmanager" + "github.com/hanchon/hanchond/playground/solidity" + "github.com/hanchon/hanchond/playground/sql" + "github.com/spf13/cobra" +) + +// deployUniswapV2MulticallyCmd represents the deploy command +var deployUniswapV2MulticallyCmd = &cobra.Command{ + Use: "deploy-uniswap-v2-multicall", + Args: cobra.ExactArgs(0), + Short: "Deploy uniswap v2 multicall", + Run: func(cmd *cobra.Command, _ []string) { + queries := sql.InitDBFromCmd(cmd) + nodeID, err := cmd.Flags().GetString("node") + if err != nil { + fmt.Println("node not set") + os.Exit(1) + } + + gasLimit, err := cmd.Flags().GetInt("gas-limit") + if err != nil { + fmt.Println("incorrect gas limit") + os.Exit(1) + } + + // TODO: allow mainnet as a valid endpoint + e := evmos.NewEvmosFromDB(queries, nodeID) + builder := e.NewTxBuilder(uint64(gasLimit)) + + contractName := "/Multicall" + // Clone v2-minified if needed + path, err := solidity.DownloadUniswapV2Minified() + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + + // Keep working with the main contract + path = path + "/contracts" + contractName + ".sol" + + // Set up temp folder + if err := filesmanager.CleanUpTempFolder(); err != nil { + fmt.Println("could not clean up the temp folder:", err.Error()) + os.Exit(1) + } + + folderName := "multicallBuilder" + if err := filesmanager.CreateTempFolder(folderName); err != nil { + fmt.Println("could not create the temp folder:", err.Error()) + os.Exit(1) + } + + // Compile the contract + err = solidity.CompileWithSolc("0.5.0", path, filesmanager.GetBranchFolder(folderName)) + if err != nil { + fmt.Println("could not compile the erc20 contract:", err.Error()) + os.Exit(1) + } + + bytecode, err := filesmanager.ReadFile(filesmanager.GetBranchFolder(folderName) + contractName + ".bin") + if err != nil { + fmt.Printf("error reading the bytecode file:%s\n", err.Error()) + os.Exit(1) + } + + bytecode, err = hex.DecodeString(string(bytecode)) + if err != nil { + fmt.Println("error converting bytecode to []byte:", err.Error()) + os.Exit(1) + } + + txHash, err := builder.DeployContract(0, bytecode, uint64(gasLimit)) + if err != nil { + fmt.Printf("error sending the transaction: %s\n", err.Error()) + os.Exit(1) + } + + receipt, err := e.NewRequester().GetTransactionReceiptWithRetry(txHash, 15) + if err != nil { + fmt.Printf("error getting the tx receipt:%s\n", err.Error()) + } + + trace, err := e.NewRequester().GetTransactionTrace(txHash) + if err != nil { + fmt.Printf("error getting the tx trace:%s\n", err.Error()) + } + if trace.Result.Error != "" { + fmt.Println("failed to execute the transaction:", trace.Result.Error) + os.Exit(1) + } + + codeHash, err := e.NewRequester().EthCodeHash(receipt.Result.ContractAddress, "latest") + if err != nil { + fmt.Println("failed to get the eth code:", err.Error()) + os.Exit(1) + } + + fmt.Printf("{\"contract_address\":\"%s\", \"code_hash\":\"%s\", \"tx_hash\":\"%s\"}\n", receipt.Result.ContractAddress, "0x"+codeHash, txHash) + + // Clean up files + if err := filesmanager.CleanUpTempFolder(); err != nil { + fmt.Println("could not clean up the temp folder:", err.Error()) + os.Exit(1) + } + os.Exit(0) + }, +} + +func init() { + SolidityCmd.AddCommand(deployUniswapV2MulticallyCmd) + deployUniswapV2MulticallyCmd.Flags().Int("gas-limit", 20_000_000, "GasLimit to be used to deploy the transaction") +} diff --git a/docs/pages/hanchond/playground/tx/solidity/deployUniswapV2.mdx b/docs/pages/hanchond/playground/tx/solidity/deployUniswapV2.mdx index 6ac13d7..e2b526b 100644 --- a/docs/pages/hanchond/playground/tx/solidity/deployUniswapV2.mdx +++ b/docs/pages/hanchond/playground/tx/solidity/deployUniswapV2.mdx @@ -28,3 +28,10 @@ Params: hanchond playground tx solidity deploy-uniswap-v2-router 0xbb48d7604b522abcbb2f2302d4c18d907c12fd31 0x491bacc7ec4569468f0c21b7ae3629cd9fa6aa39 {"contract_address":"0xd6c873ad9f220279259609ec52fe17702bc47bf8", "code_hash":"0xfffa7b2f489b21362d74fd4ff2c3462d845afc0d0d8170785496d28bb1e568b0", "tx_hash":"0x09df30323ceaa33d5167e31a2faf834b3d253602626d2b256b76bb57a82ad33d"} ``` + +## Deploy Multicall + +```sh +hanchond playground tx solidity deploy-uniswap-v2-multicall +{"contract_address":"0xdd0a082af196653c97bf0160aa5d54fe8b47b94e", "code_hash":"0x75af43a00dfd028a9b68d2fc636e3dd28fe2f452ffae82b03f6d6488118a3971", "tx_hash":"0xd1c1cff43639cc1f7baf24dfc60bf426b817739d841a49c4c5c7b3719c4f2854"} +```