Skip to content

Commit

Permalink
[ioctl]: ws cmd support router contract (#4327)
Browse files Browse the repository at this point in the history
* feat(ioctl): support router contract

* feat(ioctl): fix typo
  • Loading branch information
hunshenshi authored Jul 12, 2024
1 parent ab4a59f commit a1c97bf
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 26 deletions.
4 changes: 2 additions & 2 deletions ioctl/cmd/ws/contracts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fetch:
@echo "#### clone sprout develop branch..."
@mkdir sprout
@cd sprout && git init --quiet
@cd sprout && git remote add origin [email protected]:machinefi/sprout.git
@cd sprout && git remote add origin [email protected]: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
Expand All @@ -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 \
Expand Down
5 changes: 5 additions & 0 deletions ioctl/cmd/ws/contracts/abis/W3bstreamRouter.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@
"name": "_proverId",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_taskId",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "_data",
Expand Down
1 change: 1 addition & 0 deletions ioctl/cmd/ws/contracts/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 13 additions & 13 deletions ioctl/cmd/ws/contracts/w3bstreamrouter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions ioctl/cmd/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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端点",
}

Expand All @@ -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{
Expand All @@ -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 (
Expand Down Expand Up @@ -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),
)
}
47 changes: 47 additions & 0 deletions ioctl/cmd/ws/wsrouter.go
Original file line number Diff line number Diff line change
@@ -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)
}
85 changes: 85 additions & 0 deletions ioctl/cmd/ws/wsrouterbinddapp.go
Original file line number Diff line number Diff line change
@@ -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
}
65 changes: 65 additions & 0 deletions ioctl/cmd/ws/wsrouterunbinddapp.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions ioctl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit a1c97bf

Please sign in to comment.