Skip to content

Commit

Permalink
feat(ioctl): project register contract addr config (#4039)
Browse files Browse the repository at this point in the history
  • Loading branch information
saitofun committed Jan 3, 2024
1 parent d1ec200 commit efcc7eb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 34 deletions.
8 changes: 8 additions & 0 deletions ioctl/cmd/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var (
config.English: "set w3bsteram endpoint for once",
config.Chinese: "一次设置w3bstream端点",
}
_flagContractAddressUsages = map[config.Language]string{
config.English: "set w3bsteram project register contract address for once",
config.Chinese: "一次设置w3bstream项目注册合约地址",
}
)

func init() {
Expand All @@ -43,4 +47,8 @@ func init() {
&config.ReadConfig.WsEndpoint, "ws-endpoint",
config.ReadConfig.WsEndpoint, config.TranslateInLang(_flagWsEndpointUsages, config.UILanguage),
)
WsCmd.PersistentFlags().StringVar(
&config.ReadConfig.WsRegisterContract, "contract-address",
config.ReadConfig.WsRegisterContract, config.TranslateInLang(_flagContractAddressUsages, config.UILanguage),
)
}
14 changes: 1 addition & 13 deletions ioctl/cmd/ws/wsproject.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ var (
config.Chinese: "w3bstream项目管理",
}

_flagProjectRegisterContractAddressUsages = map[config.Language]string{
config.English: "project register contract address",
config.Chinese: "项目注册合约地址",
}

wsProjectRegisterContractAddress string
wsProjectRegisterContractABI abi.ABI

Expand All @@ -69,14 +64,7 @@ func init() {
wsProject.AddCommand(wsProjectUpdate)
wsProject.AddCommand(wsProjectQuery)

wsProject.PersistentFlags().StringVarP(
&wsProjectRegisterContractAddress,
"contract-address",
"c",
"",
config.TranslateInLang(_flagProjectRegisterContractAddressUsages, config.UILanguage),
)
_ = wsProject.MarkFlagRequired("contract-address")
wsProjectRegisterContractAddress = config.ReadConfig.WsRegisterContract
}

func convertStringToAbiBytes32(hash string) (interface{}, error) {
Expand Down
6 changes: 6 additions & 0 deletions ioctl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type Config struct {
AnalyserEndpoint string `json:"analyserEndpoint" yaml:"analyserEndpoint"`
// WsEndpoint w3bstream endpoint
WsEndpoint string `json:"wsEndpoint" yaml:"wsEndpoint"`
// WsRegisterContract w3bstream project register contract address
WsRegisterContract string `json:"wsProjectRegisterContract" yaml:"wsProjectRegisterContract"`
}

var (
Expand Down Expand Up @@ -120,6 +122,10 @@ func init() {
ReadConfig.WsEndpoint = _defaultWsEndpoint
completeness = false
}
if ReadConfig.WsRegisterContract == "" {
ReadConfig.WsRegisterContract = _defaultProjectRegisterContract
completeness = false
}
if !completeness {
err := writeConfig()
if err != nil {
Expand Down
11 changes: 9 additions & 2 deletions ioctl/config/configsetget.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ const (
_endpointPattern = "(" + _ipPattern + "|(" + _domainPattern + ")" + "|(" + _localPattern + "))" + `(:\d{1,5})?`
_defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org"
_defaultWsEndpoint = "sprout-staging.w3bstream.com:9000"
// _defaultProjectRegisterContract default project register contract address
_defaultProjectRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e"
)

var (
_supportedLanguage = []string{"English", "中文"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "all"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "projectRegisterContract"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "projectRegisterContract", "all"}
_validExpl = []string{"iotexscan", "iotxplorer"}
_endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$")
)
Expand Down Expand Up @@ -151,6 +153,8 @@ func Get(arg string) error {
fmt.Println(ReadConfig.AnalyserEndpoint)
case "wsEndpoint":
fmt.Println(ReadConfig.WsEndpoint)
case "projectRegisterContract":
fmt.Println(ReadConfig.WsRegisterContract)
case "all":
fmt.Println(ReadConfig.String())
}
Expand Down Expand Up @@ -281,6 +285,8 @@ func set(args []string) error {
ReadConfig.Nsv2height = height
case "wsEndpoint":
ReadConfig.WsEndpoint = args[1]
case "projectRegisterContract":
ReadConfig.WsRegisterContract = args[1]
}
err := writeConfig()
if err != nil {
Expand All @@ -300,6 +306,7 @@ func reset() error {
ReadConfig.Language = "English"
ReadConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
ReadConfig.WsEndpoint = _defaultWsEndpoint
ReadConfig.WsRegisterContract = _defaultProjectRegisterContract

err := writeConfig()
if err != nil {
Expand Down
14 changes: 12 additions & 2 deletions ioctl/newcmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ const (
_defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org"
_defaultWsEndpoint = "sprout-staging.w3bstream.com:9000"
_defaultConfigFileName = "config.default"
// _defaultWsProjectRegisterContract default w3bstream project register contract address
_defaultWsProjectRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e"
)

var (
_supportedLanguage = []string{"English", "中文"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "all"}
_validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "wsProjectRegisterContract"}
_validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "wsProjectRegisterContract", "all"}
_validExpl = []string{"iotexscan", "iotxplorer"}
_endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$")
_configDir = os.Getenv("HOME") + "/.config/ioctl/default"
Expand Down Expand Up @@ -120,6 +122,9 @@ func InitConfig() (config.Config, string, error) {
info.readConfig.WsEndpoint = _defaultWsEndpoint
completeness = false
}
if info.readConfig.WsRegisterContract == "" {
info.readConfig.WsRegisterContract = _defaultWsProjectRegisterContract
}
if !completeness {
if err = info.writeConfig(); err != nil {
return info.readConfig, info.defaultConfigFile, err
Expand Down Expand Up @@ -150,6 +155,7 @@ func (c *info) reset() error {
c.readConfig.Language = _supportedLanguage[0]
c.readConfig.AnalyserEndpoint = _defaultAnalyserEndpoint
c.readConfig.WsEndpoint = _defaultWsEndpoint
c.readConfig.WsRegisterContract = _defaultWsProjectRegisterContract

err := c.writeConfig()
if err != nil {
Expand Down Expand Up @@ -210,6 +216,8 @@ func (c *info) set(args []string, insecure bool, client ioctl.Client) (string, e
c.readConfig.Nsv2height = height
case "wsEndpoint":
c.readConfig.WsEndpoint = args[1]
case "wsProjectRegisterContract":
c.readConfig.WsRegisterContract = args[1]
default:
return "", config.ErrConfigNotMatch
}
Expand Down Expand Up @@ -247,6 +255,8 @@ func (c *info) get(arg string) (string, error) {
return c.readConfig.AnalyserEndpoint, nil
case "wsEndpoint":
return c.readConfig.WsEndpoint, nil
case "wsProjectRegisterContract":
return c.readConfig.WsRegisterContract, nil
case "all":
return jsonString(c.readConfig)
default:
Expand Down
46 changes: 29 additions & 17 deletions ioctl/newcmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ func TestConfigGet(t *testing.T) {
require := require.New(t)
testPath := t.TempDir()
info := newInfo(config.Config{
Wallet: testPath,
SecureConnect: true,
Aliases: make(map[string]string),
DefaultAccount: config.Context{AddressOrAlias: "test"},
Explorer: "iotexscan",
Language: "English",
AnalyserEndpoint: "testAnalyser",
WsEndpoint: "testWsEndpoint",
Wallet: testPath,
SecureConnect: true,
Aliases: make(map[string]string),
DefaultAccount: config.Context{AddressOrAlias: "test"},
Explorer: "iotexscan",
Language: "English",
AnalyserEndpoint: "testAnalyser",
WsEndpoint: "testWsEndpoint",
WsRegisterContract: "testWsProjectRegisterContract",
}, testPath)

tcs := []struct {
Expand Down Expand Up @@ -93,9 +94,13 @@ func TestConfigGet(t *testing.T) {
"wsEndpoint",
"testWsEndpoint",
},
{
"wsProjectRegisterContract",
"testWsProjectRegisterContract",
},
{
"all",
"\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\",\n \"wsEndpoint\": \"testWsEndpoint\"\n}",
"\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\",\n \"wsEndpoint\": \"testWsEndpoint\",\n \"wsProjectRegisterContract\": \"testWsProjectRegisterContract\"\n}",
},
}

Expand All @@ -115,14 +120,15 @@ func TestConfigReset(t *testing.T) {
cfgFile := fmt.Sprintf("%s/%s", cfgDir, "config.test")

info := newInfo(config.Config{
Wallet: "wallet",
Endpoint: "testEndpoint",
SecureConnect: false,
DefaultAccount: config.Context{AddressOrAlias: ""},
Explorer: "explorer",
Language: "Croatian",
AnalyserEndpoint: "testAnalyser",
WsEndpoint: "testWsEndpoint",
Wallet: "wallet",
Endpoint: "testEndpoint",
SecureConnect: false,
DefaultAccount: config.Context{AddressOrAlias: ""},
Explorer: "explorer",
Language: "Croatian",
AnalyserEndpoint: "testAnalyser",
WsEndpoint: "testWsEndpoint",
WsRegisterContract: "testProjectRegisterContract",
}, cfgFile)

// write the config to the temp dir and then reset
Expand All @@ -138,6 +144,7 @@ func TestConfigReset(t *testing.T) {
require.Equal("explorer", cfg.Explorer)
require.Equal(config.Context{AddressOrAlias: ""}, cfg.DefaultAccount)
require.Equal("testWsEndpoint", cfg.WsEndpoint)
require.Equal("testProjectRegisterContract", cfg.WsRegisterContract)

require.NoError(info.reset())
require.NoError(info.loadConfig())
Expand All @@ -150,6 +157,7 @@ func TestConfigReset(t *testing.T) {
require.Equal("English", resetCfg.Language)
require.Equal(_defaultAnalyserEndpoint, resetCfg.AnalyserEndpoint)
require.Equal(_defaultWsEndpoint, resetCfg.WsEndpoint)
require.Equal(_defaultWsProjectRegisterContract, resetCfg.WsRegisterContract)
require.Equal("iotexscan", resetCfg.Explorer)
require.Equal(*new(config.Context), resetCfg.DefaultAccount)
}
Expand Down Expand Up @@ -221,6 +229,10 @@ func TestConfigSet(t *testing.T) {
[]string{"wsEndpoint", "testWsEndpoint"},
"testWsEndpoint",
},
{
[]string{"wsProjectRegisterContract", "testWsProjectRegisterContract"},
"testWsProjectRegisterContract",
},
}

for _, tc := range tcs {
Expand Down

0 comments on commit efcc7eb

Please sign in to comment.