跳至主要内容

legacy

本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

TxTypeLegacyTransaction 代表 kaia 以前存在的一种事务类型。

**这种类型的交易可以创建账户、转移代币、部署智能合约、执行智能合约,或者执行上述交易的混合。

web3py_ext导入extend,将 web3 扩展到 kaia web3。 在这种情况下,如果要使用kaia 专用工具,如Web3.to_peb,则应扩展

eth_accountweb3py_ext 导入必要的实用程序

使用指定的 kairos 测试网 URL 创建 Web3 实例

此外,您还可以将提供商 URL 从 kairos 更改为 quicknode

从私人密钥加载账户

只用 fromtovalue 字段定义 legacy tx。 使用单位转换器 Web3.to_peb

使用 fill_transaction 为交易对象添加更多参数,如 gas 限制...

用用户的私人密钥签署传统 tx

从已签署的 tx 中的签名中恢复签名人地址

您可以使用Account.decode_transaction工具对 RLP 编码的 tx 进行解码,如果您想让输出格式更漂亮,请使用to_pretty工具。

向区块链发送 tx。 它将返回 tx 哈希值,用于等待接收

如果交易已在区块链中完成,wait_for_transaction_receipt 方法会返回交易收据。

最后,得到结果。

txTypeLegacyTransaction.py

from web3py_ext import extend
from web3 import Web3
from eth_account import Account
from web3py_ext.transaction.transaction import (
fill_transaction,
)
from web3py_ext.utils.klaytn_utils import to_pretty
w3 = Web3(Web3.HTTPProvider(
'https://public-en-kairos.node.kaia.io'
))
def web3_legacy_value_transfer_sign_recover():
user = Account.from_key('0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8')
value_transfer_tx = {
'from' : user.address,
'to' : user.address, # to self
'value' : Web3.to_peb(10, "klay"),
}
value_transfer_tx = fill_transaction(value_transfer_tx, w3)
signed_tx = Account.sign_transaction(value_transfer_tx, user.key)
print("\nraw transaction of signed tx:", signed_tx.rawTransaction.hex())
recovered_tx = Account.recover_transaction(signed_tx.rawTransaction)
print("\nrecovered sender address", recovered_tx)
decoded_tx = Account.decode_transaction(signed_tx.rawTransaction)
print("\ndecoded transaction:", to_pretty(decoded_tx))
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print('tx hash: ', tx_hash, 'receipt: ', tx_receipt)
web3_legacy_value_transfer_sign_recover()

output

❯ python txTypeLegacyTransaction.py
raw transaction of signed tx: 0x02f8738203e982036480850ba43b740082f61894a2a8854b1802d8cd5de631e690817c253d6a9153888ac7230489e8000080c080a0bf6b983904416f360b1563a92e162d15cd67efb22593873f63ccee150f4e1547a02fa3f5582c060e94b73b1cafdfa662fca93866484ef0953c06aa99d1aeceb491
recovered sender address 0xA2a8854b1802D8Cd5De631E690817c253d6a9153
decoded transaction: {
"chainId": 1001,
"to": "0xA2a8854b1802D8Cd5De631E690817c253d6a9153",
"value": 10000000000000000000,
"data": "0x",
"accessList": [],
"nonce": 868,
"maxPriorityFeePerGas": 0,
"maxFeePerGas": 50000000000,
"gas": 63000,
"v": 0,
"r": 86581857317945006820941358514738363124805940633281219922143445788962711278919,
"s": 21548393259938899817541519708353317088336325701153952868361768013661633164433,
"type": 2
}
tx hash: b'y\xe4+\xff\x8b\xed\x11D\xd9N\x85\xd4\x0f3\r\x82+\x8d\xc4$3\xaek\xc0\x0b\x81\x97\x8f\xd1\xeb\xb5\xd1' receipt: AttributeDict({'blockHash': HexBytes('0x53b38f1b5a53f67d34ab648581ae7425f1915e49ca3869ae14c56df405791a02'), 'blockNumber': 147332771, 'contractAddress': None, 'cumulativeGasUsed': 557368, 'effectiveGasPrice': 25000000000, 'from': '0xA2a8854b1802D8Cd5De631E690817c253d6a9153', 'gasUsed': 21000, 'logs': [], 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'status': 1, 'to': '0xA2a8854b1802D8Cd5De631E690817c253d6a9153', 'transactionHash': HexBytes('0x79e42bff8bed1144d94e85d40f330d822b8dc42433ae6bc00b81978fd1ebb5d1'), 'transactionIndex': 3, 'type': 2})

让这个页面变得更好