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

使用 TxType 写入

web3py_ext导入extend,将 web3 扩展为 kaia web3

web3eth_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 extend
from web3 import Web3
from eth_account import Account
from 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.py
number before: 295
receipt: 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

让这个页面变得更好