-
Notifications
You must be signed in to change notification settings - Fork 328
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
w3bsteram typo