1. 単位

1.1. 一般的な単位

  • wei はイーサの最小の副単位です(1 wei == 1)
  • gwei はガス価格を表すのに一般的に使用される副単位です(1 gwei == 1e9)
  • ether 単位(1 ether == 1e18)
注意:
Solidityでは、計算に整数を使用し、言語は 浮動小数点 型をサポートしていません。浮動小数点の表現の問題により丸め誤差が生じ(丸め)、攻撃の脆弱性につながる論理的欠陥が発生します。

1.2. 時間単位

  • 1 == 1 秒
  • 1 分 == 60 秒
  • 1 時間 == 60 分
  • 1 日 == 24 時間
  • 1 週 == 7 日

例

function f(uint start, uint daysAfter) public {
    if (block.timestamp >= start + daysAfter * 1 days) {
        // ...
    }
}

2. グローバル変数

2.1. ブロックとトランザクションのプロパティ

  • blockhash(uint blockNumber) returns (bytes32): 指定されたブロックのハッシュ( block.number が直近256ブロックのいずれかである場合。そうでない場合はゼロを返します)
  • block.basefee (uint): 現在のブロックのベース手数料
  • block.chainid (uint): 現在のチェーンID
  • block.coinbase (address payable): 現在のブロックのマイナーのアドレス
  • block.gaslimit (uint): 現在のブロックのガス制限
  • block.number (uint): 現在のブロック番号
  • block.timestamp (uint): UNIXエポックからの経過秒数としての現在のブロックタイムスタンプ
  • gasleft() returns (uint256): 残りのガス
  • msg.data (bytes calldata): 完全なコードデータ(calldata)
  • msg.sender (address): メッセージの送信者(現在のコール)
  • msg.sig (bytes4): コードデータの最初の4バイト(つまり関数識別子)
  • msg.value (uint): メッセージとともに送信されたweiの量
  • tx.gasprice (uint): トランザクションのガス価格
  • tx.origin (address): トランザクションの送信者(完全なコールチェーン)

2.2. エラーハンドリング

  • assert(bool condition): 条件が満たされない場合にPanicエラーを引き起こし、状態変更をロールバックする(内部エラーに使用する)。
  • require(bool condition): 条件が満たされない場合にロールバックする(入力または外部コンポーネントのエラーに使用する)。
  • require(bool condition, string memory message): 条件が満たされない場合にロールバックする(入力または外部コンポーネントのエラーに使用する)。エラーメッセージも提供する。
  • revert():実行を中断し、状態の変更をロールバックする
  • revert(string memory reason):実行を中断して状態の変更をロールバックし、理由を示す文字列を渡す

2.3. アドレス型のメンバー

  • <address>.balance (uint256):指定アドレスの残高(Wei単位)
  • <address>.code (bytes memory):指定アドレスのコード(空の場合もある)
  • <address>.codehash (bytes32):指定アドレスのコードハッシュ
  • <address payable>.transfer(uint256 amount):指定された量のWeiを指定アドレスに送信する。失敗した場合はロールバックし、2300ガスのスティペンドを転送する(調整不可)
  • <address payable>.send(uint256 amount) returns (bool):指定された量のWeiを指定アドレスに送信する。失敗した場合はfalseを返し、2300ガスのスティペンドを転送する(調整不可)
  • <address>.call(bytes memory) returns (bool, bytes memory):指定されたペイロードで低レベルのCALLを発行し、成否とリターンデータを返す。利用可能なすべてのガスを転送し、調整可能
  • <address>.delegatecall(bytes memory) returns (bool, bytes memory):指定されたペイロードで低レベルのDELEGATECALLを発行し、成否とリターンデータを返す。利用可能なすべてのガスを転送し、調整可能
  • <address>.staticcall(bytes memory) returns (bool, bytes memory):指定されたペイロードで低レベルのSTATICCALLを発行し、成否とリターンデータを返す。利用可能なすべてのガスを転送し、調整可能

2.4. コラント関連のキーワード

  • this:現在のコントラクト。明示的にアドレスへ変換可能
  • super:継承階層で1つ上の階層にあるコントラクト
  • selfdestruct(address payable recipient): 現在のコントラクトを破棄し、その資金を指定されたAddressに送信して実行を終了します。なお、selfdestructにはEVMから引き継がれたいくつかの固有の仕様があります:
    • 受取側コントラクトのreceive関数は実行されません。
    • コントラクトが実際に破棄されるのはトランザクションの終了時のみであり、revertによって破棄が「取り消される」場合があります。

​

3. 式と制御構造

3.1. サポートされているキーワード

次が存在します: if, else, 間, 実行, のために, break, continue, return, try/catch CやJavaScriptでおなじみの通常のセマンティクスを持ちます。

3.2. 関数呼び出し

呼び出すことができます function 1の コントラクト 別のから コントラクト。以下に2つのコントラクトを使用した例を示します 呼び出し元 および 呼び出し先.

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

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

3.3. キーワードを使用して新しいコントラクトを作成する new

キーワードを使用できます new 新しいコントラクトを作成するには AdvancedStorage.sol 例を用いてより詳細に解説します。

4. 高度なストレージ

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

contract AdvancedStorage {
    // Declare the address of a vault manager
    address public vaultManager;

    // Declare an error type for unauthorized access
    error OwnableUnauthorizedAccount(address account);

    // Constructor is a function that runs when the contract is initialized
    constructor() {
        // Assign the address of the deployer to the vault manager variable
        vaultManager = msg.sender;
    }

    // Declare the InvestmentVault Struct data type
    struct InvestmentVault {
        uint256 investmentDuration; // Thời gian đầu tư
        int256 returnOnInvestment; // % lãi suất trả về
        bool initialized; // Đã khởi tạo
        address identityCard; // Địa chỉ thẻ thông tin
    }
    // Declare a variable with the InvestmentVault type
    InvestmentVault private investmentVault;

    // This function initializes the investment vault
    function setInitialInvestmentVault(uint256 daysAfter, int256 _returnOnInvestment, address _vaultOwner) public {
        // We check if the initiator is the vaultManager
        if (msg.sender != vaultManager) {
            // This reverts all actions and reverts the transaction
            revert OwnableUnauthorizedAccount(msg.sender);
        }
        // Declare the investment duration
        uint256 _investmentDuration = block.timestamp + daysAfter * 1 days;

        // Create a new identity card for the customer
        CustomerIdentityCard customerIdentityCard = new CustomerIdentityCard(_vaultOwner);
        // Assign the address of the vault owner/customer to the mapping with the vault information
        investmentVault = InvestmentVault({investmentDuration: _investmentDuration, returnOnInvestment: _returnOnInvestment, initialized: true, identityCard: address(customerIdentityCard)});
    }

    // Function to change the return on investment
    function editReturnOnInvestment(int256 _newReturnOnInvestment) public {
        // require keyword works similarly to if and revert above
        require (msg.sender == vaultManager, "Unauthorized Manager");
        // Change the value of the interest rate
        investmentVault.returnOnInvestment = _newReturnOnInvestment;
    }

    // Function to return investmentVault information
    function retrieveInvestmentVault() public view returns (InvestmentVault memory _investmentVault) {
        return investmentVault;
    }

    // Function to return the address of the IdentityCard
    function retrieveCustomerInformation() public view returns (address) {
        return CustomerIdentityCard(investmentVault.identityCard).customer();
    }
}

// Contract that stores the address of the vault owner
contract CustomerIdentityCard {
    //  declares a variable to store the address of the customer
    address public customer;

    // initialize the contract and assign the address of the customer
    constructor(address _customer) {
        customer = _customer;
    }
}