Skip to content

Commit

Permalink
[ioctl] ioID setName (#4293)
Browse files Browse the repository at this point in the history
* add set project name cmd

* refactor code

* refactor code
  • Loading branch information
ququzone authored and dustinxie committed Jun 6, 2024
1 parent e58c7d9 commit c2f9b67
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 58 deletions.
1 change: 1 addition & 0 deletions ioctl/cmd/ioid/ioid.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func init() {
IoIDCmd.AddCommand(_applyCmd)
IoIDCmd.AddCommand(_projectCmd)
IoIDCmd.AddCommand(_deviceCmd)
IoIDCmd.AddCommand(_nameCmd)
IoIDCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint",
config.ReadConfig.Endpoint, config.TranslateInLang(_flagInsEndPointUsages,
config.UILanguage))
Expand Down
79 changes: 79 additions & 0 deletions ioctl/cmd/ioid/name.go
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
}
73 changes: 15 additions & 58 deletions ioctl/cmd/ioid/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package ioid
import (
"bytes"
_ "embed" // used to embed contract abi
"encoding/hex"
"fmt"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/iotexproject/iotex-address/address"
"github.com/spf13/cobra"

"github.com/iotexproject/iotex-core/ioctl/cmd/action"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
)
Expand Down Expand Up @@ -67,81 +65,39 @@ func project() error {
return output.NewError(output.AddressError, "failed to convert ioIDStore address", err)
}

data, err := ioIDStoreABI.Pack("project")
projectAddr, err := readContract(ioioIDStore, ioIDStoreABI, "project", "0")
if err != nil {
return output.NewError(output.ConvertError, "failed to pack project arguments", err)
}
res, err := action.Read(ioioIDStore, "0", data)
if err != nil {
return output.NewError(output.APIError, "failed to read contract", err)
}
data, _ = hex.DecodeString(res)
projectAddr, err := ioIDStoreABI.Unpack("project", data)
if err != nil {
return output.NewError(output.ConvertError, "failed to unpack project response", err)
return output.NewError(output.ConvertError, "failed to read project", err)
}

ioProjectAddr, _ := address.FromHex(projectAddr[0].(address.Address).String())
data, err = projectABI.Pack("name", new(big.Int).SetUint64(projectId))
if err != nil {
return output.NewError(output.ConvertError, "failed to pack project name arguments", err)
}
res, err = action.Read(ioProjectAddr, "0", data)
if err != nil {
return output.NewError(output.APIError, "failed to read contract", err)
}
data, _ = hex.DecodeString(res)
name, err := projectABI.Unpack("name", data)
if err != nil {
return output.NewError(output.ConvertError, "failed to unpack project name response", err)
}

data, err = ioIDStoreABI.Pack("projectDeviceContract", new(big.Int).SetUint64(projectId))
if err != nil {
return output.NewError(output.ConvertError, "failed to pack project device contract arguments", err)
}
res, err = action.Read(ioioIDStore, "0", data)
if err != nil {
return output.NewError(output.APIError, "failed to read contract", err)
}
data, _ = hex.DecodeString(res)
deviceContractAddr, err := ioIDStoreABI.Unpack("projectDeviceContract", data)
if err != nil {
return output.NewError(output.ConvertError, "failed to unpack project device contract response", err)
}

data, err = ioIDStoreABI.Pack("projectAppliedAmount", new(big.Int).SetUint64(projectId))
if err != nil {
return output.NewError(output.ConvertError, "failed to pack project applied amount arguments", err)
}
res, err = action.Read(ioioIDStore, "0", data)
name, err := readContract(ioProjectAddr, projectABI, "name", "0", new(big.Int).SetUint64(projectId))
if err != nil {
return output.NewError(output.APIError, "failed to read contract", err)
return output.NewError(output.ConvertError, "failed to read project name", err)
}
data, _ = hex.DecodeString(res)
projectAppliedAmount, err := ioIDStoreABI.Unpack("projectAppliedAmount", data)
owner, err := readContract(ioProjectAddr, projectABI, "ownerOf", "0", new(big.Int).SetUint64(projectId))
if err != nil {
return output.NewError(output.ConvertError, "failed to unpack project applied amount response", err)
return output.NewError(output.ConvertError, "failed to read project owner", err)
}

data, err = ioIDStoreABI.Pack("projectActivedAmount", new(big.Int).SetUint64(projectId))
deviceContractAddr, err := readContract(ioioIDStore, ioIDStoreABI, "projectDeviceContract", "0", new(big.Int).SetUint64(projectId))
if err != nil {
return output.NewError(output.ConvertError, "failed to pack project actived amount arguments", err)
return output.NewError(output.ConvertError, "failed to read project device contract", err)
}
res, err = action.Read(ioioIDStore, "0", data)
projectAppliedAmount, err := readContract(ioioIDStore, ioIDStoreABI, "projectAppliedAmount", "0", new(big.Int).SetUint64(projectId))
if err != nil {
return output.NewError(output.APIError, "failed to read contract", err)
return output.NewError(output.ConvertError, "failed to read project device applied amount", err)
}
data, _ = hex.DecodeString(res)
projectActivedAmount, err := ioIDStoreABI.Unpack("projectActivedAmount", data)
projectActivedAmount, err := readContract(ioioIDStore, ioIDStoreABI, "projectActivedAmount", "0", new(big.Int).SetUint64(projectId))
if err != nil {
return output.NewError(output.ConvertError, "failed to unpack project actived amount response", err)
return output.NewError(output.ConvertError, "failed to read project actived amount", err)
}

fmt.Printf(`Project #%d detail:
{
"projectContractAddress": "%s",
"projectContract": "%s",
"name": "%s",
"owner": "%s",
"deviceNFT": "%s",
"appliedIoIDs": "%s",
"activedIoIDs": "%s",
Expand All @@ -150,6 +106,7 @@ func project() error {
projectId,
projectAddr[0].(address.Address).String(),
name[0].(string),
owner[0].(address.Address).String(),
deviceContractAddr[0].(address.Address).String(),
projectAppliedAmount[0].(*big.Int).String(),
projectActivedAmount[0].(*big.Int).String(),
Expand Down
29 changes: 29 additions & 0 deletions ioctl/cmd/ioid/utils.go
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)
}

0 comments on commit c2f9b67

Please sign in to comment.