價值轉移
TxTypeValueTransfer 用於用戶發送 KAIA。
- 由於 kaia 提供了多種交易類型,使每種交易類型只服務於一個目的,因此 TxTypeValueTransfer 只限於將 KAIA 發送到外部擁有的賬戶(EOA)。
從web3py_ext導入extend,將 web3 擴展為 kaia web3
從 eth_account、web3py_ext 和 cytoolz 中導入必要的實用程序
使用指定的 kairos 測試網 URL 創建 Web3 實例
此外,您還可以將提供商 URL 從 kairos 更改為 quicknode
從私人密鑰加載賬戶
從私人密鑰加載賬戶
創建一個TxType.FEE_DELEGATED_VALUE_TRANSFER類型的空交易。 您可以使用 empty_tx 工具來獲取已填寫默認字段的 tx。
使用 merge 工具,將附加字段(如from、to和value)合併到空 tx 中。
您可以使用 Web3.to_peb 工具來轉換十進制數值。
使用 fill_transaction 為交易對象添加更多參數,如氣體限制...
用用戶的私人密鑰簽署值傳輸 tx
從已簽署的 tx 中的簽名中恢復簽名人地址
用繳費人的私人密鑰簽署價值轉移 tx
從已簽署的 tx 中的簽名中恢復繳費人地址
您可以使用Account.decode_transaction工具對 RLP 編碼的 tx 進行解碼,如果您想讓輸出格式更漂亮,請使用to_pretty工具。
向區塊鏈發送 tx。 它將返回 tx 哈希值,用於等待接收
如果交易已在區塊鏈中完成,wait_for_transaction_receipt 方法會返回交易收據。
txTypeFeeDelegatedValueTransferTransaction.py
from web3py_ext import extendfrom web3 import Web3from eth_account import Accountfrom web3py_ext.transaction.transaction import ( empty_tx, fill_transaction, TxType)from web3py_ext.utils.klaytn_utils import to_prettyfrom cytoolz import mergew3 = Web3(Web3.HTTPProvider('https://public-en-kairos.node.kaia.io'))def web3_fee_delegated_value_transfer_sign_recover(): user = Account.from_key('0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8') fee_delegator = Account.from_key('0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4') fee_delegated_value_transfer_tx = empty_tx(TxType.FEE_DELEGATED_VALUE_TRANSFER) fee_delegated_value_transfer_tx = merge(fee_delegated_value_transfer_tx, { 'from' : user.address, 'to' : user.address, # to self 'value' : Web3.to_peb(0.1, 'klay'), }) fee_delegated_value_transfer_tx = fill_transaction(fee_delegated_value_transfer_tx, w3) # sign the kaia specific transaction type with web3py signed_tx = Account.sign_transaction(fee_delegated_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) feepayer_signed_tx = Account.sign_transaction_as_feepayer(signed_tx.rawTransaction, fee_delegator.address, fee_delegator.key) print("\nraw transaction of signed tx:", feepayer_signed_tx.rawTransaction.hex()) feepayer_recovered_tx = Account.recover_transaction_as_feepayer(feepayer_signed_tx.rawTransaction) print("recovered feepayer address:", feepayer_recovered_tx) decoded_tx = Account.decode_transaction(feepayer_signed_tx.rawTransaction) print("\ndecoded transaction:", to_pretty(decoded_tx)) tx_hash = w3.eth.send_raw_transaction(feepayer_signed_tx.rawTransaction) tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash) print('tx hash: ', tx_hash, 'receipt: ', tx_receipt)web3_fee_delegated_value_transfer_sign_recover()
output
❯ python txTypeFeeDelegatedValueTransferTransaction.pyraw transaction of signed tx: 0x09f8888203a1850ba43b740082f61894a2a8854b1802d8cd5de631e690817c253d6a915388016345785d8a000094a2a8854b1802d8cd5de631e690817c253d6a9153f847f8458207f6a07006976a09e86e21411150cd88e1fec86665e6f41fed2ff66a69ba9071703e60a0147322d15ebaa18232e7347fab53fb776580e8b03901b6f597be727cb927c2a7recovered sender address 0xA2a8854b1802D8Cd5De631E690817c253d6a9153raw transaction of signed tx: 0x09f8e68203a1850ba43b740082f61894a2a8854b1802d8cd5de631e690817c253d6a915388016345785d8a000094a2a8854b1802d8cd5de631e690817c253d6a9153f847f8458207f6a07006976a09e86e21411150cd88e1fec86665e6f41fed2ff66a69ba9071703e60a0147322d15ebaa18232e7347fab53fb776580e8b03901b6f597be727cb927c2a794cb0eb737dfda52756495a5e08a9b37aab3b271daf847f8458207f5a06de7be192af966e8790828854abe89e1d24a435e68c0e7ea6c715c5766d53535a06e0db3c509ac8cc7a7bb8e0fda203e9c27d24720b0dd977ac5ef2a88e2733d74recovered feepayer address: ['0xCb0eb737dfda52756495A5e08A9b37AAB3b271dA']decoded transaction: { "type": 9, "to": "0xA2a8854b1802D8Cd5De631E690817c253d6a9153", "value": 100000000000000000, "nonce": 929, "gasPrice": 50000000000, "gas": 63000, "from": "0xA2a8854b1802D8Cd5De631E690817c253d6a9153", "feePayer": "0xCb0eb737dfda52756495A5e08A9b37AAB3b271dA", "signatures": [ { "v": 2038, "r": 50670685146210644758155850867516770752523795105304292948335149879423302385248, "s": 9249684688095617192077356690924587996686692201721973364886864134587743191719 } ], "feePayerSignatures": [ { "v": 2037, "r": 49711554177870114274420139373315961015019733371259804520392461387625282221365, "s": 49778623080725784202508291278316385863345609698048394072298591254748381527412 } ], "chainId": 1001}tx hash: 0x5234733f874f5c8a77d6628be9958166f039f82a278888c4a6acaab17c2509ab receipt: AttributeDict({'blockHash': HexBytes('0x2704d8b059545722008a338a1678d1dced73e8a96fb456e7b4cde051b9b9bbc0'), 'blockNumber': 148222496, 'contractAddress': None, 'cumulativeGasUsed': 31000, 'effectiveGasPrice': 25000000000, 'from': '0xA2a8854b1802D8Cd5De631E690817c253d6a9153', 'gasUsed': 31000, 'logs': [], 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'status': 1, 'to': '0xA2a8854b1802D8Cd5De631E690817c253d6a9153', 'transactionHash': HexBytes('0x5234733f874f5c8a77d6628be9958166f039f82a278888c4a6acaab17c2509ab'), 'transactionIndex': 0, 'type': 0})