diff --git a/ioctl/cmd/ws/contracts/Makefile b/ioctl/cmd/ws/contracts/Makefile index e84d744d17..9fe43b14e3 100644 --- a/ioctl/cmd/ws/contracts/Makefile +++ b/ioctl/cmd/ws/contracts/Makefile @@ -8,7 +8,7 @@ fetch: @echo "#### clone sprout develop branch..." @mkdir sprout @cd sprout && git init --quiet - @cd sprout && git remote add origin git@github.com:machinefi/sprout.git + @cd sprout && git remote add origin git@github.com:machinefi/sprout.git @cd sprout && git config core.sparsecheckout true @cd sprout && echo "smartcontracts" >> .git/info/sparse-checkout @cd sprout && git pull origin develop --depth=1 --quiet @@ -19,7 +19,7 @@ fetch: generate_abi: @echo "#### generate abis from latest contracts..." @cd sprout/smartcontracts && yarn install > /dev/null 2>&1 - @mkdir -p abis && cd sprout/smartcontracts/contracts && for file in 'FleetManagement' 'ProjectRegistrar' 'W3bstreamProject' 'W3bstreamProver' 'ProjectDevice'; \ + @mkdir -p abis && cd sprout/smartcontracts/contracts && for file in 'FleetManagement' 'ProjectRegistrar' 'W3bstreamProject' 'W3bstreamProver' 'ProjectDevice' 'W3bstreamRouter'; \ do \ solc --include-path ../node_modules/ --base-path . --optimize --abi --overwrite --pretty-json -o . $$file.sol > /dev/null 2>&1 ; \ if [ -e $$file.abi ]; then \ diff --git a/ioctl/cmd/ws/contracts/abis/W3bstreamRouter.json b/ioctl/cmd/ws/contracts/abis/W3bstreamRouter.json index 7a7eb825ae..31ac0bd37a 100644 --- a/ioctl/cmd/ws/contracts/abis/W3bstreamRouter.json +++ b/ioctl/cmd/ws/contracts/abis/W3bstreamRouter.json @@ -197,6 +197,11 @@ "name": "_proverId", "type": "uint256" }, + { + "internalType": "uint256", + "name": "_taskId", + "type": "uint256" + }, { "internalType": "bytes", "name": "_data", diff --git a/ioctl/cmd/ws/contracts/gen.go b/ioctl/cmd/ws/contracts/gen.go index a88553acab..2d7e1aadbd 100644 --- a/ioctl/cmd/ws/contracts/gen.go +++ b/ioctl/cmd/ws/contracts/gen.go @@ -5,3 +5,4 @@ package contracts //go:generate abigen --abi abis/ProjectRegistrar.json --pkg contracts --type ProjectRegistrar -out ./projectregistrar.go //go:generate abigen --abi abis/FleetManagement.json --pkg contracts --type FleetManagement -out ./fleetmanagement.go //go:generate abigen --abi abis/ProjectDevice.json --pkg contracts --type ProjectDevice -out ./projectdevice.go +//go:generate abigen --abi abis/W3bstreamRouter.json --pkg contracts --type W3bstreamRouter -out ./w3bstreamrouter.go diff --git a/ioctl/cmd/ws/contracts/w3bstreamrouter.go b/ioctl/cmd/ws/contracts/w3bstreamrouter.go index ccb315cf5c..268e9fb5a7 100644 --- a/ioctl/cmd/ws/contracts/w3bstreamrouter.go +++ b/ioctl/cmd/ws/contracts/w3bstreamrouter.go @@ -31,7 +31,7 @@ var ( // W3bstreamRouterMetaData contains all meta data concerning the W3bstreamRouter contract. var W3bstreamRouterMetaData = &bind.MetaData{ - ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"projectId\",\"type\":\"uint256\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"dapp\",\"type\":\"address\"}],\"name\":\"DappBound\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"projectId\",\"type\":\"uint256\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"}],\"name\":\"DappUnbound\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"projectId\",\"type\":\"uint256\"},{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"router\",\"type\":\"uint256\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"bool\",\"name\":\"success\",\"type\":\"bool\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"revertReason\",\"type\":\"string\"}],\"name\":\"DataProcessed\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint8\",\"name\":\"version\",\"type\":\"uint8\"}],\"name\":\"Initialized\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_projectId\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"_dapp\",\"type\":\"address\"}],\"name\":\"bindDapp\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"dapp\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"fleetManagement\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_fleetManagement\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"_projectStore\",\"type\":\"address\"}],\"name\":\"initialize\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"projectStore\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_projectId\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"_proverId\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"_data\",\"type\":\"bytes\"}],\"name\":\"route\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_projectId\",\"type\":\"uint256\"}],\"name\":\"unbindDapp\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]", + ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"projectId\",\"type\":\"uint256\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"dapp\",\"type\":\"address\"}],\"name\":\"DappBound\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"projectId\",\"type\":\"uint256\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"}],\"name\":\"DappUnbound\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"projectId\",\"type\":\"uint256\"},{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"router\",\"type\":\"uint256\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"bool\",\"name\":\"success\",\"type\":\"bool\"},{\"indexed\":false,\"internalType\":\"string\",\"name\":\"revertReason\",\"type\":\"string\"}],\"name\":\"DataProcessed\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"uint8\",\"name\":\"version\",\"type\":\"uint8\"}],\"name\":\"Initialized\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_projectId\",\"type\":\"uint256\"},{\"internalType\":\"address\",\"name\":\"_dapp\",\"type\":\"address\"}],\"name\":\"bindDapp\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"dapp\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"fleetManagement\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"_fleetManagement\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"_projectStore\",\"type\":\"address\"}],\"name\":\"initialize\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"projectStore\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_projectId\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"_proverId\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"_taskId\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"_data\",\"type\":\"bytes\"}],\"name\":\"route\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"_projectId\",\"type\":\"uint256\"}],\"name\":\"unbindDapp\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]", } // W3bstreamRouterABI is the input ABI used to generate the binding from. @@ -315,25 +315,25 @@ func (_W3bstreamRouter *W3bstreamRouterTransactorSession) Initialize(_fleetManag return _W3bstreamRouter.Contract.Initialize(&_W3bstreamRouter.TransactOpts, _fleetManagement, _projectStore) } -// Route is a paid mutator transaction binding the contract method 0x388ac07e. +// Route is a paid mutator transaction binding the contract method 0x0f9e7c53. // -// Solidity: function route(uint256 _projectId, uint256 _proverId, bytes _data) returns() -func (_W3bstreamRouter *W3bstreamRouterTransactor) Route(opts *bind.TransactOpts, _projectId *big.Int, _proverId *big.Int, _data []byte) (*types.Transaction, error) { - return _W3bstreamRouter.contract.Transact(opts, "route", _projectId, _proverId, _data) +// Solidity: function route(uint256 _projectId, uint256 _proverId, uint256 _taskId, bytes _data) returns() +func (_W3bstreamRouter *W3bstreamRouterTransactor) Route(opts *bind.TransactOpts, _projectId *big.Int, _proverId *big.Int, _taskId *big.Int, _data []byte) (*types.Transaction, error) { + return _W3bstreamRouter.contract.Transact(opts, "route", _projectId, _proverId, _taskId, _data) } -// Route is a paid mutator transaction binding the contract method 0x388ac07e. +// Route is a paid mutator transaction binding the contract method 0x0f9e7c53. // -// Solidity: function route(uint256 _projectId, uint256 _proverId, bytes _data) returns() -func (_W3bstreamRouter *W3bstreamRouterSession) Route(_projectId *big.Int, _proverId *big.Int, _data []byte) (*types.Transaction, error) { - return _W3bstreamRouter.Contract.Route(&_W3bstreamRouter.TransactOpts, _projectId, _proverId, _data) +// Solidity: function route(uint256 _projectId, uint256 _proverId, uint256 _taskId, bytes _data) returns() +func (_W3bstreamRouter *W3bstreamRouterSession) Route(_projectId *big.Int, _proverId *big.Int, _taskId *big.Int, _data []byte) (*types.Transaction, error) { + return _W3bstreamRouter.Contract.Route(&_W3bstreamRouter.TransactOpts, _projectId, _proverId, _taskId, _data) } -// Route is a paid mutator transaction binding the contract method 0x388ac07e. +// Route is a paid mutator transaction binding the contract method 0x0f9e7c53. // -// Solidity: function route(uint256 _projectId, uint256 _proverId, bytes _data) returns() -func (_W3bstreamRouter *W3bstreamRouterTransactorSession) Route(_projectId *big.Int, _proverId *big.Int, _data []byte) (*types.Transaction, error) { - return _W3bstreamRouter.Contract.Route(&_W3bstreamRouter.TransactOpts, _projectId, _proverId, _data) +// Solidity: function route(uint256 _projectId, uint256 _proverId, uint256 _taskId, bytes _data) returns() +func (_W3bstreamRouter *W3bstreamRouterTransactorSession) Route(_projectId *big.Int, _proverId *big.Int, _taskId *big.Int, _data []byte) (*types.Transaction, error) { + return _W3bstreamRouter.Contract.Route(&_W3bstreamRouter.TransactOpts, _projectId, _proverId, _taskId, _data) } // UnbindDapp is a paid mutator transaction binding the contract method 0xd869758c. diff --git a/ioctl/cmd/ws/ws.go b/ioctl/cmd/ws/ws.go index f698b14f73..a582cff852 100644 --- a/ioctl/cmd/ws/ws.go +++ b/ioctl/cmd/ws/ws.go @@ -28,7 +28,7 @@ var ( } _flagWsEndpointUsages = map[config.Language]string{ - config.English: "set w3bsteram endpoint for once", + config.English: "set w3bstream endpoint for once", config.Chinese: "一次设置w3bstream端点", } @@ -43,22 +43,22 @@ var ( } _flagProjectRegisterContractAddressUsages = map[config.Language]string{ - config.English: "set w3bsteram project register contract address for once", + config.English: "set w3bstream project register contract address for once", config.Chinese: "一次设置w3bstream项目注册合约地址", } _flagProjectStoreContractAddressUsages = map[config.Language]string{ - config.English: "set w3bsteram project store contract address for once", + config.English: "set w3bstream project store contract address for once", config.Chinese: "一次设置w3bstream项目存储合约地址", } _flagFleetManagementContractAddressUsages = map[config.Language]string{ - config.English: "set w3bsteram fleet management contract address for once", + config.English: "set w3bstream fleet management contract address for once", config.Chinese: "一次设置w3bstream项目管理合约地址", } _flagProverStoreContractAddressUsages = map[config.Language]string{ - config.English: "set w3bsteram prover store contract address for once", + config.English: "set w3bstream prover store contract address for once", config.Chinese: "一次设置w3bstream prover存储合约地址", } _flagTransferAmountUsages = map[config.Language]string{ @@ -67,13 +67,18 @@ var ( } _flagProjectDevicesContractAddressUsages = map[config.Language]string{ - config.English: "set w3bsteram project devices contract address for once", + config.English: "set w3bstream project devices contract address for once", config.Chinese: "一次设置w3bstream project设备合约地址", } _flagProjectIDUsages = map[config.Language]string{ config.English: "project id", config.Chinese: "项目ID", } + + _flagRouterContractAddressUsages = map[config.Language]string{ + config.English: "set w3bstream router contract address for once", + config.Chinese: "一次设置w3bstream router合约地址", + } ) var ( @@ -118,4 +123,8 @@ func init() { &config.ReadConfig.WsProjectDevicesContract, "project-devices-contract", config.ReadConfig.WsProjectDevicesContract, config.TranslateInLang(_flagProjectDevicesContractAddressUsages, config.UILanguage), ) + WsCmd.PersistentFlags().StringVar( + &config.ReadConfig.WsRouterContract, "router-contract", + config.ReadConfig.WsRouterContract, config.TranslateInLang(_flagRouterContractAddressUsages, config.UILanguage), + ) } diff --git a/ioctl/cmd/ws/wsrouter.go b/ioctl/cmd/ws/wsrouter.go new file mode 100644 index 0000000000..17f7e48877 --- /dev/null +++ b/ioctl/cmd/ws/wsrouter.go @@ -0,0 +1,47 @@ +package ws + +import ( + "bytes" + _ "embed" // used to embed contract abi + + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/spf13/cobra" + + "github.com/iotexproject/iotex-core/ioctl/config" +) + +var wsRouterCmd = &cobra.Command{ + Use: "router", + Short: config.TranslateInLang(map[config.Language]string{ + config.English: "w3bstream project routing to Dapp", + config.Chinese: "w3bstream 项目路由到Dapp合约", + }, config.UILanguage), +} + +var ( + //go:embed contracts/abis/W3bstreamRouter.json + routerJSON []byte + routerAddress string + routerABI abi.ABI +) + +const ( + funcBindDapp = "bindDapp" + funcUnbindDapp = "unbindDapp" +) + +const ( + eventDappBound = "DappBound" + eventDappUnbound = "DappUnbound" +) + +func init() { + var err error + routerABI, err = abi.JSON(bytes.NewReader(routerJSON)) + if err != nil { + panic(err) + } + routerAddress = config.ReadConfig.WsRouterContract + + WsCmd.AddCommand(wsRouterCmd) +} diff --git a/ioctl/cmd/ws/wsrouterbinddapp.go b/ioctl/cmd/ws/wsrouterbinddapp.go new file mode 100644 index 0000000000..cb59b32807 --- /dev/null +++ b/ioctl/cmd/ws/wsrouterbinddapp.go @@ -0,0 +1,85 @@ +package ws + +import ( + "fmt" + "github.com/ethereum/go-ethereum/common" + "math/big" + + "github.com/pkg/errors" + "github.com/spf13/cobra" + + "github.com/iotexproject/iotex-address/address" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/output" +) + +var ( + // wsRouterBindDapp + wsRouterBindDapp = &cobra.Command{ + Use: "bind", + Short: config.TranslateInLang(wsRouterBindDappShorts, config.UILanguage), + RunE: func(cmd *cobra.Command, args []string) error { + projectID, err := cmd.Flags().GetUint64("project-id") + if err != nil { + return errors.Wrap(err, "failed to get flag project-id") + } + dapp, err := cmd.Flags().GetString("dapp") + if err != nil { + return errors.Wrap(err, "failed to get flag dapp") + } + + out, err := bindDapp(projectID, dapp) + if err != nil { + return output.PrintError(err) + } + output.PrintResult(out) + return nil + }, + } + + wsRouterBindDappShorts = map[config.Language]string{ + config.English: "bind project to dapp", + config.Chinese: "绑定项目与dapp", + } + + _flagDappUsages = map[config.Language]string{ + config.English: "dapp address for the project", + config.Chinese: "该project的Dapp地址", + } +) + +func init() { + wsRouterBindDapp.Flags().Uint64P("project-id", "", 0, config.TranslateInLang(_flagProjectIDUsages, config.UILanguage)) + wsRouterBindDapp.Flags().StringP("dapp", "", "", config.TranslateInLang(_flagDappUsages, config.UILanguage)) + + _ = wsRouterBindDapp.MarkFlagRequired("project-id") + _ = wsRouterBindDapp.MarkFlagRequired("dapp") + + wsRouterCmd.AddCommand(wsRouterBindDapp) +} + +func bindDapp(projectID uint64, dapp string) (string, error) { + addr, err := address.FromString(dapp) + if err != nil { + return "", errors.Wrapf(err, "invalid dapp address: %s", dapp) + } + newAddr := common.BytesToAddress(addr.Bytes()) + + caller, err := NewContractCaller(routerABI, routerAddress) + if err != nil { + return "", errors.Wrap(err, "failed to create contract caller") + } + + result := NewContractResult(&routerABI, eventDappBound, nil) + if _, err = caller.CallAndRetrieveResult(funcBindDapp, []any{ + big.NewInt(int64(projectID)), + newAddr, + }, result); err != nil { + return "", errors.Wrap(err, "failed to read contract") + } + if _, err = result.Result(); err != nil { + return "", err + } + + return fmt.Sprintf("bind dapp %s success", dapp), nil +} diff --git a/ioctl/cmd/ws/wsrouterunbinddapp.go b/ioctl/cmd/ws/wsrouterunbinddapp.go new file mode 100644 index 0000000000..5f25c558ad --- /dev/null +++ b/ioctl/cmd/ws/wsrouterunbinddapp.go @@ -0,0 +1,65 @@ +package ws + +import ( + "fmt" + "math/big" + + "github.com/pkg/errors" + "github.com/spf13/cobra" + + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/output" +) + +var ( + // wsRouterUnbindDapp + wsRouterUnbindDapp = &cobra.Command{ + Use: "unbind", + Short: config.TranslateInLang(wsRouterUnbindDappShorts, config.UILanguage), + RunE: func(cmd *cobra.Command, args []string) error { + projectID, err := cmd.Flags().GetUint64("project-id") + if err != nil { + return errors.Wrap(err, "failed to get flag project-id") + } + + out, err := unbindDapp(projectID) + if err != nil { + return output.PrintError(err) + } + output.PrintResult(out) + return nil + }, + } + + wsRouterUnbindDappShorts = map[config.Language]string{ + config.English: "unbind project to dapp", + config.Chinese: "解除绑定项目与dapp", + } +) + +func init() { + wsRouterUnbindDapp.Flags().Uint64P("project-id", "", 0, config.TranslateInLang(_flagProjectIDUsages, config.UILanguage)) + + _ = wsRouterUnbindDapp.MarkFlagRequired("project-id") + + wsRouterCmd.AddCommand(wsRouterUnbindDapp) +} + +func unbindDapp(projectID uint64) (string, error) { + caller, err := NewContractCaller(routerABI, routerAddress) + if err != nil { + return "", errors.Wrap(err, "failed to create contract caller") + } + + result := NewContractResult(&routerABI, eventDappUnbound, nil) + if _, err = caller.CallAndRetrieveResult(funcUnbindDapp, []any{ + big.NewInt(int64(projectID)), + }, result); err != nil { + return "", errors.Wrap(err, "failed to read contract") + } + if _, err = result.Result(); err != nil { + return "", err + } + + return fmt.Sprintf("unbind project %d success", projectID), nil +} diff --git a/ioctl/config/config.go b/ioctl/config/config.go index 4e4c518795..7d4936450d 100644 --- a/ioctl/config/config.go +++ b/ioctl/config/config.go @@ -83,6 +83,8 @@ type Config struct { WsProverStoreContract string `json:"wsProverStoreContract" yaml:"wsProverStoreContract"` // WsProjectDevicesContract w3bstream Project devices contract address WsProjectDevicesContract string `json:"wsProjectDevicesContract" yaml:"wsProjectDevicesContract"` + // WsRouterContract w3bstream Router contract address + WsRouterContract string `json:"wsRouterContract" yaml:"wsRouterContract"` } var ( @@ -168,6 +170,10 @@ func init() { ReadConfig.WsProjectDevicesContract = _defaultWsProjectDevicesContract completeness = false } + if ReadConfig.WsRouterContract == "" { + ReadConfig.WsRouterContract = _defaultWsRouterContract + completeness = false + } if !completeness { err := writeConfig() if err != nil { diff --git a/ioctl/config/configsetget.go b/ioctl/config/configsetget.go index 74ca670e22..033f6b3b2e 100644 --- a/ioctl/config/configsetget.go +++ b/ioctl/config/configsetget.go @@ -44,12 +44,14 @@ const ( _defaultWsProverStoreContract = "0xAD480a9c1B9fA8dD118c26Ac26880727160D0448" // _defaultWsProjectDevicesContract default project devices contract address _defaultWsProjectDevicesContract = "0x3d6b6c7bDB72e8BF73780f433342759d8b329Ca5" + // _defaultWsRouterContract default router contract address + _defaultWsRouterContract = "0x90A27ab74E790Cef6e258aabee1B361a9c993e8b" ) var ( _supportedLanguage = []string{"English", "中文"} - _validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsProjectRegisterContract", "wsProjectStoreContract", "wsFleetManagementContract", "wsProverStoreContract", "wsProjectDevicesContract"} - _validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsProjectRegisterContract", "wsProjectStoreContract", "wsFleetManagementContract", "wsProverStoreContract", "wsProjectDevicesContract", "all"} + _validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsProjectRegisterContract", "wsProjectStoreContract", "wsFleetManagementContract", "wsProverStoreContract", "wsProjectDevicesContract", "wsRouterContract"} + _validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsProjectRegisterContract", "wsProjectStoreContract", "wsFleetManagementContract", "wsProverStoreContract", "wsProjectDevicesContract", "wsRouterContract", "all"} _validExpl = []string{"iotexscan", "iotxplorer"} _endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$") ) @@ -181,6 +183,8 @@ func Get(arg string) error { fmt.Println(ReadConfig.WsProverStoreContract) case "wsProjectDevicesContract": fmt.Println(ReadConfig.WsProjectDevicesContract) + case "wsRouterContract": + fmt.Println(ReadConfig.WsRouterContract) case "all": fmt.Println(ReadConfig.String()) } @@ -325,6 +329,8 @@ func set(args []string) error { ReadConfig.WsProverStoreContract = args[1] case "wsProjectDevicesContract": ReadConfig.WsProjectDevicesContract = args[1] + case "wsRouterContract": + ReadConfig.WsRouterContract = args[1] } err := writeConfig() if err != nil { @@ -351,6 +357,7 @@ func reset() error { ReadConfig.WsFleetManagementContract = _defaultWsFleetManagementContract ReadConfig.WsProverStoreContract = _defaultWsProverStoreContract ReadConfig.WsProjectDevicesContract = _defaultWsProjectDevicesContract + ReadConfig.WsRouterContract = _defaultWsRouterContract err := writeConfig() if err != nil { diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index 0b1f3b8e60..979c030011 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -50,12 +50,14 @@ const ( _defaultWsProverStoreContract = "0xAD480a9c1B9fA8dD118c26Ac26880727160D0448" // _defaultWsProjectDevicesContract default project device contract address _defaultWsProjectDevicesContract = "0x3d6b6c7bDB72e8BF73780f433342759d8b329Ca5" + // _defaultWsRouterContract default router contract address + _defaultWsRouterContract = "0x90A27ab74E790Cef6e258aabee1B361a9c993e8b" ) var ( _supportedLanguage = []string{"English", "中文"} - _validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsProjectRegisterContract", "wsProjectStoreContract", "wsFleetManagementContract", "wsProverStoreContract", "wsProjectDevicesContract"} - _validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "analyserEndpoint", "wsProjectRegisterContract", "wsProjectStoreContract", "wsFleetManagementContract", "wsProverStoreContract", "wsProjectDevicesContract", "all"} + _validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsProjectRegisterContract", "wsProjectStoreContract", "wsFleetManagementContract", "wsProverStoreContract", "wsProjectDevicesContract", "wsRouterContract"} + _validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "analyserEndpoint", "wsProjectRegisterContract", "wsProjectStoreContract", "wsFleetManagementContract", "wsProverStoreContract", "wsProjectDevicesContract", "wsRouterContract", "all"} _validExpl = []string{"iotexscan", "iotxplorer"} _endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$") _configDir = os.Getenv("HOME") + "/.config/ioctl/default" @@ -156,6 +158,9 @@ func InitConfig() (config.Config, string, error) { if info.readConfig.WsProjectDevicesContract == "" { info.readConfig.WsProjectDevicesContract = _defaultWsProjectDevicesContract } + if info.readConfig.WsRouterContract == "" { + info.readConfig.WsRouterContract = _defaultWsRouterContract + } if !completeness { if err = info.writeConfig(); err != nil { return info.readConfig, info.defaultConfigFile, err @@ -193,6 +198,7 @@ func (c *info) reset() error { c.readConfig.WsFleetManagementContract = _defaultWsFleetManagementContract c.readConfig.WsProverStoreContract = _defaultWsProverStoreContract c.readConfig.WsProjectDevicesContract = _defaultWsProjectDevicesContract + c.readConfig.WsRouterContract = _defaultWsRouterContract err := c.writeConfig() if err != nil { @@ -267,6 +273,8 @@ func (c *info) set(args []string, insecure bool, client ioctl.Client) (string, e c.readConfig.WsProverStoreContract = args[1] case "wsProjectDevicesContract": c.readConfig.WsProjectDevicesContract = args[1] + case "wsRouterContract": + c.readConfig.WsRouterContract = args[1] default: return "", config.ErrConfigNotMatch } @@ -318,6 +326,8 @@ func (c *info) get(arg string) (string, error) { return c.readConfig.WsProverStoreContract, nil case "wsProjectDevicesContract": return c.readConfig.WsProjectDevicesContract, nil + case "wsRouterContract": + return c.readConfig.WsRouterContract, nil case "all": return jsonString(c.readConfig) default: diff --git a/ioctl/newcmd/config/config_test.go b/ioctl/newcmd/config/config_test.go index e9665095eb..3bd04cc493 100644 --- a/ioctl/newcmd/config/config_test.go +++ b/ioctl/newcmd/config/config_test.go @@ -65,6 +65,7 @@ func TestConfigGet(t *testing.T) { WsFleetManagementContract: "testWsFleetManagementContract", WsProverStoreContract: "testWsProverStoreContract", WsProjectDevicesContract: "testWsProjectDevicesContract", + WsRouterContract: "testWsRouterContract", }, testPath) tcs := []struct { @@ -127,10 +128,14 @@ func TestConfigGet(t *testing.T) { "wsProverStoreContract", "testWsProverStoreContract", }, + { + "wsRouterContract", + "testWsRouterContract", + }, { "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 \"ipfsEndpoint\": \"testIPFSEndpoint\",\n \"ipfsGateway\": \"testIPFSGateway\",\n \"wsProjectRegisterContract\": \"testWsProjectRegisterContract\",\n \"wsProjectStoreContract\": \"testWsProjectStoreContract\",\n \"wsFleetManagementContract\": \"testWsFleetManagementContract\",\n \"wsProverStoreContract\": \"testWsProverStoreContract\"\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 \"ipfsEndpoint\": \"testIPFSEndpoint\",\n \"ipfsGateway\": \"testIPFSGateway\",\n \"wsProjectRegisterContract\": \"testWsProjectRegisterContract\",\n \"wsProjectStoreContract\": \"testWsProjectStoreContract\",\n \"wsFleetManagementContract\": \"testWsFleetManagementContract\",\n \"wsProverStoreContract\": \"testWsProverStoreContract\",\n \"wsProjectDevicesContract\": \"testWsProjectDevicesContract\"\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 \"ipfsEndpoint\": \"testIPFSEndpoint\",\n \"ipfsGateway\": \"testIPFSGateway\",\n \"wsProjectRegisterContract\": \"testWsProjectRegisterContract\",\n \"wsProjectStoreContract\": \"testWsProjectStoreContract\",\n \"wsFleetManagementContract\": \"testWsFleetManagementContract\",\n \"wsProverStoreContract\": \"testWsProverStoreContract\",\n \"wsProjectDevicesContract\": \"testWsProjectDevicesContract\",\n \"wsRouterContract\": \"testWsRouterContract\"\n}", }, } @@ -165,6 +170,7 @@ func TestConfigReset(t *testing.T) { WsFleetManagementContract: "testWsFleetManagementContract", WsProverStoreContract: "testWsProverStoreContract", WsProjectDevicesContract: "testWsProjectDevicesContract", + WsRouterContract: "testWsRouterContract", }, cfgFile) // write the config to the temp dir and then reset @@ -187,6 +193,7 @@ func TestConfigReset(t *testing.T) { require.Equal("testWsFleetManagementContract", cfg.WsFleetManagementContract) require.Equal("testWsProverStoreContract", cfg.WsProverStoreContract) require.Equal("testWsProjectDevicesContract", cfg.WsProjectDevicesContract) + require.Equal("testWsRouterContract", cfg.WsRouterContract) require.NoError(info.reset()) require.NoError(info.loadConfig()) @@ -206,6 +213,7 @@ func TestConfigReset(t *testing.T) { require.Equal(_defaultWsFleetManagementContract, resetCfg.WsFleetManagementContract) require.Equal(_defaultWsProverStoreContract, resetCfg.WsProverStoreContract) require.Equal(_defaultWsProjectDevicesContract, resetCfg.WsProjectDevicesContract) + require.Equal(_defaultWsRouterContract, resetCfg.WsRouterContract) require.Equal("iotexscan", resetCfg.Explorer) require.Equal(*new(config.Context), resetCfg.DefaultAccount) } @@ -304,6 +312,10 @@ func TestConfigSet(t *testing.T) { []string{"wsProverStoreContract", "testWsProverStoreContract"}, "testWsProverStoreContract", }, + { + []string{"wsRouterContract", "testWsRouterContract"}, + "testWsRouterContract", + }, } for _, tc := range tcs {