Skip to content

Commit

Permalink
feat:Faucet to receive coins.optimize command line parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted zhang committed Mar 22, 2022
1 parent 09f5246 commit f8c4612
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 41 deletions.
98 changes: 58 additions & 40 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,81 +58,90 @@ func init() {

func Command_Version() *cobra.Command {
cc := &cobra.Command{
Use: "version",
Short: "Print version information",
Run: Command_Version_Runfunc,
Use: "version",
Short: "Print version information",
Run: Command_Version_Runfunc,
DisableFlagsInUseLine: true,
}
return cc
}

func Command_Default() *cobra.Command {
cc := &cobra.Command{
Use: "default",
Short: "Generate profile template",
Run: Command_Default_Runfunc,
Use: "default",
Short: "Generate profile template",
Run: Command_Default_Runfunc,
DisableFlagsInUseLine: true,
}
return cc
}

func Command_Register() *cobra.Command {
cc := &cobra.Command{
Use: "register",
Short: "Register miner information to cess chain",
Run: Command_Register_Runfunc,
Use: "register",
Short: "Register miner information to cess chain",
Run: Command_Register_Runfunc,
DisableFlagsInUseLine: true,
}
return cc
}

func Command_State() *cobra.Command {
cc := &cobra.Command{
Use: "state",
Short: "List miners' own information",
Run: Command_State_Runfunc,
Use: "state",
Short: "List miners' own information",
Run: Command_State_Runfunc,
DisableFlagsInUseLine: true,
}
return cc
}

func Command_Mining() *cobra.Command {
cc := &cobra.Command{
Use: "mining",
Short: "Start mining at CESS mining platform",
Run: Command_Mining_Runfunc,
Use: "mining",
Short: "Start mining at CESS mining platform",
Run: Command_Mining_Runfunc,
DisableFlagsInUseLine: true,
}
return cc
}

func Command_Exit() *cobra.Command {
cc := &cobra.Command{
Use: "exit",
Short: "Exit CESS mining platform",
Run: Command_Exit_Runfunc,
Use: "exit",
Short: "Exit CESS mining platform",
Run: Command_Exit_Runfunc,
DisableFlagsInUseLine: true,
}
return cc
}

func Command_Increase() *cobra.Command {
cc := &cobra.Command{
Use: "increase",
Short: "Increase the deposit of miners",
Run: Command_Increase_Runfunc,
Use: "increase <number of tokens>",
Short: "Increase the deposit of miners",
Run: Command_Increase_Runfunc,
DisableFlagsInUseLine: true,
}
return cc
}

func Command_Withdraw() *cobra.Command {
cc := &cobra.Command{
Use: "withdraw",
Short: "Redemption deposit",
Run: Command_Withdraw_Runfunc,
Use: "withdraw",
Short: "Redemption deposit",
Run: Command_Withdraw_Runfunc,
DisableFlagsInUseLine: true,
}
return cc
}

func Command_Obtain() *cobra.Command {
cc := &cobra.Command{
Use: "obtain",
Short: "Get cess test coin",
Run: Command_Obtain_Runfunc,
Use: "obtain <pubkey> <faucet address>",
Short: "Get cess test coin",
Run: Command_Obtain_Runfunc,
DisableFlagsInUseLine: true,
}
return cc
}
Expand Down Expand Up @@ -250,6 +259,15 @@ func Command_Exit_Runfunc(cmd *cobra.Command, args []string) {
}

func Command_Increase_Runfunc(cmd *cobra.Command, args []string) {
if len(os.Args) < 3 {
fmt.Printf("\x1b[%dm[err]\x1b[0m Please enter the increased deposit amount.\n", 41)
os.Exit(1)
}
_, err := strconv.ParseUint(os.Args[2], 10, 64)
if err != nil {
fmt.Printf("\x1b[%dm[err]\x1b[0m Please enter the correct deposit amount (positive integer).\n", 41)
os.Exit(1)
}
refreshProfile(cmd)
peerid, err := queryMinerId()
if err != nil {
Expand Down Expand Up @@ -281,8 +299,18 @@ func Command_Withdraw_Runfunc(cmd *cobra.Command, args []string) {
}
}
func Command_Obtain_Runfunc(cmd *cobra.Command, args []string) {
//TODO
refreshProfile(cmd)
if len(os.Args) < 4 {
fmt.Printf("\x1b[%dm[err]\x1b[0m Please enter wallet address public key and faucet address.\n", 41)
os.Exit(1)
}
err := chain.ObtainFromFaucet(os.Args[3], os.Args[2])
if err != nil {
fmt.Printf("\x1b[%dm[err]\x1b[0m %v\n", 41, err.Error())
os.Exit(1)
} else {
fmt.Println("success")
os.Exit(0)
}
}

//
Expand Down Expand Up @@ -418,23 +446,13 @@ func register() {

//
func increase() {
if len(os.Args) < 3 {
fmt.Printf("\x1b[%dm[err]\x1b[0m Please enter the increased deposit amount.\n", 41)
os.Exit(1)
}
_, err := strconv.ParseUint(os.Args[2], 10, 64)
if err != nil {
fmt.Printf("\x1b[%dm[err]\x1b[0m Please enter the correct deposit amount (positive integer).\n", 41)
os.Exit(1)
}

tokens, ok := new(big.Int).SetString(os.Args[2]+configs.TokenAccuracy, 10)
if !ok {
fmt.Printf("\x1b[%dm[err]\x1b[0m Please enter the correct deposit amount (positive integer).\n", 41)
os.Exit(1)
}

ok, err = chain.Increase(configs.Confile.MinerData.TransactionPrK, configs.ChainTx_Sminer_Increase, tokens)
ok, err := chain.Increase(configs.Confile.MinerData.TransactionPrK, configs.ChainTx_Sminer_Increase, tokens)
if err != nil {
logger.InfoLogger.Sugar().Infof("Increase failed......,err:%v", err)
logger.ErrLogger.Sugar().Errorf("%v", err)
Expand Down
2 changes: 1 addition & 1 deletion configs/sys.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package configs

// type and version
const Version = "CESS-Storage-Mining_0.3.0_Alpha"
const Version = "CESS-Bucket_V0.3.0"

// cess chain module
const (
Expand Down
37 changes: 37 additions & 0 deletions internal/chain/transaction.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package chain

import (
"encoding/json"
"fmt"
"math/big"
"strconv"

"storage-mining/configs"
"storage-mining/internal/logger"
"storage-mining/tools"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
Expand Down Expand Up @@ -1198,3 +1200,38 @@ func Withdraw(identifyAccountPhrase, TransactionName string) (bool, error) {
}
}
}

type faucet struct {
Ans answer `json:"Result"`
Status string `json:"Status"`
}
type answer struct {
Err string `json:"Err"`
AsInBlock bool `json:"AsInBlock"`
}

func ObtainFromFaucet(faucetaddr, pbk string) error {
var ob = struct {
Address string `json:"Address"`
}{
pbk,
}
var res faucet
resp, err := tools.Post(faucetaddr, ob)
if err != nil {
return err
}
err = json.Unmarshal(resp, &res)
if err != nil {
return err
}
if res.Ans.Err != "" {
return err
}

if res.Ans.AsInBlock {
return nil
} else {
return errors.New("The address has been picked up today, please come back after 1 day.")
}
}
24 changes: 24 additions & 0 deletions tools/utl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package tools
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"math/rand"
"net"
"net/http"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -269,3 +271,25 @@ func WalkDir(filePath string) ([]string, error) {
}
return dirs, nil
}

//
func Post(url string, para interface{}) ([]byte, error) {
body, err := json.Marshal(para)
if err != nil {
return nil, err
}
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
var resp = new(http.Response)
resp, err = http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp != nil {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return respBody, err
}
return nil, err
}

0 comments on commit f8c4612

Please sign in to comment.