Nhảy tới nội dung

legacy

This page uses machine translation from English, which may contain errors or unclear language. For the most accurate information, please see the original English version. Some content may be in the original English due to frequent updates. Help us improve this page's translation by joining our effort on Crowdin. (Crowdin translation page, Contributing guide)

TxTypeLegacyTransaction represents a type of transactions existed previously in kaia.

**This type of transaction can create an account, transfer tokens, deploy a smart contract, execute a smart contract, or perform a mix of aforementioned.

Import extend from web3py_ext to extend web3 to kaia web3. In this case if you want to use kaia specific util like Web3.to_peb, you should extend

Import necessary utils from eth_account, web3py_ext

Create a Web3 instance with the specified kairos testnet URL

Also, you can change the provider URL from kairos to quicknode

Load an account from private key

Define the legacy tx with only from, to, value fields. Use unit converter Web3.to_peb

Use fill_transaction to add more params to transaction object like gas limit...

Sign the legacy tx by user's private key

Recover the signer's address from the signature in signed tx

You can decode the RLP-encoded tx by the Account.decode_transaction util and if you want to make the output format pretty, use the to_pretty util

Send the tx to the blockchain. It will return the tx hash, which will be used to wait the receipt

The wait_for_transaction_receipt method returns the tx receipt if it is completed in the blockchain

Finally, get the result.

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})

Cải thiện trang này