top of page

The End of Trust? How DeFi's Smart Contracts Are Redefining Financial Security.

  • Writer: Bohan Zhang
    Bohan Zhang
  • Nov 6, 2024
  • 6 min read

By: Bohan Zhang

Source- SoluLab


With the exponential growth of blockchain and the crypto economy in the last few years, emerges Decentralized Finance (DeFi), an alternative to traditional finance that has reshaped the finance landscape. DeFi is a collective term referring to all financial products and services that are accessible to users who can use Ethereum, including anyone on the internet. It offers alternatives to traditional finance intermediates, easier accessibility, a global market, and 100% transparency. At the core of this innovation lies the use of smart contracts. This article discusses the significance of smart contracts in DeFi, what they are, and their transformative effect on financial services. (Note: If you are interested in DeFi vs traditional finance, https://ethereum.org/en/defi/ gives a comprehensive explanation)

 

What are smart contracts?

In the Ethereum Virtual Machine, there are two types of accounts associated with ether addresses: EOA and contract accounts. EOA are user-owned accounts controlled by transactions created and cryptographically signed with a private key. Contract accounts, on the other hand, operate solely dependent on predetermined protocol: they have associated code and data storage, and they don’t have a private key. An Ethereum address identifies both of these types of accounts.

The term “smart contract” was first established by cryptographer Nick Szabo in the 1990s. He defined it as “a set of promises, specified in digital form, including protocols within which the parties perform on the other promises.” However, the definition of smart contracts is constantly evolving, especially since decentralized blockchain platforms were introduced with the invention of Bitcoin in 2009.

Here, let’s define smart contracts as immutable computer programs with predetermined terms and conditions written into code that are executed in the context of EVM.  

Essentially, look at smart contracts like a vending machine, where it executes the following steps:

1.      You select a product

2.      It shows the price

3.      You pay the price

4.      It verifies that you’ve met the precondition

5.      It gives you the product

A smart contract code resides on a decentralized blockchain, making it secure, transparent, and immutable. In the DeFi space, smart contracts perform three main functions: replacing intermediaries, setting precise rules, and optimizing AMM-based processes in trading.

 

Current Practical Usages

Here are some main integrations of smart contracts within DeFi:


Decentralized Exchanges (DEXs)

DEXs allow peer-to-peer trading without a central authority, meaning the absence of an intermediary AND Central Limit Order Book model in traditional exchanges’ context. Smart contracts in DEXs automate the matching of buy and sell orders. Smart contracts facilitate the liquidity pools that make DEX possible in the first place.

Lending and Borrowing Protocols

In this context, smart contracts handle interest rates, collateralization, and liquidation processes to enable decentralized lending and borrowing. Platforms like Aave allow users to deposit assets into liquidity pools and borrow other assets against them.

Stablecoins and Asset Management


Decentralized stablecoins, such as those issued by MakerDAO-DAI, are managed through smart contracts that will take supply for grant according to demand and collateralization ratios. This keeps the stablecoin pegged in value without needing a central reserve and fosters transparency and stability.


Yield Farming and Liquidity Mining


Yield farming involves providing liquidity to DeFi protocols in exchange for rewards, often in the form of tokens. Smart contracts, in this context, govern the liquidity pools. They track each user’s contribution to the pool, calculate trading fees, and distribute them proportionally to liquidity providers. In addition, they handle the issuance of governance tokens or other rewards that may be provided by the protocol as part of liquidity mining. All transactions are managed on-chain, which ensures transparency and prevents tampering.


Insurance Protocols  


This is a relatively minor use where companies offer insurance against smart contract failures. Here, smart contracts replace traditional underwriters and automatically assess claims, distribute funds, and manage risk pools.


Cycle of a Smart Contract

Now that we’ve covered some real-world examples, let’s discuss how smart contracts come to existence. Since EVM only runs on EVM bytecode, a language impossible for programmers to understand, arbitrary languages such as Solidity are adapted by programmers to later compile to bytecode. After compilation, contracts are deployed to the Ethereum network via a contract creation transaction, identified by being sent to the address. Each contract receives an ether address derived from the originating account and nonce, which acts as the receipt, either to transfer funds or to invoke contract functions. Unlike EOAs, a smart contract account does not have a private key associated with it and operates independently of special privileges at the protocol level, although such privileges can be baked into the code of the contract itself. A smart contract owns itself, meaning no one, not even the developer of the contract, can directly control the contract once deployed.     

Once a smart contract is deployed, it cannot be deleted unless it was programmed to do so. A smart contract stays dormant and can only operate when called by a transaction or another contract. Only an EOA account’s execution of a transaction can initiate a smart contract, and that contract would then execute and potentially call another contract if programmed to do so. This allows a chain of execution of smart contracts. Here’s an example. Suppose there are three ether accounts A, B, and C. A is an EOA while B and C are contract accounts with a causality relationship with one another. The transaction from A to B triggers B’s execution, and the code in B executed would then trigger C. As you can see, since C’s initiation for execution lies within B, B must execute first before C does. This embodies the idea that the Ethereum world computer runs contracts one by one, not parallel or in the background. However, C can activate before B’s execution is finished. The activation of another contract before the local code is finished is normally considered a vulnerability/flaw in the code. I will further cover how the vulnerability could be capitalized for attacks in another chapter.

Transactions

Let’s come back to the state of smart contracts after deployment. All transactions, including smart contract executions, are either successfully terminated or reverted. This means there is no half-execution or anything in between; transactions are atomic. Here I will provide what happens in different scenarios:

EOA -> EOA

· This is just a simple transaction; the outcome results in the change of both account balances, recorded in the global state.

EOA -> Simple Contract

·  Only the contract’s internal state, variable, and balance would change.

·  All updates to balance and contract variables are recorded in the global state, and changes to both the EOA and contract state are reverted.

EOA -> Contract calling another contract (Error-Propagation)

·         Error Propagation: If the contract structure allows errors to propagate, then any error encountered in the chain of contract calls will trigger a full rollback, canceling all changes.

·         Outcome: If successful, all changes (balances and contract variables in each contract) are updated in the global state. However, all state changes across the chain are reverted if there's any error.

EOA -> Contract calling another contract (Non-Error-Propagation)

·  This is varying since only some changes are recorded

·  Partial State Changes: Here, if a specific contract call fails, only the changes to that contract might be rolled back, while others in the transaction path may succeed and record changes in the global state. This is based on the PRECONDITION that contracts handle errors selectively which is in most cases

 

Outcome: If successful, the transaction updates only the non-failing contract states and balances. If some contracts fail, only those specific contracts’ changes are reverted, while others succeed, resulting in partial updates to the global state.

 Alternatively, here is what will happen if a transaction is reverted:

·         Attempt for the transaction is recorded in the blockchain

·         the funds are refunded, but the ether spent on the gas fee will still be charged

·         No effect on contract or contract state

 

Though the code within a smart contract is immutable upon deployment, it is possible to delete a contract. This is done through an EVM opcode called “self-destruct” which has to be programmed into a contract before deploying. After a contract is deleted, the ether address will link to a blank account like a regular account, except there’s no way to access it. The remaining assets left in the account will transfer to the specified recipient address, and any assets sent to the ether address will not be retrievable. However, the past transactions related to the account will not be erased, since it is recorded on the blockchain, which is immutable. Additionally, when a contract is deleted, potentially up to 50% of the gas fee of the original gas fee charged for deployment is refunded by EVM. The gas refund, other known as “negative gas”, is offered by EVM to incentivize people to delete contracts, since the deletion frees up network client resources.

Looking ahead, smart contracts and DeFi are poised to further transform finance, offering a glimpse into a future where trust is algorithmically guaranteed, and financial systems are democratized and decentralized. With the constant optimization and usage of smart contracts, we are likely to see even more innovative financial solutions that challenge the limits of traditional finance, shaping the future of Ethereum and the broader blockchain ecosystem. Keep in mind this is only an introduction to smart contracts and the DeFi world. I hope you found this read informative and interesting.

 

Kommentare


Top Stories

Stay ahead of the curve with our latest business insights. Subscribe to our digital magazine for the most recent updates.

  • Instagram
  • LinkedIn

Thank You for Subscribing!

All rights reserved. © 2024 youthbusinesscollective. Designed and secured by our team.

bottom of page