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

写入

当您需要更新合约中的某些状态时,可以与智能合约进行**"事务 "**交互。

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

导入必要的实用程序

从私人密钥加载账户

使用指定的 kairos 测试网 URL 创建 Web3 实例

此外,您还可以将提供商 URL 从 kairos 更改为 quicknode

账户列表定义为使用中间件的列表类型

如果想在使用 transact 函数时自动签名和发送,请使用 construct_sign_and_send_raw_middleware

创建一个合约实例,并标明其地址和 ABI

设置在部署教程中部署的合约地址计数器合约)。 您可以在部署 tx 收据中看到地址

设置合约 abi。 使用 remix 或 solc 编译器编译后,可以获得 ABI

调用合约的视图函数,并在更新之前打印结果

发送一份文件,以更新合约的状态。 这将自动签署并发送包含数据的 tx,调用增量函数**

等待来自区块链的发送收据,并在收到后打印**收据

再次调用视图函数,查看 tx 之后计数器的数字

smart_contract_write.py

from web3py_ext import extend
from web3 import Web3
from eth_account import Account
from web3.middleware import construct_sign_and_send_raw_middleware
user = Account.from_key('0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49')
w3 = Web3(Web3.HTTPProvider(
'https://public-en-kairos.node.kaia.io'
))
acc_list = [user]
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acc_list))
def contract_interaction():
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"}]
)
# call view function
print('\nnumber before: ', c.functions.number().call())
tx_hash = c.functions.increment().transact({
'from':user.address
})
print('receipt: ', w3.eth.wait_for_transaction_receipt(tx_hash))
print('\nnumber after: ', c.functions.number().call())
contract_interaction()

output

❯ py smart_contract_write.py
number before: 293
receipt: AttributeDict({'blockHash': HexBytes('0x1025a3b580e904459af4a7a03adf8322cef2cd2e6e412abf04f6eee69f9ae877'), 'blockNumber': 147329610, 'contractAddress': None, 'cumulativeGasUsed': 361377, 'effectiveGasPrice': 25000000000, 'from': '0x24e8eFD18D65bCb6b3Ba15a4698c0b0d69d13fF7', 'gasUsed': 28014, 'logs': [AttributeDict({'address': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'topics': [HexBytes('0x331bb01bcf77ec721a35a558a7984e8e6ca33b507d3ee1dd13b76f64381e54d4')], 'data': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000126'), 'blockNumber': 147329610, 'transactionHash': HexBytes('0xc04fd483bc3efe7ce61e65ded491eccb5f5401267f8e95bdae2bcc3356aab561'), 'transactionIndex': 2, 'blockHash': HexBytes('0x1025a3b580e904459af4a7a03adf8322cef2cd2e6e412abf04f6eee69f9ae877'), 'logIndex': 6, 'removed': False})], 'logsBloom': HexBytes('0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002'), 'status': 1, 'to': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'transactionHash': HexBytes('0xc04fd483bc3efe7ce61e65ded491eccb5f5401267f8e95bdae2bcc3356aab561'), 'transactionIndex': 2, 'type': 2})
number after: 294

让这个页面变得更好