Skip to main content

Write

You can make a "transaction" interacting with a Smart Contract when you **need to update some state ** in the contract.

Import extend from web3py_ext to extend web3 to kaia web3

Import necessary utils

Load an account from private key

Create a Web3 instance with the specified kaia Baobab testnet URL

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

Define an account list as a list type to use middleware

If you want to automatically sign and send when using transact function, use construct_sign_and_send_raw_middleware

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

Send a tx to update the contract state. This automatically sign and send the tx that includes the data calling the increment function

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

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

smart_contract_write.py

from web3py_ext import extend
from web3 import Web3
from eth_account import Account
from web3.middleware import construct_sign_and_send_raw_middleware
user = Account.from_key('0x4a72b3d09c3d5e28e8652e0111f9c4ce252e8299aad95bb219a38eb0a3f4da49')
w3 = Web3(Web3.HTTPProvider(
'https://public-en-kairos.node.kaia.io'
))
acc_list = [user]
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acc_list))
def contract_interaction():
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"}]
)
# call view function
print('\nnumber before: ', c.functions.number().call())
tx_hash = c.functions.increment().transact({
'from':user.address
})
print('receipt: ', w3.eth.wait_for_transaction_receipt(tx_hash))
print('\nnumber after: ', c.functions.number().call())
contract_interaction()

output

❯ py smart_contract_write.py
number before: 293
receipt: AttributeDict({'blockHash': HexBytes('0x1025a3b580e904459af4a7a03adf8322cef2cd2e6e412abf04f6eee69f9ae877'), 'blockNumber': 147329610, 'contractAddress': None, 'cumulativeGasUsed': 361377, 'effectiveGasPrice': 25000000000, 'from': '0x24e8eFD18D65bCb6b3Ba15a4698c0b0d69d13fF7', 'gasUsed': 28014, 'logs': [AttributeDict({'address': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'topics': [HexBytes('0x331bb01bcf77ec721a35a558a7984e8e6ca33b507d3ee1dd13b76f64381e54d4')], 'data': HexBytes('0x0000000000000000000000000000000000000000000000000000000000000126'), 'blockNumber': 147329610, 'transactionHash': HexBytes('0xc04fd483bc3efe7ce61e65ded491eccb5f5401267f8e95bdae2bcc3356aab561'), 'transactionIndex': 2, 'blockHash': HexBytes('0x1025a3b580e904459af4a7a03adf8322cef2cd2e6e412abf04f6eee69f9ae877'), 'logIndex': 6, 'removed': False})], 'logsBloom': HexBytes('0x00000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000001200000002'), 'status': 1, 'to': '0x95Be48607498109030592C08aDC9577c7C2dD505', 'transactionHash': HexBytes('0xc04fd483bc3efe7ce61e65ded491eccb5f5401267f8e95bdae2bcc3356aab561'), 'transactionIndex': 2, 'type': 2})
number after: 294