Notebook of solidity

Disclaimer for Visitors!

This notebook is still in progress. After pasting anything from here, do double check on what you are about to be triggered at your end. Don’t use mainnet assets to try out codes. You are accepting that I’m not responsible about anything that you used from this page.

Resources

Overriding

re-program the functions inherited from other contracts can be modified in the eventual contract and it is called overriding.

Example of disabling an inherited function:

contract Example is ERC20, ERC20FlashMint {
    // ExampleToken is getting derived from ERC20 and ERC20FlashMint
    constructor(uint256 initialSupply) ERC20("Exmple", "EXP") {
        _mint(msg.sender, initialSupply);
    }
    // Redefining flash load fee by modifying the amount variable as desired.
    function flashFee(address token, uint256 amount) public view virtual override returns (uint256) {
        require(token == address(this), "ERC20FlashMint: wrong token");
        amount += 0.1 * 10^18;
        return 0;
    }
}

Pure Functions

pure functions doesn’t return a value but a calculation, and the most important reason they exist is that don’t make a change on the blockchain, means the calculation they do occurs with eth_call procedure.

EIP165 Standard

Creates an identifier between the contract and the interfaces being used in to check whether the versions match with your contract.

Testing

Tesing in JavaScript

Checkout mocha.js based javascript testing example in Web3js Truffle Practices

Testing in Solidity

#TBC …

Unchecked

Reverts

unchecked { return 1 – 2; }


Library

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

library LibraryName {
    // find number inside of an array
    function find(uint[] storage arr, uint x) internal view returns(uint) {
        for (uint i = 0; i < arr.length; i++) {
            if (arr[i] == x) {
                return i;
            }
            revert("not found");
        }
    }
}

contract A {
    // Method 2: 
    using LibraryName for uint[];

    uint[] public arr = [3,5,7];

    function findNumber() external view returns(uint) {
        //return LibraryName.find(arr, 8);
        // Method 2:
        return arr.find(8); 
    }
}

Sorting Arrays

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.15;
pragma experimental ABIEncoderV2;

contract SortArrayPractice {

    struct Order {
        uint id;
        //address user;
        //bool buyOrder;
        //bytes32 ticker;
        //uint amount;
        uint price;
    }

    Order[] orders;

    function create(uint p) public {
        // Order[] memory bufOrders;
        orders.push(
            Order(orders.length, p)
        );
    }

    function list() public view returns(Order[] memory) {
        return orders;
    }

    function sortForward() public returns(Order[] memory) {

        bool swapped;

        for (uint i = 0; i < orders.length-1; i++) {
            swapped = false;

            for (uint j = 0; j < orders.length-1; j++) {
                /* compare the adjacent elements */   
                if (orders[j].price > orders[j+1].price) {
                    /* swap them */
                    Order memory t = orders[j];
                    orders[j] = orders[j+1];
                    orders[j+1] = t;     
                    swapped = true;
                }
            }

            if(!swapped){ 
                break;
            }
        }

        return orders;      
    }

    function sortReverse() public returns(Order[] memory) {

        bool swapped;

        for (uint i = orders.length-1; i > 0; i--) {
            swapped = false;
            // 5,2,8,7
            for (uint j = orders.length-1; j > 0; j--) {
                /* compare the adjacent elements */   
                if (orders[j].price > orders[j-1].price) {
                    /* swap them */
                    Order memory t = orders[j-1]; // 2
                    orders[j-1] = orders[j]; // 5,8,8,7
                    orders[j] = t; // 5,8,2,7
                    swapped = true;
                }
            }

            if(!swapped){ 
                break;
            }
        }

        return orders;      
    }
}

Start the Discussion!Leave a Reply