Interact with Smart Contract
Now that your contract is deployed on the blockchain, the next step is to interact with it. Interacting with a smart contract involves calling its functions to read or modify its state. In this guide you will learn how to interact with your contract using Remix, JavaScript, and Python.
Prerequisites
- Successful smart contract deployment.
- The ABI and contract address from the previous deployment are recorded. Generated after compilation and deployment.
- Funded wallet connected to the appropriate network (peaq/agung).
Instructions
Interacting Using Remix
1. Access the Deployed Contract
- In Remix, navigate to the Deployed Contracts section in the Deploy & Run Transactions sidebar.
- You’ll see your deployed contract listed, with an interactive UI displaying the contract’s functions.
2. Perform Write Operations
- In the Deployed Contracts interface, locate the set function.
- Enter an integer as an input field (e.g.
10
) and click the set button. - A MetaMask popup will be triggered to confirm the transaction. Confirm the transaction and wait for it to be appended on-chain.
- After completion the new value will be stored on the blockchain.
3. Perform Read Operations
- Locate the get function in the Deployed Contracts drop down in the Deploy & Run Transactions tab.
- Click the get button to fetch the current value stored in the contract.
- The result will appear below the function, showing the updated value (e.g.,
10
).
Interacting with the Contract Using JavaScript
1. Create an Interaction Script
- Inside the
scripts/
directory of your project, create a new file namedinteract.js
- Add the following script to interact with your deployed contract:
2. Execute the Interaction Script
- In the root directory of your program run the cmd to execute the code written above. The code will deploy the contract on the agung network as defined in the
hardhat.config.js
file.
🎉 Congratulations! You have successfully written, deployed, and interacted with a smart contract! Now let’s interact with this same contract, but now using another language - Python.
Interacting with the Contract Using Python
1. Install Dependencies
web3
- Allows the interaction with EVM deployed contracts in Python.python-dotenv
- Provides basic protection for secret variables.
2. Create Python File
- Inside the
scripts/
directory of your project, create a new file namedinteract.py
- Add the following script to interact with your deployed contract:
3. Execute the Interaction Script
- To execute the code above you can use the following script:
After doing so, the code inside the Python file interacts with the SimpleStorage contract you made earlier! Now you understand how to get a smart contract on chain and interact with it. However, there are ways to create upgradeable smart contracts which are talked about in the next section.