-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ioctl]: ws cmd support router contract (#4327)
* feat(ioctl): support router contract * feat(ioctl): fix typo
- Loading branch information
1 parent
ab4a59f
commit a1c97bf
Showing
12 changed files
with
273 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.