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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commands to operate the NFT config contract #307

Open
wants to merge 1 commit into
base: sprint-1.11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
208 changes: 208 additions & 0 deletions cmd/bridge-config-update-sc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package cmd

import (
"context"
"fmt"
"time"

"github.com/0chain/gosdk/zcnbridge"
"github.com/0chain/zwalletcli/util"
"github.com/ethereum/go-ethereum/core/types"
)

//goland:noinspection ALL
func init() {
rootCmd.AddCommand(
createCommandWithBridge(
"bridge-config-set-uint256",
"Set a uint256 key value in NFT Config contract",
"Set a uint256 key value in NFT Config contract",
setUint256InNFTConfig,
&Option{
name: "key",
typename: "string",
value: "",
usage: "key",
required: true,
},
&Option{
name: "value",
typename: "int64",
value: int64(0),
usage: "value",
required: true,
},
&Option{
name: "plan",
typename: "int64",
value: int64(0),
usage: " plan id, used when setting royalty fees",
required: false,
},
),
createCommandWithBridge(
"bridge-config-get-uint256",
"Get a uint256 value in NFT Config contract",
"Get a uint256 value in NFT Config contract",
getUint256InNFTConfig,
&Option{
name: "key",
typename: "string",
value: "",
usage: "key",
required: true,
},
&Option{
name: "plan",
typename: "int64",
value: int64(0),
usage: "royalty plan id, used when setting royalty fees",
required: false,
},
),
createCommandWithBridge(
"bridge-config-set-address",
"Set an address key value in NFT Config contract",
"Set an address key value in NFT Config contract",
setAddressInNFTConfig,
&Option{
name: "key",
typename: "string",
value: "",
usage: "key",
required: true,
},
&Option{
name: "address",
typename: "string",
value: "",
usage: "address",
required: true,
},
),
createCommandWithBridge(
"bridge-config-get-address",
"Get an address value in NFT Config contract",
"Get an address value in NFT Config contract",
getAddressInNFTConfig,
&Option{
name: "key",
typename: "string",
value: "",
usage: "key",
required: true,
},
))
}

// setUint256InNFTConfig sets a uint256 key value in NFT Config contract
func setUint256InNFTConfig(bc *zcnbridge.BridgeClient, args ...*Arg) {
key := GetNFTConfigKey(args)
value := GetNFTConfigValue(args)
plan := GetNFTConfigRoyaltyPlanID(args)

var (
tx *types.Transaction
err error
)

if plan > 0 {
k := zcnbridge.EncodePackInt64(key, plan)
tx, err = bc.NFTConfigSetUint256Raw(context.Background(), k, value)
if err != nil {
ExitWithError(err)
}
} else {
tx, err = bc.NFTConfigSetUint256(context.Background(), key, value)
if err != nil {
ExitWithError(err)
}
}

hash := tx.Hash().String()
fmt.Printf("Confirming Ethereum transaction: %s\n", hash)

status, err := zcnbridge.ConfirmEthereumTransaction(hash, 100, time.Second*5)
if err != nil {
ExitWithError(err)
}

if status == 1 {
fmt.Printf("\nTransaction verification success: %s\n", hash)
} else {
ExitWithError(fmt.Sprintf("\nVerification failed: %s\n", hash))
}
}

func getUint256InNFTConfig(bc *zcnbridge.BridgeClient, args ...*Arg) {
key := GetNFTConfigKey(args)
plan := GetNFTConfigRoyaltyPlanID(args)

var (
k string
v int64
err error
)
if plan > 0 {
k, v, err = bc.NFTConfigGetUint256(context.Background(), key, plan)
if err != nil {
ExitWithError(err)
}
} else {
k, v, err = bc.NFTConfigGetUint256(context.Background(), key)
if err != nil {
ExitWithError(err)
}
}

var response = struct {
Key string `json:"key"`
Value int64 `json:"value"`
}{
Key: k,
Value: v,
}

util.PrettyPrintJSON(response)
}

func setAddressInNFTConfig(bc *zcnbridge.BridgeClient, args ...*Arg) {
key := GetNFTConfigKey(args)
address := GetNFTConfigAddress(args)
tx, err := bc.NFTConfigSetAddress(context.Background(), key, address)
if err != nil {
ExitWithError(err)
}

hash := tx.Hash().String()
fmt.Printf("Confirming Ethereum transaction: %s\n", hash)

status, err := zcnbridge.ConfirmEthereumTransaction(hash, 100, time.Second*5)
if err != nil {
ExitWithError(err)
}

if status == 1 {
fmt.Printf("\nTransaction verification success: %s\n", hash)
} else {
ExitWithError(fmt.Sprintf("\nVerification failed: %s\n", hash))
}
}

func getAddressInNFTConfig(bc *zcnbridge.BridgeClient, args ...*Arg) {
key := GetNFTConfigKey(args)
k, address, err := bc.NFTConfigGetAddress(context.Background(), key)
if err != nil {
ExitWithError(err)
}

var response = struct {
Key string `json:"key"`
Address string `json:"address"`
}{
Key: k,
Address: address,
}

util.PrettyPrintJSON(response)
}
54 changes: 37 additions & 17 deletions cmd/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ const (
)

const (
OptionHash = "hash" // OptionHash hash passed to cmd
OptionAmount = "amount" // OptionAmount amount passed to cmd
OptionToken = "token" // OptionToken token in SAS passed to cmd
OptionRetries = "retries" // OptionRetries retries
OptionConfigFolder = "path" // OptionConfigFolder config folder
OptionChainConfigFile = "chain_config" // OptionChainConfigFile sdk config filename
OptionMnemonic = "mnemonic" // OptionMnemonic bridge config filename
OptionKeyPassword = "password" // OptionKeyPassword bridge config filename
OptionClientKey = "client_key"
OptionClientID = "client_id"
OptionEthereumAddress = "ethereum_address"
OptionURL = "url"
OptionMinStake = "min_stake"
OptionMaxStake = "max_stake"
OptionNumDelegates = "num_delegates"
OptionServiceCharge = "service_charge"
OptionWalletFile = "wallet"
OptionHash = "hash" // OptionHash hash passed to cmd
OptionAmount = "amount" // OptionAmount amount passed to cmd
OptionToken = "token" // OptionToken token in SAS passed to cmd
OptionRetries = "retries" // OptionRetries retries
OptionConfigFolder = "path" // OptionConfigFolder config folder
OptionChainConfigFile = "chain_config" // OptionChainConfigFile sdk config filename
OptionMnemonic = "mnemonic" // OptionMnemonic bridge config filename
OptionKeyPassword = "password" // OptionKeyPassword bridge config filename
OptionClientKey = "client_key"
OptionClientID = "client_id"
OptionEthereumAddress = "ethereum_address"
OptionURL = "url"
OptionMinStake = "min_stake"
OptionMaxStake = "max_stake"
OptionNumDelegates = "num_delegates"
OptionServiceCharge = "service_charge"
OptionWalletFile = "wallet"
OptionNFTConfigKey = "key"
OptionNFTConfigValue = "value"
OptionNFTConfigRoyaltyPlanID = "plan"
OptionNFTConfigAddress = "address"
)

type CommandWithBridge func(*zcnbridge.BridgeClient, ...*Arg)
Expand Down Expand Up @@ -166,6 +170,22 @@ func GetEthereumAddress(args []*Arg) string {
return getString(args, OptionEthereumAddress)
}

func GetNFTConfigKey(args []*Arg) string {
return getString(args, OptionNFTConfigKey)
}

func GetNFTConfigValue(args []*Arg) int64 {
return getInt64(args, OptionNFTConfigValue)
}

func GetNFTConfigRoyaltyPlanID(args []*Arg) int64 {
return getInt64(args, OptionNFTConfigRoyaltyPlanID)
}

func GetNFTConfigAddress(args []*Arg) string {
return getString(args, OptionNFTConfigAddress)
}

func GetURL(args []*Arg) string {
return getString(args, OptionURL)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/0chain/zwalletcli

require (
github.com/0chain/gosdk v1.8.18-0.20230901213317-53d640a9b7f9
github.com/0chain/gosdk v1.8.18-0.20230907073049-4baadee51237
github.com/ethereum/go-ethereum v1.10.26
github.com/icza/bitio v1.1.0
github.com/olekukonko/tablewriter v0.0.5
Expand Down Expand Up @@ -75,4 +75,4 @@ require (
go 1.20

// temporary, for development
//replace github.com/0chain/gosdk => ../gosdk
// replace github.com/0chain/gosdk => ../gosdk
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM=
github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc=
github.com/0chain/gosdk v1.8.18-0.20230901213317-53d640a9b7f9 h1:GHTdYTmhNY9genBkNWLXdn3Z1yCtcbSNkcIFaKrqBRU=
github.com/0chain/gosdk v1.8.18-0.20230901213317-53d640a9b7f9/go.mod h1:3NKNYzmnMIYqZwwwOgZwMmTW1DT1ZUAmKyVPmYQOiT4=
github.com/0chain/gosdk v1.8.18-0.20230907073049-4baadee51237 h1:aWWq7N7kf9IK6GJxR8JodVwgv8LIkvUii9JuTinRsiU=
github.com/0chain/gosdk v1.8.18-0.20230907073049-4baadee51237/go.mod h1:3NKNYzmnMIYqZwwwOgZwMmTW1DT1ZUAmKyVPmYQOiT4=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Luzifer/go-openssl/v3 v3.1.0 h1:QqKqo6kYXGGUsvtUoCpRZm8lHw+jDfhbzr36gVj+/gw=
Expand Down