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

キャンセル

TxTypeCancelは、トランザクションプール内の同じnonceを持つトランザク ションの実行をキャンセルする。 このトランザクション・タイプは、送信されたトランザクションが一定時間未処理のように見える場合に有用である。

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

eth_accountweb3py_ext、*cytoolz**から必要なutilsをインポートする。

指定されたkairosテストネットURLでWeb3インスタンスを作成する

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

秘密鍵からアカウントを読み込む

値転送TXを送信する。 それはtxプールにあり、実行前にキャンセルされる。 キャンセルTXは、すでに実行されていれば失敗する。

TxType.CANCEL型の空のトランザクションを作成する。 empty_txユーティリティを使えば、デフォルトのフィールドが埋められたtxを得ることができる。

fromフィールドとnonceフィールドを、mergeユーティリティを使用して空のtxにマージする。

nonceを、以前に送信された値転送tx同じにする。 キャンセルしたいのと同じtx nonce**を設定する必要がある。

fill_transactionを使用して、トランザクション・オブジェクトにガス・リミットなどのパラメータを追加する。

ユーザーの秘密鍵でcancel txに署名する。

署名されたTxの署名から署名者のアドレスを復元する。

RLPエンコードされたtxはAccount.decode_transactionユーティリティでデコードできます。

ブロックチェーンにtxを送信する。 このハッシュは受信を待つために使用される。

wait_for_transaction_receiptメソッドは、ブロックチェーンで完了した場合、txレシートを返す。

最後に結果を出す。

cancel_example.py

from web3py_ext import extend
from web3 import Web3
from eth_account import Account
from web3py_ext.transaction.transaction import (
empty_tx,
fill_transaction,
TxType
)
from web3py_ext.utils.klaytn_utils import to_pretty
from cytoolz import merge
w3 = Web3(Web3.HTTPProvider(
'https://public-en-kairos.node.kaia.io'
))
def web3_cancel_sign_recover():
user1 = Account.from_key('0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8')
value_transfer_tx = empty_tx(TxType.VALUE_TRANSFER)
value_transfer_tx = merge(value_transfer_tx, {
'from':user1.address,
'to':user1.address,
'value':Web3.to_peb(10, "klay")
})
value_transfer_tx = fill_transaction(value_transfer_tx, w3)
signed_tx = Account.sign_transaction(value_transfer_tx, user1.key)
w3.eth.send_raw_transaction(signed_tx.rawTransaction)
cancel_tx = empty_tx(TxType.CANCEL)
cancel_tx = merge(cancel_tx, {
'from' : user1.address,
'nonce' : value_transfer_tx['nonce']
})
cancel_tx = fill_transaction(cancel_tx, w3)
# sign the kaia specific transaction type with web3py
signed_tx = Account.sign_transaction(cancel_tx, user1.key)
print("\nraw transaction of signed tx:", signed_tx.rawTransaction.hex())
recovered_tx = Account.recover_transaction(signed_tx.rawTransaction)
print("\nrecovered sender address", recovered_tx)
decoded_tx = Account.decode_transaction(signed_tx.rawTransaction)
print("\ndecoded transaction:", to_pretty(decoded_tx))
tx_hash = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print('tx hash: ', tx_hash, 'receipt: ', tx_receipt)
web3_cancel_sign_recover()

output

❯ cancel_example.py
signed tx の raw トランザクション:0x38f86b82036f850ba43b740083026d1894a2a8854b1802d8cd5de631e690817c253d6a9153f847f8458207f5a0a559852e977476bfa6ba263a9c1f1f4b60608b82752d73a3f1162e341e7b5f06a07f12a242ad1cdbc08966e62af4c8749fc438e058b1344a4f11ba206f207ddc84
recovered sender address 0xA2a8854b1802D8Cd5De631E690817c253d6a9153
decoded transaction:{
"type":56,
"nonce":879,
"gasPrice":50000000000,
"gas":159000,
"from":"0xA2a8854b1802D8Cd5De631E690817c253d6a9153",
"signatures":[
{
"v":2037,
"r":74789788593375966616662801192824717129914792416281096168085546212656613646086,
"s":57476654897736400463186412232435765147976539862827431359170424955204316028036
}.
],
"chainId":1001
}
tx ハッシュ: 0x3b1278d3748b638a41ffc40ed4cffc6c789ceffbd924f9b00ae7b53f26815686 receipt: AttributeDict({'blockHash':HexBytes('0x672deecd07b84c2b524f160fccc6ce0d20486bdde914a7c7065e2de0ed723635'), 'blockNumber':147364137, 'contractAddress':None, 'cumulativeGasUsed':349674, 'effectiveGasPrice': 25000000000, 'from': '0xA2a8854b1802D8Cd5De631E690817c253d6a9153', 'gasUsed': 21000, 'logs': [], 'logsBloom':HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'),'status':1, 'to': '0xA2a8854b1802D8Cd5De631E690817c253d6a9153', 'transactionHash':HexBytes('0x3b1278d3748b638a41ffc40ed4cffc6c789ceffbd924f9b00ae7b53f26815686'), 'transactionIndex':3, 'type': 0})

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