Commit 59a23feb authored by lynAzrael's avatar lynAzrael Committed by 33cn

add evm rpc test

parent 156dc659
all:
chmod +x ./build.sh
./build.sh $(OUT) $(FLAG)
\ No newline at end of file
#!/usr/bin/env bash
strpwd=$(pwd)
strcmd=${strpwd##*dapp/}
strapp=${strcmd%/cmd*}
OUT_DIR="${1}/$strapp"
#FLAG=$2
mkdir -p "${OUT_DIR}"
cp ./build/* "${OUT_DIR}"
OUT_TESTDIR="${1}/dapptest/$strapp"
mkdir -p "${OUT_TESTDIR}"
chmod +x ./build/test-rpc.sh
cp ./build/test-rpc.sh "${OUT_TESTDIR}"
This diff is collapsed.
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"github.com/33cn/chain33/pluginmgr" "github.com/33cn/chain33/pluginmgr"
"github.com/33cn/plugin/plugin/dapp/evm/commands" "github.com/33cn/plugin/plugin/dapp/evm/commands"
"github.com/33cn/plugin/plugin/dapp/evm/executor" "github.com/33cn/plugin/plugin/dapp/evm/executor"
"github.com/33cn/plugin/plugin/dapp/evm/rpc"
"github.com/33cn/plugin/plugin/dapp/evm/types" "github.com/33cn/plugin/plugin/dapp/evm/types"
) )
...@@ -17,6 +18,6 @@ func init() { ...@@ -17,6 +18,6 @@ func init() {
ExecName: executor.GetName(), ExecName: executor.GetName(),
Exec: executor.Init, Exec: executor.Init,
Cmd: commands.EvmCmd, Cmd: commands.EvmCmd,
RPC: nil, RPC: rpc.Init,
}) })
} }
...@@ -146,4 +146,36 @@ message EvmQueryResp { ...@@ -146,4 +146,36 @@ message EvmQueryResp {
string caller = 3; string caller = 3;
string rawData = 4; string rawData = 4;
string jsonData = 5; string jsonData = 5;
}
message EvmContractCreateReq {
string code = 1;
int64 fee = 2;
string note = 3;
string alias = 4;
string caller = 5;
string abi = 6;
string expire = 7;
string paraName = 8;
}
message EvmContractCallReq {
uint64 amount = 1;
string code = 2;
int64 fee = 3;
string note = 4;
string caller = 5;
string abi = 6;
string exec = 7;
string expire = 8;
string paraName = 9;
}
message EvmContractTransferReq {
string caller = 1;
float amount = 2;
string exec = 3;
string expire = 4;
bool isWithdraw = 5;
string paraName = 6;
} }
\ No newline at end of file
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpc
import (
"context"
"encoding/hex"
"github.com/33cn/chain33/types"
evm "github.com/33cn/plugin/plugin/dapp/evm/types"
)
// EvmCreateTx 创建Evm合约接口
func (c *Jrpc) EvmCreateTx(parm *evm.EvmContractCreateReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
}
reply, err := c.cli.Create(context.Background(), *parm)
if err != nil {
return err
}
*result = hex.EncodeToString(reply.Data)
return nil
}
// EvmCallTx 调用Evm合约接口
func (c *Jrpc) EvmCallTx(parm *evm.EvmContractCallReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
}
reply, err := c.cli.Call(context.Background(), *parm)
if err != nil {
return err
}
*result = hex.EncodeToString(reply.Data)
return nil
}
// EvmTransferTx Evm转账接口
func (c *Jrpc) EvmTransferTx(parm *evm.EvmContractTransferReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
}
reply, err := c.cli.Transfer(context.Background(), *parm, false)
if err != nil {
return err
}
*result = hex.EncodeToString(reply.Data)
return nil
}
// EvmWithdrawTx Evm转账接口
func (c *Jrpc) EvmWithdrawTx(parm *evm.EvmContractTransferReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
}
reply, err := c.cli.Transfer(context.Background(), *parm, true)
if err != nil {
return err
}
*result = hex.EncodeToString(reply.Data)
return nil
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpc
import (
"context"
"fmt"
"math/rand"
"os"
"time"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
cty "github.com/33cn/chain33/system/dapp/coins/types"
"github.com/33cn/chain33/types"
evmtypes "github.com/33cn/plugin/plugin/dapp/evm/types"
)
// CreateEvmCallTx 创建未签名的调用evm交易
func (c *channelClient) Create(ctx context.Context, in evmtypes.EvmContractCreateReq) (*types.UnsignTx, error) {
bCode, err := common.FromHex(in.Code)
if err != nil {
fmt.Fprintln(os.Stderr, "parse evm code error", err)
return nil, err
}
action := evmtypes.EVMContractAction{Amount: 0, Code: bCode, GasLimit: 0, GasPrice: 0, Note: in.Note, Abi: in.Abi}
execer := types.ExecName(in.ParaName + "evm")
addr := address.ExecAddress(types.ExecName(in.ParaName + "evm"))
tx := &types.Transaction{Execer: []byte(execer), Payload: types.Encode(&action), Fee: 0, To: addr}
tx.Fee, _ = tx.GetRealFee(types.GInt("MinFee"))
if tx.Fee < in.Fee {
tx.Fee += in.Fee
}
random := rand.New(rand.NewSource(time.Now().UnixNano()))
tx.Nonce = random.Int63()
txHex := types.Encode(tx)
return &types.UnsignTx{Data: txHex}, nil
}
func (c *channelClient) Call(ctx context.Context, in evmtypes.EvmContractCallReq) (*types.UnsignTx, error) {
amountInt64 := in.Amount * 1e4 * 1e4
feeInt64 := in.Fee * 1e4 * 1e4
toAddr := address.ExecAddress(in.Exec)
bCode, err := common.FromHex(in.Code)
if err != nil {
fmt.Fprintln(os.Stderr, "parse evm code error", err)
return nil, err
}
action := evmtypes.EVMContractAction{Amount: amountInt64, Code: bCode, GasLimit: 0, GasPrice: 0, Note: in.Note, Abi: in.Abi}
tx := &types.Transaction{Execer: []byte(in.Exec), Payload: types.Encode(&action), Fee: 0, To: toAddr}
tx.Fee, _ = tx.GetRealFee(types.GInt("MinFee"))
if tx.Fee < feeInt64 {
tx.Fee += feeInt64
}
random := rand.New(rand.NewSource(time.Now().UnixNano()))
tx.Nonce = random.Int63()
txHex := types.Encode(tx)
return &types.UnsignTx{Data: txHex}, nil
}
func (c *channelClient) Transfer(ctx context.Context, in evmtypes.EvmContractTransferReq, isWithdraw bool) (*types.UnsignTx, error) {
var tx *types.Transaction
transfer := &cty.CoinsAction{}
amountInt64 := int64(in.Amount*1e4) * 1e4
execName := in.Exec
if isWithdraw {
transfer.Value = &cty.CoinsAction_Withdraw{Withdraw: &types.AssetsWithdraw{Amount: amountInt64, ExecName: execName, To: address.ExecAddress(execName)}}
transfer.Ty = cty.CoinsActionWithdraw
} else {
transfer.Value = &cty.CoinsAction_TransferToExec{TransferToExec: &types.AssetsTransferToExec{Amount: amountInt64, ExecName: execName, To: address.ExecAddress(execName)}}
transfer.Ty = cty.CoinsActionTransferToExec
}
if in.ParaName == "" {
tx = &types.Transaction{Execer: []byte(types.ExecName(in.ParaName + "coins")), Payload: types.Encode(transfer), To: address.ExecAddress(execName)}
} else {
tx = &types.Transaction{Execer: []byte(types.ExecName(in.ParaName + "coins")), Payload: types.Encode(transfer), To: address.ExecAddress(types.ExecName(in.ParaName + "coins"))}
}
var err error
tx.Fee, err = tx.GetRealFee(types.GInt("MinFee"))
if err != nil {
return nil, err
}
random := rand.New(rand.NewSource(time.Now().UnixNano()))
tx.Nonce = random.Int63()
txHex := types.Encode(tx)
return &types.UnsignTx{Data: txHex}, nil
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpc
import (
"github.com/33cn/chain33/rpc/types"
)
// Jrpc json rpc struct
type Jrpc struct {
cli *channelClient
}
// Grpc grpc struct
type Grpc struct {
*channelClient
}
type channelClient struct {
types.ChannelClient
}
// Init init grpc param
func Init(name string, s types.RPCServer) {
cli := &channelClient{}
grpc := &Grpc{channelClient: cli}
cli.Init(name, s, &Jrpc{cli: cli}, grpc)
}
This diff is collapsed.
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