1. 単位
1.1. 共通単位
weiイーサの最小単位(1wei== 1)グウェイガス価格を表す一般的な小数単位である(1グウェイ== 1e9)エーテル単位 (1エーテル== 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) は (bytes32) を返す: 指定されたブロックのハッシュ値ブロック番号直近256ブロックのうちの1つである場合、そうでない場合は0を返すブロック基本手数料 (uint): 現在のブロックの基本手数料ブロックチェーンID (uint)現在のチェーンIDブロック.コインベース(支払い先アドレス)現在のブロックを採掘したマイナーのアドレスblock.gaslimit (uint): 現在のブロックのガス制限ブロック番号 (uint)現在のブロック番号ブロックのタイムスタンプ (uint)現在のブロックのタイムスタンプ(Unixエポックからの経過秒数)gasleft() は (uint256) を返します残りのガスmsg.data (バイト単位のコールデータ)完全なコールデータmsg.sender (アドレス)メッセージの送信者(現在の通話)msg.sig (バイト数)コールデータの最初の4バイト(すなわち関数識別子)msg.value (uint)メッセージと共に送信されたウェイの数tx.gasprice (uint)取引のガス代tx.origin (アドレス)トランザクションの送信元(完全なコールチェーン)
2.2. エラー処理
assert(bool 条件)条件が満たされない場合、パニックエラーを引き起こし、状態変更の取り消しを実行する - 内部エラー用に使用する。require(bool 条件)条件が満たされない場合に元に戻す - 入力や外部コンポーネントのエラーに使用する。require(bool 条件, string メモリメッセージ)条件が満たされない場合に元に戻す - 入力や外部コンポーネントのエラーに使用する。エラーメッセージも提供する。元に戻す()実行を中止し、状態変更を元に戻すrevert(string memory reason)実行を中止し、状態変更を元に戻すとともに、説明用の文字列を提供する
2.3. アドレスタイプの構成要素
<address>.balance (uint256)魏の住所の残高<address>.code (bytes memory)アドレスのコード(空でも可)<address>.codehash (bytes32): アドレスのコードハッシュ<address payable>.transfer(uint256 amount)指定された量のウェイをアドレスに送信、失敗時は元に戻り、2300ガスの手当を転送、調整不可<address payable>.send(uint256 amount) returns (bool)指定された量のウェイをアドレスに送信し、失敗時は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. 契約関連のキーワード
これ現在の契約は、明示的にアドレスに変換可能である超継承階層において一つ上のレベルの契約selfdestruct(アドレス ペイアブル 受取人)現在の契約を破棄し、その資金を指定されたアドレスに送金して実行を終了します。なお、selfdestructにはEVMから継承されたいくつかの特異点があります:- 受信契約の受信関数は実行されません。
- 契約は取引終了時にのみ実際に破棄され、リバート操作によって破棄が「取り消される」可能性がある。
3. 表現と制御構造
3.1. サポートされているキーワード
以下があります: もし, それ以外, 一方で, 行う, のために, 休憩, 続ける, 戻る, 試す/捕まえる CやJavaScriptで知られる通常のセマンティクスを用いて。
3.2. 関数呼び出し
呼び出すことができます 関数 の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. キーワード付きの新規契約を作成する 新しい
キーワードを使用できます 新しい 新しい契約を作成する。 アドバンストストレージ.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;
}
}