Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automatic gas estimation #855

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/seth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `seth index` command returns the slot number for the specified mapping type and input data
- `seth --from-fix` command converts fixed point numbers into parsed integers with the specified number of decimals
- `seth run-tx` now fetches contract source from etherscan if `ETHERSCAN_API_KEY` is set
- `seth mktx` now estimates gas limit if `ETH_GAS` is not set
- `seth send` now estimates gas limit if `ETH_GAS` is not set

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions src/seth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ wei—the smallest possible amount of ether—to the [Ethereum
Foundation's donation address]:

$ seth send --value 1 0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359
seth-send: warning: `ETH_GAS' not set; using default gas amount
Ethereum account passphrase (not echoed):
seth-send: Published transaction with 0 bytes of calldata.
seth-send: 0xe428d4bb148ded426777ae892578507e4f394f608ad9d3a9d0229e8348ba72e3
Expand Down Expand Up @@ -660,6 +659,7 @@ node's gas estimation.
seth estimate [<options>] <to> <sig> [<args>]
seth estimate [<options>] --create <code> <sig> [<args>]
seth estimate [<options>] --create <code> <data>
seth estimate [<options>] --create <data>

Options are similar to [`seth send`], but no transaction is published.

Expand Down Expand Up @@ -829,7 +829,7 @@ Sign and publish a transaction to the blockchain.
| ------------- | --------------- | ------------ | --------------- |
| `--block` | `ETH_BLOCK` | `latest` | block number |
| `--from` | `ETH_FROM` | n/a | sender |
| `--gas` | `ETH_GAS` | `200000` | gas quantity |
| `--gas` | `ETH_GAS` | estimated | gas quantity |
| `--gas-price` | `ETH_GAS_PRICE` | | gas price |
| `--prio-fee` | `ETH_PRIO_FEE` | | EIP-1559 priority fee (miner tip) |
| `--value` | `ETH_VALUE` | `0` | ether value |
Expand Down
3 changes: 2 additions & 1 deletion src/seth/libexec/seth/seth-estimate
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### or: seth estimate [<options>] <receiver> <data>
### or: seth estimate [<options>] --create <code> <sig> [<args>]
### or: seth estimate [<options>] --create <code> <data>
### or: seth estimate [<options>] --create <data>
###
### Perform a local call to <receiver> and return the gas usage.
###
Expand All @@ -15,7 +16,7 @@
### With `-F <sender>', simulate calling <receiver> from <sender>
### With `-V <value>', simulate transferring <value> to <receiver>.
set -e
[[ $2 ]] || seth --fail-usage "$0"
[[ $1 ]] || seth --fail-usage "$0"
d-xo marked this conversation as resolved.
Show resolved Hide resolved

if [[ $SETH_CREATE ]]; then
DATA=$(seth --to-hexdata "$1")
Expand Down
20 changes: 17 additions & 3 deletions src/seth/libexec/seth/seth-mktx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,34 @@ if [[ $2 ]]; then
data=$(seth calldata "${@:2}")
fi

data=${data:-0x}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we put this in an else for the above if statement?


if [[ -z "$SETH_CREATE" ]]; then
TO=$(seth --to-address "$1")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only for --create?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If SETH_CREATE is unset, then we are doing a normal transaction that has a recipient. If not, we are creating a contract, and TO must be unset since this is a requirement for a contract creation tx.

fi

if [[ -z "$ETH_GAS" ]]; then
if [[ $SETH_CREATE ]]; then
ETH_GAS=$(seth estimate --create $data)
else
ETH_GAS=$(seth estimate $TO $data)
fi
fi

args=(
--from "$(seth --to-address "$ETH_FROM")"
--nonce "${ETH_NONCE:-$(seth nonce "$ETH_FROM")}"
--chain-id "$(seth chain-id)"
--gas-price "${ETH_GAS_PRICE:-$(seth gas-price)}"
--gas-limit "${ETH_GAS:-200000}"
--gas-limit "$ETH_GAS"
--value "$value"
--data "${data:-0x}"
--data "$data"
)

if [[ $SETH_CREATE ]]; then
args+=(--create)
else
args+=(--to "$(seth --to-address "$1")")
args+=(--to "$TO")
fi

if [[ $ETH_PRIO_FEE ]]; then
Expand Down
11 changes: 9 additions & 2 deletions src/seth/libexec/seth/seth-send
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ set -e

[[ $ETH_FROM ]] ||
seth --fail "${0##*/}: error: \`ETH_FROM' not set; send from which account?"
[[ $ETH_GAS ]] ||
echo >&2 "${0##*/}: warning: \`ETH_GAS' not set; using default gas amount"
if [[ $SETH_RESEND ]]; then
nonce=$(seth nonce "$ETH_FROM")
export ETH_NONCE=$nonce
Expand All @@ -44,9 +42,18 @@ else
fi
fi

if [[ -z "$ETH_GAS" ]]; then
if [[ $SETH_CREATE ]]; then
ETH_GAS=$(seth estimate --create $DATA)
else
ETH_GAS=$(seth estimate $TO ${DATA:-0x})
fi
fi

jshon+=(-n {})
[[ $TO ]] && jshon+=(-s "$TO" -i to)
[[ $DATA ]] && jshon+=(-s "$DATA" -i data)
[[ $ETH_GAS ]] && jshon+=(-s "$ETH_GAS" -i gas)
# shellcheck disable=SC2207
jshon+=($(seth --send-params))
jshon+=(-i append)
Expand Down