Skip to main content

Write (Fee Delegation)

You can make a "transaction" that including payment request to feepayer when you want to execute the Smart Contract.

Import extend from web3py_ext to extend web3 to kaia web3

Import necessary utils from web3 and eth_account

Create a Web3 instance with the kaia Baobab testnet endpoint

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

Load the user and fee delegator account from each private key

Create the contract instance with its address and ABI

Set the contract address that you deployed in the deploy tutorial (Counter contract). You can see the address in your deploy tx receipt

Set the contract abi. You can get the ABI after compiled in remix or solc compiler

Call the view function of the contract and print the result

Make a tx with build_transaction that returns the tx instance along with passed fields as parameter. You should set the TxType.FEE_DELEGATED_SMART_CONTRACT_EXECUTION type to request fee delegation to fee payer

increment() automatically makes the function call data and set it to the data field

Use fill_transaction to add rest of the fields of the transaction like gas limit, Nonce, etc. You can print all the fields after this line if you want to see

Sign the transaction with the user's private key

Sign the transaction as fee payer with the fee delegator's key. You need to pass the fee payer's address as well.

Send the raw transaction and get the transaction hash

Wait for the transaction receipt

Check if contract has updated the state

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