github.com/annchain/OG@v0.0.9/client/cmd/token.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package cmd 15 16 import ( 17 "fmt" 18 "github.com/annchain/OG/client/tx_client" 19 "github.com/annchain/OG/common" 20 "github.com/annchain/OG/common/crypto" 21 "github.com/annchain/OG/common/math" 22 "github.com/spf13/cobra" 23 ) 24 25 var ( 26 tokenCmd = &cobra.Command{ 27 Use: "token", 28 Short: "token processing", 29 Run: tokenList, 30 } 31 tokenIPOCmd = &cobra.Command{ 32 Use: "ipo", 33 Short: "token initial public offering", 34 Run: tokenIPO, 35 } 36 37 tokenSPOCmd = &cobra.Command{ 38 Use: "spo", 39 Short: "token second public offering", 40 Run: tokenSPO, 41 } 42 tokenDestroyCmd = &cobra.Command{ 43 Use: "destroy", 44 Short: "token second public offering", 45 Run: tokenDestroy, 46 } 47 tokenTransferCmd = &cobra.Command{ 48 Use: "transfer", 49 Short: "token transfer", 50 Run: tokenTransfer, 51 } 52 tokenName = "btc" 53 tokenId = int32(0) 54 enableSPO bool 55 ) 56 57 func tokenInit() { 58 tokenCmd.AddCommand(tokenIPOCmd, tokenSPOCmd, tokenDestroyCmd, tokenTransferCmd) 59 tokenCmd.PersistentFlags().StringVarP(&priv_key, "priv_key", "k", "", "priv_key ***") 60 tokenIPOCmd.PersistentFlags().Int64VarP(&value, "value", "v", 0, "value 1") 61 62 tokenSPOCmd.PersistentFlags().Int64VarP(&value, "value", "v", 0, "value 1") 63 tokenTransferCmd.PersistentFlags().Int64VarP(&value, "value", "v", 0, "value 1") 64 tokenSPOCmd.PersistentFlags().Int32VarP(&tokenId, "token_id", "i", 0, "token_id 1") 65 tokenDestroyCmd.PersistentFlags().Int32VarP(&tokenId, "token_id", "i", 0, "token_id 1") 66 tokenTransferCmd.PersistentFlags().Int32VarP(&tokenId, "token_id", "i", 0, "token_id 1") 67 tokenCmd.PersistentFlags().Uint64VarP(&nonce, "nonce", "n", 0, "nonce 1") 68 tokenTransferCmd.PersistentFlags().StringVarP(&to, "to", "t", "", "to 0x***") 69 tokenIPOCmd.PersistentFlags().StringVarP(&tokenName, "toke_name", "t", "test_token", "toke_name btc") 70 tokenIPOCmd.PersistentFlags().BoolVarP(&enableSPO, "enable_spo", "e", false, "enable_spo true") 71 72 } 73 74 func tokenIPO(cmd *cobra.Command, args []string) { 75 76 if priv_key == "" || value < 1 || tokenName == "" { 77 cmd.HelpFunc() 78 } 79 privKey, err := crypto.PrivateKeyFromString(priv_key) 80 if err != nil { 81 fmt.Println(err) 82 return 83 } 84 txClient := tx_client.NewTxClient(Host, true) 85 requester := tx_client.NewRequestGenerator(privKey) 86 87 if nonce <= 0 { 88 nonce, err = txClient.GetNonce(requester.Address()) 89 if err != nil { 90 fmt.Println(err) 91 return 92 } 93 } 94 data := requester.TokenPublishing(nonce+1, enableSPO, tokenName, math.NewBigInt(value)) 95 resp, err := txClient.SendTokenIPO(&data) 96 if err != nil { 97 fmt.Println(err) 98 return 99 } 100 fmt.Println(resp) 101 } 102 103 func tokenSPO(cmd *cobra.Command, args []string) { 104 if priv_key == "" || value < 1 { 105 cmd.HelpFunc() 106 } 107 privKey, err := crypto.PrivateKeyFromString(priv_key) 108 if err != nil { 109 fmt.Println(err) 110 return 111 } 112 txClient := tx_client.NewTxClient(Host, true) 113 requester := tx_client.NewRequestGenerator(privKey) 114 115 if nonce <= 0 { 116 nonce, err = txClient.GetNonce(requester.Address()) 117 if err != nil { 118 fmt.Println(err) 119 return 120 } 121 } 122 data := requester.SecondPublicOffering(tokenId, nonce+1, math.NewBigInt(value)) 123 resp, err := txClient.SendTokenSPO(&data) 124 if err != nil { 125 fmt.Println(err) 126 return 127 } 128 fmt.Println(resp) 129 } 130 131 func tokenDestroy(cmd *cobra.Command, args []string) { 132 if priv_key == "" { 133 cmd.HelpFunc() 134 } 135 privKey, err := crypto.PrivateKeyFromString(priv_key) 136 if err != nil { 137 fmt.Println(err) 138 return 139 } 140 txClient := tx_client.NewTxClient(Host, true) 141 requester := tx_client.NewRequestGenerator(privKey) 142 143 if nonce <= 0 { 144 nonce, err = txClient.GetNonce(requester.Address()) 145 if err != nil { 146 fmt.Println(err) 147 return 148 } 149 } 150 data := requester.TokenDestroy(tokenId, nonce+1) 151 resp, err := txClient.SendTokenDestroy(&data) 152 if err != nil { 153 fmt.Println(err) 154 return 155 } 156 fmt.Println(resp) 157 } 158 159 func tokenTransfer(cmd *cobra.Command, args []string) { 160 if to == "" || value < 1 || priv_key == "" { 161 cmd.HelpFunc() 162 } 163 toAddr := common.HexToAddress(to) 164 privKey, err := crypto.PrivateKeyFromString(priv_key) 165 if err != nil { 166 fmt.Println(err) 167 return 168 } 169 txClient := tx_client.NewTxClient(Host, true) 170 requester := tx_client.NewRequestGenerator(privKey) 171 172 if nonce <= 0 { 173 nonce, err = txClient.GetNonce(requester.Address()) 174 if err != nil { 175 fmt.Println(err) 176 return 177 } 178 } 179 data := requester.NormalTx(tokenId, nonce+1, toAddr, math.NewBigInt(value)) 180 resp, err := txClient.SendNormalTx(&data) 181 if err != nil { 182 fmt.Println(err) 183 return 184 } 185 fmt.Println(resp) 186 } 187 188 func tokenList(cmd *cobra.Command, args []string) { 189 txClient := tx_client.NewTxClient(Host, true) 190 list, err := txClient.GetTokenList() 191 if err != nil { 192 fmt.Println(err) 193 return 194 } 195 fmt.Println(list) 196 }