본문으로 건너뛰기
이 페이지는 영어로 된 기계 번역을 사용하므로 오류나 불명확한 언어가 포함될 수 있습니다. 가장 정확한 정보는 영어 원문을 참조하시기 바랍니다. 잦은 업데이트로 인해 일부 콘텐츠는 원래 영어로 되어 있을 수 있습니다. Crowdin에서 이 페이지의 번역을 개선하는 데 동참하여 도움을 주세요. (Crowdin translation page, Contributing guide)

Read

You can call functions in Smart Contract via "Call" RPC API. What you can call here is limited to view functions that is not changing any states in the Contract.

Import extend from web3py_ext to extend web3 to kaia web3

Create a Web3 instance with the specified kairos testnet URL

Also, you can change the provider URL from kairos to quicknode.

Create a contract instance with contract 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. It is just a RPC call to the blockchain node, not transaction

smart_contract_interaction.py

from web3py_ext import extend
from web3 import Web3
w3 = Web3(Web3.HTTPProvider(
'https://public-en-kairos.node.kaia.io'
))
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 : ', c.functions.number().call())
contract_interaction()

output

❯ py smart_contract_interaction.py
number : 294

페이지를 개선해 주세요