-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockchain.html
303 lines (273 loc) · 24 KB
/
blockchain.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Blockchain Cheatsheet</title>
<!-- Bootstrap CDN -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<!-- CSS -->
<link rel="stylesheet" href="assets/style.css">
</head>
<body data-theme="light">
<div class="container">
<!-- Theme Toggle -->
<div class="button float-end"><i class="bi bi-circle-half"></i></div>
<h3 class="title">Blockchain</h3>
<div class="section-list">
<ul>
<li><a href="#blockchain">Blockchain</a></li>
<li><a href="#blockchain">Types of Blockchain</a></li>
<li><a href="#smart-contracts">Smart Contracts</a></li>
<li><a href="#consensus-mechanism">Consensus Mechanism</a></li>
<li><a href="#tokens">Tokens</a></li>
<li><a href="#ethereum">Ethereum</a></li>
<li><a href="#truffle">Truffle</a></li>
<li><a href="#hardhat">Hardhat</a></li>
</ul>
</div>
<div class="sections" id="blockchain">
<h4 class="heading"># Blockchain</h4>
<div class="content">
<p>A blockchain is a digitized, decentralized ledger of transactions. Blockchains record a continuously growing list of records, called blocks, which are linked and secured using cryptography.</p>
<ul class="justify">
<li>A blockchain is a distributed database that maintains a continuously growing list of records, called blocks, secured from tampering and revision.</li>
<li>Each block contains a timestamp and a link to a previous block.</li>
<li>A blockchain is typically managed by a peer-to-peer network collectively adhering to a protocol for validating new blocks.</li>
<li>Once recorded, information cannot be altered without the alteration of all subsequent blocks.</li>
</ul>
<h5>• Genesis Block :</h5>
<p>The Genesis Block, also known as Block 0, is the very first block upon which additional blocks in a blockchain are added. It is effectively the ancestor that every other block can trace its lineage back to since every block references the one preceding it. The Genesis Block is a critical element of a blockchain network because it serves as the foundation for the entire network.</p>
<h5>• Blockchain Explorer :</h5>
<p>A blockchain explorer is a platform that allows people to search and navigate a blockchain. They're not necessary for using the blockchain. However, they offer a valuable service to those who wish to inspect a blockchain in detail. You can view transactions on the blockchain, smart contract code, account balances, and more.</p>
</div>
</div>
<div class="sections" id="blockchain-type">
<h4 class="heading"># Types of Blockchain</h4>
<div class="content">
<ul class="justify">
<li>
<h6>Public :</h6>
<ul>
<li>Anyone is allowed to join and participate in the consensus.</li>
<li>Fully decentralized, secured and immutable ledge system.</li>
<li>Transactions are anonymous but transparent to everyone.</li>
</ul>
</li>
<li>
<h6>Private :</h6>
<ul>
<li> A single organization will have authority over the network.</li>
<li> Privacy, faster output and power efficient.</li>
<li> Simplified data handling process but not open to everyone.</li>
</ul>
</li>
<li>
<h6>Federated or Consortium :</h6>
<ul>
<li>Multiple organizations influences the blockchain newtork.</li>
<li>Decentralized, extremely fast and scalable system.</li>
<li>Network regulations preserves security and privacy.</li>
</ul>
</li>
<li>
<h6>Hybrid :</h6>
<ul>
<li>Authorative access, only certain elements are private.</li>
<li>Flexible controll over what data is kept public and private.</li>
<li>Decentralized regulated and highly scalable system.</li>
</ul>
</li>
</ul>
</div>
</div>
<div class="sections" id="smart-contracts">
<h4 class="heading"># Smart Contracts</h4>
<div class="content">
<p>Smart contracts are self-executing contracts with predefined rules and conditions encoded on a blockchain. They are written in programming languages such as Solidity (for Ethereum) or Vyper and operate autonomously without the need for intermediaries.</p>
<h5>• Web3.JS :</h5>
<p>Web3.JS is a popular JavaScript library that allows you to interact with the Ethereum network (and smart contracts). If you want to build an application, you can use Web3.JS to connect to the Ethereum blockchain and make the front-end of your application.</p>
<h5>• ABI Bytecode :</h5>
<p>ABI stands for Application Binary Interface. The ABI is typically provided as a JSON (JavaScript Object Notation) file or an ABI-encoded string. Smart contracts are written in high-level languages like Solidity and compiled into bytecode, which is then deployed on the blockchain or Ethereum Virtual Machine (EVM) for execution. The ABI Array contains all functions, inputs, and outputs, as well as all variables and their types, from a smart contract. Inside, there are assembly opcodes telling the EVM during execution of a smart contract where to jump to. Those jump destinations are the first 4 bytes of the keccak hash of the function signature.</p>
</div>
</div>
<div class="sections" id="consensus-mechanism">
<h4 class="heading"># Consensus Mechanism</h4>
<div class="content">
<p>Blockchain networks rely on a consensus mechanism to achieve agreement among nodes about the validity of transactions and the order in which they are added to the blockchain. Common consensus mechanisms include Proof of Work (PoW), Proof of Stake (PoS), and Practical Byzantine Fault Tolerance (PBFT).</p>
<h5>• Proof of Work (PoW) :</h5>
<p>In a PoW-based blockchain network, miners compete to solve a complex mathematical puzzle or algorithm. The process of solving this puzzle is called mining.</p>
<h5>• Proof of Stake (PoS) :</h5>
<p>In a PoS-based blockchain network, participants are called validators. Validators are chosen to create new blocks and validate transactions based on the amount of cryptocurrency they hold and "stake" in the network.</p>
<h6>PoW and PoS :</h6>
<p>In a PoW system, miners compete to find a solution to an equation that will allow them to add a block to the blockchain. In a PoS system, validators stake their cryptocurrency on the chain they want to mine. The more they stake, the more likely they will solve the next block. When they reach a consensus on a block, they get rewarded with more of the cryptocurrency. Critics argue that PoS may lead to centralization.</p>
</div>
</div>
<div class="sections" id="tokens">
<h4 class="heading"># Tokens</h4>
<div class="content">
<p>A token refers to a digital asset or a unit of value that is created, issued, and transferred within a blockchain system. Tokens can represent various types of assets, such as cryptocurrencies, utility tokens, security tokens, or even non-fungible tokens (NFTs).</p>
<h5>• Fungible Tokens :</h5>
<p>A fungible token is a type of digital asset that is interchangeable with other tokens of the same type. Fungible tokens are identical, non-unique and indistinguishable from one another, meaning that each token holds the same value and can be exchanged on a one-to-one basis.</p>
<ul>
<li>Non-Unique (Uniform)</li>
<li>Interchangeable</li>
<li>Divisible</li>
<li>ERC-20 or ERC-777 Standard</li>
</ul>
<h5>• Non-Fungible Tokens (NFT) :</h5>
<p>Non-Fungible Tokens are unique digital assets that represent ownership or proof of authenticity for a specific item or piece of content. NFTs have gained popularity in digital art, collectibles, virtual real estate, and gaming. Each NFT has a distinct value and cannot be exchanged on a one-to-one basis like cryptocurrencies. One token may be similar to another, but they have properties that ensure no two tokens can ever be the same.</p>
<ul>
<li>Unique</li>
<li>Non-Interchangeable</li>
<li>Non-Divisible</li>
<li>ERC-721 Standard</li>
</ul>
</div>
</div>
<div class="sections" id="ethereum">
<h4 class="heading"># Ethereum</h4>
<div class="content">
<p>Ethereum is the blockchain platform that enables the creation and execution of smart contracts and decentralized applications (dApps).</p>
<h5>• ERC-20 :</h5>
<p>ERC-20 stands for Ethereum Request for Comment 20, which is a technical standard or protocol used for creating and implementing fungible tokens on the Ethereum blockchain. It defines a set of rules and standards that a token contract must follow in order to be considered an ERC-20 token. ERC-20 allows developers to create smart-contract-enabled tokens that can be used with other products and services.</p>
<h6>ERC-20 Contents :</h6>
<p>ERC-20 is a list of functions and events that must be implemented into a token for it to be considered ERC-20 compliant. These functions (called methods in the ERC) describe what must be included in the smart-contract-enabled token, while events describe an action.</p>
<h6>The functions a token must have are -</h6>
<ul class="justify">
<li><b><code>TotalSupply</code> :</b> The total number of tokens that will ever be issued.</li>
<li><b><code>BalanceOf</code> :</b> The account balance of a token owner's account.</li>
<li><b><code>Transfer</code> :</b> Automatically executes transfers of a specified number of tokens to a specified address for transactions using the token.</li>
<li><b><code>TransferFrom</code> :</b> Automatically executes transfers of a specified number of tokens from a specified address using the token.</li>
<li><b><code>Approve</code> :</b> Allows a spender to withdraw a set number of tokens from a specified account, up to a specific amount.</li>
<li><b><code>Allowance</code> :</b> Returns a set number of tokens from a spender to the owner.</li>
</ul>
<h6>The events that must be included in the token are -</h6>
<ul class="justify">
<li><b><code>Transfer</code> :</b> An event triggered when a transfer is successful.</li>
<li><b><code>Approval</code> :</b> A log of an approved event (an event).</li>
</ul>
<h6>ETH vs ERC-20 :</h6>
<p>Ether (ETH) is the native token used by the Ethereum blockchain and network as a payment system for verifying transactions. ERC-20 is the standard for creating smart contract-enabled fungible tokens to be used in the Ethereum ecosystem.</p>
</div>
</div>
<div class="sections" id="truffle">
<h4 class="heading"># Truffle</h4>
<div class="content">
<p>Truffle is a development framework for Ethereum; It is used to streamline the process of building, testing, and deploying smart contracts on the Ethereum network. Truffle provides a suite of tools and libraries that aid in the development and management of Ethereum-based decentralized applications (dApps).</p>
<ul class="justify">
<li>Truffle downloads a solidity compiler in the background (the version is defined in truffle-config).</li>
<li>Then it looks in the contracts folder for all solidity files and runs them through the solidity compiler.</li>
<li>From the compiled contracts it creates json-artifacts, which contain not only the ABI, but also the bytecode and the AST and much more.</li>
<li>When we deploy the code using truffle migrations, it will also contain the contract address on a specific network.</li>
</ul>
<h6>Truffle Folder Structure :</h6>
<ul class="justify">
<li><b><code>Contracts</code> :</b> This directory contains the Solidity smart contracts for your project. It usually has a separate file for each contract, along with any libraries or interfaces.</li>
<li><b><code>Migrations</code> :</b> The migrations folder contains JavaScript files that handle deploying and updating your smart contracts to the Ethereum network. Each migration script represents a deployment or an update, and they are executed in order.</li>
<li><b><code>Test</code> :</b> This directory is used for writing automated tests for your smart contracts. It usually contains JavaScript or Solidity test files that ensure your contracts behave as expected.</li>
<li><b><code>Truffle Config</code> :</b> The truffle-config.js (or truffle.js) file holds the configuration settings for your Truffle project. It specifies the network settings, compiler version, and other project-specific options.</li>
<li><b><code>Build</code> :</b> The build folder is automatically generated by Truffle and contains the compiled contracts and build artifacts. It includes the ABI (Application Binary Interface) JSON files, bytecode, and other necessary information for interacting with your contracts.</li>
<li><b><code>App</code> (Optional) :</b> If you are building a decentralized application (DApp), you may have an "app" or "client" folder that contains the frontend code (HTML, CSS, JavaScript, etc.) for your DApp. This folder is not specific to Truffle but is commonly included in Truffle projects for convenience.</li>
</ul>
<h6>Truffle Commands :</h6>
<ul class="justify">
<li><b><code>truffle init</code> :</b> Initializes a new Truffle project by creating the necessary directory structure and configuration files.</li>
<li><b><code>truffle compile</code> :</b> Compiles the smart contracts written in Solidity language into bytecode that can be executed on the Ethereum Virtual Machine (EVM). It generates artifacts refer to the JSON files generated during the compilation process. These artifact files contain metadata and information about the compiled smart contracts, including their bytecode, application binary interface (ABI), and other details.</li>
<li><b><code>truffle migrate</code> :</b> Deploys the compiled smart contracts to a specified Ethereum network. It uses migration scripts to manage the deployment process.</li>
<li><b><code>truffle test</code> :</b> Runs the test cases written for your smart contracts using a built-in testing framework (usually Mocha and Chai). It helps ensure that your contracts behave as expected.</li>
<li><b><code>truffle console</code> :</b> Opens a Truffle console, which is an interactive JavaScript environment connected to an Ethereum network. It allows you to interact with deployed contracts and test their functionality manually.</li>
<li><b><code>truffle develop</code> :</b> Starts a local development blockchain, also known as the Truffle Develop Network. It provides a pre-configured blockchain environment for rapid contract development and testing.</li>
<li><b><code>truffle networks</code> :</b> Lists the available network configurations defined in the Truffle project's configuration file. You can define multiple networks for different Ethereum networks like mainnet, ropsten, rinkeby, etc.</li>
<li><b><code>truffle create</code> :</b> Creates new contract, migration, test, or other files/templates within the Truffle project's directory structure.</li>
<li><b><code>truffle build</code> :</b> Compiles and generates an optimized build of your DApp for production deployment. It typically creates a bundled version of your frontend code and deploys it to an appropriate location.</li>
</ul>
<h5>• Ganache :</h5>
<p>Truffle Ganache is a local blockchain development tool that provides a personal Ethereum blockchain for development and testing purposes. It is part of the Truffle suite. Ganache allows developers to create a local Ethereum network with predefined accounts and pre-funded Ether. This allows you to simulate a blockchain environment on your local machine without the need to connect to a public or test network.</p>
<h6>Deploy smart contracts on ganache (cli) -</h6>
<ul>
<li>Start ganache: <b><code>ganache</code></b></li>
<li>Migrate in new terminal: <b><code>truffle migrate --network ganache</code></b></li>
</ul>
</div>
</div>
<div class="sections" id="hardhat">
<h4 class="heading"># Hardhat</h4>
<div class="content">
<p>Hardhat is a development environment for Ethereum smart contracts. It is a popular alternative to Truffle and provides a comprehensive toolset for compiling, testing, and deploying smart contracts.</p>
<ul class="justify">
<li>
<h6>Set Up Project :</h6>
<ul>
<li>Create a new directory for project.</li>
<li>Run <b><code>npm init</code></b> to initialize a new Node.js project (if haven't already).</li>
<li>Install Hardhat by running: <b><code>npm install --save-dev hardhat</code></b>.</li>
</ul>
</li>
<li>
<h6>Configure Hardhat :</h6>
<ul>
<li>Create a <b><code>hardhat.config.js</code></b> file in the root of project directory.</li>
<li>In the configuration file, we can specify the network settings, compiler version, plugins, and other options.</li>
</ul>
</li>
<li>
<h6>Write Smart Contracts :</h6>
<ul>
<li>Create a contracts directory in project and write Solidity smart contracts in separate <b><code>.sol</code></b> files.</li>
<li>We can import other contracts, libraries, or interfaces as needed.</li>
</ul>
</li>
<li>
<h6>Write Tests :</h6>
<ul>
<li>Create a test directory in project.</li>
<li>Write automated tests for contracts using the testing framework (e.g., Mocha or Hardhat's built-in testing framework).</li>
</ul>
</li>
<li>
<h6>Compile Contracts :</h6>
<ul>
<li>Run <b><code>npx hardhat compile</code></b> to compile Solidity contracts.</li>
<li>The compiled contracts and build artifacts will be generated in a build directory.</li>
</ul>
</li>
<li>
<h6>Run Tests :</h6>
<ul>
<li>Execute test scripts to ensure that contracts behave as expected.</li>
<li>Run <b><code>npx hardhat test</code></b> to run the tests located in the test directory.</li>
<li>Hardhat will automatically deploy the contracts to its built-in local network for testing.</li>
</ul>
</li>
<li>
<h6>Deploy Contracts :</h6>
<ul>
<li>Define deployment scripts in the scripts directory or create a new script specifically for deploying contracts.</li>
<li>Use the Hardhat API to deploy contracts to a specific network or the local development network.</li>
<li>Run the deployment script using: <b><code>npx hardhat run <script-name></code></b>.</li>
<li>Deploy on hardhat development node with: <b><code>npx hardhat node</code></b> (starts local ethereum node i.e. hardhat Network).</li>
<li>Run scripts: <b><code>npx hardhat run --network localhost scripts/deploy.js</code></b></li>
</ul>
</li>
<li>
<h6>Interact With Contracts :</h6>
<ul>
<li>Use the Hardhat console or write JavaScript/TypeScript scripts to interact with deployed contracts.</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Navigation Buttons -->
<nav>
<a href="https://itspatkar.github.io/Cheatsheets/"><i class="bi bi-arrow-left-square-fill"></i></a>
<a href="#" class="float-end"><i class="bi bi-arrow-up-square-fill"></i></a>
</nav>
</div>
<!-- JavaScript -->
<script src="assets/script.js"></script>
</body>
</html>