测试交易
>>> web3.eth.send_transaction({
'to': '0xd3CdA913deB6f67967B99D67aCDFa1712C293601',
'from': web3.eth.coinbase,
'value': 12345,
'gas': 21000,
'maxFeePerGas': web3.toWei(250, 'gwei'),
'maxPriorityFeePerGas': web3.toWei(2, 'gwei'),
})
测试查询
https://goerli.etherscan.io/tx/0x4a14aa0f0b18a8c7aa5677fdd057ffdb77791497d849da164ad42574ce778e5f
显示为
Txn Type: 2 (EIP-1559)
查看交易体
curl https://goerli.infura.io/v3/87aba2e668f9410498cdf74bc7a35467 -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0x4a14aa0f0b18a8c7aa5677fdd057ffdb77791497d849da164ad42574ce778e5f"],"id":1}' | jq
返回
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"accessList": [],
"blockHash": "0x5114398c54853f6ad6df7002ff88fad8f95c4ea74a32048275e72795e9381050",
"blockNumber": "0x51a84c",
"chainId": "0x5",
"from": "0x10210572d6b4924af7ef946136295e9b209e1fa0",
"gas": "0x5208",
"gasPrice": "0xad731380",
"hash": "0x4a14aa0f0b18a8c7aa5677fdd057ffdb77791497d849da164ad42574ce778e5f",
"input": "0x",
"maxFeePerGas": "0xad731384",
"maxPriorityFeePerGas": "0xad731379",
"nonce": "0xf9",
"r": "0x86e133483aff5415fffc73b30b9c781438655362fe070160d1baf5c6dd4a7b43",
"s": "0x1241e3560cb2e2354168adfe25f3a6c361694b422ff92bc9eebc124403be846e",
"to": "0x48f155527f25eb1d4cb2aa32b7e84692aa0025c0",
"transactionIndex": "0xb",
"type": "0x2",
"v": "0x1",
"value": "0x8ac7230489e80000"
}
}
链上交易体中存在type
字段
跟踪type加入时机
拿web3.py
sdk举例
https://github.com/ethereum/web3.py/blob/810b5904466896143315d68352cdc1add93fea1a/web3/_utils/transactions.py#L42
https://github.com/ethereum/web3.py/blob/810b5904466896143315d68352cdc1add93fea1a/web3/_utils/transactions.py#L108
def fill_transaction_defaults(web3: "Web3", transaction: TxParams) -> TxParams:
"""
if web3 is None, fill as much as possible while offline
"""
defaults = {}
for key, default_getter in TRANSACTION_DEFAULTS.items():
if key not in transaction:
if key == 'gasPrice' and any(_ in transaction for _ in (
'maxFeePerGas', 'maxPriorityFeePerGas'
)): # if EIP-1559 params in transaction, do not set a default gasPrice if missing
continue
if callable(default_getter):
if web3 is not None:
default_val = default_getter(web3, transaction)
else:
raise ValueError("You must specify %s in the transaction" % key)
else:
default_val = default_getter
defaults[key] = default_val
if 'type' not in transaction and any(_ in transaction for _ in (
'maxFeePerGas', 'maxPriorityFeePerGas'
)):
# default transaction type to '2' if 1559 transaction params are present
defaults['type'] = '0x2'
return merge(defaults, transaction)
可以看到当参数中包含maxFeePerGas
, maxPriorityFeePerGas
任一字段时,则认为是['type'] = '0x2'
结论
交易中的type字段由相应的sdk根据参数规则,自动填充