Skip to content

Commit

Permalink
Use EstimateGas and SuggestGasPrice for defaults.
Browse files Browse the repository at this point in the history
  • Loading branch information
Immutability, LLC committed Feb 20, 2018
1 parent 7a2955a commit d3b34ee
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 24 deletions.
4 changes: 2 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,8 @@ This endpoint will debit an Ethereum account.
* `name` (`string: <required>`) - Specifies the name of the account to use for signing. This is specified as part of the URL.
* `to` (`string: <required>`) - A Hex string specifying the Ethereum address to send the Ether `to`.
* `value` (`string: <required>`) - The amount of ether - in wei.
* `gas_price` (`string: <optional> - defaults to 20000000000`) - The price in gas for the transaction.
* `gas_limit` (`string: <optional> - defaults to 50000`) - The gas limit for the transaction.
* `gas_price` (`string: <optional>`) - The price in gas for the transaction. If omitted, we will use the suggested gas price.
* `gas_limit` (`string: <optional>`) - The gas limit for the transaction. If omitted, we will estimate the gas limit.

#### Sample Payload

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ tx_hash 0xe99f3de1dfbae82121a009b9d3a2a60174f2904721ec114a8fc5454a96e62ba8
```

This defaults `gas_limit` to 50000 with a default `gas_price` of 20 gwei.
If the gas limit is omitted, we will try to estimate it; if the gas price is omitted, we will use a suggested gas price.

#### Rudimentary controls

Expand Down
2 changes: 1 addition & 1 deletion ethereum/path_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (b *backend) pathDebit(ctx context.Context, req *logical.Request, data *fra
if !allowed {
return nil, err
}
gasLimit, gasPrice, err := b.getEstimates(client, ctx, fromAddress, &toAddress, nil)
gasLimit, gasPrice, err := b.getEstimates(client, ctx, fromAddress, &toAddress, nil, nil)
if gasLimitIn.Cmp(&big.Int{}) != 0 {
gasLimit = gasLimitIn.Uint64()
}
Expand Down
21 changes: 4 additions & 17 deletions ethereum/path_contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ethereum
import (
"context"
"fmt"
"math/big"
"strings"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -51,12 +50,12 @@ Deploys an Ethereum contract.
"gas_price": &framework.FieldSchema{
Type: framework.TypeString,
Description: "The price in gas for the transaction.",
Default: "0",
Default: "21000000000",
},
"gas_limit": &framework.FieldSchema{
Type: framework.TypeString,
Description: "The gas limit (in Gwei) for the transaction.",
Default: "0",
Default: "1500000",
},
},
ExistenceCheck: b.pathExistenceCheck,
Expand All @@ -81,6 +80,8 @@ func (b *backend) pathCreateContract(ctx context.Context, req *logical.Request,
nonce := math.MustParseUint64(data.Get("nonce").(string))
gasLimitIn := math.MustParseBig256(data.Get("gas_limit").(string))
gasPriceIn := math.MustParseBig256(data.Get("gas_price").(string))
gasLimit := gasLimitIn.Uint64()
gasPrice := gasPriceIn
input := []byte(data.Get("transaction_data").(string))
var accountPath string
parsedPath := strings.Split(req.Path, "/contracts/")
Expand All @@ -106,19 +107,6 @@ func (b *backend) pathCreateContract(ctx context.Context, req *logical.Request,
return nil, err
}
fromAddress := common.HexToAddress(account.Address)
gasLimit, gasPrice, err := b.getEstimates(client, ctx, fromAddress, nil, input)
if gasLimitIn.Cmp(&big.Int{}) != 0 {
gasLimit = gasLimitIn.Uint64()
}
if err != nil {
return nil, err
}
if gasPriceIn.Cmp(&big.Int{}) != 0 {
gasPrice = gasPriceIn
}
if err != nil {
return nil, err
}

chainID := math.MustParseBig256(account.ChainID)
key, err := b.getAccountPrivateKey(accountPath, *account)
Expand All @@ -133,7 +121,6 @@ func (b *backend) pathCreateContract(ctx context.Context, req *logical.Request,
if err != nil {
return nil, err
}

rawTx = types.NewContractCreation(nonce, amount, gasLimit, gasPrice, input)

signedTx, err := transactor.Signer(types.NewEIP155Signer(chainID), common.HexToAddress(account.Address), rawTx)
Expand Down
13 changes: 11 additions & 2 deletions ethereum/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ethereum

import (
"bytes"
"context"
"crypto/ecdsa"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -31,6 +33,13 @@ const (
PassphraseSeparator string = "-"
)

func prettyPrint(v interface{}) string {
jsonString, _ := json.Marshal(v)
var out bytes.Buffer
json.Indent(&out, jsonString, "", " ")
return out.String()
}

func (b *backend) writeTemporaryKeystoreFile(path string, filename string, data []byte) (string, error) {
keystorePath := path + "/" + filename
err := ioutil.WriteFile(keystorePath, data, 0644)
Expand Down Expand Up @@ -252,8 +261,8 @@ func contains(stringSlice []string, searchString string) bool {
return false
}

func (b *backend) getEstimates(client *ethclient.Client, ctx context.Context, fromAddress common.Address, toAddress *common.Address, data []byte) (uint64, *big.Int, error) {
msg := ethereum.CallMsg{From: fromAddress, To: toAddress, Data: data}
func (b *backend) getEstimates(client *ethclient.Client, ctx context.Context, fromAddress common.Address, toAddress *common.Address, data []byte, value *big.Int) (uint64, *big.Int, error) {
msg := ethereum.CallMsg{From: fromAddress, To: toAddress, Data: data, Value: value}
gasLimit, err := client.EstimateGas(ctx, msg)
if err != nil {
return 0, nil, err
Expand Down
2 changes: 1 addition & 1 deletion test/send_contract.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"transaction_data": "6060604052341561000f57600080fd5b60d38061001d6000396000f3006060604052600436106049576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b114604e5780636d4ce63c14606e575b600080fd5b3415605857600080fd5b606c60048080359060200190919050506094565b005b3415607857600080fd5b607e609e565b6040518082815260200191505060405180910390f35b8060008190555050565b600080549050905600a165627a7a72305820d4b4961183894cf1196bcafbbe4d2573a925296dff82a9dcbc0e8bd8027b153f0029",
"value":"10000000000",
"value":"100000000",
"gas_limit":"1500000",
"gas_price":"21000000000",
"nonce":"1"
Expand Down

0 comments on commit d3b34ee

Please sign in to comment.