diff --git a/cmd/bridge-config-update-sc.go b/cmd/bridge-config-update-sc.go new file mode 100644 index 00000000..849655b8 --- /dev/null +++ b/cmd/bridge-config-update-sc.go @@ -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) +} diff --git a/cmd/bridge.go b/cmd/bridge.go index b189e3cc..8bbbf81d 100644 --- a/cmd/bridge.go +++ b/cmd/bridge.go @@ -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) @@ -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) } diff --git a/go.mod b/go.mod index fda56e82..01af6c0a 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -75,4 +75,4 @@ require ( go 1.20 // temporary, for development -//replace github.com/0chain/gosdk => ../gosdk +// replace github.com/0chain/gosdk => ../gosdk diff --git a/go.sum b/go.sum index 4af00dab..59af5449 100644 --- a/go.sum +++ b/go.sum @@ -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=