Skip to content

Instantly share code, notes, and snippets.

Last active March 18, 2022 14:16
What would you like to do?
# Ethereum helper methods
# source this in your .bashrc or .zshrc file with `. ~/.ethrc`
# --- Token addresses ---
# --- Prices / gas ---
# Returns the price in USD of the specified token
# example: `price eth`
# example: `price $gtc`
# example: `price <token>`
price() {
if [[ $1 = 'eth' ]]; then
echo $(curl -s\?ids\=ethereum\&vs_currencies\=usd | jq '.ethereum.usd')
echo $(curl -s$1 | jq '.market_data.current_price.usd')
# prints the basefee in gwei, and the cost in USD of sending transactions based on current basefee and no priority fee
gas() {
wad=1000000000000000000 # 1e18
price_eth=$(price eth)
price_gas=$(seth basefee)
price_gas_gwei=$(seth --to-fix 9 $price_gas)
cost_transfer=$(echo "scale=10;$price_eth*$price_gas*21000/$wad" | bc) # ETH transfer
cost_swap=$(echo "scale=10;$price_eth*$price_gas*130000/$wad" | bc) # Uniswap V3 swap
cost_deploy=$(echo "scale=10;$price_eth*$price_gas*1000000/$wad" | bc) # Something expensive, like contract deployment
printf 'Basefee \t%1.3f\n' $price_gas_gwei
echo ""
printf 'Transfer \t21k gas \t $%1.3f\n' $cost_transfer
printf 'Swap \t130k gas \t $%1.3f\n' $cost_swap
printf 'Deploy \t1M gas \t $%1.3f\n' $cost_deploy
# --- 4byte lookups ---
# Pass in a 4 byte function signature and get matching functions from This will also strip
# any appended calldata after the first four bytes
# example: `4byte 0x70a08231`
# example: `4byte 0xa9059cbb000000000000000000000000ae804d5b383350c487748c364ad7ec8f480e5b63000000000000000000000000000000000000000000000660a546d2d9a8e73ce3`
4byte() {
curl -s "${1:0:10}" | jq .
# Pass in a 32 byte topic and get matching events from
# example: `4byteEvent 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef`
4byteEvent() {
curl -s "$1" | jq .
# --- Calls ---
# example: `balanceOf <token> <address>`
# example: `balanceOf $dai <address>`
balanceOf() {
seth call $1 "balanceOf(address)(uint256)" $2
# --- Seth helpers ---
# returns the basefee in gwei
alias basefee='seth --from-wei $(seth basefee) gwei'
# example: `tracetx <txhash>` (`trace` is a builtin command)
tracetx() {
seth run-tx $1 --trace
# example: `debug <txhash>`
debug() {
seth bundle-source $(seth tx $1 to) > tmp.json && \ # often might need to bundle other sources
seth run-tx $1 --source=tmp.json --debug && \
rm tmp.json # <toContractName>.sol is often leftover, so delete that manually afterwards
# Returns the function selector for given data
# example: `selector <sig>`
selector() {
hash=$(seth keccak $1)
echo ${hash:0:10}
# ABI encode values, and return encoded values without the function signature
# example: `abiEncode <sig> [<args>]`
abiEncode() {
x=$(seth calldata $@);
echo "0x${x:10}" # slice off the function signature, i.e. provided function name doesn't matter
# Returns the slot used to store the value of a Solidity mapping for a specified address
# example: `getSolidityAddressSlot <address> <mappingSlot>`
getSolidityAddressSlot() {
echo $(seth keccak $(abiEncode "x(address,uint256)" $1 $2))
# Returns the slot used to store the value of a Vyper mapping for a specified address
# note: not guaranteed to work for all Vyper versions since storage layout is not yet stable. More info:
# example: `getVyperAddressSlot <address> <mappingSlot>`
getVyperAddressSlot() {
echo $(seth keccak $(abiEncode "x(uint256,address)" $2 $1))
Copy link

pyk commented Sep 18, 2021

@mds1 btw is there any example on how to use getSolidityAddressSlot() ser?

I tried to use it like this:

$ getSolidityAddressSlot 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2 1

But I don't know how to use it for slot loc in

Thank you ser

Copy link

pyk commented Sep 18, 2021

Eh nvm ser, I found that I can use to find the slot number ser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment