-
Understanding Blockchain
- What is Blockchain?
- How Does Blockchain Work?
- Types of Blockchain
- Consensus Mechanisms
- Blockchain Use Cases
-
Smart Contracts
- What are Smart Contracts?
- Solidity Basics
- Solidity Advanced Topics
- Smart Contract Use Cases
-
DApp Development
- What is a DApp?
- Components of a DApp
- DApp Development Tools
- DApp Use Cases
-
Web3 Libraries
- Web3.js
- Ethers.js
- Hardhat
- Truffle Suite
-
IPFS and Filecoin
- What is IPFS?
- Using IPFS
- Pinata Cloud
- Filecoin
-
Oracles
- What are Oracles?
- Chainlink
- Chainlink VRF
- Chainlink Price Feeds
-
DeFi
- What is DeFi?
- DeFi Protocols
- 0x Protocol
- Uniswap
-
NFTs
- What are NFTs?
- Minting NFTs
- Rarible
- OpenSea
-
DAOs
- What are DAOs?
- DAO Functions
- Aragon
- DAOstack
-
Security
- Gas Costs
- Avoid Using Solidity's transfer() and send()
- Protect Against Reentrancy
- Checks-Effects-Interactions Pattern
- Reentrancy Guard
-
Testing and Deployment
- Setting up a Testing Environment
- Writing Unit Tests
- Performing Complex Assertions
-
Further Learning
- ConsenSys Academy Bootcamp
Blockchain is like a digital ledger, but it's not maintained by any single entity. Instead, it's distributed across a network of computers, known as nodes. It's like a Google Doc that everyone can access and update, but no one can erase.
-
Decentralization: Unlike traditional databases, blockchain doesn't have a central authority. It's a team player, with all users collectively retaining control. This means we can say goodbye to middlemen like auditors, reducing costs and errors.
-
Immutability: Once data enters the blockchain, it's there to stay. This makes blockchain a reliable source of truth. In the world of Bitcoin, transactions are permanently recorded and viewable to anyone.
-
Security: Blockchain is like a fortress. Once a block is added, it can't be changed. If someone tries to alter a block, the network would reject it because it would disrupt the chain of hashes linking the blocks.
-
Transparency: Blockchain is like a glass box. All transactions can be transparently viewed by anyone with access to the network. This transparency builds trust among users.
Blockchain burst onto the scene with Bitcoin in 2009. Since then, it's been making waves in various areas, from creating cryptocurrencies and decentralized finance (DeFi) applications to non-fungible tokens (NFTs) and smart contracts. It's like a Swiss Army knife, with a tool for every job.
For example, imagine a voting system where each citizen is issued a single cryptocurrency or token. They send their token to their chosen candidate's wallet address. The transparent and traceable nature of blockchain would make fraudulent voting a thing of the past.
Ready to dive deeper into blockchain? Here are some resources to get you started:
- Blockchain Basics: A Practical Approach by Daniel Drescher
- Mastering Blockchain: Unlocking the Power of Cryptocurrencies, Smart Contracts, and Decentralized Applications by Imran Bashir
- Blockchain Revolution: How the Technology Behind Bitcoin Is Changing Money, Business, and the World by Don Tapscott and Alex Tapscott
Remember, the best way to learn is by doing. So, why not participate in blockchain projects or create your own? Happy learning! 🚀
Smart contracts are like self-driving cars for agreements. They automatically execute the actions required in a contract, making transactions trackable and irreversible. It's like having a robot lawyer that ensures all parties stick to the agreement.
-
Automation: Smart contracts are all about automation. They're scripts that automate the actions specified in a contract. It's like having a personal assistant that takes care of all the paperwork for you.
-
Immutability: Once a smart contract is deployed, it's set in stone. This ensures the integrity of the contract and builds trust among the parties involved.
-
Efficiency: Smart contracts are speed demons. They speed up contract execution by automating processes, making them a powerful tool for improving efficiency in various industries.
-
Security: Smart contracts are as secure as a vault. They rely on the underlying blockchain for security, inheriting the same level of security as the blockchain they're deployed on.
Smart contracts are versatile. They can be used to ensure transactions between two parties occur, manage real estate transactions, facilitate stock and commodity trading, streamline lending processes, and much more. It's like having a Swiss Army knife for the digital world.
Ready to dive deeper into smart contracts? Here are some resources to get you started:
- Mastering Ethereum: Building Smart Contracts and DApps by Andreas M. Antonopoulos and Gavin Wood
- Blockchain Basics: A Non-Technical Introduction in 25 Steps by Daniel Drescher
- Ethereum: Blockchains, Digital Assets, Smart Contracts, Decentralized Autonomous Organizations by Henning Diedrich
Remember, the best way to learn is by doing. So, why not participate in projects that use smart contracts or create your own? Happy learning! 🚀
This tutorial is a comprehensive guide on how to build your first decentralized application (dApp) on the Ethereum blockchain. It covers the process of writing your first Ethereum smart contract, testing it, deploying it to the Ethereum blockchain, and developing a client-side application that allows accounts to cast votes. The tutorial also explains key concepts like "what is a blockchain?", "what is a smart contract?", and "how does a dApp work?".
A blockchain is a peer-to-peer network of computers, called nodes, that share all the data and the code in the network. Instead of a centralized database, all the transaction data that is shared across the nodes in the blockchain is contained in bundles of records called blocks, which are chained together to create the public ledger. This public ledger represents all the data in the blockchain. All the data in the public ledger is secured by cryptographic hashing, and validated by a consensus algorithm.
Smart contracts are where all the business logic of our application lives. They are in charge of reading and writing data to the blockchain, as well as executing business logic. Smart contacts are written in a programming language called Solidity, which looks a lot like Javascript. The function of smart contracts on the blockchain is very similar to a microservice on the web.
The tutorial provides step-by-step instructions on how to build a dApp. It starts with setting up the necessary dependencies, including Node Package Manager (NPM), Truffle Framework, Ganache, and Metamask. The tutorial then guides you through creating a smart contract, deploying it to the blockchain, and interacting with it.
The dApp being built in this tutorial is a voting system. It includes a client-side application that communicates with the smart contract on the blockchain. The application has a table of candidates, each with an id, name, and vote count. It also has a form for casting a vote and displays the account connected to the blockchain.
The tutorial emphasizes the importance of testing when developing smart contracts. Since all code on the Ethereum blockchain is immutable, it's crucial to ensure that the contracts are bug-free before deployment. The tutorial provides a guide on how to write tests using the Mocha testing framework and the Chai assertion library.
This tutorial provides a comprehensive guide for anyone interested in developing a dApp on the Ethereum blockchain. It covers all the necessary steps, from setting up the environment to writing and testing a smart contract, and finally deploying it to the blockchain and interacting with it through a client-side application.
- How does the immutability of smart contracts on the blockchain impact the development process?
- What are the benefits and challenges of developing a dApp compared to a traditional web application?
- How does the use of Solidity for smart contracts compare to other programming languages you are familiar with?
Web3.js is a collection of libraries that allow you to interact with a local or remote Ethereum node using HTTP, IPC, or WebSocket. It's the Ethereum compatible JavaScript API which implements the Generic JSON RPC spec. It's available on npm as a node module, for bower and component as an embeddable js and as a meteor.js package.
-
Make Synchronous Requests: This feature allows you to make requests synchronously to the blockchain.
-
Handle Events: Web3.js allows you to handle blockchain events.
-
Interact with Smart Contracts: Web3.js provides the necessary tools to interact with smart contracts on the Ethereum blockchain.
-
Work with Ethereum Accounts: You can generate, manipulate and sign transactions with this library.
Web3.js can be added to your project using npm install web3
.
To interact with the Ethereum blockchain, you need to connect Web3.js to an Ethereum node. Here's how you can do it:
var Web3 = require('web3');
// Connect to the node
var web3 = new Web3('http://localhost:8545');
You can then use the web3
instance to interact with the Ethereum blockchain.
The official documentation provides a comprehensive overview of Web3.js, including its various modules such as web3-eth
(for Ethereum blockchain and smart contract interactions), web3-shh
(for whisper protocol to communicate p2p and broadcast), and web3-bzz
(for swarm protocol, the decentralized file storage).
Web3 libraries are essential tools for interacting with the Ethereum blockchain. They provide a set of APIs that allow developers to read blockchain data, send transactions, interact with smart contracts, and more. Here are some of the most popular libraries used in dApp development:
Web3.js is the Ethereum compatible JavaScript API which implements the Generic JSON RPC spec. It's available on npm as a node module, for bower and component as an embeddable js and as a meteor.js package.
Ethers.js is a complete Ethereum wallet implementation and utilities in JavaScript and TypeScript. It aims to be an easy-to-use, lightweight, and thoroughly tested Ethereum library.
The Graph is a protocol for indexing Ethereum and IPFS data and querying it using GraphQL. It allows developers to build serverless dApps on Ethereum and IPFS.
Light.js is a high-level reactive JS library optimized for light clients. It provides a set of high-level utilities that make it easy to build light-client-friendly dApps.
Web3-wrapper is a TypeScript alternative to Web3.js. It provides a set of TypeScript typings for Ethereum contracts.
Alchemyweb3 is a wrapper around Web3.js with automatic retries and enhanced APIs. It's built specifically for Alchemy users.
Viem is a TypeScript Interface for Ethereum. It provides a set of TypeScript typings for Ethereum contracts.
Web3 libraries are essential tools for interacting with the Ethereum blockchain. They provide convenient methods for Ethereum blockchain programming, including functionality for transactions, smart contracts, and decentralized applications (dApps). Here are some of the most popular libraries for dApp development:
Web3.js is the Ethereum compatible JavaScript API which implements the Generic JSON RPC spec. It's available on npm as a node module, for bower and component as an embeddable js and as a meteor.js package.
var Web3 = require('web3');
var web3 = new Web3('http://localhost:8545');
console.log(web3);
As of July 2023, Web3.js has over 14.6k stars on GitHub.
Ethers.js is a complete, compact and simple Ethereum library in JavaScript. It allows you to interact with the Ethereum blockchain and its smart contracts easily.
var ethers = require('ethers');
var provider = ethers.getDefaultProvider('rinkeby');
console.log(provider);
As of July 2023, Ethers.js has over 6.9k stars on GitHub. According to npm trends, it has a significant number of weekly downloads, making it a popular choice among developers.
OpenZeppelin Network.js is a front-end library that helps you interact with your contracts.
import { useWeb3Network } from '@openzeppelin/network/react';
const local = useWeb3Network('http://127.0.0.1:8545');
As of July 2023, OpenZeppelin has over 2.2k stars on GitHub.
0x.js is a JavaScript library for interacting with the 0x protocol.
import { Web3Wrapper } from '@0x/web3-wrapper';
const provider = web3.currentProvider;
const web3Wrapper = new Web3Wrapper(provider);
As of July 2023, 0x has over 1.1k stars on GitHub.
Alchemy-web3 is a wrapper around Web3.js which provides enhanced API methods and other crucial benefits.
import { createAlchemyWeb3 } from "@alch/alchemy-web3";
const web3 = createAlchemyWeb3("https://eth-rinkeby.alchemyapi.io/v2/your-api-key");
console.log(web3);
As of July 2023, Alchemy has over 1k stars on GitHub.
Graph Node indexes data from blockchains such as Ethereum and serves it over GraphQL.
git clone https://github.com/graphprotocol/graph-node/
cd graph-node/docker
docker-compose up
As of July 2023, Graph Protocol has over 2.5k stars on GitHub.
Solidity is an object-oriented, high-level language for implementing smart contracts on Ethereum. Smart contracts are programs that govern the behavior of accounts within the Ethereum state. Solidity is statically typed, supports inheritance, libraries, and complex user-defined types, among other features.
With Solidity, you can create contracts for uses such as voting, crowdfunding, blind auctions, and multi-signature wallets. It is designed to target the Ethereum Virtual Machine (EVM) and is influenced by C++, Python, and JavaScript.
-
Understand the Smart Contract Basics: If you are new to the concept of smart contracts, start by understanding the basics. This includes a simple example of a smart contract written in Solidity, blockchain basics, and the Ethereum Virtual Machine.
-
Get to Know Solidity: Once you are accustomed to the basics, read about Solidity and understand the core concepts of the language.
-
Install the Solidity Compiler: There are various ways to install the Solidity compiler. Choose your preferred option and follow the steps outlined on the installation page.
-
Learn More: If you want to learn more about building decentralized applications on Ethereum, the Ethereum Developer Resources can help you with further general documentation around Ethereum, and a wide selection of tutorials, tools, and development frameworks.
For more detailed information, you can refer to the Solidity documentation.
Here is an example of a simple Solidity contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
This contract includes a single variable storedData
and two functions set
and get
for modifying and retrieving the value of the variable.
Solidity is an object-oriented, high-level language for implementing smart contracts, designed to target the Ethereum Virtual Machine (EVM). It is best for creating applications such as voting systems, crowdfunding platforms, blind auctions, and multi-signature wallets. Solidity is highly influenced by C++, Python, and JavaScript.
Solidity is influenced by C++, Python, and JavaScript, and it has been designed to target the Ethereum Virtual Machine (EVM). Here's how Solidity resembles these languages:
-
C++: Solidity's syntax is similar to that of C++. It also shares many concepts with C++, such as classes and memory management.
-
Python: Solidity's ease of use and simplicity of syntax are similar to Python. It also supports Python-like function names.
-
JavaScript: Solidity is executed on the EVM, similar to how JavaScript is executed in a web browser. It also supports JavaScript-like function calls and variable declarations.
Here are some resources to learn more about Solidity:
-
Solidity Documentation: The official documentation is a comprehensive resource that covers all aspects of Solidity.
-
Solidity Tutorial by BitDegree: This tutorial targets novice developers and those new to Solidity. It guides them in understanding basic through more advanced concepts in Solidity.
-
Solidity Tutorial by TutorialsPoint: This tutorial assumes your familiarity with blockchain, and general programming. It covers basic syntax, first application, comments, types, variables, variable scope, operators, loops, decision making, strings, arrays, enums, structs, mappings, conversions, ether units, special variables, style guide, and functions.
Here is an example of a simple Solidity contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
This contract includes a single variable storedData
and two functions set
and get
for modifying and retrieving the value of the variable.
Smart contracts are programs that run on the Ethereum blockchain. They are a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. Smart contracts are a type of Ethereum account, meaning they have a balance and can be the target of transactions. However, they're not controlled by a user, instead, they are deployed to the network and run as programmed. User accounts can then interact with a smart contract by submitting transactions that execute a function defined on the smart contract. Smart contracts can define rules, like a regular contract, and automatically enforce them via the code. Smart contracts cannot be deleted by default, and interactions with them are irreversible.
Here's a simple example of how a vending machine would look if it were a smart contract written in Solidity:
pragma solidity 0.8.7;
contract VendingMachine {
// Declare state variables of the contract
address public owner;
mapping (address => uint) public cupcakeBalances;
// When 'VendingMachine' contract is deployed:
// 1. set the deploying address as the owner of the contract
// 2. set the deployed smart contract's cupcake balance to 100
constructor() {
owner = msg.sender;
cupcakeBalances[address(this)] = 100;
}
// Allow the owner to increase the smart contract's cupcake balance
function refill(uint amount) public {
require(msg.sender == owner, "Only the owner can refill.");
cupcakeBalances[address(this)] += amount;
}
// Allow anyone to purchase cupcakes
function purchase(uint amount) public payable {
require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cupcake");
require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase");
cupcakeBalances[address(this)] -= amount;
cupcakeBalances[msg.sender] += amount;
}
}
Here are some resources to learn more about Smart Contracts:
-
Introduction to Smart Contracts: This is a comprehensive guide provided by Ethereum.org that covers all aspects of Smart Contracts.
-
Smart Contracts Tutorial by BitDegree: This tutorial targets novice developers and those new to Smart Contracts. It guides them in understanding basic through more advanced concepts.
-
Testing Your Contracts: This guide by Truffle Suite provides an overview of how to test your Smart Contracts.
Frontend Integration in the context of DApps refers to the process of connecting the user interface of your application (the frontend) with the smart contracts deployed on the Ethereum blockchain (the backend). This is a crucial step in DApp development as it allows users to interact with your smart contracts.
A decentralized application (DApp) is an application built on a decentralized network that combines a smart contract and a frontend user interface. On Ethereum, smart contracts are accessible and transparent – like open APIs – so your DApp can even include a smart contract that someone else has written.
-
Zero downtime – Once the smart contract is deployed on the blockchain, the network as a whole will always be able to serve clients looking to interact with the contract. Malicious actors, therefore, cannot launch denial-of-service attacks targeted towards individual DApps.
-
Privacy – You don’t need to provide real-world identity to deploy or interact with a DApp.
-
Resistance to censorship – No single entity on the network can block users from submitting transactions, deploying DApps, or reading data from the blockchain.
-
Complete data integrity – Data stored on the blockchain is immutable and indisputable, thanks to cryptographic primitives. Malicious actors cannot forge transactions or other data that has already been made public.
-
Trustless computation/verifiable behavior – Smart contracts can be analyzed and are guaranteed to execute in predictable ways, without the need to trust a central authority.
-
Maintenance – DApps can be harder to maintain because the code and data published to the blockchain are harder to modify.
-
Performance overhead – There is a huge performance overhead, and scaling is really hard.
-
Network congestion – When one DApp uses too many computational resources, the entire network gets backed up.
-
User experience – It may be harder to engineer user-friendly experiences because the average end-user might find it too difficult to set up a tool stack necessary to interact with the blockchain in a truly secure fashion.
-
Centralization – User-friendly and developer-friendly solutions built on top of the base layer of Ethereum might end up looking like centralized services anyways.
-
Scaffold-ETH - Quickly experiment with Solidity using a frontend that adapts to your smart contract.
-
Create Eth App - Create Ethereum-powered apps with one command.
-
One Click Dapp - FOSS tool for generating DApp frontends from an ABI.
-
Etherflow - FOSS tool for Ethereum developers to test their node, and compose & debug RPC calls from the browser.
-
The Architecture of a Web 3.0 application - Preethi Kasireddy
-
A 2021 guide to decentralized applications - LimeChain
-
What Are Decentralized Apps? - Gemini
-
Popular DApps - Alchemy
This tutorial provides a step-by-step guide on how to build a decentralized application (dApp) that uses a Solidity smart contract, Chainlink Data Feeds, React, and Ethers.js.
The backend of a dApp is typically composed of smart contracts that store the business logic and the state of the application. This is the primary difference between a dApp and a traditional web application. The backend logic of a dApp is housed in highly secure, tamper-proof smart contracts, which provide benefits such as zero downtime, enhanced privacy, censorship resistance, and trust-minimized execution of logic.
However, these benefits come with some drawbacks. Maintenance of dApps is more involved, as code deployed to the blockchain isn’t modifiable by default. There is also a performance overhead due to running the logic across a distributed network as opposed to a centralized server.
Most applications need to store data, but due to the distributed nature of blockchains, storing large amounts of data on-chain is not feasible and can get very expensive. This is why many dApps that need to store data make use of off-chain data storage services such as IPFS or Filecoin, leaving the blockchain to store crucial business logic and state only.
The tutorial then provides a detailed guide on building a simple end-to-end dApp. The steps include:
-
Creating the Smart Contract: The smart contract in the dApp is used to look up data and reflect state changes on the blockchain. In this case, the value of ETH/USD is looked up using the ETH/USD Chainlink Data Feed and then permanently stored in the smart contract.
-
Deploying the Smart Contract: The smart contract is compiled and deployed to the Rinkeby test network.
-
Creating the Frontend Application: The frontend logic and user interface of the dApp are built using React and Ethers.js. The frontend connects the user interface with the deployed smart contract, giving a full end-to-end dApp.
-
Understanding the Backend of a dApp: How does the backend of a dApp differ from that of a traditional web application? What are the benefits and drawbacks of this approach?
-
Data Storage in dApps: Why is storing large amounts of data on-chain not feasible? How do off-chain data storage services like IPFS or Filecoin help in this regard?
-
Building a Simple dApp: What are the key steps involved in building a simple dApp? How does each component contribute to the overall functionality of the dApp?
Interacting with the Ethereum network involves reading data from the blockchain and writing transactions to it. This can be done using a library like Web3.js. Here's a brief introduction to some of the things you can do with Web3.js:
Web3.js is a collection of libraries that allow you to interact with a local or remote Ethereum node using HTTP, IPC, or WebSocket. It's used to interact with the Ethereum blockchain and its ecosystem.
You can install Web3.js via npm using the command npm install web3
.
After installation, you can create an instance of Web3 like so:
var Web3 = require('web3');
var web3 = new Web3('http://localhost:8545');
This code creates a new instance of Web3 and connects it to an Ethereum node running locally on your machine at http://localhost:8545
.
You can get a list of accounts on the blockchain with web3.eth.getAccounts()
. This returns a promise that resolves to an array of accounts.
You can get the balance of an account with web3.eth.getBalance(account)
. This also returns a promise that resolves to the balance of the account.
You can send Ether from one account to another with web3.eth.sendTransaction({from: 'account1', to: 'account2', value: 'amount'})
. This returns a promise that resolves to the transaction receipt.
You can interact with smart contracts on the Ethereum blockchain using Web3.js. You'll need the ABI (Application Binary Interface) of the contract and its address. You can create a new contract instance with new web3.eth.Contract(ABI, address)
and call its methods with contract.methods.myMethod().call()
.
You can listen for events emitted by smart contracts with contract.events.MyEvent()
. This returns an event emitter that you can use to listen for new events.
Remember, all these operations are asynchronous and return promises, so you'll need to handle them appropriately in your code.
The InterPlanetary File System (IPFS) is a protocol and network designed to create a content-addressable, peer-to-peer method of storing and sharing hypermedia in a distributed file system. IPFS was initially designed by Juan Benet and is now an open-source project developed with the help of the community.
IPFS enables the creation of completely distributed applications. It aims to make the web faster, safer, and more open.
IPFS is a peer-to-peer distributed file system that seeks to connect all computing devices with the same system of files. In some ways, this is similar to the original aims of the Web, but IPFS is actually more similar to a single BitTorrent swarm exchanging git objects.
You can use the js-ipfs library to interact with IPFS in your JavaScript applications.
Filecoin is a decentralized storage system that aims to “store humanity’s most important information.” It is an open-source, public cryptocurrency and digital payment system intended to be a blockchain-based cooperative digital storage and data retrieval method. This system is made by Protocol Labs and builds on top of IPFS, allowing users to rent unused hard drive space.
Filecoin's system includes a native cryptocurrency (denoted FIL), which is used as payment for these storage and retrieval services. The system is designed so that the more Filecoin a user chooses to stake, the more storage space they earn on the network. This creates an incentive for users to contribute as much storage as they can, which in turn makes the network more robust.
Filecoin's ICO, which took place in 2017, was one of the largest initial coin offerings ever, raising over $200 million in less than an hour. The project was highly anticipated due to its potential to disrupt the cloud storage industry, which is dominated by large corporations.
Pinata is a third-party service that makes it easy to use IPFS for data storage. It provides an easy-to-use API and a dashboard for managing your data on IPFS. You can use it to pin your data to the IPFS network, ensuring that it's always available. Pinata has a free tier that you can use to try out the service and see if it meets your needs.
Pinata is a third-party service that makes it easy to use IPFS for data storage. It provides an easy-to-use API and a dashboard for managing your data on IPFS. You can use it to pin your data to the IPFS network, ensuring that it's always available.
To connect to the Pinata API, you will need to generate Pinata API Keys. Visit the Pinata API Keys page to generate new keys.
When you generate your keys, you will see a modal with the Pinata API Key, Pinata API Secret, and the JWT. Your "Pinata API Key" acts as your public key for the REST API, and your "Pinata Secret API Key" acts as the password for your public key. The JWT is an encoded mix of the two. Be sure to keep your secret key private.
The base URL for Pinata requests is: https://api.pinata.cloud
You have two ways of connecting to the Pinata API:
- Bearer Authentication: To use the bearer authentication model, you will need the JWT that is generated when creating API keys. This token can be used as an Authorization header for all your API requests in the following format:
"Authorization": "Bearer YOUR_JWT"
- Custom Headers: If not using bearer authentication, your API requests will need to include two headers:
pinata_api_key: (put your personal pinata api key here)
pinata_secret_api_key: (put your personal pinata secret api key here)
You can test your connection to the Pinata API via the data/testAuthentication
endpoint. Here's how you can do it using Node.js:
const axios = require('axios');
axios
.get('https://api.pinata.cloud/data/testAuthentication', {
headers: {
Authorization: `Bearer YOUR_JWT`,
},
})
.then((response) => {
// handle your response here
console.log(response.data);
})
.catch((error) => {
// handle error here
console.error('Error:', error);
});
This code sends a GET request to the data/testAuthentication
endpoint with your JWT in the Authorization header. If your JWT is valid, you should see a response with the message: "Congratulations! You are communicating with the Pinata API!"
In the context of blockchain and smart contracts, an oracle is an agent that finds and verifies real-world occurrences and submits this information to a blockchain to be used by smart contracts. They provide the bridge between off-chain and on-chain systems.
Smart contracts are self-executing contracts with the terms of the agreement directly written into code. They exist across a decentralized blockchain network, and they need a way to interact with data outside of their network. That's where oracles come in.
Oracles are vital within the blockchain ecosystem because they broaden the scope in which smart contracts can operate. Without oracles, smart contracts would be limited to the data on their blockchain and would be unable to interact with outside information.
There are several types of oracles based on the nature of the data they provide, including:
-
Software Oracles: These handle online information, such as temperature, prices, flight or train delays, etc. They extract their data from online sources.
-
Hardware Oracles: These are for receiving data from the physical world, for example, a sensor measuring the temperature in a cargo ship to track if the conditions are keeping with the terms set in a smart contract.
-
Consensus Oracles: These oracles send data to the contract after they reach consensus on data accuracy from several oracles.
-
Inbound Oracles: These provide the smart contract with data from the external world.
-
Outbound Oracles: These provide smart contracts with the ability to send data to the outside world.
Here are some resources for further reading:
Please note that while oracles are a necessary component for many complex smart contracts, they do introduce a point of trust into a system (the blockchain) that is otherwise trustless. This is often referred to as the "oracle problem". Various solutions have been proposed, including decentralized oracle networks.
In the next section, we will explore some popular oracle services in the blockchain industry and how to use them.
This example contract obtains the latest price answer from the BTC / USD feed on the Sepolia testnet, but you can modify it to read any of the different Types of Data Feeds.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
/**
* THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
* THIS IS AN EXAMPLE CONTRACT WHICH USES UNAUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/
/**
* If you are reading data feeds on L2 networks, you must
* check the latest answer from the L2 Sequencer Uptime
* Feed to ensure that the data is accurate in the event
* of an L2 sequencer outage. See the
* https://docs.chain.link/data-feeds/l2-sequencer-feeds
* page for details.
*/
contract DataConsumerV3 {
AggregatorV3Interface internal dataFeed;
/**
* Network: Sepolia
* Aggregator: BTC/USD
* Address: 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43
*/
constructor() {
dataFeed = AggregatorV3Interface(
0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43
);
}
/**
* Returns the latest answer.
*/
function getLatestData() public view returns (int) {
// prettier-ignore
(
/* uint80 roundID */,
int answer,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = dataFeed.latestRoundData();
return answer;
}
}
The contract has the following components:
-
The import line imports an interface named AggregatorV3Interface. Interfaces define functions without their implementation, which leaves inheriting contracts to define the actual implementation themselves. In this case, AggregatorV3Interface defines that all v3 Aggregators have the function latestRoundData. You can see the complete code for the AggregatorV3Interface on GitHub.
-
The constructor() {} initializes an interface object named dataFeed that uses AggregatorV3Interface and connects specifically to a proxy aggregator contract that is already deployed at 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43. The interface allows your contract to run functions on that deployed aggregator contract.
-
The getLatestData() function calls your dataFeed object and runs the latestRoundData() function. When you deploy the contract, it initializes the dataFeed object to point to the aggregator at 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43, which is the proxy address for the Sepolia BTC / USD data feed. Your contract connects to that address and executes the function. The aggregator connects with several oracle nodes and aggregates the pricing data from those nodes. The response from the aggregator includes several variables, but getLatestData() returns only the answer variable.
DeFi, or decentralized finance, refers to financial products and services built as open-source software on top of blockchain technology. These can be pieced together like "money legos" via shared infrastructure. DeFi is powered by smart contracts, which are publicly accessible and highly interoperable. This allows projects in DeFi to easily connect together to create powerful new innovations.
DeFi is creating an alternative financial system that's open to everyone and minimizes the need for trust and reliance on central authorities. It uses technologies like the internet, cryptography, and blockchain to build and control a financial system for the users, by the users.
Most DeFi applications today are built on the Ethereum blockchain, which is a network technology that maintains a shared ledger of digital value. Developers can program applications on Ethereum using smart contracts that can create, store, and manage digital assets on the blockchain.
DeFi can be traced back to the launch of Bitcoin in 2009. However, the first true DeFi DApp, MakerDAO, launched on Ethereum at the end of 2017. Since then, the DeFi ecosystem has blossomed across a variety of sectors, including borrowing protocols like Compound, derivatives protocols like dYdX, trading protocols like Uniswap, and more.
DeFi activities and projects can be divided into different categories, including borrowing/lending, decentralized exchanges (DEXes), derivatives, and tokenized assets. One of the newer phenomena in DeFi is yield farming, where DeFi projects incentivize liquidity providers by rewarding them with token rewards.
Despite its potential, it's important to note that there are varying degrees of decentralization in DeFi services. Not everything can be or needs to be fully decentralized. For example, while you can trade, send, and receive tokens on the blockchain, you cannot completely eliminate the need to physically manage or redeem the real-world asset that the token represents.
The 0x protocol provides a Swap API that allows you to interact with the protocol for retail trade. Here's a step-by-step guide on how to use the Swap API:
-
Get a 0x API key: If you are creating an application on the mainnet, you will need to create a 0x account and get a live API key. You can get your API key from the 0x Dashboard.
-
Set a Token Allowance: A token allowance is required if you want a third-party to move funds on your behalf. In this case, you would like the 0x Exchange Proxy smart contract to trade your ERC20 tokens for you, so you will need to approve an allowance for this contract to move a certain amount of your ERC20 tokens on your behalf.
-
Fetch a Swap Quote: Once the token allowance has been set, you can fetch a swap quote. Here's an example of how to do this:
const qs = require('qs');
const params = {
sellToken: '0x6B175474E89094C44Da98b954EedeAC495271d0F', //DAI
buyToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2', //WETH
sellAmount: '100000000000000000000', // 100 DAI
};
const headers = {'0x-api-key: [api-key]'}; // Replace [api-key] with your live API key
const response = await fetch(`https://api.0x.org/swap/v1/quote?${qs.stringify(params)}`, { headers });
console.log(await response.json());
- Send the Transaction to the Network: Once you've received the API response, you will need to sign the transaction with your preferred web3 library (web3.js, ethers.js, wagmi, etc.) and submit it to the network.
Here's an example of how to do this using web3.js:
// Fetch the swap quote.
const response = await fetch(`https://api.0x.org/swap/v1/quote?${qs.stringify(params)}`, { headers });
const quote = await response.json();
// Submit the transaction.
const receipt = await web3.eth.sendTransaction(quote);
Please note that this is a simplified example and in a production implementation, there would be some error handling for the API response.
You can find more detailed examples and advanced options in the 0x documentation.
Non-fungible tokens (NFTs) are unique, digital items with blockchain-managed ownership. Examples include collectibles, game items, digital art, event tickets, domain names, and even ownership records for physical assets.
Most discussions about non-fungible tokens begin by introducing the idea of fungibility, which is defined as “able to replace or be replaced by another identical item”. Non-fungible assets are just normal stuff. Fungible assets are the odd ones out!
Just as we had digital currencies (think airline points, in-game currencies) before cryptocurrencies emerged, we’ve had non-fungible digital assets since the dawn of the internet. Domain names, event tickets, in-game items, even handles on social networks like Twitter or Facebook, are all non-fungible digital assets; they just vary in their tradeability, liquidity, and interoperability.
Traditional digital assets—from event tickets to domain names—have no unified representation in the digital world. By representing non-fungible tokens on public blockchains, developers can build common, reusable, inheritable standards relevant to all non-fungible tokens.
Non-fungible token standards allow non-fungible tokens to move easily across multiple ecosystems. When a developer launches a new NFT project, these NFTs are immediately viewable inside dozens of different wallet providers, tradeable on marketplaces, and, most recently, displayable inside of virtual worlds.
The most compelling feature enabled by interoperability is free trade on open marketplaces. For the first time, users can move items outside of their original environments and into a marketplace where they can take advantage of sophisticated trading capabilities, like eBay-style auctions, bidding, bundling, and the ability to sell in any currency, like stablecoins and application-specific currencies.
Instant tradeability of non-fungible tokens will lead to higher liquidity. NFT marketplaces can cater to a variety of audiences—from hardcore traders to more novice players—allowing for greater exposure of the assets to a wider pool of buyers.
Smart contracts allow developers to place hard caps on the supply of non-fungible tokens and enforce persistent properties that cannot be modified after the NFTs are issued.
Of course, like traditional digital assets, NFTs are fully programmable. Many of today’s NFTs have more complex mechanics, like forging, crafting, redeeming, random generation, etc. The design space is full of possibilities.
Standards are part of what makes non-fungible tokens powerful. They give developers the guarantee that assets will behave in a specific way and describe exactly how to interact with the basic functionality of the assets. The two main standards are ERC721 and ERC1155.
Metadata provides descriptive information for a specific token ID. In the case of the CryptoKittty, the metadata is the name of the cat, the picture of the cat, a description, and any additional traits (called “cattributes”, in the case of CryptoKitties).
The first decision for developers is what metadata to represent on-chain vs. off-chain. That is, do you bake the metadata directly into the smart contract representing the tokens, or do you host it separately?
Experiments in NFTs began with the emergence of colored coins on the Bitcoin network. Rare Pepes, illustrations of the Pepe the Frog character built on the Bitcoin counterparty system, were among the first. Some of them actually sold on eBay, and a set of Rare Pepes later sold in a live auction in New York. The first Ethereum-based NFT experiment was CryptoPunks, which consisted of 10,000 unique collectible punks, each of which has a set of unique characteristics.
CryptoKitties was the first project to take NFTs to the mainstream. Launched in late 2017 at the ETH Waterloo hackathon, CryptoKitties featured a primitive on-chain game that allowed users to breed digital cats together to produce new cats of varying rarity.
Despite the market downturn, the early days of CryptoKitties provided a magical moment for many. For the first time, a team had deployed a non-financial blockchain-based application that made its way to the tech mainstream, albeit only for a few weeks. After CryptoKitties, NFTs underwent a second small hype cycle in early 2018 as investors and entrepreneurs started to think about a new way to own digital stuff.
Rarible is an aggregated NFT marketplace that allows users to discover, sell, and buy NFTs. It supports Ethereum, Solana, and Tezos NFTs.
Rarible provides a platform for launching your own NFT collection and trading on third-party marketplaces. Here are some of the advantages of using Rarible for your project:
- Foolproof revenue for your project: The fees from each trade go directly to your project's treasury, benefiting your project and the community.
- Aggregated liquidity + enforced royalties: On a community marketplace, royalties are paid on each sale. Even if the listing was aggregated from a marketplace that would allow users to circumvent royalties.
- No copycat scams + better navigation: Your community is safe from copycat scam collections and has a much better time navigating your project's NFTs.
- Web3 freedom: Your project no longer has to rely on centralized marketplaces that technically can delist your collection at any time, causing massive damage.
- On-brand shopping experience for your holders: Your community feels at home and has an awesome, deeper experience trading NFTs on their new marketplace. Especially when it's designed with on-brand colors, font, and other customizable elements.
Here is an example of how to create an NFT on Rarible:
// Connect to the Ethereum network
const provider = new ethers.providers.Web3Provider(window.ethereum);
// Create a new instance of the Rarible SDK
const sdk = createRaribleSdk(new Web3Ethereum({ web3: provider }), 'rinkeby');
// Define the NFT metadata
const metadata = {
name: 'My NFT',
description: 'This is my first NFT on Rarible',
image: 'ipfs://QmWrzT4AzEf26G7wN8Pb4MWA6oU7pK2qJXHRT8z1WBtPuK',
attributes: [{ key: 'Rarity', value: 'Legendary' }]
};
// Mint the NFT
const item = await sdk.nft.mint({
collection: '0xCollectionAddress',
uri: '/ipfs/QmWrzT4AzEf26G7wN8Pb4MWA6oU7pK2qJXHRT8z1WBtPuK',
supply: 1,
royalties: [],
lazy: true
});
console.log('NFT minted:', item);
For more information, check out the Rarible Documentation.
OpenSea is the largest NFT marketplace on the Ethereum blockchain, offering a wide range of NFTs from digital art and virtual real estate to digital goods and more.
OpenSea provides a platform for launching your own NFT collection and trading on the marketplace. Here are some of the advantages of using OpenSea for your project:
- Ease of use: OpenSea's user-friendly interface makes it easy for anyone to create, buy, and sell NFTs.
- Wide range of NFTs: OpenSea supports a wide range of NFTs, including ERC721 and ERC1155 assets.
- Community: With a large user base, OpenSea has a vibrant community of artists, collectors, and developers.
- On-chain: All items on OpenSea are on-chain and immutable, which means they truly belong to you.
Here is an example of how to create an NFT on OpenSea:
// Connect to the Ethereum network
const provider = new ethers.providers.Web3Provider(window.ethereum);
// Create a new instance of the OpenSea SDK
const seaport = new OpenSeaPort(provider);
// Define the NFT metadata
const metadata = {
name: 'My NFT',
description: 'This is my first NFT on OpenSea',
image: 'ipfs://QmWrzT4AzEf26G7wN8Pb4MWA6oU7pK2qJXHRT8z1WBtPuK',
attributes: [{ trait_type: 'Rarity', value: 'Legendary' }]
};
// Mint the NFT
const asset = await seaport.createAsset({
tokenName: 'MyNFT',
tokenSymbol: 'MNFT',
tokenDescription: 'My first NFT on OpenSea',
tokenURI: '/ipfs/QmWrzT4AzEf26G7wN8Pb4MWA6oU7pK2qJXHRT8z1WBtPuK'
});
console.log('NFT minted:', asset);
For more information, check out the OpenSea Documentation.
A DAO, or Decentralized Autonomous Organization, is a collectively-owned, blockchain-governed organization working towards a shared mission. DAOs allow us to work with like-minded folks around the globe without trusting a benevolent leader to manage the funds or operations. There is no CEO who can spend funds on a whim or CFO who can manipulate the books. Instead, blockchain-based rules baked into the code define how the organization works and how funds are spent.
They have built-in treasuries that no one has the authority to access without the approval of the group. Decisions are governed by proposals and voting to ensure everyone in the organization has a voice, and everything happens transparently on-chain.
Starting an organization with someone that involves funding and money requires a lot of trust in the people you're working with. But it’s hard to trust someone you’ve only ever interacted with on the internet. With DAOs you don’t need to trust anyone else in the group, just the DAO’s code, which is 100% transparent and verifiable by anyone.
This opens up so many new opportunities for global collaboration and coordination.
To help this make more sense, here's a few examples of how you could use a DAO:
- A charity – you could accept donations from anyone in the world and vote on which causes to fund.
- Collective ownership – you could purchase physical or digital assets and members can vote on how to use them.
- Ventures and grants – you could create a venture fund that pools investment capital and votes on ventures to back. Repaid money could later be redistributed amongst DAO-members.
The backbone of a DAO is its smart contract, which defines the rules of the organization and holds the group's treasury. Once the contract is live on Ethereum, no one can change the rules except by a vote. If anyone tries to do something that's not covered by the rules and logic in the code, it will fail. And because the treasury is defined by the smart contract too that means no one can spend the money without the group's approval either. This means that DAOs don't need a central authority. Instead, the group makes decisions collectively, and payments are automatically authorized when votes pass.
This is possible because smart contracts are tamper-proof once they go live on Ethereum. You can't just edit the code (the DAOs rules) without people noticing because everything is public.
For more information, check out the Ethereum DAO Documentation.
The backbone of a DAO is its smart contract, which defines the rules of the organization and holds the group's treasury. Here are the key functions or steps involved in a DAO:
-
Smart Contract Deployment: The first step in creating a DAO is to deploy a smart contract on the Ethereum blockchain. This contract defines the rules of the organization and holds the group's treasury.
-
Membership: There are different models for DAO membership. Membership can determine how voting works and other key parts of the DAO. It can be token-based, share-based, or reputation-based.
-
Voting and Proposals: DAOs operate on a consensus mechanism, which means that decisions are made based on the votes of the members. Proposals are put forward and members vote to decide whether they should be implemented.
-
Delegation: Some DAOs operate on a delegation system, where members can delegate their voting power to other members.
-
Automatic Transaction Governance: In many DAOs, transactions will be automatically executed if a quorum of members votes affirmative.
-
Multisig Governance: Funds can live in a wallet shared by active community members who are trusted and usually doxxed (public identities known to the community). After a vote, the multisig signers execute the will of the community.
-
DAO Laws: Some jurisdictions have laws that establish legal status for DAOs. This can provide additional legal protection for DAOs.
For more information, check out the Ethereum DAO Documentation.
There are several platforms that you can use to create and manage your own DAO. Here are some of the most popular ones:
Aragon is an open-source project that provides the tools and infrastructure necessary for creating and managing decentralized organizations. It offers a highly customizable platform for DAOs, with features like token management, voting, and governance.
Example of creating a DAO with Aragon:
// Install the Aragon CLI
npm install -g @aragon/cli
// Create a new DAO
aragon dao new
For more information, check out the Aragon Documentation.
DAOstack is a complete operating system for decentralized coordination. It provides the foundational tools for the creation and management of DAOs, including a friendly JavaScript developer environment and an intuitive user interface.
Example of creating a DAO with DAOstack:
// Install the DAOstack CLI
npm install -g @daostack/migration
// Create a new DAO
daostack migrate new
For more information, check out the DAOstack Documentation.
Colony is a platform for creating decentralized organizations, with a focus on collaboration and meritocracy. It provides a suite of smart contracts and a JavaScript library for building collaborative applications.
Example of creating a DAO with Colony:
// Install the ColonyJS library
npm install @colony/colony-js
// Create a new colony
const colonyClient = await colonyNetworkClient.getColonyClient(colonyId);
For more information, check out the Colony Documentation.
DAOhaus is a platform for summoning Moloch DAOs. It provides an easy-to-use interface for creating and managing DAOs, with features like proposal creation, voting, and member management.
Example of creating a DAO with DAOhaus:
// Visit the DAOhaus website
// Click on 'Summon a DAO'
// Follow the instructions to create your DAO
For more information, check out the DAOhaus Documentation.
Remember, the choice of platform depends on the specific needs and goals of your DAO. It's important to research each platform and understand their strengths and weaknesses before making a decision.
Security is a critical aspect of smart contract development. Here are some key points to consider:
Each opcode supported by the EVM has an associated gas cost. These costs are not arbitrary; they're meant to reflect the underlying resources consumed by each operation on the nodes that make up Ethereum. However, these costs can change, and smart contracts can't depend on any particular gas costs.
Solidity's transfer()
and send()
methods were introduced to help guard against reentrancy attacks. However, they take a hard dependency on gas costs by forwarding a fixed amount of gas: 2300. As gas costs can change, it's recommended to avoid using these methods and switch to using call()
instead.
Reentrancy attacks are a common security issue in smart contracts. They occur when a contract's function is invoked while it's in the middle of its execution, leading to unexpected behavior. To protect against reentrancy, you can use the checks-effects-interactions pattern or a reentrancy guard.
The checks-effects-interactions pattern is a coding pattern that can help eliminate reentrancy bugs. The idea is to make sure that all your interactions (external calls) happen at the end of your function.
A reentrancy guard is a mechanism that explicitly checks for and rejects reentrant calls. OpenZeppelin's ReentrancyGuard
contract is a good example of this.
For more information, check out the ConsenSys Diligence Blog.
Testing and deployment are crucial stages in smart contract development. Here are some key points to consider:
When it comes to smart contract development, practice has shown that contract unit testing is exceptionally worthwhile. These tests are simple to write and quick to run, and let you add features and fix bugs in your code with confidence.
Smart contract unit testing consists of multiple small, focused tests, which each check a small part of your contract for correctness. They can often be expressed in single sentences that make up a specification, such as 'the admin is able to pause the contract', 'transferring tokens emits an event' or 'non-admins cannot mint new tokens'.
We’ll use Chai assertions for our unit tests. We will keep our test files in a test directory. Tests are best structured by mirroring the contracts directory: for each .sol file there, create a corresponding test file.
Many interesting properties of your contracts may be hard to capture, such as verifying that the contract reverts on errors, measuring by how much an account’s Ether balance changed, or checking that the proper events are emitted.
OpenZeppelin Test Helpers is a library designed to help you test all of these properties. It will also simplify the tasks of simulating time passing on the blockchain and handling very large numbers.
For more information, check out the OpenZeppelin Docs.
After you've mastered the basics of smart contract development, you might want to consider further learning opportunities to deepen your knowledge and skills. Here are some resources you might find useful:
The flagship ConsenSys developer program is offered in an online bootcamp format and provides access to mentors and course creators, community-based peer support, networking opportunities, as well as self-paced learning materials, multi-modal content, interactive exercises, assignments, and hands-on projects. The course is administered in English and costs 985 USD, which includes certification.
The Bootcamp is a 10-12 week online course (varies depending on your personal pace of completion). It requires 10-15 hours per week. The course has multi-modal content (videos, interactive exercises, assignments, and hands-on projects), mentors, community-based peer support, and networking opportunities.
This course is suitable for experienced and passionate Object-Oriented Programmers, with at least 1 year of programming experience (full-time developers and students welcome). You should be familiar with general web development, JavaScript, HTML, and Git, and be passionate about becoming a top-notch Blockchain developer.
For more information, check out the ConsenSys Academy Bootcamp.