Commit 55e2f303 authored by 张振华's avatar 张振华

Merge branch 'master' into guess

parents ad86eb98 ef941882
This diff is collapsed.
...@@ -151,13 +151,15 @@ function token_create() { ...@@ -151,13 +151,15 @@ function token_create() {
echo "wrong finish created total" echo "wrong finish created total"
exit 1 exit 1
fi fi
${1} token token_balance -a 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -e token -s GD ${1} token token_balance -a 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -e token -s GD
balance=$(${1} token token_balance -a 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -e token -s GD | jq -r '.[]|.balance') balance=$(${1} token token_balance -a 1KSBd17H7ZK8iT37aJztFB22XGwsPTdwE4 -e token -s GD | jq -r '.[]|.balance')
if [ "${balance}" != "10000.0000" ]; then if [ "${balance}" != "10000.0000" ]; then
echo "wrong para token genesis create, should be 10000.0000" echo "wrong para token genesis create, should be 10000.0000"
exit 1 exit 1
fi fi
}
function token_transfer() {
echo "=========== # 2.token transfer =============" echo "=========== # 2.token transfer ============="
hash=$(${1} send token transfer -a 11 -s GD -t 1GGF8toZd96wCnfJngTwXZnWCBdWHYYvjw -k 0x6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b) hash=$(${1} send token transfer -a 11 -s GD -t 1GGF8toZd96wCnfJngTwXZnWCBdWHYYvjw -k 0x6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b)
echo "${hash}" echo "${hash}"
...@@ -170,6 +172,34 @@ function token_create() { ...@@ -170,6 +172,34 @@ function token_create() {
echo "wrong para token transfer, should be 11.0000" echo "wrong para token transfer, should be 11.0000"
exit 1 exit 1
fi fi
echo "=========== # 3.token send exec ============="
hash=$(${1} send token send_exec -a 11 -s GD -e paracross -k 0x6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b)
echo "${hash}"
block_wait "${1}" 3
# $ ./build/chain33-cli exec addr -e user.p.para.paracross
# 19WJJv96nKAU4sHFWqGmsqfjxd37jazqii
${1} tx query -s "${hash}"
${1} token token_balance -a 19WJJv96nKAU4sHFWqGmsqfjxd37jazqii -e token -s GD
balance=$(${1} token token_balance -a 19WJJv96nKAU4sHFWqGmsqfjxd37jazqii -e token -s GD | jq -r '.[]|.balance')
if [ "${balance}" != "11.0000" ]; then
echo "wrong para token send exec, should be 11.0000"
exit 1
fi
echo "=========== # 4.token withdraw ============="
hash=$(${1} send token withdraw -a 11 -s GD -e paracross -k 0x6da92a632ab7deb67d38c0f6560bcfed28167998f6496db64c258d5e8393a81b)
echo "${hash}"
block_wait "${1}" 3
${1} tx query -s "${hash}"
${1} token token_balance -a 19WJJv96nKAU4sHFWqGmsqfjxd37jazqii -e token -s GD
balance=$(${1} token token_balance -a 19WJJv96nKAU4sHFWqGmsqfjxd37jazqii -e token -s GD | jq -r '.[]|.balance')
if [ "${balance}" != "0.0000" ]; then
echo "wrong para token withdraw, should be 0.0000"
exit 1
fi
} }
function para_cross_transfer_withdraw() { function para_cross_transfer_withdraw() {
...@@ -204,6 +234,7 @@ function para_cross_transfer_withdraw() { ...@@ -204,6 +234,7 @@ function para_cross_transfer_withdraw() {
function para_test() { function para_test() {
echo "=========== # para chain test =============" echo "=========== # para chain test ============="
token_create "${PARA_CLI}" token_create "${PARA_CLI}"
token_transfer "${PARA_CLI}"
para_cross_transfer_withdraw para_cross_transfer_withdraw
} }
......
...@@ -7,6 +7,7 @@ package commands ...@@ -7,6 +7,7 @@ package commands
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"math"
"os" "os"
"strconv" "strconv"
"strings" "strings"
...@@ -40,6 +41,7 @@ func TokenCmd() *cobra.Command { ...@@ -40,6 +41,7 @@ func TokenCmd() *cobra.Command {
CreateRawTokenPreCreateTxCmd(), CreateRawTokenPreCreateTxCmd(),
CreateRawTokenFinishTxCmd(), CreateRawTokenFinishTxCmd(),
CreateRawTokenRevokeTxCmd(), CreateRawTokenRevokeTxCmd(),
CreateTokenTransferExecCmd(),
) )
return cmd return cmd
...@@ -74,12 +76,89 @@ func createTokenTransfer(cmd *cobra.Command, args []string) { ...@@ -74,12 +76,89 @@ func createTokenTransfer(cmd *cobra.Command, args []string) {
amount, _ := cmd.Flags().GetFloat64("amount") amount, _ := cmd.Flags().GetFloat64("amount")
note, _ := cmd.Flags().GetString("note") note, _ := cmd.Flags().GetString("note")
symbol, _ := cmd.Flags().GetString("symbol") symbol, _ := cmd.Flags().GetString("symbol")
txHex, err := CreateRawTx(cmd, toAddr, amount, note, false, symbol, "")
payload := &types.AssetsTransfer{
To: toAddr,
Amount: int64(math.Trunc((amount+0.0000001)*1e4)) * 1e4,
Note: []byte(note),
Cointoken: symbol,
}
data, err := json.Marshal(&payload)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
return return
} }
fmt.Println(txHex) params := &rpctypes.CreateTxIn{
Execer: types.ExecName(tokenty.TokenX),
ActionName: "Transfer",
Payload: data,
}
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, nil)
ctx.RunWithoutMarshal()
}
// CreateTokenTransferExecCmd 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) {
paraName, _ := cmd.Flags().GetString("paraName")
exec, _ := cmd.Flags().GetString("exec")
exec = getRealExecName(paraName, exec)
to, err := GetExecAddr(exec)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
amount, _ := cmd.Flags().GetFloat64("amount")
note, _ := cmd.Flags().GetString("note")
symbol, _ := cmd.Flags().GetString("symbol")
payload := &types.AssetsTransferToExec{
To: to,
Amount: int64(math.Trunc((amount+0.0000001)*1e4)) * 1e4,
Note: []byte(note),
Cointoken: symbol,
ExecName: exec,
}
data, err := json.Marshal(&payload)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(tokenty.TokenX),
ActionName: "TransferToExec",
Payload: data,
}
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, nil)
ctx.RunWithoutMarshal()
} }
// CreateTokenWithdrawCmd create raw withdraw tx // CreateTokenWithdrawCmd create raw withdraw tx
...@@ -113,17 +192,34 @@ func createTokenWithdraw(cmd *cobra.Command, args []string) { ...@@ -113,17 +192,34 @@ func createTokenWithdraw(cmd *cobra.Command, args []string) {
amount, _ := cmd.Flags().GetFloat64("amount") amount, _ := cmd.Flags().GetFloat64("amount")
note, _ := cmd.Flags().GetString("note") note, _ := cmd.Flags().GetString("note")
symbol, _ := cmd.Flags().GetString("symbol") symbol, _ := cmd.Flags().GetString("symbol")
exec = getRealExecName(paraName, exec)
execAddr, err := GetExecAddr(exec) execAddr, err := GetExecAddr(exec)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
return return
} }
txHex, err := CreateRawTx(cmd, execAddr, amount, note, true, symbol, exec) payload := &types.AssetsWithdraw{
To: execAddr,
Amount: int64(math.Trunc((amount+0.0000001)*1e4)) * 1e4,
Note: []byte(note),
Cointoken: symbol,
ExecName: exec,
}
data, err := json.Marshal(&payload)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
return return
} }
fmt.Println(txHex) params := &rpctypes.CreateTxIn{
Execer: types.ExecName(tokenty.TokenX),
ActionName: "Withdraw",
Payload: data,
}
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, nil)
ctx.RunWithoutMarshal()
} }
// GetTokensPreCreatedCmd get precreated tokens // GetTokensPreCreatedCmd get precreated tokens
......
...@@ -5,53 +5,12 @@ ...@@ -5,53 +5,12 @@
package commands package commands
import ( import (
"encoding/hex"
"math"
"strings" "strings"
"github.com/33cn/chain33/common/address" "github.com/33cn/chain33/common/address"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
tokenty "github.com/33cn/plugin/plugin/dapp/token/types"
"github.com/spf13/cobra"
) )
// CreateRawTx 创建交易
func CreateRawTx(cmd *cobra.Command, to string, amount float64, note string, isWithdraw bool, tokenSymbol, execName string) (string, error) {
if amount < 0 {
return "", types.ErrAmount
}
paraName, _ := cmd.Flags().GetString("paraName")
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
transfer := &tokenty.TokenAction{}
if !isWithdraw {
v := &tokenty.TokenAction_Transfer{Transfer: &types.AssetsTransfer{Cointoken: tokenSymbol, Amount: amountInt64, Note: []byte(note), To: to}}
transfer.Value = v
transfer.Ty = tokenty.ActionTransfer
} else {
v := &tokenty.TokenAction_Withdraw{Withdraw: &types.AssetsWithdraw{Cointoken: tokenSymbol, Amount: amountInt64, Note: []byte(note), To: to}}
transfer.Value = v
transfer.Ty = tokenty.ActionWithdraw
}
execer := []byte(getRealExecName(paraName, "token"))
if paraName == "" {
tx = &types.Transaction{Execer: execer, Payload: types.Encode(transfer), To: to}
} else {
tx = &types.Transaction{Execer: execer, Payload: types.Encode(transfer), To: address.ExecAddress(string(execer))}
}
tx, err := types.FormatTx(string(execer), tx)
if err != nil {
return "", err
}
txHex := types.Encode(tx)
return hex.EncodeToString(txHex), nil
}
// GetExecAddr 获取执行器地址 // GetExecAddr 获取执行器地址
func GetExecAddr(exec string) (string, error) { func GetExecAddr(exec string) (string, error) {
if ok := types.IsAllowExecName([]byte(exec), []byte(exec)); !ok { if ok := types.IsAllowExecName([]byte(exec), []byte(exec)); !ok {
......
...@@ -24,7 +24,7 @@ func (t *token) ExecTransWithdraw(accountDB *account.DB, tx *types.Transaction, ...@@ -24,7 +24,7 @@ func (t *token) ExecTransWithdraw(accountDB *account.DB, tx *types.Transaction,
return accountDB.Transfer(from, tx.GetRealToAddr(), transfer.Amount) return accountDB.Transfer(from, tx.GetRealToAddr(), transfer.Amount)
} else if (action.Ty == tokenty.ActionWithdraw) && action.GetWithdraw() != nil { } else if (action.Ty == tokenty.ActionWithdraw) && action.GetWithdraw() != nil {
withdraw := action.GetWithdraw() withdraw := action.GetWithdraw()
if types.IsFork(t.GetHeight(), "ForkWithdraw") { if !types.IsFork(t.GetHeight(), "ForkWithdraw") {
withdraw.ExecName = "" withdraw.ExecName = ""
} }
from := tx.From() from := tx.From()
......
...@@ -5,11 +5,16 @@ ...@@ -5,11 +5,16 @@
package types package types
import ( import (
"encoding/json"
"reflect" "reflect"
log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
) )
var tokenlog = log.New("module", "execs.token.types")
func init() { func init() {
types.AllowUserExec = append(types.AllowUserExec, []byte(TokenX)) types.AllowUserExec = append(types.AllowUserExec, []byte(TokenX))
types.RegistorExecutor(TokenX, NewType()) types.RegistorExecutor(TokenX, NewType())
...@@ -92,3 +97,28 @@ func (t *TokenType) RPC_Default_Process(action string, msg interface{}) (*types. ...@@ -92,3 +97,28 @@ func (t *TokenType) RPC_Default_Process(action string, msg interface{}) (*types.
} }
return tx, err return tx, err
} }
// CreateTx token 创建合约
func (t *TokenType) CreateTx(action string, msg json.RawMessage) (*types.Transaction, error) {
tx, err := t.ExecTypeBase.CreateTx(action, msg)
if err != nil {
tokenlog.Error("token CreateTx failed", "err", err, "action", action, "msg", string(msg))
return nil, err
}
if !types.IsPara() {
var transfer TokenAction
err = types.Decode(tx.Payload, &transfer)
if err != nil {
tokenlog.Error("token CreateTx failed", "decode payload err", err, "action", action, "msg", string(msg))
return nil, err
}
if action == "Transfer" {
tx.To = transfer.GetTransfer().To
} else if action == "Withdraw" {
tx.To = transfer.GetWithdraw().To
} else if action == "TransferToExec" {
tx.To = transfer.GetTransferToExec().To
}
}
return tx, nil
}
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