# Survey of Blockchain-Based Applications for IoT
## 7 Smart Contracts and Automation  
### Smart Contracts  
Smart contracts are a fundamental aspect of blockchain technology that enables the creation of decentralized applications (dApps) that operate independently of any single authority. DApps are applications built on blockchain networks that provide transparency, security, and decentralization, thereby allowing users to interact directly without intermediaries. Smart contracts are crucial for functioning decentralized autonomous organizations (DAOs) and Decentralized Finance (DeFi). DAOs are member-governed organizations that operate based on smart contract rules, whereas DeFi refers to blockchain-based financial systems that offer services, such as lending, trading, and payments, without traditional financial institutions [50]. Smart contracts can be categorized into three main types [51]:  
1. Primary smart contracts: These operate within a single blockchain and are used for automation, transactions, staking, and token management;
2. Oracle smart contracts: These facilitate the integration of off-chain data, allowing blockchain systems to interact with external information sources;
3. Bridge smart contracts: These enable cross-chain communication, making it possible to exchange assets and data seamlessly between different blockchains. [PERSON] et al. proposed the utilization of AI-driven classification to sort smart contracts into these three distinct categories [51]. This approach uses machine learning models  
Figure 9: Verifiable Credentials framework. to analyze opcode patterns, thereby enabling automated classification and enhancing security within blockchain ecosystems. The following sections of this survey will offer a more detailed discussion on the concepts of oracles and multi-layer blockchains. By using smart contracts, devices can automatically engage based on predefined criteria, thereby eliminating the need for manual actions or third parties. This streamlines operations and reduces costs [52]. Figure 10 below shows a simple smart contract that monitors the water level in a tank and closes the valve when the water level is 90% or higher. The logic of this contract is as follows:  
1. Water level sensor: A device that measures the current water level in the tank and provides real-time data;
2. If Water-Level is greater than or equal to 90% and the Valve-Status is \"open,\" the smart contract executes a command to close the valve (valve status = closed). Smart contracts can achieve more complex automation tasks and collect inputs from multiple sources to trigger actions or contracts based on user requirements. In addition to automating tasks, smart contracts can be set up to trigger authentication by enforcing predefined access rules and verifying identity attributes on the blockchain. These contracts serve as decentralized gatekeepers, permitting only authorized entities to perform specific actions [53]. Storing credentials in a blockchain, even when hashed or encrypted, may raise security concerns and complicate password modification. This is why the concept of ZKPs can be applied for privacy preservation while taking advantage of smart contracts for payment and authentication [54]. Another application of smart contracts involves linking multiple blockchain networks and establishing the rules for information transfer between them. These cross-chain smart contracts can be deployed through gateways to trigger events that initiate cross-chain interactions. The concept of ZKPs can be leveraged here also to authenticate transactions without revealing user identity [44]. Like any code or software, smart contracts need updates to fix bugs, enhance security, and add new functionalities. However, their immutable nature poses challenges for upgrades, often necessitating workarounds such as proxy contracts, data segregation, or redeployment. Traditional upgrade methods add complexity and security risks, making well-structured and seamless solutions vital.