Nhảy tới nội dung

Write with TxType

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 an account from private key

Create a 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 before it is updated

Make a tx with build_transaction that returns the tx instance along with the additional fields passed as parameter

You should pass TxType.SMART_CONTRACT_EXECUTION type to make a kaia tx contract execution type

Use fill_transaction util 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 tx with the user's private key

Send the tx to the blockchain. It will return the tx hash

Wait the tx receipt from the blockchain and print after received the tx receipt

Call the view function again to see what number the counter have after the tx

contract_interaction_with_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'
))
user = Account.from_key('0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49')
def contract_interaction_with_klaytn_type():
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())
# with smart contract execution type
tx = c.functions.increment().build_transaction({
"type":TxType.SMART_CONTRACT_EXECUTION,
"from":user.address,
})
tx = fill_transaction(tx, w3)
signed_tx = Account.sign_transaction(tx, user.key)
tx_hash = w3.eth.send_raw_transaction(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_klaytn_type()

output

❯ py contract_interaction_with_kaia_type.py
number before: 295
receipt: AttributeDict({'blockHash': HexBytes('0xe12ce22f39c5104ceae275bd326e6aef8378ca4cf750bec87d28f5bc8416c09b'), 'blockNumber': 147325275, 'contractAddress': None, 'cumulativeGasUsed': 497423, 'effectiveGasPrice': 25000000000, 'from': '0x24e8eFD18D65bCb6b3Ba15a4698c0b0d69d13fF7', 'gasUsed': 28014, 'logs': [AttributeDict({'address': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'topics': [HexBytes('0x331bb01bcf77ec721a35a558a7984e8e6ca33b507d3ee1dd13b76f64381e54d4')], 'data': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000128'), 'blockNumber': 147325275, 'transactionHash': HexBytes('0x1eff7f363dc48eb91fe825939097ee8795f4a080339a9173cfdba444de69e61e'), 'transactionIndex': 3, 'blockHash': HexBytes('0xe12ce22f39c5104ceae275bd326e6aef8378ca4cf750bec87d28f5bc8416c09b'), 'logIndex': 9, 'removed': False})], 'logsBloom': HexBytes('0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002'), 'status': 1, 'to': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'transactionHash': HexBytes('0x1eff7f363dc48eb91fe825939097ee8795f4a080339a9173cfdba444de69e61e'), 'transactionIndex': 3, 'type': 0})
number before: 296