Skip to content

Commit

Permalink
add a way to send evm tx
Browse files Browse the repository at this point in the history
  • Loading branch information
Green committed Dec 2, 2021
1 parent 9d0d298 commit 2472921
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
10 changes: 7 additions & 3 deletions exposed/evm.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package exposed

import (
"github.com/okex/exchain/libs/cosmos-sdk/crypto/keys"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"crypto/ecdsa"
"math/big"

ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethcore "github.com/ethereum/go-ethereum/core/types"
"github.com/okex/exchain-go-sdk/module/evm/types"
gosdktypes "github.com/okex/exchain-go-sdk/types"
rpctypes "github.com/okex/exchain/app/rpc/types"
"github.com/okex/exchain/libs/cosmos-sdk/crypto/keys"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
)

// Evm shows the expected behavior for inner farm client
Expand All @@ -26,7 +29,8 @@ type EvmTx interface {
sdk.TxResponse, error)
CreateContract(fromInfo keys.Info, passWd, amountStr, payloadStr, memo string, accNum, seqNum uint64) (
sdk.TxResponse, string, error)
SendTxEthereum(privHex, toAddrStr, amountStr, payloadStr string, gasLimit, seqNum uint64) (sdk.TxResponse, error)
SendTxEthereum2(privHex, toAddrStr, amountStr, payloadStr string, gasLimit, seqNum uint64) (sdk.TxResponse, error)
SendTxEthereum(priv *ecdsa.PrivateKey, nonce uint64, to ethcmn.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) (resp sdk.TxResponse, err error)
CreateContractEthereum(privHex, amountStr, payloadStr string, gasLimit, seqNum uint64) (sdk.TxResponse, error)
}

Expand Down
6 changes: 3 additions & 3 deletions module/auth/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"errors"
"fmt"

"github.com/okex/exchain-go-sdk/module/auth/types"
"github.com/okex/exchain-go-sdk/utils"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/libs/cosmos-sdk/x/auth"
authtypes "github.com/okex/exchain/libs/cosmos-sdk/x/auth/types"
"github.com/okex/exchain-go-sdk/module/auth/types"
"github.com/okex/exchain-go-sdk/utils"
)

// QueryAccount gets the account info
Expand All @@ -26,7 +26,7 @@ func (ac authClient) QueryAccount(accAddrStr string) (account types.Account, err

res, _, err := ac.Query(path, bytes)
if res == nil {
return account, errors.New("failed. your account has no record on the chain")
return account, fmt.Errorf("failed. your account has no record on the chain. error: %s", err)
}

if err = ac.GetCodec().UnmarshalJSON(res, &account); err != nil {
Expand Down
36 changes: 32 additions & 4 deletions module/evm/tx.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package evm

import (
"crypto/ecdsa"
"fmt"
"math/big"
"strings"

"github.com/okex/exchain/libs/cosmos-sdk/crypto/keys"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/okex/exchain-go-sdk/module/evm/types"
"github.com/okex/exchain-go-sdk/utils"
apptypes "github.com/okex/exchain/app/types"
"github.com/okex/exchain/libs/cosmos-sdk/crypto/keys"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
evmtypes "github.com/okex/exchain/x/evm/types"
)

Expand Down Expand Up @@ -92,8 +94,8 @@ func (ec evmClient) CreateContract(fromInfo keys.Info, passWd, amountStr, payloa
return
}

// SendTxEthereum sends an ethereum tx
func (ec evmClient) SendTxEthereum(privHex, toAddrStr, amountStr, payloadStr string, gasLimit, seqNum uint64) (
// SendTxEthereum2 sends an ethereum tx
func (ec evmClient) SendTxEthereum2(privHex, toAddrStr, amountStr, payloadStr string, gasLimit, seqNum uint64) (
resp sdk.TxResponse, err error) {
priv, err := ethcrypto.HexToECDSA(privHex)
if err != nil {
Expand Down Expand Up @@ -147,6 +149,32 @@ func (ec evmClient) SendTxEthereum(privHex, toAddrStr, amountStr, payloadStr str
return ec.Broadcast(bytes, ec.GetConfig().BroadcastMode)
}

// SendTxEthereum sends an ethereum tx
func (ec evmClient) SendTxEthereum(priv *ecdsa.PrivateKey, nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) (
resp sdk.TxResponse, err error) {

ethMsg := evmtypes.NewMsgEthereumTx(
nonce,
&to,
amount,
gasLimit,
gasPrice,
data,
)

config := ec.GetConfig()
if err = ethMsg.Sign(config.ChainIDBigInt, priv); err != nil {
return
}

bytes, err := ec.GetCodec().MarshalBinaryLengthPrefixed(ethMsg)
if err != nil {
return resp, fmt.Errorf("failed. encoded MsgEthereumTx error: %s", err)
}

return ec.Broadcast(bytes, ec.GetConfig().BroadcastMode)
}

// CreateContractEthereum generates an ethereum tx to deploy a smart contract
func (ec evmClient) CreateContractEthereum(privHex, amountStr, payloadStr string, gasLimit, seqNum uint64) (
resp sdk.TxResponse, err error) {
Expand Down

0 comments on commit 2472921

Please sign in to comment.