Nhảy tới nội dung

Cancel

TxTypeCancel cancels the execution of the transaction with the same nonce in the transaction pool. This transaction type is useful when a submitted transaction seems unprocessed for a certain amount of time.

Import extend from web3py_ext to extend web3 to kaia web3

Import necessary utils from eth_account, web3py_ext and cytoolz

Create a Web3 instance with the specified kaia Baobab testnet URL

Also, you can change the provider URL from baobab to allthatnode

Load an account from private key

Load an fee payer account from private key to sign and send the transaction instead of above account

Create an empty transaction of type TxType.FEE_DELEGATED_CANCEL. You can use empty_tx util to get a tx with default fields filled.

Merge the from and nonce fields into the empty tx by using the merge util.

Use fill_transaction to add more params to transaction object like gas limit...

Sign the cancel tx by user's private key

Recover the signer's address from the signature in signed tx

You can decode the RLP-encoded tx by the Account.decode_transaction util and if you want to make the output format pretty, use the to_pretty util

Sign the raw transaction with the fee delegator's account

Recover the fee payer's address from the signed transaction

You can decode the RLP-encoded tx by the Account.decode_transaction util and if you want to make the output format pretty, use the to_pretty util

web3_fee_delegated_cancel_sign_recover.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,
TX_TYPE_FEE_DELEGATED_CANCEL
)
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_fee_delegated_cancel_sign_recover():
user = Account.from_key('0x0e4ca6d38096ad99324de0dde108587e5d7c600165ae4cd6c2462c597458c2b8')
fee_delegator = Account.from_key('0x9435261ed483b6efa3886d6ad9f64c12078a0e28d8d80715c773e16fc000cff4')
cancel_tx = empty_tx(TX_TYPE_FEE_DELEGATED_CANCEL)
cancel_tx = merge(cancel_tx, {
'from' : user.address,
})
cancel_tx = fill_transaction(cancel_tx, w3)
# sign the kaia specific transaction type with web3py
signed_tx = Account.sign_transaction(cancel_tx, user.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))
feepayer_signed_tx = Account.sign_transaction_as_feepayer(signed_tx.rawTransaction, fee_delegator.address, fee_delegator.key)
print("\nraw transaction of feePayer signed tx:", feepayer_signed_tx.rawTransaction.hex())
feepayer_recovered_tx = Account.recover_transaction_as_feepayer(feepayer_signed_tx.rawTransaction)
print("recovered feepayer address:", feepayer_recovered_tx)
decoded_tx = Account.decode_transaction(feepayer_signed_tx.rawTransaction)
print("\ndecoded transaction:", to_pretty(decoded_tx))
web3_fee_delegated_cancel_sign_recover()

output

❯ py web3_fee_delegated_cancel_sign_recover.py
# raw transaction of signed tx: 0x39f86b8203a3850ba43b740083026d1894a2a8854b1802d8cd5de631e690817c253d6a9153f847f8458207f5a00eec532ebe17fade46561d3ff503c97e07d8d9ba611b6a7f8a1cc11621dfc03fa006e3ee9cf4f5bfd6674d656c571cfb80b0bfd99bebe87f017e288be7ea4fec21
# recovered sender address 0xA2a8854b1802D8Cd5De631E690817c253d6a9153
# decoded transaction: {
# "type": 57,
# "nonce": 931,
# "gasPrice": 50000000000,
# "gas": 159000,
# "from": "0xA2a8854b1802D8Cd5De631E690817c253d6a9153",
# "signatures": [
# {
# "v": 2037,
# "r": 6749929892576958642714739615979538685230067780127794094925331144130783330367,
# "s": 3116598222383802394999638931088885558560578183897443089888519072063383661601
# }
# ],
# "chainId": 1001
# }
# raw transaction of feePayer signed tx: 0x39f8c98203a3850ba43b740083026d1894a2a8854b1802d8cd5de631e690817c253d6a9153f847f8458207f5a00eec532ebe17fade46561d3ff503c97e07d8d9ba611b6a7f8a1cc11621dfc03fa006e3ee9cf4f5bfd6674d656c571cfb80b0bfd99bebe87f017e288be7ea4fec2194cb0eb737dfda52756495a5e08a9b37aab3b271daf847f8458207f6a06697d97df68a0c4a2c59b3d7a8c5c479a6b43e7a34394518b858ef908d10b76aa0492c8da32c77a71ce26650f9ac842496bb33f0f17877ce1b23e1763b7c87e28d
# recovered feepayer address: ['0xCb0eb737dfda52756495A5e08A9b37AAB3b271dA']
# decoded transaction: {
# "type": 57,
# "nonce": 931,
# "gasPrice": 50000000000,
# "gas": 159000,
# "from": "0xA2a8854b1802D8Cd5De631E690817c253d6a9153",
# "feePayer": "0xCb0eb737dfda52756495A5e08A9b37AAB3b271dA",
# "signatures": [
# {
# "v": 2037,
# "r": 6749929892576958642714739615979538685230067780127794094925331144130783330367,
# "s": 3116598222383802394999638931088885558560578183897443089888519072063383661601
# }
# ],
# "feePayerSignatures": [
# {
# "v": 2038,
# "r": 46404205537188908000560167654303187399733564148219565625489961916532872230762,
# "s": 33097556762817862610763009383178378013466927896867683158585566455859245474445
# }
# ],
# "chainId": 1001
# }