Public blockchain technology serves as the foundational layer for cryptocurrency transactions, but its function is limited to the secure recording and broadcasting of data in a decentralized manner. However, the introduction of the Ethereum blockchain in 2015 extended blockchains’ capabilities by allowing anyone to develop applications and systems that leverage its core functions. Among these are DeFi protocols, which go beyond just P2P transactions to a wider range of financial services. DeFi consists of sets of a blockchain-based software application known as a smart contract to automatically execute certain functions upon users’ instructions and when predefined conditions are met, eliminating the need for an intermediary.
A common analogy for a smart contract is that of a vending machine: the vending machine automatically releases a bag of chips on the condition that it receives $2. The consumer initiates the transaction then solely relies on code to execute it, not a third party vendor. And while the smart contract automatically executes transactions, the transactions are still initiated by the user and still verified by the blockchain network and recorded on the ledger – i.e., the fundamentals of a P2P blockchain transaction do not change. In other words, a smart contract is simply a software tool for users to conduct a variety of financial activities without an intermediary and employ the verifiability and security of a blockchain.
The deployment of a smart contract is no different than other blockchain transactions. Essentially, anyone can take software code and deploy it on a blockchain, and the blockchain’s nodes will accept the code so long as the deployment transaction is a valid transaction. Here is how it works:
- Developer writes the code;
- The developer, or another user wishing to deploy the code, creates a deployment transaction that includes the bytecode of the smart contract and its initialization parameters, and signs the transaction with their private key to authenticate and authorize it – the sender does not specify the recipient;
- The deployment transaction is then sent to the sender’s connected nodes within the blockchain network;
- These nodes then relay the transaction to their own connected nodes and the transaction continues to propagate across the network;
- Each receiving node verifies and validates the transaction’s digital signature and sufficient gas, and ensures that it complies with the network’s rules – they do not audit the smart contract’s code;
- Once the deployment transaction has reached consensus, miners or validators include it in their new block, which finalized the deployment;
- Once it is added to the blockchain, the smart contract is activated and is assigned a unique address on the blockchain – its bytecode and initialization parameters are stored in the contract’s storage.
- Once it is deployed, the smart contract is autonomous and immutable, and anyone can use it.
Using a smart contract to transact involves specifying details such as the sender address, recipient (i.e., smart contract) address, transaction value, and gas fees. This includes the data field which contains the instructions (i.e., the function) for the smart contract’s execution. Specifically, the data field consists of two elements: a function identifier and a function argument.
The function identifier signals to the smart contract which function to execute (e.g., borrowing funds, token swapping, or voting on a governance proposal). The function argument for a transaction consists of the specific data or parameters input into the smart contract function for it to execute it properly (e.g., amount of tokens or the voter’s choice). The two elements ensure that a smart contract knows which operation to perform.
Constructing a transaction can be done manually by users with technical expertise; however, it is more commonly done by connecting the user’s unhosted wallet to the DeFi protocol’s front-end website (later discussed), as the process is much more intuitive and approachable. After constructing the transaction, the user then uses their private key, securely stored in their wallet, to sign the transaction and broadcasts it to the blockchain network. Once the transaction is included in a block and validated, it triggers the smart contract to automatically execute the logic defined in its code.