So, you want to create a blockchain product?
Okay fine! Let's start with a definition of the blockchain itself, now, I'm sure you either already know all of this, either through your own self-study, or during your time in the Blockchain conference, but I'll try and cover as much as possible. Please feel free to reach out on anything you would like more information on.
What is a "blockchain"?
A blockchain is a distributed database or ledger shared among a network of nodes (computers).
That's it.
They are best known for their crucial role in cryptocurrency systems for maintaining a secure and decentralised record of transactions, but they are not limited to cryptocurrency uses. The main intention is to make data immutable (the general tech term to describe something with the inability to be altered).
Because the is supposed to be no way to change a block, the only place where trust is needed is at the point where a user or program enters data. This aspect reduced the need for trusted third-parties (and is also in my opinion the whole point of blockchain technology, trustlessness), which are usually auditors or other humans that add costs or mistakes.
Okay, but how does a blockchain "work"?
I'm sure that you are familiar with spreadsheets (or other types of databases). A blockchain is the same, because it is a place where data is structured, stored and and accessed.
A blockchain consists of programs called scripts that conduct the tasks you usually would in a database: Entering and accessing information and saving and storing it somewhere. A (colloquial) blockchain is distributed, which means multiple copies are saved on many machines, and they must all match for it to be valid.
The blockchain collects transaction information and enters it into a (usually) 4MB file called a block. Once it is full, certain information is run through an encryption algorithm, which creates a hexadecimal number called the block header hash.
The hash is then entered into the following block header and encrypted with the other information in that block's header, creating a chain of blocks.
Did you say "transacti-
Way ahead of you!
Transactions in Ethereum are cryptographically signed data messages that contain a set of instructions. These instructions can interpret to sending Ether from one Ethereum account to another or interacting with a smart contract deployed on the blockchain. Transactions are a simple but powerful concept that has allowed users worldwide to interact on a decentralised network.
Transactions can get pretty technical, and there's no real need to go too in depth before you decide on a blockchain, but I'll share these few things that are relevant to the Etherium blockchain running the Etherium Virtual Machine (EVM).
Accounts
There are two types of accounts, smart contract accounts and externally owned accounts (EOA):
-
Externally owned accounts (EOA) refer to accounts that humans manage, such as a personal Metamask or Coinbase wallet. This account is identified by a public key (also known as an account address) and is controlled by a private key. The public key is derived from the private key using a cryptographic algorithm. It's important to note that these accounts cannot store information other than your accounts balance and nonce.
-
Smart contract accounts (also known as contract accounts) also contain an address to balance mapping but differ because they can also include EVM code and storage. Contract accounts control themselves by the logic in the EVM code stored within the account.
Ethereum utilises the elliptic curve digital signature algorithm (ECDSA) to prove authentication (i.e., prove that we have a private key for our public address) and verify that our transaction comes from the account signing the transaction and is not fraudulent.
Types of Transactions
Let us tie the information we just learned about accounts with the different types of transactions:
- Message call transaction: A message call derives from an externally owned account that wants to interact with another EOA or contract account. An example of a message call would be sending Ether from one account to another, or interacting with a smart contract (e.g, swapping tokens on Uniswap).
- Contract creation transaction: A contract creation derives from an EOA to create a smart contract account (generally to store code and storage). An example of this type of transaction would be deploying a storage smart contract to store data.
Transaction States
- Pending: Transactions broadcasted to the network waiting to be mined. If the transaction is taking longer than expected, it's possible that your gas fee is not high enough to meet execution at the current time.
- Queued: A transaction that cannot be mined yet due to another pending transaction in the queue first or an out of sequence nonce.
- Cancelled: Can no longer be mined. Replaced by a transaction with a higher gas fee, same nonce value, and a null value for the data and/or value field.
- Replaced: Can no longer be mined. Used to replace current pending orders for faster execution or modification of values and data. This also consists of using the same nonce as the transaction you want to cancel and a higher gas fee.
- Failed: A transaction that resulted in an error due to a revert error, bad instructions, illogical code, or not enough gas to run the remainder of a function call.
Smart Contracts
A smart contract is computer code that can be built into the blockchain to facilitate transactions. It operates under a set of conditions to which users agree. When those conditions are met, the smart contract conducts the transaction for the users.
Data Storage
Another significant implication of blockchains is that they require storage. This may not appear to be substantial because we already store lots of information and data. However, as time passes, the number of growing blockchain uses will require more storage, especially on blockchains where nodes store the entire chain.
Currently, data storage is centralised in large centres. But if the world transitions to blockchain for every industry and use, its exponentially growing size would mean more advanced techniques to reduce its size or that any participants would need to continually upgrade their storage.
This could become significantly expensive in terms of both money and physical space needed, as the Bitcoin blockchain itself was more than 575 gigabytes on June 14, 2024—and this blockchain records only bitcoin transactions. This is small compared to the amount of data stored in large data centres, but a growing number of blockchains will only add to the amount of storage already required for the connected and digital world.
Wait, if the blockchain is immutable, does that mean that things don't change ever?
Yes, and no. What is immutable are the transactions on the blockchain, you don't get to change the values of the existing transactions, but what you can do is create a new transaction, that can change the value.
huh?
Okay, let's give an example:
Stephanie creates a wallet
great, we both have wallet addresses now, however, we have done nothing on the blockchain just yet. But you do have some state to observe.
We look at your wallet value and we will see that your balance is zero:
Stephanie wallet value: 0u
Okay, so let's fund your wallet
Stephanie buys 10u from an on-ramp platform.
Transaction:
type: message_call
from: Onramp platform
to: Stephanie
value: 10u
Nice!
so if we look at what happened, you made a purchase with a onramp platform much like Coinbase, and they transfered 10u
from their wallet to yours. This is a transaction, and this transaction is how stored values are changed!
What about smart contracts?
Okay, let's look at these!
Contracts in Solidity are similar to classes in object-oriented languages. Each contract can contain declarations of State Variables, Functions, Function Modifiers, Events, Struct Types and Enum Types. Furthermore, contracts can inherit from other contracts. They can even create other contracts! 👀
I'm going to create a basic smart contract based on solidity EVM:
// Specifies the version of Solidity for this smart contract.
pragma solidity >=0.7.3;
contract HelloStephanie {
// Declares a state variable `message` of type `string`.
// State variables are variables whose values are permanently stored in contract storage. The keyword `public` makes variables accessible from outside a contract and creates a function that other contracts or clients can call to access the value.
string public message;
// Declares a variable for the contract owner, the lack of "public" keyword means that this variable is not accessible from outside the contractr.
address owner;
TokenCreator creator;
// This is the constructor that is run when the smart contract is initialised, it will register the creator and the initial message.
constructor(string memory initMessage) {
// Accepts a string argument `initMessage` and sets the value into the contract's `message` storage variable.
message = initMessage;
// sets the value of the address of the smart contract owner, this wallet will have admin permissions over this smart contract (this should be changed when transferring the smart contract)
owner = msg.sender;
// The token creator should never change, could be used to ensure royalties are gathered when withdrawing any value from the contract. (Yes, smart contracts can hold onto funds)
creator = TokenCreator(msg.sender);
}
//Emitted when update function is called
//Smart contract events are a way for your contract to communicate that something happened on the blockchain to your app front-end, which can be 'listening' for certain events and take action when they happen.
event UpdatedMessages(string oldStr, string newStr);
// A public function that accepts a string argument and updates the `message` storage variable.
function changeMessage(string memory newMessage) public {
// if you aren't the current owner of this smart contract, you cannot change the value of the message, so we exit early.
if (msg.sender != owner) return;
string memory oldMsg = message;
message = newMessage;
emit UpdatedMessages(oldMsg, newMessage);
}
}
But the above would be the source code to create a smart contract that, when deployed would provide a message to anyone that wishes to look at it, for free!
But only one account can make changes to them. If you want to see an example of a production level smart contract, let's look at the Bored Ape Yacht Club (BAYC) smart contract: found here Clicking that link, will take you directly to the source code for the contract, available for all to see!
But that's not the important thing, if you click on the read contract
tab, you will be able to see all of the public
variables which are all "immutable" unless you allow them to be updated via another transaction.
(It is common practice to enable a way to "freeze" a contract once all the values are finalised, with similar logic to how we checked for the contract owner)
e.g.
pragma solidity >=0.7.3;
contract CheckFrozen {
bool public isFrozen;
string public message;
address owner;
TokenCreator creator;
constructor(string memory initMessage) {
message = initMessage;
owner = msg.sender;
creator = TokenCreator(msg.sender);
}
function freezeContract() public {
// can only be ran once. Cannot be undone.
if (isFrozen == true) return;
if (msg.sender != owner) return;
isFrozen = true;
}
function changeMessage(string memory newMessage) public {
if (isFrozen == true) return;
if (msg.sender != owner) return;
string memory oldMsg = message;
message = newMessage;
}
}
Take note, that every interaction with a smart contract is a transaction
and therefore will have a transaction fee!
Okay, what else is there?
I mean, I think that we have covered everything already to get you at the level of the base Web3 developer/project owner.
Other topics to look into, considering the project that you are working on:
- Real World Events triggering a smart contract
- What are the nature of the data that needs to be stored? Do you need Web3 file storage systems? (for things like PDFs?) please take note, that there isn't any real privacy on the blockchain, and if the files are added onto the blockchain, there is no encryption that will endure a concentrated attack attempt for very long. So making careful use of private variables is essential!
- IPFS is a distributed file system designed to help store and access data across a peer-to-peer network. IPFS empowers developers to store, timestamp, and secure large files without having to put the data itself on-chain.
- Arweave takes the idea of decentralized file storage further by ensuring the permanence of data. Arweave is building a permaweb, managed by a global community of users and developers who are incentivized to maintain the data storage layer.
- Gaia is a decentralized storage platform available to developers building apps on Stacks. The transactional metadata of the apps is stored on the blockchain itself, while user application data is stored in Gaia to ensure that users enjoy high performance and availability.
- Security, for the things that you are trying to so, a high level of assurance and security may be essential. Blockchain will only be as good as the people designing it. And once something exists, even if you make a new transaction to update it please, be aware that the prior version that is potentially compromised will forever exist on the blockchain.