0%

eth | KeyError:baseFeePerGas

这是新版本 web3.py 导致的问题。

之前我们可以这样

1
2
3
4
5
6
7
8
9
10
11
12
tx_dic = test_contract_obj.functions.swapCoin(
routers,
tokenContracts,
receiveAddress,
receiveAmount,
one_router
). \
buildTransaction(
{
'gas': 500000,
}
)

但是,在新版本的 web3.py 中,这样会导致错误。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Traceback (most recent call last):
File "/Users/licong/python/Rhino/Rhino-Script/RhinoScript/RhinoGateway/Other/EVMSwapSol/SwapCoin.py", line 44, in <module>
print(swap_coin(routers, tokenContracts, receiveAddress, receiveAmount, one_router))
File "/Users/licong/python/Rhino/Rhino-Script/RhinoScript/RhinoGateway/Other/EVMSwapSol/SwapCoin.py", line 7, in swap_coin
tx_dic = test_contract_obj.functions.swapCoin(
File "/Users/licong/anaconda3/envs/py3.9/lib/python3.9/site-packages/web3/_utils/decorators.py", line 51, in wrapper
return to_wrap(*args, **kwargs)
File "/Users/licong/anaconda3/envs/py3.9/lib/python3.9/site-packages/web3/contract.py", line 1112, in buildTransaction
return self.build_transaction(transaction)
File "/Users/licong/anaconda3/envs/py3.9/lib/python3.9/site-packages/web3/contract.py", line 1099, in build_transaction
return build_transaction_for_function(
File "/Users/licong/anaconda3/envs/py3.9/lib/python3.9/site-packages/web3/contract.py", line 1672, in build_transaction_for_function
prepared_transaction = fill_transaction_defaults(web3, prepared_transaction)
File "cytoolz/functoolz.pyx", line 249, in cytoolz.functoolz.curry.__call__
File "/Users/licong/anaconda3/envs/py3.9/lib/python3.9/site-packages/web3/_utils/transactions.py", line 114, in fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "/Users/licong/anaconda3/envs/py3.9/lib/python3.9/site-packages/web3/_utils/transactions.py", line 64, in <lambda>
web3.eth.max_priority_fee + (2 * web3.eth.get_block('latest')['baseFeePerGas'])
File "/Users/licong/anaconda3/envs/py3.9/lib/python3.9/site-packages/web3/datastructures.py", line 51, in __getitem__
return self.__dict__[key] # type: ignore
KeyError: 'baseFeePerGas'

应该这样使用

1
2
3
4
5
6
7
8
9
10
11
12
13
tx_dic = test_contract_obj.functions.swapCoin(
routers,
tokenContracts,
receiveAddress,
receiveAmount,
one_router
). \
buildTransaction(
{
'gas': 500000,
'gasPrice': test_w3.eth.gasPrice,
}
)
请我喝杯咖啡吧~