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

[ioctl]: ws cmd support router contract #4327

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
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.

9 changes: 9 additions & 0 deletions ioctl/cmd/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ var (
config.English: "project id",
config.Chinese: "项目ID",
}

_flagRouterContractAddressUsages = map[config.Language]string{
config.English: "set w3bsteram router contract address for once",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w3bsteram typo

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
Loading