github.com/turingchain2020/turingchain@v1.1.21/system/dapp/commands/asset.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  	"fmt"
     9  	"math"
    10  	"os"
    11  	"strings"
    12  
    13  	"github.com/turingchain2020/turingchain/common/address"
    14  	"github.com/turingchain2020/turingchain/rpc/jsonclient"
    15  	rpcTypes "github.com/turingchain2020/turingchain/rpc/types"
    16  	"github.com/turingchain2020/turingchain/types"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  // AssetCmd command
    21  func AssetCmd() *cobra.Command {
    22  	cmd := &cobra.Command{
    23  		Use:   "asset",
    24  		Short: "Asset query",
    25  		Args:  cobra.MinimumNArgs(1),
    26  	}
    27  	cmd.AddCommand(
    28  		GetAssetBalanceCmd(),
    29  	)
    30  	return cmd
    31  }
    32  
    33  // GetAssetBalanceCmd query asset balance
    34  func GetAssetBalanceCmd() *cobra.Command {
    35  	cmd := &cobra.Command{
    36  		Use:   "balance",
    37  		Short: "Query asset balance",
    38  		Run:   assetBalance,
    39  	}
    40  	addAssetBalanceFlags(cmd)
    41  	return cmd
    42  }
    43  
    44  func addAssetBalanceFlags(cmd *cobra.Command) {
    45  	cmd.Flags().StringP("addr", "a", "", "account addr")
    46  	cmd.MarkFlagRequired("addr")
    47  	cmd.Flags().StringP("exec", "e", "", getExecuterNameString())
    48  	cmd.Flags().StringP("asset_exec", "", "coins", "the asset executor")
    49  	cmd.Flags().StringP("asset_symbol", "", "trc", "the asset symbol")
    50  	cmd.Flags().IntP("height", "", -1, "block height")
    51  }
    52  
    53  func assetBalance(cmd *cobra.Command, args []string) {
    54  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
    55  	addr, _ := cmd.Flags().GetString("addr")
    56  	execer, _ := cmd.Flags().GetString("exec")
    57  	assetSymbol, _ := cmd.Flags().GetString("asset_symbol")
    58  	assetExec, _ := cmd.Flags().GetString("asset_exec")
    59  	height, _ := cmd.Flags().GetInt("height")
    60  
    61  	err := address.CheckAddress(addr)
    62  	if err != nil {
    63  		if err = address.CheckMultiSignAddress(addr); err != nil {
    64  			fmt.Fprintln(os.Stderr, types.ErrInvalidAddress)
    65  			return
    66  		}
    67  	}
    68  	if execer == "" {
    69  		execer = assetExec
    70  	}
    71  
    72  	if ok := types.IsAllowExecName([]byte(execer), []byte(execer)); !ok {
    73  		fmt.Fprintln(os.Stderr, types.ErrExecNameNotAllow)
    74  		return
    75  	}
    76  
    77  	stateHash := ""
    78  	if height >= 0 {
    79  		params := types.ReqBlocks{
    80  			Start:    int64(height),
    81  			End:      int64(height),
    82  			IsDetail: false,
    83  		}
    84  		var res rpcTypes.Headers
    85  		ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetHeaders", params, &res)
    86  		_, err := ctx.RunResult()
    87  		if err != nil {
    88  			fmt.Fprintln(os.Stderr, err)
    89  			return
    90  		}
    91  		h := res.Items[0]
    92  		stateHash = h.StateHash
    93  	}
    94  
    95  	var addrs []string
    96  	addrs = append(addrs, addr)
    97  	params := types.ReqBalance{
    98  		Addresses:   addrs,
    99  		Execer:      execer,
   100  		StateHash:   stateHash,
   101  		AssetExec:   assetExec,
   102  		AssetSymbol: assetSymbol,
   103  	}
   104  	var res []*rpcTypes.Account
   105  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetBalance", params, &res)
   106  	ctx.SetResultCb(parseGetBalanceRes)
   107  	ctx.Run()
   108  }
   109  
   110  // CreateAssetSendToExec 通用的创建 send_exec 交易, 额外指定资产合约
   111  func CreateAssetSendToExec(cmd *cobra.Command, args []string, fromExec string) {
   112  	title, _ := cmd.Flags().GetString("title")
   113  	cfg := types.GetCliSysParam(title)
   114  	paraName, _ := cmd.Flags().GetString("paraName")
   115  	exec, _ := cmd.Flags().GetString("exec")
   116  	exec = getRealExecName(paraName, exec)
   117  	to, err := GetExecAddr(exec)
   118  	if err != nil {
   119  		fmt.Fprintln(os.Stderr, err)
   120  		return
   121  	}
   122  
   123  	amount, _ := cmd.Flags().GetFloat64("amount")
   124  	note, _ := cmd.Flags().GetString("note")
   125  	symbol, _ := cmd.Flags().GetString("symbol")
   126  
   127  	payload := &types.AssetsTransferToExec{
   128  		To:        to,
   129  		Amount:    int64(math.Trunc((amount+0.0000001)*1e4)) * 1e4,
   130  		Note:      []byte(note),
   131  		Cointoken: symbol,
   132  		ExecName:  exec,
   133  	}
   134  
   135  	params := &rpcTypes.CreateTxIn{
   136  		Execer:     cfg.ExecName(fromExec),
   137  		ActionName: "TransferToExec",
   138  		Payload:    types.MustPBToJSON(payload),
   139  	}
   140  
   141  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
   142  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.CreateTransaction", params, nil)
   143  	ctx.RunWithoutMarshal()
   144  }
   145  
   146  // CreateAssetWithdraw 通用的创建 withdraw 交易, 额外指定资产合约
   147  func CreateAssetWithdraw(cmd *cobra.Command, args []string, fromExec string) {
   148  	title, _ := cmd.Flags().GetString("title")
   149  	cfg := types.GetCliSysParam(title)
   150  	exec, _ := cmd.Flags().GetString("exec")
   151  	paraName, _ := cmd.Flags().GetString("paraName")
   152  	exec = getRealExecName(paraName, exec)
   153  	amount, _ := cmd.Flags().GetFloat64("amount")
   154  	note, _ := cmd.Flags().GetString("note")
   155  	symbol, _ := cmd.Flags().GetString("symbol")
   156  
   157  	exec = getRealExecName(paraName, exec)
   158  	execAddr, err := GetExecAddr(exec)
   159  	if err != nil {
   160  		fmt.Fprintln(os.Stderr, err)
   161  		return
   162  	}
   163  	payload := &types.AssetsWithdraw{
   164  		To:        execAddr,
   165  		Amount:    int64(math.Trunc((amount+0.0000001)*1e4)) * 1e4,
   166  		Note:      []byte(note),
   167  		Cointoken: symbol,
   168  		ExecName:  exec,
   169  	}
   170  	params := &rpcTypes.CreateTxIn{
   171  		Execer:     cfg.ExecName(fromExec),
   172  		ActionName: "Withdraw",
   173  		Payload:    types.MustPBToJSON(payload),
   174  	}
   175  
   176  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
   177  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.CreateTransaction", params, nil)
   178  	ctx.RunWithoutMarshal()
   179  }
   180  
   181  // CreateAssetTransfer 通用的创建 transfer 交易, 额外指定资产合约
   182  func CreateAssetTransfer(cmd *cobra.Command, args []string, fromExec string) {
   183  	title, _ := cmd.Flags().GetString("title")
   184  	cfg := types.GetCliSysParam(title)
   185  	toAddr, _ := cmd.Flags().GetString("to")
   186  	amount, _ := cmd.Flags().GetFloat64("amount")
   187  	note, _ := cmd.Flags().GetString("note")
   188  	symbol, _ := cmd.Flags().GetString("symbol")
   189  
   190  	payload := &types.AssetsTransfer{
   191  		To:        toAddr,
   192  		Amount:    int64(math.Trunc((amount+0.0000001)*1e4)) * 1e4,
   193  		Note:      []byte(note),
   194  		Cointoken: symbol,
   195  	}
   196  	params := &rpcTypes.CreateTxIn{
   197  		Execer:     cfg.ExecName(fromExec),
   198  		ActionName: "Transfer",
   199  		Payload:    types.MustPBToJSON(payload),
   200  	}
   201  
   202  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
   203  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.CreateTransaction", params, nil)
   204  	ctx.RunWithoutMarshal()
   205  }
   206  
   207  // GetExecAddr 获取执行器地址
   208  func GetExecAddr(exec string) (string, error) {
   209  	if ok := types.IsAllowExecName([]byte(exec), []byte(exec)); !ok {
   210  		return "", types.ErrExecNameNotAllow
   211  	}
   212  
   213  	addrResult := address.ExecAddress(exec)
   214  	result := addrResult
   215  	return result, nil
   216  }
   217  
   218  func getRealExecName(paraName string, name string) string {
   219  	if strings.HasPrefix(name, "user.p.") {
   220  		return name
   221  	}
   222  	return paraName + name
   223  }