github.com/turingchain2020/turingchain@v1.1.21/system/dapp/commands/coins.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package commands 6 7 import ( 8 "bufio" 9 "encoding/hex" 10 "fmt" 11 "io" 12 "os" 13 "strings" 14 15 commandtypes "github.com/turingchain2020/turingchain/system/dapp/commands/types" 16 "github.com/turingchain2020/turingchain/types" 17 "github.com/spf13/cobra" 18 ) 19 20 // CoinsCmd coins command func 21 func CoinsCmd() *cobra.Command { 22 cmd := &cobra.Command{ 23 Use: "coins", 24 Short: "Construct system coins transactions", 25 Args: cobra.MinimumNArgs(1), 26 } 27 cmd.AddCommand( 28 CreateRawTransferCmd(), 29 CreateRawWithdrawCmd(), 30 CreateRawSendToExecCmd(), 31 CreateTxGroupCmd(), 32 ) 33 return cmd 34 } 35 36 // CreateRawTransferCmd create raw transfer tx 37 func CreateRawTransferCmd() *cobra.Command { 38 cmd := &cobra.Command{ 39 Use: "transfer", 40 Short: "Create a transfer transaction", 41 Run: createTransfer, 42 } 43 addCreateTransferFlags(cmd) 44 return cmd 45 } 46 47 func addCreateTransferFlags(cmd *cobra.Command) { 48 cmd.Flags().StringP("to", "t", "", "receiver account address") 49 cmd.MarkFlagRequired("to") 50 51 cmd.Flags().Float64P("amount", "a", 0, "transaction amount") 52 cmd.MarkFlagRequired("amount") 53 54 cmd.Flags().StringP("note", "n", "", "transaction note info") 55 } 56 57 func createTransfer(cmd *cobra.Command, args []string) { 58 toAddr, _ := cmd.Flags().GetString("to") 59 amount, _ := cmd.Flags().GetFloat64("amount") 60 note, _ := cmd.Flags().GetString("note") 61 txHex, err := commandtypes.CreateRawTx(cmd, toAddr, amount, note, false, "", "") 62 if err != nil { 63 fmt.Fprintln(os.Stderr, err) 64 return 65 } 66 fmt.Println(txHex) 67 } 68 69 // CreateRawWithdrawCmd create raw withdraw tx 70 func CreateRawWithdrawCmd() *cobra.Command { 71 cmd := &cobra.Command{ 72 Use: "withdraw", 73 Short: "Create a withdraw transaction", 74 Run: createWithdraw, 75 } 76 addCreateWithdrawFlags(cmd) 77 return cmd 78 } 79 80 func addCreateWithdrawFlags(cmd *cobra.Command) { 81 cmd.Flags().StringP("exec", "e", "", "execer withdrawn from") 82 cmd.MarkFlagRequired("exec") 83 84 cmd.Flags().Float64P("amount", "a", 0, "withdraw amount") 85 cmd.MarkFlagRequired("amount") 86 87 cmd.Flags().StringP("note", "n", "", "transaction note info") 88 } 89 90 func createWithdraw(cmd *cobra.Command, args []string) { 91 exec, _ := cmd.Flags().GetString("exec") 92 amount, _ := cmd.Flags().GetFloat64("amount") 93 note, _ := cmd.Flags().GetString("note") 94 paraName, _ := cmd.Flags().GetString("paraName") 95 realExec := getRealExecName(paraName, exec) 96 execAddr, err := commandtypes.GetExecAddr(realExec) 97 if err != nil { 98 fmt.Fprintln(os.Stderr, err) 99 return 100 } 101 txHex, err := commandtypes.CreateRawTx(cmd, execAddr, amount, note, true, "", realExec) 102 if err != nil { 103 fmt.Fprintln(os.Stderr, err) 104 return 105 } 106 fmt.Println(txHex) 107 } 108 109 // CreateRawSendToExecCmd create send to exec 110 func CreateRawSendToExecCmd() *cobra.Command { 111 cmd := &cobra.Command{ 112 Use: "send_exec", 113 Short: "Create a send to executor transaction", 114 Run: sendToExec, 115 } 116 addCreateRawSendToExecFlags(cmd) 117 return cmd 118 } 119 120 func addCreateRawSendToExecFlags(cmd *cobra.Command) { 121 cmd.Flags().StringP("exec", "e", "", "executor to be sent to") 122 cmd.MarkFlagRequired("exec") 123 124 cmd.Flags().Float64P("amount", "a", 0, "send amount") 125 cmd.MarkFlagRequired("amount") 126 127 cmd.Flags().StringP("note", "n", "", "transaction note info") 128 } 129 130 func sendToExec(cmd *cobra.Command, args []string) { 131 exec, _ := cmd.Flags().GetString("exec") 132 amount, _ := cmd.Flags().GetFloat64("amount") 133 note, _ := cmd.Flags().GetString("note") 134 paraName, _ := cmd.Flags().GetString("paraName") 135 realExec := getRealExecName(paraName, exec) 136 execAddr, err := commandtypes.GetExecAddr(realExec) 137 if err != nil { 138 fmt.Fprintln(os.Stderr, err) 139 return 140 } 141 txHex, err := commandtypes.CreateRawTx(cmd, execAddr, amount, note, false, "", realExec) 142 if err != nil { 143 fmt.Fprintln(os.Stderr, err) 144 return 145 } 146 fmt.Println(txHex) 147 } 148 149 // CreateTxGroupCmd create tx group 150 func CreateTxGroupCmd() *cobra.Command { 151 cmd := &cobra.Command{ 152 Use: "txgroup", 153 Short: "Create a transaction group", 154 Run: createTxGroup, 155 } 156 addCreateTxGroupFlags(cmd) 157 return cmd 158 } 159 160 func addCreateTxGroupFlags(cmd *cobra.Command) { 161 cmd.Flags().StringP("txs", "t", "", "transactions in hex, separated by space") 162 cmd.Flags().StringP("file", "f", "", "name of file which contains hex style transactions, separated by new line") 163 } 164 165 func createTxGroup(cmd *cobra.Command, args []string) { 166 title, _ := cmd.Flags().GetString("title") 167 cfg := types.GetCliSysParam(title) 168 txs, _ := cmd.Flags().GetString("txs") 169 file, _ := cmd.Flags().GetString("file") 170 171 var txsArr []string 172 if txs != "" { 173 txsArr = strings.Split(txs, " ") 174 } else if file != "" { 175 f, err := os.Open(file) 176 if err != nil { 177 fmt.Fprintln(os.Stderr, err) 178 return 179 } 180 defer f.Close() 181 rd := bufio.NewReader(f) 182 i := 1 183 for { 184 line, _, err := rd.ReadLine() 185 if err != nil && err != io.EOF { 186 fmt.Fprintln(os.Stderr, err) 187 return 188 } 189 if err == io.EOF { 190 break 191 } 192 cSet := " " // space and tab 193 lineStr := strings.Trim(string(line), cSet) 194 if lineStr == "" { 195 continue 196 } 197 fmt.Printf("tx %d: %s", i, lineStr+"\n") 198 txsArr = append(txsArr, lineStr) 199 i++ 200 } 201 } else { 202 fmt.Println("please input -t or -f; else, input -h to see help") 203 return 204 } 205 var transactions []*types.Transaction 206 for _, t := range txsArr { 207 txByte, err := hex.DecodeString(t) 208 if err != nil { 209 fmt.Fprintln(os.Stderr, err) 210 return 211 } 212 var transaction types.Transaction 213 types.Decode(txByte, &transaction) 214 transactions = append(transactions, &transaction) 215 } 216 group, err := types.CreateTxGroup(transactions, cfg.GetMinTxFeeRate()) 217 if err != nil { 218 fmt.Fprintln(os.Stderr, err) 219 return 220 } 221 err = group.CheckWithFork(cfg, true, true, 0, cfg.GetMinTxFeeRate(), cfg.GetMaxTxFee()) 222 if err != nil { 223 fmt.Fprintln(os.Stderr, err) 224 return 225 } 226 newtx := group.Tx() 227 grouptx := hex.EncodeToString(types.Encode(newtx)) 228 fmt.Println(grouptx) 229 }