Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract command #2008

Merged
merged 24 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7f64698
add interchain command group, file renaming to ictt where needed
felipemadero Jun 29, 2024
3f345f7
rename bridge to ictt
felipemadero Jun 29, 2024
7969ded
fix unit test tmp file generation
felipemadero Jun 29, 2024
5446eae
start replacing to transferer
felipemadero Jun 29, 2024
3758327
create package localnet to be able to fix some import cycles
felipemadero Jun 29, 2024
3cabd4b
add missing files
felipemadero Jun 29, 2024
d0156a6
add transferer word to lint
felipemadero Jun 29, 2024
96a4a7c
last renaming
felipemadero Jun 29, 2024
8b6dac2
base for contract command
felipemadero Jun 30, 2024
a45b78b
keep build env
felipemadero Jun 30, 2024
58cff97
add skel for avalanche contract deploy erc20
felipemadero Jun 30, 2024
33db6ea
move private key stuff to pkg
felipemadero Jun 30, 2024
89d9dbe
add missing file
felipemadero Jun 30, 2024
6d9a897
add flags for chain options. v0
felipemadero Jun 30, 2024
ece7013
add missing file
felipemadero Jul 1, 2024
ab675bb
add private key prompt
felipemadero Jul 1, 2024
e8995c8
add prompts for token and balance
felipemadero Jul 1, 2024
b8ef878
cmd working
felipemadero Jul 1, 2024
d6102c0
transferer -> transferrer
felipemadero Jul 1, 2024
ab21775
use main branch of ictt repo
felipemadero Jul 1, 2024
2343c2c
rm transferer word from lint exceptions
felipemadero Jul 1, 2024
3fd05ac
Merge branch 'avalanche-itt' into contract-command
felipemadero Jul 1, 2024
d4be982
address PR comments
felipemadero Jul 1, 2024
e2e2ac9
Merge branch 'main' into contract-command
felipemadero Jul 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ tests/e2e/hardhat/dynamic_conf.json
tests/e2e/hardhat/greeter.json

tests/e2e/ledgerSim/node_modules/

pkg/contract/contracts/foundry.toml
pkg/contract/contracts/remappings.txt
pkg/contract/contracts/lib/
pkg/contract/contracts/cache/
pkg/contract/contracts/out/
26 changes: 26 additions & 0 deletions cmd/contractcmd/contract.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package contractcmd

import (
"github.com/ava-labs/avalanche-cli/pkg/application"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/spf13/cobra"
)

var app *application.Avalanche

// avalanche contract
func NewCmd(injectedApp *application.Avalanche) *cobra.Command {
cmd := &cobra.Command{
Use: "contract",
Short: "Manage smart contracts",
Long: `The contract command suite provides a collection of tools for deploying
and interacting with smart contracts.`,
RunE: cobrautils.CommandSuiteUsage,
}
app = injectedApp
// contract deploy
cmd.AddCommand(newDeployCmd())
return cmd
}
22 changes: 22 additions & 0 deletions cmd/contractcmd/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package contractcmd

import (
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/spf13/cobra"
)

// avalanche contract deploy
func newDeployCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "deploy",
Short: "Deploy smart contracts",
Long: `The contract command suite provides a collection of tools for deploying
smart contracts.`,
RunE: cobrautils.CommandSuiteUsage,
}
// contract deploy erc20
cmd.AddCommand(newDeployERC20Cmd())
return cmd
}
191 changes: 191 additions & 0 deletions cmd/contractcmd/deployERC20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package contractcmd

import (
"fmt"
"math/big"

cmdflags "github.com/ava-labs/avalanche-cli/cmd/flags"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/ava-labs/avalanche-cli/pkg/contract"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/prompts"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ethereum/go-ethereum/common"

"github.com/spf13/cobra"
)

type DeployERC20Flags struct {
Network networkoptions.NetworkFlags
PrivateKeyFlags contract.PrivateKeyFlags
chainFlags contract.ChainFlags
symbol string
funded string
supply uint64
}

var (
deployERC20SupportedNetworkOptions = []networkoptions.NetworkOption{
networkoptions.Local,
networkoptions.Devnet,
networkoptions.Fuji,
}
deployERC20Flags DeployERC20Flags
)

// avalanche contract deploy erc20
func newDeployERC20Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "erc20",
Short: "Deploy an ERC20 token into a given Network and Blockchain",
Long: "Deploy an ERC20 token into a given Network and Blockchain",
RunE: deployERC20,
Args: cobrautils.ExactArgs(0),
}
networkoptions.AddNetworkFlagsToCmd(cmd, &deployERC20Flags.Network, true, deployERC20SupportedNetworkOptions)
contract.AddPrivateKeyFlagsToCmd(cmd, &deployERC20Flags.PrivateKeyFlags, "as contract deployer")
contract.AddChainFlagsToCmd(
cmd,
&deployERC20Flags.chainFlags,
"deploy the ERC20 contract",
"",
"",
)
cmd.Flags().StringVar(&deployERC20Flags.symbol, "symbol", "", "set the token symbol")
cmd.Flags().Uint64Var(&deployERC20Flags.supply, "supply", 0, "set the token supply")
cmd.Flags().StringVar(&deployERC20Flags.funded, "funded", "", "set the funded address")
return cmd
}

func deployERC20(_ *cobra.Command, _ []string) error {
network, err := networkoptions.GetNetworkFromCmdLineFlags(
app,
"",
deployERC20Flags.Network,
true,
false,
deployERC20SupportedNetworkOptions,
"",
)
if err != nil {
return err
}
// flags exclusiveness
if !cmdflags.EnsureMutuallyExclusive([]bool{
deployERC20Flags.chainFlags.SubnetName != "",
deployERC20Flags.chainFlags.CChain,
}) {
return fmt.Errorf("--subnet and --c-chain are mutually exclusive flags")
}
if deployERC20Flags.chainFlags.SubnetName == "" && !deployERC20Flags.chainFlags.CChain {
subnetNames, err := app.GetSubnetNamesOnNetwork(network)
if err != nil {
return err
}
prompt := "Where do you want to Deploy the ERC-20 Token?"
cancel, _, _, cChain, subnetName, err := prompts.PromptChain(
app.Prompt,
prompt,
subnetNames,
true,
true,
false,
"",
)
if cancel {
return nil
}
if err == nil {
deployERC20Flags.chainFlags.SubnetName = subnetName
deployERC20Flags.chainFlags.CChain = cChain
}
}
genesisAddress, genesisPrivateKey, err := contract.GetEVMSubnetPrefundedKey(
app,
network,
deployERC20Flags.chainFlags.SubnetName,
deployERC20Flags.chainFlags.CChain,
"",
)
if err != nil {
return err
}
privateKey, err := contract.GetPrivateKeyFromFlags(
app,
deployERC20Flags.PrivateKeyFlags,
genesisPrivateKey,
)
if err != nil {
return err
}
if privateKey == "" {
ux.Logger.PrintToUser("A private key is needed to pay for the contract deploy fees.")
ux.Logger.PrintToUser("It will also be considered the owner address of the contract, beign able to call")
ux.Logger.PrintToUser("the contract methods only available to owners.")
privateKey, err = prompts.PromptPrivateKey(
app.Prompt,
"deploy the contract",
app.GetKeyDir(),
app.GetKey,
genesisAddress,
genesisPrivateKey,
)
if err != nil {
return err
}
}
if deployERC20Flags.symbol == "" {
ux.Logger.PrintToUser("Which is the token symbol?")
deployERC20Flags.symbol, err = app.Prompt.CaptureString("Token symbol")
if err != nil {
return err
}
}
supply := new(big.Int).SetUint64(deployERC20Flags.supply)
if deployERC20Flags.supply == 0 {
ux.Logger.PrintToUser("Which is the total token supply?")
supply, err = app.Prompt.CapturePositiveBigInt("Token supply")
if err != nil {
return err
}
}
if deployERC20Flags.funded == "" {
ux.Logger.PrintToUser("Which address should receive the supply?")
deployERC20Flags.funded, err = prompts.PromptAddress(
app.Prompt,
"receive the total token supply",
app.GetKeyDir(),
app.GetKey,
genesisAddress,
)
if err != nil {
return err
}
}
rpcURL, err := contract.GetRPCURL(
app,
network,
deployERC20Flags.chainFlags.SubnetName,
deployERC20Flags.chainFlags.CChain,
)
if err != nil {
return err
}
address, err := contract.DeployERC20(
rpcURL,
privateKey,
deployERC20Flags.symbol,
common.HexToAddress(deployERC20Flags.funded),
supply,
)
if err != nil {
return err
}
ux.Logger.PrintToUser("")
ux.Logger.PrintToUser("Token Address: %s", address.Hex())
ux.Logger.PrintToUser("")
ux.Logger.PrintToUser("ERC20 Contract Successfully Deployed!")
return nil
}
28 changes: 17 additions & 11 deletions cmd/interchaincmd/tokentransferrercmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

cmdflags "github.com/ava-labs/avalanche-cli/cmd/flags"
"github.com/ava-labs/avalanche-cli/pkg/cobrautils"
"github.com/ava-labs/avalanche-cli/pkg/contract"
"github.com/ava-labs/avalanche-cli/pkg/ictt"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/prompts"
Expand All @@ -19,13 +20,8 @@ import (
"github.com/spf13/cobra"
)

type ChainFlags struct {
SubnetName string
CChain bool
}

type HomeFlags struct {
chainFlags ChainFlags
chainFlags contract.ChainFlags
homeAddress string
native bool
erc20Address string
Expand All @@ -34,7 +30,7 @@ type HomeFlags struct {
type DeployFlags struct {
Network networkoptions.NetworkFlags
homeFlags HomeFlags
remoteFlags ChainFlags
remoteFlags contract.ChainFlags
version string
}

Expand All @@ -57,13 +53,23 @@ func newDeployCmd() *cobra.Command {
Args: cobrautils.ExactArgs(0),
}
networkoptions.AddNetworkFlagsToCmd(cmd, &deployFlags.Network, true, deploySupportedNetworkOptions)
cmd.Flags().StringVar(&deployFlags.homeFlags.chainFlags.SubnetName, "home-subnet", "", "use the given CLI subnet as the Transferrer's Home Chain")
cmd.Flags().BoolVar(&deployFlags.homeFlags.chainFlags.CChain, "c-chain-home", false, "use C-Chain as the Transferrer's Home Chain")
contract.AddChainFlagsToCmd(
cmd,
&deployFlags.homeFlags.chainFlags,
"set the Transferrer's Home Chain",
"home-subnet",
"c-chain-home",
)
contract.AddChainFlagsToCmd(
cmd,
&deployFlags.remoteFlags,
"set the Transferrer's Remote Chain",
"remote-subnet",
"c-chain-remote",
)
cmd.Flags().BoolVar(&deployFlags.homeFlags.native, "deploy-native-home", false, "deploy a Transferrer Home for the Chain's Native Token")
cmd.Flags().StringVar(&deployFlags.homeFlags.erc20Address, "deploy-erc20-home", "", "deploy a Transferrer Home for the given Chain's ERC20 Token")
cmd.Flags().StringVar(&deployFlags.homeFlags.homeAddress, "use-home", "", "use the given Transferrer's Home Address")
cmd.Flags().BoolVar(&deployFlags.remoteFlags.CChain, "c-chain-remote", false, "use C-Chain as the Transferrer's Remote Chain")
cmd.Flags().StringVar(&deployFlags.remoteFlags.SubnetName, "remote-subnet", "", "use the given CLI subnet as the Transferrer's Remote Chain")
cmd.Flags().StringVar(&deployFlags.version, "version", "", "tag/branch/commit of Avalanche InterChain Token Transfer to be used (defaults to main branch)")
return cmd
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/interchaincmd/tokentransferrercmd/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
_ "embed"
"fmt"

"github.com/ava-labs/avalanche-cli/pkg/contract"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/prompts"
"github.com/ava-labs/avalanchego/ids"
Expand All @@ -27,7 +28,7 @@ func promptChain(
network models.Network,
avoidCChain bool,
avoidSubnet string,
chainFlags *ChainFlags,
chainFlags *contract.ChainFlags,
) (bool, error) {
subnetNames, err := app.GetSubnetNamesOnNetwork(network)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/keycmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func getClients(networks []models.Network, pchain bool, cchain bool, xchain bool
if err != nil {
return nil, err
}
b, _, err := subnetcmd.HasSubnetEVMGenesis(subnetName)
b, _, err := app.HasSubnetEVMGenesis(subnetName)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/nodecmd/wiz.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func wiz(cmd *cobra.Command, args []string) error {
return err
}

isEVMGenesis, _, err := subnetcmd.HasSubnetEVMGenesis(subnetName)
isEVMGenesis, _, err := app.HasSubnetEVMGenesis(subnetName)
if err != nil {
return err
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func hasTeleporterDeploys(
return false, err
}
for _, deployedSubnetName := range clusterConfig.Subnets {
deployedSubnetIsEVMGenesis, _, err := subnetcmd.HasSubnetEVMGenesis(deployedSubnetName)
deployedSubnetIsEVMGenesis, _, err := app.HasSubnetEVMGenesis(deployedSubnetName)
if err != nil {
return false, err
}
Expand All @@ -449,7 +449,7 @@ func updateProposerVMs(
return err
}
for _, deployedSubnetName := range clusterConfig.Subnets {
deployedSubnetIsEVMGenesis, _, err := subnetcmd.HasSubnetEVMGenesis(deployedSubnetName)
deployedSubnetIsEVMGenesis, _, err := app.HasSubnetEVMGenesis(deployedSubnetName)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ava-labs/avalanche-cli/cmd/backendcmd"
"github.com/ava-labs/avalanche-cli/cmd/configcmd"
"github.com/ava-labs/avalanche-cli/cmd/contractcmd"
"github.com/ava-labs/avalanche-cli/cmd/interchaincmd"
"github.com/ava-labs/avalanche-cli/cmd/keycmd"
"github.com/ava-labs/avalanche-cli/cmd/networkcmd"
Expand Down Expand Up @@ -100,6 +101,9 @@ in with avalanche subnet create myNewSubnet.`,
// add interchain command
rootCmd.AddCommand(interchaincmd.NewCmd(app))

// add contract command
rootCmd.AddCommand(contractcmd.NewCmd(app))

cobrautils.ConfigureRootCmd(rootCmd)

return rootCmd
Expand Down
Loading
Loading