对应崔棉大师 41-50课程
https://www.bilibili.com/video/BV1yS4y1N7yu/?spm_id_from=333.788&vd_source=c81b130b6f8bb3082bdb42226729d69c
调用其他合约

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10; contract Callee {
    uint public x;
    uint public value;     function setX(uint _x) public returns (uint) {
        x = _x;
        return x;
    }     function setXandSendEther(uint _x) public payable returns (uint, uint) {
        x = _x;
        value = msg.value;         return (x, value);
    }
} contract Caller {
    function setX(Callee _callee, uint _x) public {
        uint x = _callee.setX(_x);
    }     function setXFromAddress(address _addr, uint _x) public {
        Callee callee = Callee(_addr);
        callee.setX(_x);
    }     function setXandSendEther(Callee _callee, uint _x) public payable {
        (uint x, uint value) = _callee.setXandSendEther{value: msg.value}(_x);
    }
}   接口合约 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.10; contract Counter {
    uint public count;     function increment() external {
        count += 1;
    }
} interface ICounter {
    function count() external view returns (uint);     function increment() external;
} contract MyContract {
    function incrementCounter(address _counter) external {
        ICounter(_counter).increment();
    }     function getCount(address _counter) external view returns (uint) {
        return ICounter(_counter).count();
    }
} // Uniswap example
interface UniswapV2Factory {
    function getPair(address tokenA, address tokenB)
        external
        view
        returns (address pair);
} interface UniswapV2Pair {
    function getReserves()
        external
        view
        returns (
            uint112 reserve0,
            uint112 reserve1,
            uint32 blockTimestampLast
        );
} contract UniswapExample {
    address private factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
    address private dai = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
    address private weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;     function getTokenReserves() external view returns (uint, uint) {
        address pair = UniswapV2Factory(factory).getPair(dai, weth);
        (uint reserve0, uint reserve1, ) = UniswapV2Pair(pair).getReserves();
        return (reserve0, reserve1);
    }
} 低级call // SPDX-License-Identifier: MIT
pragma solidity ^0.8.10; contract Receiver {
    event Received(address caller, uint amount, string message);     fallback() external payable {
        emit Received(msg.sender, msg.value, "Fallback was called");
    }     function foo(string memory _message, uint _x) public payable returns (uint) {
        emit Received(msg.sender, msg.value, _message);         return _x + 1;
    }
} contract Caller {
    event Response(bool success, bytes data);     // Let's imagine that contract B does not have the source code for
    // contract A, but we do know the address of A and the function to call.
    function testCallFoo(address payable _addr) public payable {
        // You can send ether and specify a custom gas amount 
     //uint要写成uint256 特殊 ,不指定gas 会带全部gas发送交易
        (bool success, bytes memory data) = _addr.call{value: msg.value, gas: 5000}(
            abi.encodeWithSignature("foo(string,uint256)", "call foo", 123)
        );         emit Response(success, data);
    }     // Calling a function that does not exist triggers the fallback function.
    function testCallDoesNotExist(address _addr) public {
        (bool success, bytes memory data) = _addr.call(
            abi.encodeWithSignature("doesNotExist()")
        );         emit Response(success, data);
    }
} 委托调用
http://www.theblockbeats.info/en/news/31694
https://learnblockchain.cn/article/4125
工厂合约 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract Account{
    address public bank;
    address public owner;     constructor(address _owner) payable{
            bank = msg.sender;
            owner = _owner;
    } } contract Factory{     Account[] public Accounts;     function createAccount(address _owner) external payable{
        Account acc = new Account{value:111}(_owner);
        Accounts.push(acc);
    } } 库合约 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.10; library SafeMath {
    function add(uint x, uint y) internal pure returns (uint) {
        uint z = x + y;
        require(z >= x, "uint overflow");         return z;
    }
} library Math {
    function sqrt(uint y) internal pure returns (uint z) {
        if (y > 3) {
            z = y;
            uint x = y / 2 + 1;
            while (x < z) {
                z = x;
                x = (y / x + x) / 2;
            }
        } else if (y != 0) {
            z = 1;
        }
        // else z = 0 (default value)
    }
} contract TestSafeMath {
    using SafeMath for uint;     uint public MAX_UINT = 2**256 - 1;     function testAdd(uint x, uint y) public pure returns (uint) {
        return x.add(y);
    }     function testSquareRoot(uint x) public pure returns (uint) {
        return Math.sqrt(x);
    }
} // Array function to delete element at index and re-organize the array
// so that their are no gaps between the elements.
library Array {
    function remove(uint[] storage arr, uint index) public {
        // Move the last element into the place to delete
        require(arr.length > 0, "Can't remove from empty array");
        arr[index] = arr[arr.length - 1];
        arr.pop();
    }
} contract TestArray {
    using Array for uint[];     uint[] public arr;     function testArrayRemove() public {
        for (uint i = 0; i < 3; i++) {
            arr.push(i);
        }         arr.remove(1);         assert(arr.length == 2);
        assert(arr[0] == 0);
        assert(arr[1] == 2);
    }
} 哈希运算 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.10; contract HashFunction {
    function hash(string memory _text,uint _num,address _addr) public pure returns (bytes32) {
        return keccak256(abi.encodePacked(_text, _num, _addr));
    }  
    function encode_keccak256(string memory _text, string memory _text2) public pure returns (bytes32){
        {
            return keccak256(abi.encode(_text, _text2));
        }
    }
    function encode(string memory _text, string memory _text2) public pure returns (bytes memory){
        {
            return abi.encode(_text, _text2);
        }
    }
        /*
            encode 将数据转为16进制并补0到长度64
            encodePacked将数据转为16进制拼接
            当两个字符串使用encodePacked "AAA","BBB" 和 "AA","ABBB"结果相同 特别注意
            当返回非定长数组时 返回类型为  bytes
        */
     function encodePacked(string memory _text,string memory _text2) public pure returns (bytes32) {
        return keccak256(abi.encodePacked(_text, _text2));
    }     function collision(string memory _text,uint _num,string memory _text2) public pure returns (bytes32) {
        return keccak256(abi.encodePacked(_text, _num, _text2));
    }
} 验证签名
  // SPDX-License-Identifier: MIT
pragma solidity ^0.8.10; /* Signature Verification How to Sign and Verify
# Signing
1. Create message to sign 创建签名
2. Hash the message 对签名hash
3. Sign the hash (off chain, keep your private key secret) 线下对签名hash用私钥加密 # Verify
1. Recreate hash from the original message  取椭圆加密 r s v
2. Recover signer from signature and hash   ecrocver('签名hash',r,s,v) 解密签名的钱包地址
3. Compare recovered signer to claimed signer 比较解密后的地址和加密的钱包地址是否一致
*/ contract VerifySignature {
    /* 1. Unlock MetaMask account
    ethereum.enable()
    */     /* 2. Get message hash to sign
    getMessageHash(
        0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C,
        123,
        "coffee and donuts",
        1
    )     hash = "0xcf36ac4f97dc10d91fc2cbb20d718e94a8cbfe0f82eaedc6a4aa38946fb797cd"
    */
    function getMessageHash(
        address _to,
        uint _amount,
        string memory _message,
        uint _nonce
    ) public pure returns (bytes32) {
        return keccak256(abi.encodePacked(_to, _amount, _message, _nonce));
    }     /* 3. Sign message hash
    # using browser
    account = "copy paste account of signer here"
    ethereum.request({ method: "personal_sign", params: [account, hash]}).then(console.log)     # using web3
    web3.personal.sign(hash, web3.eth.defaultAccount, console.log)     Signature will be different for different accounts
    0x993dab3dd91f5c6dc28e17439be475478f5635c92a56e17e82349d3fb2f166196f466c0b4e0c146f285204f0dcb13e5ae67bc33f4b888ec32dfe0a063e8f3f781b
    */
    function getEthSignedMessageHash(bytes32 _messageHash)
        public
        pure
        returns (bytes32)
    {
        /*
        Signature is produced by signing a keccak256 hash with the following format:
        "\x19Ethereum Signed Message\n" + len(msg) + msg
        */
        return
            keccak256(
                abi.encodePacked("\x19Ethereum Signed Message:\n32", _messageHash)
            );
    }     /* 4. Verify signature
    signer = 0xB273216C05A8c0D4F0a4Dd0d7Bae1D2EfFE636dd
    to = 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C
    amount = 123
    message = "coffee and donuts"
    nonce = 1
    signature =
        0x993dab3dd91f5c6dc28e17439be475478f5635c92a56e17e82349d3fb2f166196f466c0b4e0c146f285204f0dcb13e5ae67bc33f4b888ec32dfe0a063e8f3f781b
    */
    function verify(
        address _signer,
        address _to,
        uint _amount,
        string memory _message,
        uint _nonce,
        bytes memory signature
    ) public pure returns (bool) {
        bytes32 messageHash = getMessageHash(_to, _amount, _message, _nonce);
        bytes32 ethSignedMessageHash = getEthSignedMessageHash(messageHash);         return recoverSigner(ethSignedMessageHash, signature) == _signer;
    }     function recoverSigner(bytes32 _ethSignedMessageHash, bytes memory _signature)
        public
        pure
        returns (address)
    {
        (bytes32 r, bytes32 s, uint8 v) = splitSignature(_signature);         return ecrecover(_ethSignedMessageHash, v, r, s);
    }     function splitSignature(bytes memory sig)
        public
        pure
        returns (
            bytes32 r,
            bytes32 s,
            uint8 v
        )
    {
        require(sig.length == 65, "invalid signature length");         assembly {
            /*
            First 32 bytes stores the length of the signature             add(sig, 32) = pointer of sig + 32
            effectively, skips first 32 bytes of signature             mload(p) loads next 32 bytes starting at the memory address p into memory
            */             // first 32 bytes, after the length prefix
            r := mload(add(sig, 32))
            // second 32 bytes
            s := mload(add(sig, 64))
            // final byte (first byte of the next 32 bytes)
            v := byte(0, mload(add(sig, 96)))
        }         // implicitly return (r, s, v)
    }
} 权限控制合约 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract AccessControl{
    event GrantRole(bytes32 indexed role,address indexed account);
    event RevokeRole(bytes32 indexed role,address indexed account);
   
    constructor(){
        _grantRole(ADMIN,msg.sender);
    }     mapping(bytes32 => mapping(address => bool)) public roles;     bytes32 public constant ADMIN = keccak256(abi.encodePacked("ADMIN"));
    bytes32 public constant USER = keccak256(abi.encodePacked("USER"));      modifier onlyAdmin(bytes32 _role){
         require(roles[_role][msg.sender],"not admin");
         _;
     }     function _grantRole(bytes32 _role,address _account) internal {
        roles[_role][_account] = true;
        emit GrantRole(_role,_account);
    }
    function grantRole(bytes32 _role,address _account) external onlyAdmin(ADMIN){
            _grantRole(_role,_account);
    }
    function revokeRole(bytes32 _role,address _account) external onlyAdmin(ADMIN){
        roles[_role][_account] = false;
        emit RevokeRole(_role,_account);
    } } 自毁合约 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract Kill{
    constructor() payable {
       
    }
    /*
    1.删除合约
    2.强制发送主币 目标是合约地址没有fallback也可以发送主币
    */
    function kill() external{
        selfdestruct(payable(msg.sender));
    }
    function testCall() external pure returns(uint){
        return 123;
    }
} contract Helper{
 
    function getBalance() external view returns  (uint){
        return address(this).balance;
    }     function Killer(Kill _kill) external {
        _kill.kill();
    }
 
}
 

Solidity8.0-03的更多相关文章

  1. 更新32位Spyder从3.0.0-> 3.2.3

    https://stackoverflow.com/questions/51222550/how-to-update-spyder-3-3-0 It works!! 1. went to the An ...

  2. 集群架构03·MySQL初识,mysql8.0环境安装,mysql多实例

    官方网址 https://dev.mysql.com/downloads/mysql/社区版本分析 MySQL5.5:默认存储引擎改为InnoDB,提高性能和可扩展性,增加半同步复制 MySQL5.6 ...

  3. Time.deltaTime 的平均值在0.1-0.2左右

    Time.deltaTime 平均值在0.1-0.2左右 低的在0.03 高的在0.3

  4. Hdu 2955 Robberies 0/1背包

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. zabbix3.0安装部署文档

    zabbix v3.0安装部署 摘要: 本文的安装过程摘自http://www.ttlsa.com/以及http://b.lifec-inc.com ,和站长凉白开的<ZABBIX从入门到精通v ...

  6. ASP.Net MVC3安全升级导致程序集从3.0.0.0变为3.0.0.1

    开发环境一般引用的是本机 C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies下的System.Web.Mvc.dll,当 ...

  7. CentOS-6.5安装zabbix 3.0.4

    关闭selinux [root@localhost /]# sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux ...

  8. Hadoop 2.2.0 4结点集群安装 非HA

    总体介绍 虚拟机4台,分布在1个物理机上,配置基于hadoop的集群中包括4个节点: 1个 Master, 3个 Salve,i p分布为: 10.10.96.33 hadoop1 (Master) ...

  9. Hadoop 2.2.0学习笔记20131209

    1.下载java 7并安装 [root@server- ~]# rpm -ivh jdk-7u40-linux-x64.rpm Preparing... ####################### ...

  10. 【C#进阶系列】03 配置文件管理与程序集的引用版本重定向

    先来点与标题不相关的: CLR支持两种程序集:弱命名程序集和强命名程序集. 两者的区别在于强命名程序集使用发布者的公钥和私钥进行签名.由于程序集被唯一性地标识,所以当应用程序绑定到强命名程序集时,CL ...

随机推荐

  1. 【机器学习】李宏毅——Flow-based Generative Models

    前文我介绍了部分关于生成学习的内容,可以参考我这篇博文点此 前面介绍的各个生成模型,都存在一定的问题: 对于PixelRNN这类模型来说,就是从左上角的像素开始一个个地进行生成,那么这个生成顺序是否合 ...

  2. 温故知新 - 靶机练习-Toppo

    今天闲来无事,重新做了一下以前做过的第一个靶机(https://www.cnblogs.com/sallyzhang/p/12792042.html),这个靶机主要是练习sudo提权,当时不会也没理解 ...

  3. [WPF]auto和*总结

    Auto和*效果 Auto 表示自动适应显示内容的宽度, 控件有多大,就显示多大. * 则表示按比例来分配宽度. 话不多说,直接上例子理解 例子1 代码: <Grid ShowGridLines ...

  4. [Leetcode]寻找峰值

    题目 思路 如果常规解法不考虑时间复杂度,直接遍历即可得到峰值,时间复杂度为O(n),题目要求O(logn),因此我们需要使用二分法. 首先考虑题目要求:nums[-1]=nums[n]=-∞,因此在 ...

  5. vue3+quasar+capacitor开发多平台项目,使用quasar改变主题背景

    quasar的样式和其他的框架修改不太一样,需要我们使用动态的方式来进行变更,一般来说有两种方案进行主题修改 方案一: 修改样式所需文档: 这里是样式修改的说明:https://quasar.dev/ ...

  6. 重写Object类的equals方法-Objects类的equals方法

    重写Object类的equals方法 Object类的equals方法默认比较的是两个对象的地址值,没有意义所以我们学要重写equals方法,比较两个对象的属性值(name,age) 对象的属性值一样 ...

  7. 论文翻译:2020:ECAPA-TDNN: Emphasized Channel Attention, Propagation and Aggregation in TDNN Based Speaker Verification

    论文地址:ECAPA-TDNN:在基于TDNN的说话人验证中强调通道注意.传播和聚集 论文代码:https://github.com/TaoRuijie/ECAPA-TDNN 引用格式:Desplan ...

  8. 论文翻译:2022_Time-Shift Modeling-Based Hear-Through System for In-Ear Headphones

    论文地址:基于时移建模的入耳式耳机透听系统 引用格式: 摘要 透传(hear-through,HT)技术是通过增强耳机佩戴者对环境声音的感知来主动补偿被动隔离的.耳机中的材料会减少声音 500Hz以上 ...

  9. 11月15日内容总结——软件开发架构、网络编程介绍和OSI七层协议介绍

    目录 一.软件开发架构 1.什么是软件开发架构 2.软件开发架构 架构方式一:c/s架构 架构方式二:b/s架构 架构优劣势 二.架构总结 三.网络编程前戏 1.什么是网络编程 2.学习网络编程的目的 ...

  10. 硬件协议之uart

    1. 常规状态下,高电平 2. Start位, 低电平 3. 数据信号次序LSB,  即bit0最先传输,   低电平代表0, 高电平代表1 4. Stop位,  高电平 由此可见传送一个字节,需要1 ...