Cover photo

Smart Contract Basics

Solidity Syntax

post image
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    bool public a = true;
    bool public b = false;
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    uint8 public a = 3;
    uint16 public b = 268;
    uint256 public sum = a + b;
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    int8 public a = 10;
    int8 public b = -15;
    int16 public difference = a - b;
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    bytes32 public msg1 = "Hello World";
    string public msg2 = "ccccccccccccccccccccccccccccccccabcd";
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    enum Foods { Apple, Pizza, Bagel, Banana }

    Foods public food1 = Foods.Apple;
    Foods public food2 = Foods.Pizza;
    Foods public food3 = Foods.Bagel;
    Foods public food4 = Foods.Banana;
}

Functions

post image
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    uint public x;

    constructor(uint _x) {
        x = _x;
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    uint public x;

    constructor(uint _x) {
        x = _x;
    }

    function increment() external {
        x = x + 1;
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    uint public x;

    constructor(uint _x) {
        x = _x;
    }

    function increment() external {
        x = x + 1;
    }

    function add(uint _x) external view returns(uint) {
        uint sum = x + _x;
        return sum;
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    function double(uint x) external pure returns(uint d) {
        d = x * 2;
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    function double(uint x) external pure returns(uint d) {
        d = x * 2;
    }

    function double(uint x, uint y) public pure returns(uint, uint) {         
            return (x*2, y*2);
    }
}

Smart Contract Communication

post image
post image
/**
 * Find the `value` stored in the contract
 *
 * @param {ethers.Contract} contract - ethers.js contract instance
 * @return {promise} a promise which resolves with the `value`
 */
function getValue(contract) {
    let value = contract.value();
    return value;
}

module.exports = getValue;
post image
/**
 * Modify the `value` stored in the contract
 *
 * @param {ethers.Contract} contract - ethers.js contract instance
 * @return {promise} a promise of transaction
 */
function setValue(contract) {
    return contract.modify(6);
}

module.exports = setValue;
post image
/**
 * Transfer funds on the contract from the current signer 
 * to the friends address
 *
 * @param {ethers.Contract} contract - ethers.js contract instance
 * @param {string} friend - a string containing a hexadecimal ethereum address
 * @return {promise} a promise of the transfer transaction
 */
function transfer(contract, friend) {
    return contract.transfer(friend, 1000);
}

module.exports = transfer;
post image
/**
 * Set the message on the contract using the signer passed in
 *
 * @param {ethers.Contract} contract - ethers.js contract instance
 * @param {ethers.types.Signer} signer - ethers.js signer instance
 * @return {promise} a promise of transaction modifying the `message`
 */
function setMessage(contract, signer) {
    return contract.connect(signer).modify("abc");
}

module.exports = setMessage;
post image
const ethers = require('ethers');

/**
 * Deposit at least 1 ether into the contract 
 *
 * @param {ethers.Contract} contract - ethers.js contract instance
 * @return {promise} a promise of the deposit transaction 
 */
function deposit(contract) {
    return contract.deposit({ value: ethers.utils.parseEther("3") });
}

module.exports = deposit;

Address Interactions

post image
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    address public owner;

    constructor() {
        owner = msg.sender;
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    receive() external payable {
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    receive() external payable {
    }

    function tip() public payable {
        (bool ret, ) = owner.call{ value: msg.value }("");
        require(ret);
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    address public owner;
    address public charity;

    constructor(address _charity) {
        owner = msg.sender;
        charity = _charity;
    }

    receive() external payable {
    }

    function tip() public payable {
        (bool ret, ) = owner.call{ value: msg.value }("");
        require(ret);
    }

    function donate() public {
        (bool ret, ) = charity.call{ value: address(this).balance }("");
        require(ret);
        selfdestruct(payable(msg.sender));
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    address public owner;
    address public charity;

    constructor(address _charity) {
        owner = msg.sender;
        charity = _charity;
    }

    receive() external payable {
    }

    function tip() public payable {
        (bool ret, ) = owner.call{ value: msg.value }("");
        require(ret);
    }

    function donate() public {
        (bool ret, ) = charity.call{ value: address(this).balance }("");
        require(ret);
        selfdestruct(payable(msg.sender));
    }
}
post image
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract  {
    address deployer;

    constructor() payable {
        require(msg.value >= 1000000000000000000);
        deployer = msg.sender;
    }

    function withdraw() public {
        (bool ret, ) = deployer.call{ value: address(this).balance }("");
        require(ret);
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract  {
    address deployer;

    constructor() payable {
        require(msg.value >= 1000000000000000000);
        deployer = msg.sender;
    }

    function withdraw() public {
        require(deployer == msg.sender);
        (bool ret, ) = deployer.call{ value: address(this).balance }("");
        require(ret);
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.5;

contract Contract {
    address owner;
    uint configA;
    uint configB;
    uint configC;

    constructor() {
        owner = msg.sender;
    }

    function setA(uint _configA) public onlyOwner {
        configA = _configA;
    }

    function setB(uint _configB) public onlyOwner {
        configB = _configB;
    }

    function setC(uint _configC) public onlyOwner {
        configC = _configC;
    }

    modifier onlyOwner {
        require(owner == msg.sender);
        _;
    }
}
post image
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

interface IHero {
    function alert() external;
}

contract Sidekick {
    function sendAlert(address hero) external {
        IHero(hero).alert();
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Sidekick {
    function sendAlert(address hero) external {
        bytes4 signature = bytes4(keccak256("alert()"));

        (bool ret, ) = hero.call(abi.encodePacked(signature));

        require(ret);
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Sidekick {
    function sendAlert(address hero, uint enemies, bool armed) external {
        (bool ret, ) = hero.call(
            abi.encodeWithSignature("alert(uint256,bool)", enemies, armed)
        );

        require(ret);
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Sidekick {
    function relay(address hero, bytes memory data) external {
        (bool ret, ) = hero.call(data);
        require(ret);
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Sidekick {
    function makeContact(address hero) external {
        (bool ret, ) = hero.call(abi.encodeWithSignature("fallback()"));
        require(ret);
    }
}
post image
pragma solidity ^0.8.4;

contract Contract {
    function sumAndAverage(uint a, uint b, uint c, uint d) external pure returns(uint,uint) {
        uint sum = a + b + c + d;
        uint average = sum / 4;

        return (sum, average);
    }
}
post image
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract Contract {
    uint countdown = 10;

    function tick() external {
        countdown--;
        if (0 == countdown) {
            selfdestruct(payable(msg.sender));
        }
    }
}