skka3134

skka3134

email
telegram

智能契約のガス最適化、1. openzeppelinを拒否します。

能不用 openzeppelin 就不用 openzeppelin
比如書く一个访问控制和所有权转移
1. 自己書くロジック

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

contract test {
    address owner;
    uint256 x;
    constructor() {
        owner = msg.sender;
    }
    modifier onlyOwer() {
        require(msg.sender == owner);
        _;
    }
    function setOwner(address owner_)public onlyOwer{
        owner=owner_;
    }
    function add ()public onlyOwer {
        x=x+1;
    }
}

image
2.openzeppelin を使う

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
contract test is Ownable {
    uint256 x;
    constructor() {}
    function add ()public onlyOwner  {
        x=x+1;
    }
}

image
差は 168235gas ですね、もしメインネットであれば、おおよそ 30 ドル以上かかります、https://www.cryptoneur.xyz/zh/gas-fees-calculator ガス料金計算ウェブサイト
image

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。