寫入 (SmartContractExecution TxType)
從web3py_ext導入extend,將 web3 擴展為 kaia web3
從 web3 和 eth_account 導入必要的實用程序
使用 kairos testnet 端點創建 Web3 實例
此外,您還可以將提供商 URL 從 kairos 更改為 quicknode
從私人密鑰加載賬戶
創建一個合約實例,並標明其地址和 ABI
設置在部署教程中部署的合約地址(計數器合約)。 您可以在部署 tx 收據中看到地址
設置合約 abi。 使用 remix 或 solc 編譯器編譯後,可以獲得 ABI
調用合約的視圖函數,並在更新之前打印結果
使用 build_transaction創建一個 tx,返回 tx 實例和作為參數傳遞的附加字段
您應通過 TxType.SMART_CONTRACT_EXECUTION 類型來創建 kaia tx 合約執行類型。
使用 fill_transaction 工具添加交易的其餘字段,如氣量限制、Nonce等。 如果您想查看以下內容,可以打印這一行之後的所有字段
用用戶的私人密鑰簽署 tx
向區塊鏈發送 tx。 它將返回 tx 哈希值
等待來自區塊鏈的發送回執,並在收到發送回執後打印**。
再次調用視圖函數,查看 tx 之後計數器的數字
contract_interaction_with_kaia_type.py
from web3py_ext import extendfrom web3 import Web3from eth_account import Accountfrom web3py_ext.transaction.transaction import ( fill_transaction, TxType)w3 = Web3(Web3.HTTPProvider( 'https://public-en-kairos.node.kaia.io' ))user = Account.from_key('0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49')def contract_interaction_with_klaytn_type(): c = w3.eth.contract( address="0x95Be48607498109030592C08aDC9577c7C2dD505", abi = [{"inputs":[{"internalType":"uint256","name":"initNumber","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":False,"inputs":[{"indexed":False,"internalType":"uint256","name":"number","type":"uint256"}],"name":"SetNumber","type":"event"},{"inputs":[],"name":"increment","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"number","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"newNumber","type":"uint256"}],"name":"setNumber","outputs":[],"stateMutability":"nonpayable","type":"function"}] ) # view before write transaction print('\nnumber before: ', c.functions.number().call()) # with smart contract execution type tx = c.functions.increment().build_transaction({ "type":TxType.SMART_CONTRACT_EXECUTION, "from":user.address, }) tx = fill_transaction(tx, w3) signed_tx = Account.sign_transaction(tx, user.key) tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction) print("receipt: ", w3.eth.wait_for_transaction_receipt(tx_hash)) # view after write transaction print('\nnumber after: ', c.functions.number().call())contract_interaction_with_klaytn_type()
output
❯ py contract_interaction_with_kaia_type.pynumber before: 295receipt: AttributeDict({'blockHash': HexBytes('0xe12ce22f39c5104ceae275bd326e6aef8378ca4cf750bec87d28f5bc8416c09b'), 'blockNumber': 147325275, 'contractAddress': None, 'cumulativeGasUsed': 497423, 'effectiveGasPrice': 25000000000, 'from': '0x24e8eFD18D65bCb6b3Ba15a4698c0b0d69d13fF7', 'gasUsed': 28014, 'logs': [AttributeDict({'address': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'topics': [HexBytes('0x331bb01bcf77ec721a35a558a7984e8e6ca33b507d3ee1dd13b76f64381e54d4')], 'data': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000128'), 'blockNumber': 147325275, 'transactionHash': HexBytes('0x1eff7f363dc48eb91fe825939097ee8795f4a080339a9173cfdba444de69e61e'), 'transactionIndex': 3, 'blockHash': HexBytes('0xe12ce22f39c5104ceae275bd326e6aef8378ca4cf750bec87d28f5bc8416c09b'), 'logIndex': 9, 'removed': False})], 'logsBloom': HexBytes('0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002'), 'status': 1, 'to': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'transactionHash': HexBytes('0x1eff7f363dc48eb91fe825939097ee8795f4a080339a9173cfdba444de69e61e'), 'transactionIndex': 3, 'type': 0})number before: 296