Skip to content

Commit

Permalink
Merge branch 'staging' into fmt-zcn-#10
Browse files Browse the repository at this point in the history
  • Loading branch information
bbist committed Sep 9, 2021
2 parents 25c0f5a + dfba10d commit 436fd0e
Show file tree
Hide file tree
Showing 17 changed files with 682 additions and 304 deletions.
11 changes: 0 additions & 11 deletions .github/dependabot.yml

This file was deleted.

47 changes: 47 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package cmd

import (
"fmt"
"log"
"os"
"sort"
"strings"
"sync"

"github.com/0chain/gosdk/core/common"
"github.com/spf13/pflag"

"github.com/0chain/gosdk/zcncore"
"gopkg.in/cheggaaa/pb.v1"
)
Expand Down Expand Up @@ -124,3 +129,45 @@ func ExitWithError(v ...interface{}) {
fmt.Fprintln(os.Stderr, v...)
os.Exit(1)
}

func setupInputMap(flags *pflag.FlagSet) map[string]string {
var err error
var keys []string
if flags.Changed("keys") {
keys, err = flags.GetStringSlice("keys")
if err != nil {
log.Fatal(err)
}
}

var values []string
if flags.Changed("values") {
values, err = flags.GetStringSlice("values")
if err != nil {
log.Fatal(err)
}
}

input := make(map[string]string)
if len(keys) != len(values) {
log.Fatal("number keys must equal the number values")
}
for i := 0; i < len(keys); i++ {
v := strings.TrimSpace(values[i])
k := strings.TrimSpace(keys[i])
input[k] = v
}
return input
}

func printMap(outMap map[string]string) {
keys := make([]string, 0, len(outMap))
for k := range outMap {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
fmt.Println(k, "\t", outMap[k])
}
}
34 changes: 34 additions & 0 deletions cmd/fc-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
"log"
)

var getFaucetConfigCmd = &cobra.Command{
Use: "fc-config",
Short: "Show facuet configurations.",
Long: `Show facuet configurations.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {

var (
fields = new(zcncore.InputMap)
cb = NewJSONInfoCB(fields)
err error
)
if err = zcncore.GetFaucetSCConfig(cb); err != nil {
log.Fatal(err)
}
if err = cb.Waiting(); err != nil {
log.Fatal(err)
}

printMap(fields.Fields)
},
}

func init() {
rootCmd.AddCommand(getFaucetConfigCmd)
}
62 changes: 62 additions & 0 deletions cmd/fc-update-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"fmt"
"log"
"sync"

"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
)

var updateFaucetCmd = &cobra.Command{
Use: "fc-update-config",
Short: "Update the Faucet smart contract",
Long: `Update the Faucet smart contract.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var err error

input := new(zcncore.InputMap)
input.Fields = setupInputMap(cmd.Flags())
if err != nil {
log.Fatal(err)
}

var wg sync.WaitGroup
statusBar := &ZCNStatus{wg: &wg}
txn, err := zcncore.NewTransaction(statusBar, 0)
if err != nil {
log.Fatal(err)
}

wg.Add(1)
if err = txn.FaucetUpdateConfig(input); err != nil {
log.Fatal(err)
}
wg.Wait()

if !statusBar.success {
log.Fatal("fatal:", statusBar.errMsg)
}

statusBar.success = false
wg.Add(1)
if err = txn.Verify(); err != nil {
log.Fatal(err)
}
wg.Wait()

if !statusBar.success {
log.Fatal("fatal:", statusBar.errMsg)
}

fmt.Printf("faucet smart contract settings updated\nHash: %v\n", txn.GetTransactionHash())
},
}

func init() {
rootCmd.AddCommand(updateFaucetCmd)
updateFaucetCmd.PersistentFlags().StringSlice("keys", nil, "list of keys")
updateFaucetCmd.PersistentFlags().StringSlice("values", nil, "list of new values")
}
34 changes: 34 additions & 0 deletions cmd/global-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"log"

"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
)

var mnGlobalsCmd = &cobra.Command{
Use: "global-config",
Short: "Show global configurations.",
Long: `Show global configurations.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
fields = new(zcncore.InputMap)
cb = NewJSONInfoCB(fields)
err error
)
if err = zcncore.GetMinerSCGlobals(cb); err != nil {
log.Fatal(err)
}
if err = cb.Waiting(); err != nil {
log.Fatal(err)
}

printMap(fields.Fields)
},
}

func init() {
rootCmd.AddCommand(mnGlobalsCmd)
}
34 changes: 34 additions & 0 deletions cmd/ip-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
"log"
)

var getInterestPoolConfigCmd = &cobra.Command{
Use: "ip-config",
Short: "Show interest pool configurations.",
Long: `Show interest pool configurations.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {

var (
fields = new(zcncore.InputMap)
cb = NewJSONInfoCB(fields)
err error
)
if err = zcncore.GetInterestPoolSCConfig(cb); err != nil {
log.Fatal(err)
}
if err = cb.Waiting(); err != nil {
log.Fatal(err)
}

printMap(fields.Fields)
},
}

func init() {
rootCmd.AddCommand(getInterestPoolConfigCmd)
}
62 changes: 62 additions & 0 deletions cmd/ip-update-config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"fmt"
"log"
"sync"

"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
)

var updateInterestPoolConfigCmd = &cobra.Command{
Use: "ip-update-config",
Short: "Update the interest pool configurations.",
Long: `Update the interest pool configurations.`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var err error

input := new(zcncore.InputMap)
input.Fields = setupInputMap(cmd.Flags())
if err != nil {
log.Fatal(err)
}

var wg sync.WaitGroup
statusBar := &ZCNStatus{wg: &wg}
txn, err := zcncore.NewTransaction(statusBar, 0)
if err != nil {
log.Fatal(err)
}

wg.Add(1)
if err = txn.InterestPoolUpdateConfig(input); err != nil {
log.Fatal(err)
}
wg.Wait()

if !statusBar.success {
log.Fatal("fatal:", statusBar.errMsg)
}

statusBar.success = false
wg.Add(1)
if err = txn.Verify(); err != nil {
log.Fatal(err)
}
wg.Wait()

if !statusBar.success {
log.Fatal("fatal:", statusBar.errMsg)
}

fmt.Printf("interest pool smart contract settings updated\nHash: %v\n", txn.GetTransactionHash())
},
}

func init() {
rootCmd.AddCommand(updateInterestPoolConfigCmd)
updateInterestPoolConfigCmd.PersistentFlags().StringSlice("keys", nil, "list of keys")
updateInterestPoolConfigCmd.PersistentFlags().StringSlice("values", nil, "list of new values")
}
Loading

0 comments on commit 436fd0e

Please sign in to comment.