You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.4 KiB
84 lines
2.4 KiB
5 years ago
|
package apiv1
|
||
6 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"context"
|
||
|
"errors"
|
||
|
"math/big"
|
||
|
|
||
|
"github.com/ethereum/go-ethereum/common"
|
||
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
||
|
"github.com/harmony-one/harmony/core/types"
|
||
|
)
|
||
|
|
||
|
// SendTxArgs represents the arguments to sumbit a new transaction into the transaction pool.
|
||
|
type SendTxArgs struct {
|
||
|
From common.Address `json:"from"`
|
||
|
To *common.Address `json:"to"`
|
||
5 years ago
|
ShardID uint32 `json:"shardID"`
|
||
5 years ago
|
Gas *hexutil.Uint64 `json:"gas"`
|
||
|
GasPrice *hexutil.Big `json:"gasPrice"`
|
||
|
Value *hexutil.Big `json:"value"`
|
||
|
Nonce *hexutil.Uint64 `json:"nonce"`
|
||
6 years ago
|
// We accept "data" and "input" for backwards-compatibility reasons. "input" is the
|
||
|
// newer name and should be preferred by clients.
|
||
|
Data *hexutil.Bytes `json:"data"`
|
||
|
Input *hexutil.Bytes `json:"input"`
|
||
|
}
|
||
|
|
||
|
// setDefaults is a helper function that fills in default values for unspecified tx fields.
|
||
6 years ago
|
func (args *SendTxArgs) setDefaults(ctx context.Context, b Backend) error {
|
||
5 years ago
|
if args.Gas == nil {
|
||
|
args.Gas = new(hexutil.Uint64)
|
||
|
*(*uint64)(args.Gas) = 90000
|
||
6 years ago
|
}
|
||
|
// TODO(ricl): add check for shardID
|
||
|
if args.GasPrice == nil {
|
||
|
// TODO(ricl): port
|
||
|
// price, err := b.SuggestPrice(ctx)
|
||
|
// if err != nil {
|
||
|
// return err
|
||
|
// }
|
||
|
// args.GasPrice = (*hexutil.Big)(price)
|
||
|
}
|
||
|
if args.Value == nil {
|
||
5 years ago
|
args.Value = new(hexutil.Big)
|
||
6 years ago
|
}
|
||
5 years ago
|
if args.Nonce == nil {
|
||
6 years ago
|
nonce, err := b.GetPoolNonce(ctx, args.From)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
5 years ago
|
args.Nonce = (*hexutil.Uint64)(&nonce)
|
||
6 years ago
|
}
|
||
|
if args.Data != nil && args.Input != nil && !bytes.Equal(*args.Data, *args.Input) {
|
||
|
return errors.New(`both "data" and "input" are set and not equal. Please use "input" to pass transaction call data`)
|
||
|
}
|
||
|
if args.To == nil {
|
||
|
// Contract creation
|
||
|
var input []byte
|
||
|
if args.Data != nil {
|
||
|
input = *args.Data
|
||
|
} else if args.Input != nil {
|
||
|
input = *args.Input
|
||
|
}
|
||
|
if len(input) == 0 {
|
||
|
return errors.New(`contract creation without any data provided`)
|
||
|
}
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (args *SendTxArgs) toTransaction() *types.Transaction {
|
||
|
var input []byte
|
||
|
if args.Data != nil {
|
||
|
input = *args.Data
|
||
|
} else if args.Input != nil {
|
||
|
input = *args.Input
|
||
|
}
|
||
|
if args.To == nil {
|
||
5 years ago
|
return types.NewContractCreation(uint64(*args.Nonce), args.ShardID, (*big.Int)(args.Value), uint64(*args.Gas), (*big.Int)(args.GasPrice), input)
|
||
6 years ago
|
}
|
||
5 years ago
|
return types.NewTransaction(uint64(*args.Nonce), *args.To, args.ShardID, (*big.Int)(args.Value), uint64(*args.Gas), (*big.Int)(args.GasPrice), input)
|
||
6 years ago
|
}
|