-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add set project name cmd * refactor code * refactor code
- Loading branch information
Showing
4 changed files
with
124 additions
and
58 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
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,79 @@ | ||
package ioid | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/iotexproject/iotex-address/address" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/iotexproject/iotex-core/ioctl/cmd/ws" | ||
"github.com/iotexproject/iotex-core/ioctl/config" | ||
"github.com/iotexproject/iotex-core/ioctl/output" | ||
) | ||
|
||
// Multi-language support | ||
var ( | ||
_nameUsages = map[config.Language]string{ | ||
config.English: "name [PROJECT_NAME]", | ||
config.Chinese: "name [项目名称]", | ||
} | ||
_nameShorts = map[config.Language]string{ | ||
config.English: "Set project name", | ||
config.Chinese: "设置项目名称", | ||
} | ||
) | ||
|
||
// _nameCmd represents the ioID project name command | ||
var _nameCmd = &cobra.Command{ | ||
Use: config.TranslateInLang(_nameUsages, config.UILanguage), | ||
Short: config.TranslateInLang(_nameShorts, config.UILanguage), | ||
Args: cobra.MinimumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := setName(args) | ||
return output.PrintError(err) | ||
}, | ||
} | ||
|
||
func init() { | ||
_nameCmd.Flags().StringVarP( | ||
&ioIDStore, "ioIDStore", "i", | ||
"0xA0C9f9A884cdAE649a42F16b057735Bc4fE786CD", | ||
config.TranslateInLang(_ioIDStoreUsages, config.UILanguage), | ||
) | ||
_nameCmd.Flags().Uint64VarP( | ||
&projectId, "projectId", "p", 0, | ||
config.TranslateInLang(_projectIdUsages, config.UILanguage), | ||
) | ||
} | ||
|
||
func setName(args []string) error { | ||
name := args[0] | ||
|
||
ioioIDStore, err := address.FromHex(ioIDStore) | ||
if err != nil { | ||
return output.NewError(output.AddressError, "failed to convert ioIDStore address", err) | ||
} | ||
|
||
projectAddr, err := readContract(ioioIDStore, ioIDStoreABI, "project", "0") | ||
if err != nil { | ||
return output.NewError(output.ConvertError, "failed to read project", err) | ||
} | ||
|
||
caller, err := ws.NewContractCaller(projectABI, projectAddr[0].(address.Address).String()) | ||
if err != nil { | ||
return output.NewError(output.SerializationError, "failed to create contract caller", err) | ||
} | ||
|
||
tx, err := caller.CallAndRetrieveResult("setName", []any{ | ||
new(big.Int).SetUint64(projectId), | ||
name, | ||
}) | ||
if err != nil { | ||
return output.NewError(output.SerializationError, "failed to call contract", err) | ||
} | ||
|
||
fmt.Printf("Set project name txHash: %s\n", tx) | ||
|
||
return 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
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,29 @@ | ||
package ioid | ||
|
||
import ( | ||
"encoding/hex" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/iotexproject/iotex-address/address" | ||
|
||
"github.com/iotexproject/iotex-core/ioctl/cmd/action" | ||
) | ||
|
||
func readContract( | ||
contract address.Address, | ||
contractABI abi.ABI, | ||
method string, | ||
value string, | ||
args ...interface{}, | ||
) ([]interface{}, error) { | ||
data, err := contractABI.Pack(method, args...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
res, err := action.Read(contract, value, data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
data, _ = hex.DecodeString(res) | ||
return contractABI.Unpack(method, data) | ||
} |