Commit 01b150b4 authored by linj's avatar linj Committed by vipwzw

client support token send to exec

parent 43c22d00
...@@ -40,6 +40,7 @@ func TokenCmd() *cobra.Command { ...@@ -40,6 +40,7 @@ func TokenCmd() *cobra.Command {
CreateRawTokenPreCreateTxCmd(), CreateRawTokenPreCreateTxCmd(),
CreateRawTokenFinishTxCmd(), CreateRawTokenFinishTxCmd(),
CreateRawTokenRevokeTxCmd(), CreateRawTokenRevokeTxCmd(),
CreateTokenTransferExecCmd(),
) )
return cmd return cmd
...@@ -82,6 +83,43 @@ func createTokenTransfer(cmd *cobra.Command, args []string) { ...@@ -82,6 +83,43 @@ func createTokenTransfer(cmd *cobra.Command, args []string) {
fmt.Println(txHex) fmt.Println(txHex)
} }
// CreateTokenTransferCmd create raw transfer tx
func CreateTokenTransferExecCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "send_exec",
Short: "Create a token send to executor transaction",
Run: createTokenSendToExec,
}
addCreateTokenSendToExecFlags(cmd)
return cmd
}
func addCreateTokenSendToExecFlags(cmd *cobra.Command) {
cmd.Flags().StringP("exec", "e", "", "receiver executor address")
cmd.MarkFlagRequired("exec")
cmd.Flags().Float64P("amount", "a", 0, "transaction amount")
cmd.MarkFlagRequired("amount")
cmd.Flags().StringP("note", "n", "", "transaction note info")
cmd.Flags().StringP("symbol", "s", "", "token symbol")
cmd.MarkFlagRequired("symbol")
}
func createTokenSendToExec(cmd *cobra.Command, args []string) {
exec, _ := cmd.Flags().GetString("exec")
amount, _ := cmd.Flags().GetFloat64("amount")
note, _ := cmd.Flags().GetString("note")
symbol, _ := cmd.Flags().GetString("symbol")
txHex, err := CreateRawTx(cmd, "", amount, note, false, symbol, exec)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Println(txHex)
}
// CreateTokenWithdrawCmd create raw withdraw tx // CreateTokenWithdrawCmd create raw withdraw tx
func CreateTokenWithdrawCmd() *cobra.Command { func CreateTokenWithdrawCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
......
...@@ -6,8 +6,9 @@ package commands ...@@ -6,8 +6,9 @@ package commands
import ( import (
"encoding/hex" "encoding/hex"
"fmt"
"math" "math"
"os"
"strings" "strings"
"github.com/33cn/chain33/common/address" "github.com/33cn/chain33/common/address"
...@@ -17,22 +18,38 @@ import ( ...@@ -17,22 +18,38 @@ import (
) )
// CreateRawTx 创建交易 // CreateRawTx 创建交易
// 主链交易使用 Transaction 的 To
// 平行链使用的 payload 的 To, Transaction 的 To 作为执行器的合约地址
// 在执行合约时, 有GetRealTo 用来读取这个项
// 在命令行中, 参数需要加前缀才是对的
// 在平行链上 执行器地址注册不带前缀, withdraw To地址有前缀, 算出来不一致
func CreateRawTx(cmd *cobra.Command, to string, amount float64, note string, isWithdraw bool, tokenSymbol, execName string) (string, error) { func CreateRawTx(cmd *cobra.Command, to string, amount float64, note string, isWithdraw bool, tokenSymbol, execName string) (string, error) {
if amount < 0 { if amount < 0 {
return "", types.ErrAmount return "", types.ErrAmount
} }
paraName, _ := cmd.Flags().GetString("paraName") paraName, _ := cmd.Flags().GetString("paraName")
amountInt64 := int64(math.Trunc((amount+0.0000001)*1e4)) * 1e4 amountInt64 := int64(math.Trunc((amount+0.0000001)*1e4)) * 1e4
execName = getRealExecName(paraName, execName)
if execName != "" && !types.IsAllowExecName([]byte(execName), []byte(execName)) {
return "", types.ErrExecNameNotMatch
}
var tx *types.Transaction var tx *types.Transaction
transfer := &tokenty.TokenAction{} transfer := &tokenty.TokenAction{}
if isWithdraw { if isWithdraw {
v := &tokenty.TokenAction_Withdraw{Withdraw: &types.AssetsWithdraw{Cointoken: tokenSymbol, Amount: amountInt64, Note: []byte(note), To: to}} v := &tokenty.TokenAction_Withdraw{Withdraw: &types.AssetsWithdraw{Cointoken: tokenSymbol, Amount: amountInt64,
Note: []byte(note), To: to, ExecName: getRealExecName(paraName, execName)}}
transfer.Value = v transfer.Value = v
transfer.Ty = tokenty.ActionWithdraw transfer.Ty = tokenty.ActionWithdraw
} else if execName != "" {
execName = getRealExecName(paraName, execName)
if execName != "" && !types.IsAllowExecName([]byte(execName), []byte(execName)) {
return "", types.ErrExecNameNotMatch
}
to, err := GetExecAddr(execName)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return "", err
}
v := &tokenty.TokenAction_TransferToExec{TransferToExec: &types.AssetsTransferToExec{Cointoken: tokenSymbol,
Amount: amountInt64, Note: []byte(note), ExecName: execName, To: to}}
transfer.Value = v
transfer.Ty = tokenty.TokenActionTransferToExec
} else { } else {
v := &tokenty.TokenAction_Transfer{Transfer: &types.AssetsTransfer{Cointoken: tokenSymbol, Amount: amountInt64, Note: []byte(note), To: to}} v := &tokenty.TokenAction_Transfer{Transfer: &types.AssetsTransfer{Cointoken: tokenSymbol, Amount: amountInt64, Note: []byte(note), To: to}}
transfer.Value = v transfer.Value = v
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment