本文へスキップ
このページは英語からの機械翻訳を使用しており、誤りや不明瞭な表現が含まれている可能性があります。最も正確な情報については、オリジナルの英語版をご覧ください。頻繁な更新のため、一部のコンテンツはオリジナルの英語になっている可能性があります。Crowdinでの取り組みに参加して、このページの翻訳改善にご協力ください。 (Crowdin translation page, Contributing guide)

書き込み(手数料の委任)

スマートコントラクトを実行したい場合、feepayerに支払い要求を含むtransactionを行うことができる。

web3をkaia web3に拡張するために、web3py_extからextendをインポートする。

web3eth_accountから必要なutilsをインポートする。

kairosテストネットエンドポイントでWeb3インスタンスを作成する

また、プロバイダのURLをkairosからquicknodeに変更することができます。

各秘密鍵からユーザー料金デリゲーターアカウントをロードする。

アドレスとABIを持つ契約インスタンスを作成する。

デプロイチュートリアルでデプロイしたコントラクトアドレスを設定します(カウンターコントラクト)。 デプロイされたTXの領収書に住所が記載されています。

契約アビを設定する。 remixまたはsolcコンパイラでコンパイル後、ABIを取得できます。

コントラクトのビュー関数を呼び出し、その結果をプリントする。

パラメータとして渡されたフィールドと共にtxインスタンスを返すbuild_transactionでtxを作成する。 料金支払者への料金委任を要求するには、TxType.FEE_DELEGATED_SMART_CONTRACT_EXECUTIONタイプを設定する必要があります。

increment()関数は自動的にデータを呼び出し、それをdataフィールドに設定する。

fill_transactionを使用して、gas limitNonceなどのようなトランザクションの残りのフィールドを追加する。 この行以降のすべてのフィールドを印刷することができる。

ユーザーの秘密鍵でトランザクションに署名する。

fee payerとして、料金委任者の鍵でトランザクションに署名する。 料金支払者の住所も伝える必要がある。

生のトランザクションを送信し、トランザクション・ハッシュを取得する。

トランザクションの受領を待つ

契約がupdateされているかチェックします

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': '0x95Be48607498109030592C08aDC9577c7C2d505', '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})
以降: 295

ページを改善してください。