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

撰写(费用委托)

当您要执行智能合约时,您可以向 feepayer 发送**"交易 ",其中包括支付请求**。

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.FEE_DELEGATED_SMART_CONTRACT_EXECUTION类型,以便向费用支付方申请费用委托。

increment() 会自动让函数调用数据并将其设置到 data 字段中

使用 fill_transaction 添加交易的其余字段,如气量限制Nonce等。 如果您想查看以下内容,可以打印这一行之后的所有字段

用户的私钥签署交易

使用收费委托人的密钥,以收费人的身份签署交易。 您还需要提供缴费人的地址。

发送原始交易并获取交易哈希值

等待交易收据

检查合约是否已更新状态

contract_interaction_with_fee_delegation_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'
))
def contract_interaction_with_fee_delegation_klaytn_type():
user = Account.from_key('0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49')
fee_delegator = Account.from_key('0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4')
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())
# sender sign with fee delegated smart contract execution type
tx = c.functions.increment().build_transaction({
"type":TxType.FEE_DELEGATED_SMART_CONTRACT_EXECUTION,
"from":user.address,
})
tx = fill_transaction(tx, w3)
user_signed_tx = Account.sign_transaction(tx, user.key)
# feePayer sign
feepayer_signed_tx = Account.sign_transaction_as_feepayer(
user_signed_tx.rawTransaction,
fee_delegator.address,
fee_delegator.key
)
tx_hash = w3.eth.send_raw_transaction(feepayer_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_fee_delegation_klaytn_type()

output

❯ py contract_interaction_with_fee_delegation_kaia_type.py
number before: 294
receipt: AttributeDict({'blockHash': HexBytes('0xa1ecb35a068736c6257915f8a89dfeec30cace985dc244a1b82c887fd9360f3a'), 'blockNumber': 147174601, 'contractAddress': None, 'cumulativeGasUsed': 377941, 'effectiveGasPrice': 25000000000, 'from': '0x24e8eFD18D65bCb6b3Ba15a4698c0b0d69d13fF7', 'gasUsed': 38014, 'logs': [AttributeDict({'address': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'topics': [HexBytes('0x331bb01bcf77ec721a35a558a7984e8e6ca33b507d3ee1dd13b76f64381e54d4')], 'data': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000127'), 'blockNumber': 147174601, 'transactionHash': HexBytes('0xded6817cb1c5112a2a4c8aebd0c74a56bfcdde10c393261b7e8212db67958743'), 'transactionIndex': 2, 'blockHash': HexBytes('0xa1ecb35a068736c6257915f8a89dfeec30cace985dc244a1b82c887fd9360f3a'), 'logIndex': 6, 'removed': False})], 'logsBloom': HexBytes('0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002'), 'status': 1, 'to': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'transactionHash': HexBytes('0xded6817cb1c5112a2a4c8aebd0c74a56bfcdde10c393261b7e8212db67958743'), 'transactionIndex': 2, 'type': 0})
number after: 295

让这个页面变得更好