Skip to content

Commit

Permalink
implement ts support for hardhat provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Malcador committed Dec 1, 2020
1 parent 701a2c2 commit 1bdbfa5
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 44 deletions.
6 changes: 1 addition & 5 deletions buidler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"strings"
)

const (
BuidlerConfigFile = "buidler.config.js"
)

type BuidlerConfig struct {
ProjectDirectory string `json:"project_directory"`
BuildDirectory string `json:"contracts_build_directory"`
Expand Down Expand Up @@ -127,7 +123,7 @@ func getDivider() string {

func (dp *DeploymentProvider) MustGetConfig() (*providers.Config, error) {
projectDir, err := filepath.Abs(config.ProjectDirectory)
buidlerConfigFile := BuidlerConfigFile
buidlerConfigFile := providers.BuidlerConfigFile

if err != nil {
return nil, userError.NewUserError(
Expand Down
44 changes: 33 additions & 11 deletions commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,12 @@ func promptProviderSelect(deploymentProviders []providers.DeploymentProviderName
}

func initProvider() {
trufflePath := filepath.Join(config.ProjectDirectory, truffle.NewTruffleConfigFile)
openZeppelinPath := filepath.Join(config.ProjectDirectory, openzeppelin.OpenzeppelinConfigFile)
oldTrufflePath := filepath.Join(config.ProjectDirectory, truffle.OldTruffleConfigFile)
buidlerPath := filepath.Join(config.ProjectDirectory, buidler.BuidlerConfigFile)
hardhatPath := filepath.Join(config.ProjectDirectory, hardhat.HardhatConfigFile)
trufflePath := filepath.Join(config.ProjectDirectory, providers.NewTruffleConfigFile)
openZeppelinPath := filepath.Join(config.ProjectDirectory, providers.OpenzeppelinConfigFile)
oldTrufflePath := filepath.Join(config.ProjectDirectory, providers.OldTruffleConfigFile)
buidlerPath := filepath.Join(config.ProjectDirectory, providers.BuidlerConfigFile)
hardhatPath := filepath.Join(config.ProjectDirectory, providers.HardhatConfigFile)
hardhatPathTs := filepath.Join(config.ProjectDirectory, providers.HardhatConfigFileTs)

var provider providers.DeploymentProviderName

Expand Down Expand Up @@ -308,7 +309,7 @@ func initProvider() {

logrus.Debugf("couldn't read new Buidler config file")

logrus.Debug("Trying hardhat config path: %s", hardhatPath)
logrus.Debugf("Trying hardhat config path: %s", hardhatPath)

if provider == providers.HardhatDeploymentProvider || provider == "" {
_, err := os.Stat(hardhatPath)
Expand All @@ -329,6 +330,27 @@ func initProvider() {
)
}

logrus.Debugf("Trying hardhat ts config path: %s", hardhatPathTs)

if provider == providers.HardhatDeploymentProvider || provider == "" {
_, err := os.Stat(hardhatPathTs)

if err == nil {
deploymentProvider = hardhat.NewDeploymentProvider()

if deploymentProvider == nil {
logrus.Error("Error initializing hardhat")
}

return
}

logrus.Debugf(
fmt.Sprintf("unable to fetch config\n%s",
" Couldn't read Hardhat config file"),
)
}

logrus.Debugf("Trying truffle config path: %s", trufflePath)

_, err := os.Stat(trufflePath)
Expand Down Expand Up @@ -364,26 +386,26 @@ func initProvider() {
}

func GetConfigPayload(providerConfig *providers.Config) *payloads.Config {
if providerConfig.ConfigType == truffle.NewTruffleConfigFile && providerConfig.Compilers != nil {
if providerConfig.ConfigType == providers.NewTruffleConfigFile && providerConfig.Compilers != nil {
return payloads.ParseNewTruffleConfig(providerConfig.Compilers)
}

if providerConfig.ConfigType == truffle.OldTruffleConfigFile {
if providerConfig.ConfigType == providers.OldTruffleConfigFile {
if providerConfig.Solc != nil {
return payloads.ParseOldTruffleConfig(providerConfig.Solc)
} else if providerConfig.Compilers != nil {
return payloads.ParseNewTruffleConfig(providerConfig.Compilers)
}
}
if providerConfig.ConfigType == openzeppelin.OpenzeppelinConfigFile && providerConfig.Compilers != nil {
if providerConfig.ConfigType == providers.OpenzeppelinConfigFile && providerConfig.Compilers != nil {
return payloads.ParseOpenZeppelinConfig(providerConfig.Compilers)
}

if providerConfig.ConfigType == buidler.BuidlerConfigFile && providerConfig.Compilers != nil {
if providerConfig.ConfigType == providers.BuidlerConfigFile && providerConfig.Compilers != nil {
return payloads.ParseBuidlerConfig(providerConfig.Compilers)
}

if providerConfig.ConfigType == hardhat.HardhatConfigFile && providerConfig.Compilers != nil {
if (providerConfig.ConfigType == providers.HardhatConfigFile || providerConfig.ConfigType == providers.HardhatConfigFileTs) && providerConfig.Compilers != nil {
return payloads.ParseHardhatConfig(providerConfig.Compilers)
}

Expand Down
20 changes: 11 additions & 9 deletions hardhat/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (
"strings"
)

const (
HardhatConfigFile = "hardhat.config.js"
)

type HardhatConfig struct {
ProjectDirectory string `json:"project_directory"`
BuildDirectory string `json:"contracts_build_directory"`
Expand Down Expand Up @@ -133,7 +129,7 @@ func getDivider() string {

func (dp *DeploymentProvider) MustGetConfig() (*providers.Config, error) {
projectDir, err := filepath.Abs(config.ProjectDirectory)
hardhatConfigFile := HardhatConfigFile
hardhatConfigFile := providers.HardhatConfigFile

if err != nil {
return nil, userError.NewUserError(
Expand All @@ -144,10 +140,16 @@ func (dp *DeploymentProvider) MustGetConfig() (*providers.Config, error) {

hardhatConfig, err := dp.GetConfig(hardhatConfigFile, projectDir)
if err != nil {
return nil, userError.NewUserError(
fmt.Errorf("unable to fetch config: %s", err),
"Couldn't read Hardhat config file",
)
hardhatConfigFile = providers.HardhatConfigFileTs

hardhatConfig, err = dp.GetConfig(hardhatConfigFile, projectDir)

if err != nil {
return nil, userError.NewUserError(
fmt.Errorf("unable to fetch config: %s", err),
"Couldn't read Hardhat config file",
)
}
}

return hardhatConfig, nil
Expand Down
8 changes: 5 additions & 3 deletions hardhat/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ func (dp *DeploymentProvider) GetContracts(
return nil, 0, errors.Wrap(err, "failed parsing build file")
}

err = json.Unmarshal([]byte(hardhatContract.Metadata), &hardhatMeta)
if err != nil {
return nil, 0, errors.Wrap(err, "failed parsing build file")
if hardhatContract.Metadata != "" {
err = json.Unmarshal([]byte(hardhatContract.Metadata), &hardhatMeta)
if err != nil {
return nil, 0, errors.Wrap(err, "failed parsing build file")
}
}

contract := providers.Contract{
Expand Down
11 changes: 3 additions & 8 deletions openzeppelin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ import (
"strings"
)

const (
OpenzeppelinConfigFile = "networks.js"
OpenZeppelinProjectConfigFile = "project.json"
)

func (dp *DeploymentProvider) GetConfig(configName string, projectDir string) (*providers.Config, error) {
openzeppelinPath := filepath.Join(projectDir, configName)
openzeppelinProjectPath := filepath.Join(projectDir, ".openzeppelin", OpenZeppelinProjectConfigFile)
openzeppelinProjectPath := filepath.Join(projectDir, ".openzeppelin", providers.OpenZeppelinProjectConfigFile)
divider := getDivider()

logrus.Debugf("Trying openzeppelin config path: %s", openzeppelinPath)
Expand Down Expand Up @@ -98,7 +93,7 @@ func (dp *DeploymentProvider) GetConfig(configName string, projectDir string) (*
var openzeppelinCompilerData providers.OZProjectData
err = json.Unmarshal(data, &openzeppelinCompilerData)
if err != nil {
return nil, fmt.Errorf("cannot read %s", OpenZeppelinProjectConfigFile)
return nil, fmt.Errorf("cannot read %s", providers.OpenZeppelinProjectConfigFile)
}

if openzeppelinCompilerData.Compiler == nil {
Expand Down Expand Up @@ -132,7 +127,7 @@ func getDivider() string {

func (dp *DeploymentProvider) MustGetConfig() (*providers.Config, error) {
projectDir, err := filepath.Abs(config.ProjectDirectory)
openzeppelinConfigFile := OpenzeppelinConfigFile
openzeppelinConfigFile := providers.OpenzeppelinConfigFile

if err != nil {
return nil, userError.NewUserError(
Expand Down
2 changes: 1 addition & 1 deletion providers/deployment_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *Config) AbsoluteBuildDirectoryPath() string {
c.BuildDirectory = filepath.Join(".", "build", "contracts")
}

if c.ConfigType == "buidler.config.js" || c.ConfigType == "hardhat.config.js" {
if c.ConfigType == BuidlerConfigFile || c.ConfigType == HardhatConfigFile || c.ConfigType == HardhatConfigFileTs {
c.BuildDirectory = filepath.Join(".", "deployments")
}

Expand Down
11 changes: 11 additions & 0 deletions providers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ const (
OpenZeppelinDeploymentProvider DeploymentProviderName = "OpenZeppelin"
BuidlerDeploymentProvider DeploymentProviderName = "Buidler"
HardhatDeploymentProvider DeploymentProviderName = "Hardhat"

HardhatConfigFile = "hardhat.config.js"
HardhatConfigFileTs = "hardhat.config.ts"

BuidlerConfigFile = "buidler.config.js"

NewTruffleConfigFile = "truffle-config.js"
OldTruffleConfigFile = "truffle.js"

OpenzeppelinConfigFile = "networks.js"
OpenZeppelinProjectConfigFile = "project.json"
)

var AllProviders = []DeploymentProviderName{
Expand Down
9 changes: 2 additions & 7 deletions truffle/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (
"strings"
)

const (
NewTruffleConfigFile = "truffle-config.js"
OldTruffleConfigFile = "truffle.js"
)

func (dp *DeploymentProvider) GetConfig(configName string, projectDir string) (*providers.Config, error) {
trufflePath := filepath.Join(projectDir, configName)
divider := getDivider()
Expand Down Expand Up @@ -85,7 +80,7 @@ func getDivider() string {

func (dp *DeploymentProvider) MustGetConfig() (*providers.Config, error) {
projectDir, err := filepath.Abs(config.ProjectDirectory)
truffleConfigFile := NewTruffleConfigFile
truffleConfigFile := providers.NewTruffleConfigFile

if err != nil {
return nil, userError.NewUserError(
Expand All @@ -103,7 +98,7 @@ func (dp *DeploymentProvider) MustGetConfig() (*providers.Config, error) {
}
if os.IsNotExist(err) {
logrus.Debugf("couldn't read new truffle config file: %s", err)
truffleConfigFile = OldTruffleConfigFile
truffleConfigFile = providers.OldTruffleConfigFile
truffleConfig, err = dp.GetConfig(truffleConfigFile, projectDir)
}

Expand Down

0 comments on commit 1bdbfa5

Please sign in to comment.