0xB387
June 27th, 2022

If you've been dipping your toes in the awesome Rust language, you must've encountered the clone() method which is present in almost every object out there to make a deep copy of it. It comes from the implementation of Clone trait for a struct.

But, you must also have encountered this Copy trait if you ventured a bit into docs deeper or in an error message. Copy also allows some type to replicate its instances. So, what's the deal here with Clone and Copy traits? That is exactly what this article is about.

Ownership in Rust

I won't delve too deep into ownership here, but this is what we need to know to get the gist of this article:

0xB387
April 29th, 2022

In the previous part (Part 1) we understood some facts about solidity compiler and wrote the Box contract's functions using inline assembly i.e. in Yul. This part would guide you to writing the Box contract in pure assembly. As a reminder here is the Box contract with inline assembly from previous part:

pragma solidity ^0.8.7;

contract Box {
    uint256 public value;

    function retrieve() public view returns(uint256) {
        assembly {
            // load value at slot 0 of storage
            let v := sload(0) 

            // store into memory at 0
            mstore(0x80, v)

            // return value at 0 memory address of size 32 bytes
            return(0x80, 32) 
        }
    }

    function store(uint256 newValue) public {
        assembly {
            // store value at slot 0 of storage
            sstore(0, newValue)

            // emit event
            mstore(0x80, newValue)
            log1(0x80, 0x20,0xac3e966f295f2d5312f973dc6d42f30a6dc1c1f76ab8ee91cc8ca5dad1fa60fd)
        }
    }
}

Prelude

You've already had a taste of assembly with inline assembly. We're going to use the body of the functions similarly using opcodes to perform operations. Note that when Box will be purely written in Yul, it will no longer be a Solidity contract. The solidity compiler only recognizes the inline assembly with the assembly { } blocks. So, if you're writing it on Remix IDE, be sure to select Yul compiler.

0xB387
April 28th, 2022

In the previous part (Part 0) we covered some pre-requisite knowledge of EVM. In this part, we will write the Box contract partially in assembly - the function bodies of retrieve() and store(). Here is our Box contract in Solidity:

pragma solidity ^0.8.7;

contract Box {
    uint256 private _value;

    event NewValue(uint256 newValue);

    function store(uint256 newValue) public {
        _value = newValue;
        emit NewValue(newValue);
    }

    function retrieve() public view returns (uint256) {
        return _value;
    }
}

You can interleave Solidity statements with assembly { } blocks and can write inline assembly within that code block. As mentioned previously language used for assembly for EVM is Yul.

0xB387
April 27th, 2022

Solidity is by far the most used language for smart contracts on Ethereum blockchain. It is a high-level language that abstracts away various underlying nitty-gritty details (like several important safety checks) - and that is for good!

However, sometimes however you may want to go deep down to fine-grain controls of EVM whether it is for gas golfing, writing optimized libraries, or maybe some other witch-craft, who knows. Luckily, you can actually write part of your contract (or whole contract!) assembly language.

The language used for assembly here is called Yul which is your portal to access EVM's fine-grain controls. Be warned - writing assembly bypasses many important checks of Solidity. You should only use it when needed and if you know what the heck is going on!

Before you proceed, I assume you know basics of Ethereum and Solidity language, already. Alright, let's go!

0xB387
March 22nd, 2022

Before you move further I assume you already of some Solidity experience and know how storage slots work in any contract.

Why Proxies anyway?

If you have following blockchain and smart-contracts stuff you must've come across the word - "immutable". Well, the smart-contracts are immutable. Yes, you cannot tweak any functionality of the contract at that address. You can only interact with it. That's it and it is for the better of it! Otherwise, it wouldn't be so "trustable" if say one day someone in control suddenly dictates the rules in contract in their favor! This is a stark difference from traditional systems where fixes are pushed everyday.

0xB387
February 15th, 2022

Before you continue, I'm assuming you know what The Graph is and what problems it tackles. If you haven't, I highly recommend checking out - Why The Graph.

Installation

First of all, you'll need Graph CLI tool. Install it via:

yarn

0xB387
February 12th, 2022

I'm assuming the reader of this article has prior experience in writing basic smart contracts, at minimum. If not, it's a great time to start.

What actually is The Graph?

From the official docs:

The Graph is a decentralized protocol for indexing and querying data from blockchains, starting with Ethereum. It makes it possible to query data that is difficult to query directly.

0xB387
January 13th, 2022

Tokens are arguably one of the most powerful tools in the blockchain right now. A token can be a representation of anything on-chain — be it voting weight, shares in the company, or virtual assets (aka NFTs). A smart contract stores who has what token and how much of it. It also takes care of the business logic of transfers, trades, loans, etc. Tokens are nothing but a mapping of what token belongs to what address (or account) inside the smart contract. So, when you transfer your token to someone else, that mapping changes so that the traded token maps to the receiver’s address.

The beauty of the token representation is that tokens can be transferred/traded from one user to another without the need of any third party, acting as middleman or broker, moderating that everything is fair (given we “trust” the third party) & chopping off a nice amount from that trade as a fee. Ah! the token economy! But how can we guarantee everything will be fair without a trusted third party? That is what Smart Contracts ensure.

Smart Contracts

But, first, let’s imagine a scenario to get the essence of it. Consider you want to purchase & have ownership of a piece of land to build your home sweet home. Now, you have two options -