Alchemy University 学习教程,Smart Contract Basics

估值102亿融资5.45亿的Alchemy 项目,大学每周任务逐步开始。持有大学生早期卡的伙伴们可以开始大学生活了:

官方大佬的 lens ,可以关注下:

了解更多,请关注作者:https://twitter.com/bitc2024

这一期学习Smart Contract Basics:

Solidity Syntax

Data Types

1: Booleans

查看要求:

输入:

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

contract Contract {
    bool public a = true;
    bool public b = false;
}

点击运行。

2: Unsigned Integers

查看要求:

输入:

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

contract Contract {
    uint8 public a = 3;
    uint16 public b = 268;
    uint256 public sum = a + b;
}

点击运行。

3: Signed Integers

查看要求:

输入:

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

contract Contract {
    int8 public a = 10;
    int8 public b = -15;
    int16 public difference = a - b;
}

点击运行。

4: String Literals

查看要求:

输入:

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

contract Contract {
	bytes32 public msg1 = "Hello World";
    string public msg2 = "ccccccccccccccccccccccccccccccccabcd";
}

点击运行。

5: Enum

查看要求:

输入:

// 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

Functions

1: Arguments

查看要求:

输入:

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

contract Contract {
    uint public x;

    constructor(uint _x) {
        x = _x;
    }
}

点击运行。

2: Increment

查看要求:

输入:

// 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;
    }
}

点击运行。

3: View Addition

查看要求:

输入:

// 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;
    }
}

点击运行。

4: Pure Double

查看要求:

输入:

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

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

点击运行。

5: Double Overload

查看要求:

输入:

// 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

Contracts with ethers.js

1: Getter

查看要求:

输入:

/**
 * 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;

点击运行。

2: Setter

查看要求:

输入:

/**
 * 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;

点击运行。

3: Transfer

查看要求:

输入:

/**
 * 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;

点击运行。

4: Signer

查看要求:

输入:

/**
 * 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;

点击运行。

5: Deposit

查看要求:

输入:

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

Sending Ether

1: Storing Owner

查看要求:

输入:

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

contract Contract {
    address public owner;

    constructor() {
        owner = msg.sender;
    }
}

点击运行。

2: Receive Ether

查看要求:

输入:

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

contract Contract {
    address public owner;

    constructor() {
        owner = msg.sender;
    }

    receive() external payable {
    }
}

点击运行。

3: Tip Owner

查看要求:

输入:

// 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);
    }
}

点击运行。

4: Charity

查看要求:

输入:

// 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));
    }
}

点击运行。

5: Self Destruct

查看要求:

输入:

// 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));
    }
}

点击运行。

Learning Revert

1: Constructor Revert

查看要求:

输入:

// 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);
    }
}

点击运行。

2: Only Owner

查看要求:

输入:

// 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);
    }
}

点击运行。

3: Owner Modifier

查看要求:

输入:

// 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);
		_;
	}
}

点击运行。

Sending Data

1: Call Function

查看要求:

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

interface IHero {
    function alert() external;
}

contract Sidekick {
    function sendAlert(address hero) external {
        IHero(hero).alert();
    }
}

点击运行。

2: Signature

查看要求:

输入:

// 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);
    }
}

点击运行。

3: With Signature

查看要求:

输入:

// 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);
    }
}

点击运行。

4: Arbitrary Alert

查看要求:

输入:

// 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);
    }
}

点击运行。

5: Fallback

查看要求:

输入:

// 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);
    }
}

点击运行。

Practice Solidity

Sum and Average

1: Sum and Average

查看要求:

输入:

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);
    }
}

点击运行。

Countdown

1: Countdown

查看要求:

输入:

// 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));
        }
    }
}

点击运行。

以上,Smart Contract Basics,课程学习完成。

未完待续……

了解更多,请关注作者:https://twitter.com/bitc2024

Subscribe to 冰糖vs橙子
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.