@@ -426,30 +426,88 @@ def test_multipayout_ERC20_web3_tester( testnet, provider, chain_id, src, src_pr
426426 latest = w3 .eth .get_block ('latest' )
427427 print ( f"{ testnet :10} : Web3 Latest block: { json .dumps ( latest , indent = 4 , default = str )} " )
428428
429+
430+ def gas_pricing (
431+ of = f"{ testnet :10} : Web3 Tester" ,
432+ gas = None ,
433+ ** kwds ,
434+ ):
435+ """Estimate what the gas will cost in USD$, given the transaction's parameters (None if unknown).
436+
437+ Defaults to the gas of a standard ETH transfer.
438+
439+ If an old-style fixed gasPrice is given, use that.
440+
441+ Otherwise, looks for EIP-1559 maxPriorityFeePerGas and maxFeePerGas.
442+
443+ """
444+ if gas is None :
445+ gas = 21000
446+ if 'gasPrice' in kwds :
447+ # Old fixed gas pricing
448+ est_gas_wei = int (kwds ['gasPrice' ])
449+ max_cost_gwei = int (gas ) * est_gas_wei / ETH .GWEI_WEI
450+ max_cost_eth = max_cost_gwei / ETH .ETH_GWEI
451+ max_cost_usd = max_cost_eth * ETH .ETH_USD
452+ print ( "{} Gas Price at USD${:9,.2f}/ETH: fee wei/gas fixed {:.4f}: cost per {:,d} Gas: {:,.4f} gwei == {:,.6f} ETH == USD${:10,.6f} ({})" .format ( # noqa: E501
453+ of ,
454+ ETH .ETH_USD ,
455+ est_gas_wei ,
456+ int (gas ),
457+ max_cost_gwei ,
458+ max_cost_eth ,
459+ max_cost_usd ,
460+ ETH .STATUS or 'estimated' ,
461+ ))
462+ elif 'maxFeePerGas' in kwds or 'baseFeePerGas' in kwds :
463+ # New EIP-1599 gas pricing
464+ max_gas_wei = int (kwds .get ( 'maxFeePerGas' , kwds .get ( 'baseFeePerGas' )))
465+ max_cost_gwei = int (gas ) * max_gas_wei / ETH .GWEI_WEI
466+ max_cost_eth = max_cost_gwei / ETH .ETH_GWEI
467+ max_cost_usd = max_cost_eth * ETH .ETH_USD
468+ print ( "{} Gas Price at USD${:9,.2f}/ETH: fee gwei/gas max. {:.4f}: cost per {:,d} Gas: {:,.4f} gwei == {:,.6f} ETH == USD${:10,.6f} ({})" .format ( # noqa: E501
469+ of ,
470+ ETH .ETH_USD ,
471+ max_gas_wei ,
472+ int (gas ),
473+ max_cost_gwei ,
474+ max_cost_eth ,
475+ max_cost_usd ,
476+ ETH .STATUS or 'estimated' ,
477+ ))
478+ else :
479+ max_cost_usd = None # Unknown
480+ print ( "{} Gas Price at USD${:9,.2f}/ETH: fee gwei/gas unknown from {} ({})" .format ( # noqa: E501
481+ of ,
482+ ETH .ETH_USD ,
483+ ', ' .join ( f"{ k } == { v !r} " for k ,v in kwds .items () ),
484+ ETH .STATUS or 'estimated' ,
485+ ))
486+
487+ return max_cost_usd
488+
429489 # Ask the connected Ethereum testnet what it thinks gas prices are. This will
430490 # (usually) be a Testnet, where gas prices are artificially low vs. the real Ethereum
431491 # network.
432492 max_priority_fee = w3 .eth .max_priority_fee
433493 base_fee = latest ['baseFeePerGas' ]
434- est_gas_wei = base_fee + max_priority_fee
435- max_gas_wei = base_fee * 2 + max_priority_fee # fail transaction if gas prices go wild
436- print ( "{:10}: Web3 Tester Gas Price at USD${:9,.2f}/ETH: fee gwei/gas est. base (latest): {:9,.2f} priority: {:9,.2f}; cost per {:,d} Gas: {:,.4f} gwei == USD${:9,.2f}; max: USD${:9,.2f} ({})" .format ( # noqa: E501
437- testnet , ETH .ETH_USD ,
438- base_fee / ETH .GWEI_WEI , max_priority_fee / ETH .GWEI_WEI ,
439- 100000 ,
440- 100000 * est_gas_wei / ETH .GWEI_WEI ,
441- 100000 * est_gas_wei * ETH .ETH_USD / ETH .ETH_WEI ,
442- 100000 * max_gas_wei * ETH .ETH_USD / ETH .ETH_WEI , ETH .STATUS or 'estimated' ,
443- ))
494+ gas = 21000
495+
496+ gas_pricing (
497+ gas = gas ,
498+ maxPriorityFeePerGas = max_priority_fee ,
499+ baseFeePerGas = base_fee ,
500+ )
444501
502+ max_gas_wei = base_fee * 2 + max_priority_fee
445503 gas_price_testnet = dict (
446504 maxFeePerGas = max_gas_wei , # If we want to fail if base fee + priority fee exceeds some limits
447505 maxPriorityFeePerGas = max_priority_fee ,
448506 )
449- print ( f"{ testnet :10} : Gas Pricing EIP-1559 for max $1.50 per 21,000 Gas transaction: { json .dumps ( gas_price_testnet )} " )
507+ print ( f"{ testnet :10} : Gas Pricing EIP-1559 for max $1.50 per 21,000 Gas transaction (using Testnet Gas pricing) : { json .dumps ( gas_price_testnet )} " )
450508
451509 # Let's say we're willing to pay up to $1.50 for a standard Ethereum transfer costing 21,000 gas
452- max_usd_per_gas = 1.50 / 21000
510+ max_usd_per_gas = 1.50 / gas
453511
454512 print ( f"{ testnet :10} : Web3 Tester Accounts, start of test:" )
455513 for a in ( src , ) + tuple ( destination ):
@@ -503,14 +561,21 @@ def test_multipayout_ERC20_web3_tester( testnet, provider, chain_id, src, src_pr
503561
504562 MultiPayoutERC20_contract = w3 .eth .contract ( abi = mp_ERC20_abi , bytecode = mp_ERC20_bytecode )
505563
506- gas = 3000000
564+ gas = 1400000 # 1388019 Gas is actual cost, as of 20230910
507565 spend = gas * max_usd_per_gas
508566 gas_price = ETH .maxPriorityFeePerGas ( spend = spend , gas = gas ) if ETH .UPDATED else gas_price_testnet
509- mc_cons_hash = MultiPayoutERC20_contract . constructor ( payees , tokens ). transact ( {
567+ mc_cons_tx = {
510568 'from' : src ,
511569 'nonce' : w3 .eth .get_transaction_count ( src ),
512570 'gas' : gas ,
513- } | gas_price )
571+ } | gas_price
572+
573+ gas_pricing ( f"{ testnet :10} : Web3 Tester Construct MultiPayoutERC20" , ** mc_cons_tx )
574+
575+ print ( f"{ testnet :10} : Web3 Tester Construct MultiPayoutERC20 Tx (using { 'Mainnet' if ETH .UPDATED else 'Testnet' } Gas pricing): { json .dumps (mc_cons_tx , indent = 4 )} " )
576+ # Let's see what this would cost, using the estimated gas:
577+
578+ mc_cons_hash = MultiPayoutERC20_contract .constructor ( payees , tokens ).transact ( mc_cons_tx )
514579 print ( f"{ testnet :10} : Web3 Tester Construct MultiPayoutERC20 hash: { mc_cons_hash .hex ()} " )
515580 mc_cons = w3 .eth .get_transaction ( mc_cons_hash )
516581 print ( f"{ testnet :10} : Web3 Tester Construct MultiPayoutERC20 transaction: { json .dumps ( mc_cons , indent = 4 , default = str )} " )
@@ -534,7 +599,7 @@ def tx_gas_cost( tx, receipt ):
534599 return base_fee + prio_fee
535600
536601 gas_cost = tx_gas_cost ( mc_cons , mc_cons_receipt )
537- print ( "{:10}: Web3 Tester Construct MultiPayoutERC20 Gas Used: {} == {:7.4f}Gwei == USD${:9,.2f } ({}): ${:7.6f}/byte" .format (
602+ print ( "{:10}: Web3 Tester Construct MultiPayoutERC20 Gas Used: {} == {:7.4f}Gwei == USD${:10,.6f } ({}): ${:7.6f}/byte" .format (
538603 testnet ,
539604 mc_cons_receipt .gasUsed ,
540605 mc_cons_receipt .gasUsed * gas_cost / ETH .GWEI_WEI ,
0 commit comments