Understanding How DeFi Projects Work: A Deep Dive into Ethereum, MetaMask, and Smart Contracts

Introduction to DeFi Projects

Muhammad Waqas
Coinmonks
Published in
6 min readJun 23, 2024

--

Have you ever wondered how these fancy DeFi projects work? These projects where you can seemingly send money around as if you’re adding code to a bank account? It’s time to delve into the inner workings of blockchain technology and see how real blockchain applications function, especially from the perspective of a blockchain developer.

Photo by Shubham Dhage on Unsplash

Setting Up the Environment: MetaMask and Test Ether

Before diving into the technical details, we need to set up our environment. This involves installing MetaMask, a popular Ethereum wallet, and obtaining some test ether from a faucet.

Installing MetaMask

  1. Download MetaMask: Visit the official MetaMask website (be cautious of scams) and download the extension for your browser. MetaMask supports Chrome, Firefox, Brave, and even mobile versions for iOS and Android.
  2. Setup MetaMask: Once installed, run through the setup procedure. You’ll receive a seed phrase — this is crucial for recovering your wallet, so store it securely.
  3. Network Configuration: MetaMask allows you to connect to different Ethereum networks. For development, we’ll use test networks like Ropsten or Sepolia where you can get test ether for free.

Obtaining Test Ether

Using Faucets: Faucets are services that provide free test ether. Navigate to a faucet, connect your MetaMask wallet, and request test ether. This ether is used for testing and development purposes only.

Sending Ether: Wallet vs. Smart Contract

In this project, we will create a smart contract capable of managing its own funds, sending ether to other addresses, and demonstrating the differences between sending funds from a wallet versus a smart contract.

Sending Ether from a Wallet

When you send ether from one wallet to another, you initiate a transaction specifying the recipient’s address, the amount of ether, and optionally, additional data. MetaMask facilitates this by providing a user-friendly interface for transaction creation and signing.

Sending Ether via a Smart Contract

Smart contracts can also send ether. However, this involves additional complexities. The contract must hold ether, and the code must specify the conditions under which ether is sent.

Behind the Scenes: How Ethereum Transactions Work

Understanding Ethereum transactions is crucial for developing and interacting with smart contracts. Let’s break down the components and processes involved.

Transaction Components

  1. Nonce: A counter that ensures each transaction is processed once.
  2. Gas Price: The fee per unit of gas, incentivizing miners to include the transaction in a block.
  3. Gas Limit: The maximum amount of gas the transaction can consume.
  4. To: The recipient address.
  5. Value: The amount of ether to transfer.
  6. Data: Optional data payload for executing specific functions on smart contracts.
  7. Signature: The cryptographic signature of the sender, ensuring authenticity and integrity.

Process Flow

  1. Transaction Creation: A user creates a transaction, specifying the necessary details.
  2. Signing: The transaction is signed with the sender’s private key.
  3. Broadcasting: The signed transaction is broadcast to the Ethereum network.
  4. Validation: Nodes validate the transaction against Ethereum’s rules.
  5. Mining: Miners include the transaction in a block, which is then added to the blockchain.
  6. Finalization: Once included in a block, the transaction is considered final and immutable.

Hashing and Ethereum: Ensuring Security and Integrity

Hashing is fundamental to blockchain technology, ensuring data integrity and security. Ethereum uses hashing in several key areas:

What is Hashing?

Hashing involves converting input data of any size into a fixed-size string of characters, which appears random. This is achieved using a hash function. Ethereum primarily uses the Keccak-256 hash function.

Properties of Cryptographic Hash Functions

  1. Deterministic: The same input always produces the same output.
  2. Fast Computation: The hash value is quickly computed for any given input.
  3. Pre-image Resistance: It’s infeasible to generate the original input from its hash value.
  4. Small Changes Impact: A small change in input drastically changes the hash.
  5. Collision Resistance: It’s infeasible for two different inputs to produce the same hash.

Applications in Ethereum

  1. Transaction Hashes: Each transaction is hashed, creating a unique identifier.
  2. Block Hashes: Each block header is hashed, linking blocks together in the blockchain.
  3. Merkle Trees: Transactions within a block are hashed and structured in a Merkle tree, ensuring efficient and secure verification.

MetaMask: The Gateway to Ethereum

MetaMask serves as both a wallet and a gateway to interact with the Ethereum blockchain. Here’s how it works behind the scenes:

How MetaMask Works

  1. Wallet Creation: Generates a public-private key pair and provides a seed phrase for recovery.
  2. Transaction Signing: Signs transactions locally, ensuring private keys never leave the user’s device.
  3. Network Connection: Connects to Ethereum nodes (e.g., Infura) to broadcast transactions and interact with smart contracts.
  4. Account Management: Allows users to manage multiple accounts, each with its own address and private key.
  5. Secure Interface: Provides a secure interface for interacting with decentralized applications (dApps).

Using MetaMask

  1. Initiating Transactions: Users can send ether, interact with dApps, and manage their assets.
  2. Handling Private Keys: MetaMask securely stores private keys in the browser’s local storage, encrypted with the user’s password.
  3. Connecting to dApps: dApps request connection to MetaMask, enabling users to interact seamlessly with decentralized services.

Developing a Smart Contract to Manage Funds

In this project, we’ll develop a smart contract capable of managing its own funds and sending ether. Let’s break down the key components and functionality:

Smart Contract Code Example

Explanation of Key Functions

  1. deposit Function: Allows the contract to accept ether and updates the balancedReceived state variable.
  2. getContractBalance Function: Returns the current balance of the contract.
  3. withdrawAll Function: Transfers the entire contract balance to the sender's address.
  4. witdrawToAddress Function: Transfers the entire contract balance to a specified address.

Deploying and Interacting with the Contract

Deploy the Contract

Using Remix, deploy the SendWithdrawMoney contract to the Sepolia test network. Here’s a screenshot of the deployment interface in Remix:

In the screenshot:

  • The environment is set to “Injected Provider — MetaMask” connected to the Sepolia test network.
  • The gas limit is set to a custom value of 3,000,000.
  • The contract is selected, and the deploy button is ready to be clicked.

After Clicking the Deploy button below MetaMask popup appears:

In the provided screenshots, we see the interface for interacting with our smart contract in Remix. Here’s a brief description of the main components:

  1. Deposit: This button allows you to send ether to the smart contract. When clicked, it executes the deposit function, which increments the balancedReceived variable by the amount of ether sent.
  2. Withdraw To Address: This field allows you to specify an address to which you want to send ether from the contract. By inputting an address and clicking transact, it calls the withdrawToAddress function.
  3. Withdraw All: Clicking this button will execute the withdrawAll function, transferring all ether from the contract to the sender's address.
  4. Balance Received: This displays the total amount of ether received by the contract so far.
  5. Get Contract Balance: This shows the current ether balance held by the contract.

These functionalities illustrate the interaction with the smart contract, demonstrating how you can manage ether transactions within the Ethereum blockchain using Remix and MetaMask. The interface provides a user-friendly way to deploy, test, and interact with smart contracts seamlessly.

Interact via Metamask

Use Metamask to send ether to the contract, trigger the withdraw and transfer functions, and observe the behavior on Etherscan.

In the screenshots:

  • Metamask Activity: Shows the deposit and contract interaction activities.
  • Etherscan Transaction History: Shows the contract creation and deposit transactions.

Conclusion

Understanding how DeFi projects and Ethereum transactions work provides a solid foundation for developing blockchain applications. By setting up MetaMask, deploying smart contracts, and learning the intricacies of Ethereum transactions, you gain the skills necessary to build and interact with decentralized applications securely and efficiently.

--

--