跳至主要內容
本頁面使用機器翻譯自英語,可能包含錯誤或不清楚的語言。如需最準確的信息,請參閱英文原文。由於更新頻繁,部分內容可能與英文原文有出入。請加入我們在 Crowdin 上的努力,幫助我們改進本頁面的翻譯。 (Crowdin translation page, Contributing guide)

智能合約執行

TxTypeSmartContractExecution 使用給定數據執行智能合約。 只有當 "to "是智能合約賬戶時,才接受TxTypeSmartContractExecution

web3py_ext導入extend,將 web3 擴展為 kaia web3

eth_accountweb3py_extcytoolz 中導入必要的實用程序

使用指定的 kairos 測試網 URL 創建 Web3 實例

此外,您還可以將提供商 URL 從 kairos 更改為 quicknode

從私人密鑰加載 account

創建一個TxType.SMART_CONTRACT_EXECUTION類型的**空事務。 您可以使用 **empty_tx** 工具來獲取已填寫默認字段的 tx。

使用 merge 工具將附加字段(如fromtodata)合併到空 tx 中。

字段中設置要執行的 contract address

在本教程中,data to call a functionencodedbytes 流。 請參閱智能合約部分編寫教程,以製作編碼數據

使用 fill_transaction 為交易對象添加更多參數,如 gas 限制...

用用戶的私人密鑰簽署值傳輸 tx

從已簽署的 tx 中的簽名中恢復簽名人地址

您可以使用Account.decode_transaction工具對 RLP 編碼的 tx 進行解碼,如果您想讓輸出格式更漂亮,請使用to_pretty工具。

tx發送到區塊鏈。 它將返回 tx 哈希值,用於等待接收

如果交易已在區塊鏈中完成,wait_for_transaction_receipt 方法會返回交易收據。

最後,得到結果。

smart_contract_execution_sign_recover.py

from web3py_ext import extend
from web3 import Web3
from eth_account import Account
from web3py_ext.transaction.transaction import (
empty_tx,
fill_transaction,
TxType
)
from web3py_ext.utils.klaytn_utils import to_pretty
from cytoolz import merge
w3 = Web3(Web3.HTTPProvider(
'https://public-en-kairos.node.kaia.io'
))
def web3_smart_contract_execution_sign_recover():
user = Account.from_key('0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8')
smart_contract_execution_tx = empty_tx(TxType.SMART_CONTRACT_EXECUTION)
smart_contract_execution_tx = merge(smart_contract_execution_tx, {
'from' : user.address,
'to' : '0x108bF12b50c9ef65525F0495C721aEc55015e111', # already deployed contract for test before
'data' : '0x3d7403a30000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c48656c6c6f204b6c6179746e0000000000000000000000000000000000000000',
})
smart_contract_execution_tx = fill_transaction(smart_contract_execution_tx, w3)
# sign the kaia specific transaction type with web3py
signed_tx = Account.sign_transaction(smart_contract_execution_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_smart_contract_execution_sign_recover()

output

❯ py smart_contract_execution_sign_recover.py
raw transaction of signed tx: 0x30f8e7820368850ba43b740083023eda94108bf12b50c9ef65525f0495c721aec55015e1118094a2a8854b1802d8cd5de631e690817c253d6a9153b8643d7403a30000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c48656c6c6f204b6c6179746e0000000000000000000000000000000000000000f847f8458207f5a076bb764f64faba013dd74d29c5d446324fc6ea3acfff334b2344c8270402ab86a0287e81224d8a2156894ee38fd2b03a4ce3555755341a4b7a5c304f85e5fc31ff
recovered sender address 0xA2a8854b1802D8Cd5De631E690817c253d6a9153
decoded transaction: {
"type": 48,
"value": 0,
"nonce": 872,
"gasPrice": 50000000000,
"gas": 147162,
"to": "0x108bF12b50c9ef65525F0495C721aEc55015e111",
"from": "0xA2a8854b1802D8Cd5De631E690817c253d6a9153",
"data": "0x3d7403a30000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000c48656c6c6f204b6c6179746e0000000000000000000000000000000000000000",
"signatures": [
{
"v": 2037,
"r": 53704133080478071789069753996858108922624268560525338682079935754802299054982,
"s": 18316027923575524564170275051333963542245959777309578523749620923746446225919
}
],
"chainId": 1001
}
tx hash: 0xde228b907506204482ec8e82a7409c3ab887b4c0e586d8d85287ef46c3549b3e receipt: AttributeDict({'blockHash': HexBytes('0xeaa9de93191210426570cc0078448e104f1cf839d0833eae91382e7650f59ffd'), 'blockNumber': 147348464, 'contractAddress': None, 'cumulativeGasUsed': 39454, 'effectiveGasPrice': 25000000000, 'from': '0xA2a8854b1802D8Cd5De631E690817c253d6a9153', 'gasUsed': 39454, 'logs': [], 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'), 'status': 1, 'to': '0x108bF12b50c9ef65525F0495C721aEc55015e111', 'transactionHash': HexBytes('0xde228b907506204482ec8e82a7409c3ab887b4c0e586d8d85287ef46c3549b3e'), 'transactionIndex': 0, 'type': 0})

讓這個頁面變得更好