forked from cerc-io/laconicd-deprecated
Implement eth_sendTransaction (#104)
* Set up framework for sending transaction with correct args and nonce mutex locking * Set up printing ethereum address through emintkeys and getting chainid from flags * Implemented defaults for eth_sendTransaction * Fix bug with no data provided * Updated comments and error, as well as RLP encoded tx bytes for return instead of amino encoded
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ecdsa"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -10,8 +11,11 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
"github.com/cosmos/ethermint/rpc/args"
|
||||
"github.com/cosmos/ethermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/context"
|
||||
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
||||
ethcrypto "github.com/ethereum/go-ethereum/crypto"
|
||||
@@ -355,3 +359,60 @@ func recoverEthSig(R, S, Vb *big.Int, sigHash ethcmn.Hash) (ethcmn.Address, erro
|
||||
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
// PopulateFromArgs populates tx message with args (used in RPC API)
|
||||
func GenerateFromArgs(args args.SendTxArgs, ctx context.CLIContext) (msg *EthereumTxMsg, err error) {
|
||||
var nonce uint64
|
||||
|
||||
var gasLimit uint64
|
||||
|
||||
amount := (*big.Int)(args.Value)
|
||||
|
||||
gasPrice := (*big.Int)(args.GasPrice)
|
||||
|
||||
if args.GasPrice == nil {
|
||||
// Set default gas price
|
||||
// TODO: Change to min gas price from context once available through server/daemon
|
||||
gasPrice = big.NewInt(20)
|
||||
}
|
||||
|
||||
if args.Nonce == nil {
|
||||
// Get nonce (sequence) from account
|
||||
from := sdk.AccAddress(args.From.Bytes())
|
||||
_, nonce, err = authtypes.NewAccountRetriever(ctx).GetAccountNumberSequence(from)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
nonce = (uint64)(*args.Nonce)
|
||||
}
|
||||
|
||||
if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) {
|
||||
return nil, fmt.Errorf(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`)
|
||||
}
|
||||
|
||||
// Sets input to either Input or Data, if both are set and not equal error above returns
|
||||
var input []byte
|
||||
if args.Input != nil {
|
||||
input = *args.Input
|
||||
} else if args.Data != nil {
|
||||
input = *args.Data
|
||||
}
|
||||
|
||||
if args.To == nil {
|
||||
// Contract creation
|
||||
if len(input) == 0 {
|
||||
return nil, fmt.Errorf("contract creation without any data provided")
|
||||
}
|
||||
}
|
||||
|
||||
if args.Gas == nil {
|
||||
// Estimate the gas usage if necessary.
|
||||
// TODO: Set gas based on estimate when simulating txs are setup
|
||||
gasLimit = 22000
|
||||
} else {
|
||||
gasLimit = (uint64)(*args.Gas)
|
||||
}
|
||||
|
||||
return newEthereumTxMsg(nonce, args.To, amount, gasLimit, gasPrice, input), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user