1. 値型
1.1. INT / UINT
uint はの略語です 符号なし整数、サイズは以下から選択できます uint8 ~へ uint256
uint8~から0~へ2 ** 8 - 1uint16~から0~へ2 ** 16 - 1
...uint256~から0~へ2 × 256 - 1
uint8 public u8 = 1;
uint256 public u256 = 456;
uint public u = 123; // uint は uint256 の省略形です
int はの略語です 整数、サイズは以下から選択できます int8 ~へ int256
int8~から-2 × 7~へ2 ** 7 - 1int16~から-2 × 15~へ2 ** 15 - 1...int128~から-2 × 127~へ2 × 127 - 1int256~から-2 × 255~へ2 × 255 - 1
int8 public i8 = -1;
int256 public i256 = 456;
int public i = -123; // int は int256 の省略形ですint および uint の演算子:
- Comparisons: <=, <, ==, !=, >=, > (returns
bool) - ビット演算:&、|、^(ビット単位の排他的論理和)、~(ビット単位の否定)
- Shifts: << (left shift), >> (right shift)
- 足し算、引き算、かけ算:
+,-,否定形 -(つまり符号付き整数),*,/,%(モジュロ)、**(累乗)
あるタイプについて 整数 変数 X については、次のように使用できます type(X).min そして type(X).max その型における最小値および最大値にそれぞれアクセスするため。
// int型の最小値と最大値:
int public minInt = type(int).min;
int public maxInt = type(int).max;
// uint型の最小値と最大値:
uint public minUint = type(uint).min;
uint public maxUint = type(uint).max;1.2. BOOL
bool を意味する ブール値 そして、2つの取り得る値があり、それらは true そして false
bool public trueVar = true;
bool public falseVar = false;1.3. 演算子:
!(論理的否定)&&(論理積、「かつ」)||(論理和、「または」)==(平等)!=(不平等)
事業者 || そして && 一般的な短絡の規則を適用します。つまり、式 f(x) || g(y)、もし f(x) 次の値となる true, g(y) たとえ副作用があるとしても、評価は行われません。
1.4. 住所
住所Solidityにおける特別なデータ型であり、Kaiaアカウントのアドレス(20バイト)を格納することができます受取人名義~に似た住所ただし、さらに2つのメソッドが追加されます転送そして送信
公開アドレス exampleAddress = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
支払先アドレス public examplePayableAddress = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;1.5. バイト
Solidity では、byte データ型はバイトのシーケンスを表します。Solidity には 2 種類のバイト型があります:
- 固定サイズのバイト配列
- 動的にサイズが変更されるバイト配列。
Solidityにおける「bytes」という単語は、動的なバイト配列を表します。基本的にはbyte[]を意味します。
bytes1 a = 0xb5; // [10110101]
bytes1 b = 0x56; // [01010110]
bytes c = "abc"; // [01100001, 01100010, 01100011]1.6. デフォルト値
値が代入されていない宣言済み変数には、デフォルト値が設定されます。
bool public defaultBool; // false
uint public defaultUint; // 0
int public defaultInt; // 0
address public defaultAddr; // 0x0000000000000000000000000000000000000000
bytes1 public defaultByte; // 0x001.7. 契約
契約 Solidityで契約を宣言するために使用されます。
contract HelloWorld {}契約 キーワードを使用して、別のコントラクトから継承することもできます は
契約車両:メルセデス {}1.8. ENUM
列挙型 は、Solidityでユーザー定義型を作成する方法の一つです。これらはすべての整数型との間で明示的に変換可能ですが、暗黙的な変換は許可されていません。整数型からの明示的な変換では、実行時に値が列挙型の範囲内にあるかどうかがチェックされ、その結果、 パニックエラー そうでなければ。 列挙型 少なくとも1つの要素が必要であり、宣言時のデフォルト値は最初の要素となります。列挙型は256個を超える要素を持つことはできません。
データの表現方法は、 列挙型 C言語では:オプションは、0から始まる一連の符号なし整数値で表されます。
使用して type(列挙型の名前).min そして type(NameOfEnum).max 指定された列挙型の最小値および最大値を取得できます。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Enum {
// Enum representing shipping status
enum Status {
Pending,
Shipped,
Accepted,
Rejected,
Canceled
}
// Default value is the first element listed in
// definition of the type, in this case "Pending"
Status public status;
// Returns uint
// Pending - 0
// Shipped - 1
// Accepted - 2
// Rejected - 3
// Canceled - 4
function get() public view returns (Status) {
return status;
}
// Update status by passing uint into input
function set(Status _status) public {
status = _status;
}
// You can update to a specific enum like this
function cancel() public {
status = Status.Canceled;
}
// delete resets the enum to its first value, 0
function reset() public {
delete status;
}
}1.9. タイプ
ユーザー定義値型を使用すると、基本値型に対してコストゼロの抽象化層を構築できます。これはエイリアスに似ていますが、型要件がより厳格です。
ユーザー定義の値型は、次のように定義します タイプCはVです、そこで C は新しく導入された型の名前であり、 V 組み込みの値型(「基底型」)でなければならない
// SPDX-License-Identifier: GPL-3.0
pragma solidity^0.8.8;
// ユーザー定義の値型を使用して、18桁の256ビット幅の固定小数点型を表す。
type UFixed256x18 is uint256;1.10. 関数
関数 Solidityでは、キーワードを使用して関数を宣言します。
次のように宣言できます 関数 以下のような感じです:
contract Counter {
uint public count;
// Function to view count variable
function get() public view returns (uint) {
return count;
}
}2. 参照型
2.1. データの保存場所
変数は次のキーワードを使って宣言します ストレージ, 記憶 または calldata データを保存する場所を指定します。
ストレージ- 変数は状態変数(ブロックチェーン上に保存される)記憶- 変数はメモリ上にあり、関数実行中calldata- 関数に渡されたデータを格納する専用のデータストア
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract DataLocations {
uint storage varStorage
uint memory varMemory
uint calldata varCallData
}2.2. 配列
配列 は、同じ形式の値要素の組み合わせであり、次のようなものと同様です 一覧 Pythonで、そして 配列 in JavaScript.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Array {
// Several ways to initialize an array
uint[] public arr;
uint[] public arr2 = [1, 2, 3];
// Fixed sized array, all elements initialize to 0
uint[10] public myFixedSizeArr;
function get(uint i) public view returns (uint) {
return arr[i];
}
// Solidity can return the entire array.
// But this function should be avoided for
// arrays that can grow indefinitely in length.
function getArr() public view returns (uint[] memory) {
return arr;
}
function push(uint i) public {
// Append to array
// This will increase the array length by 1.
arr.push(i);
}
function pop() public {
// Remove last element from array
// This will decrease the array length by 1
arr.pop();
}
function getLength() public view returns (uint) {
return arr.length;
}
function remove(uint index) public {
// Delete does not change the array length.
// It resets the value at index to it's default value,
// in this case 0
delete arr[index];
}
function examples() external {
// create array in memory, only fixed size can be created
uint[] memory a = new uint[](5);
}
}
2.3. 構造体
構造体 これは、プログラマーが、さまざまな形式の変数を1つの名前のもとにまとめて、 契約.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Todos {
struct Todo {
string text;
bool completed;
}
// An array of 'Todo' structs
Todo[] public todos;
function create(string calldata _text) public {
// 3 ways to initialize a struct
// - calling it like a function
todos.push(Todo(_text, false));
// key value mapping
todos.push(Todo({text: _text, completed: false}));
// initialize an empty struct and then update it
Todo memory todo;
todo.text = _text;
// todo.completed initialized to false
todos.push(todo);
}
// Solidity automatically created a getter for 'todos' so
// you don't actually need this function.
function get(uint _index) public view returns (string memory text, bool completed) {
Todo storage todo = todos[_index];
return (todo.text, todo.completed);
}
// update text
function updateText(uint _index, string calldata _text) public {
Todo storage todo = todos[_index];
todo.text = _text;
}
// update completed
function toggleCompleted(uint _index) public {
Todo storage todo = todos[_index];
todo.completed = !todo.completed;
}
}3. マッピングの種類
マッピング
マッピング を使用して、 ハッシュマップ (~に似た) 辞書 in Python) 1の間 タイプ 別の タイプ.
注記:
を作成するときマッピング、すべてキー同時に存在すること。つまり:
たとえば、次のように作成しますmapping(address => uint256) addressToValue;. まだ設定していない場合はキーそして値それならすべて住所入力した内容は、デフォルト値であるuint256つまり0です。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract Mapping {
// Mapping from address to uint
mapping(address => uint) public myMap;
function get(address _addr) public view returns (uint) {
// Mapping always returns a value.
// If the value was never set, it will return the default value.
return myMap[_addr];
}
function set(address _addr, uint _i) public {
// Update the value at this address
myMap[_addr] = _i;
}
function remove(address _addr) public {
// Reset the value to the default value.
delete myMap[_addr];
}
}
contract NestedMapping {
// Nested mapping (mapping from address to another mapping)
mapping(address => mapping(uint => bool)) public nested;
function get(address _addr1, uint _i) public view returns (bool) {
// You can get values from a nested mapping
// even when it is not initialized
return nested[_addr1][_i];
}
function set(address _addr1, uint _i, bool _boo) public {
nested[_addr1][_i] = _boo;
}
function remove(address _addr1, uint _i) public {
delete nested[_addr1][_i];
}
}4. シンプルなストレージ
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract SimpleStorage {
// Declare a variable to store the name of the maintainer
string public maintainerName = "zxstim";
// Declare the version of the contract
uint8 public version = 1;
// Declare an address to receive donation
address public donationAddress = 0xe3d25540BA6CED36a0ED5ce899b99B5963f43d3F;
// Declare a Person type to store information of a person
struct Person {
string name; // name
uint8 age; // age
bool overEighteen; // Over eighteen?
address uuid; // UUID
uint256 assetValue; // asset value
int256 debtValue; // debt value
}
Person[] private listOfPeople; // this syntax means creating an array to store Person named listOfPeople
mapping(address => Person) uuidToPerson; // this syntax means creating a mapping from address to Person named uuidToPerson
// this function will store the information of a new person with name, age, overEighteen, assetValue, debtValue
function storePerson(string memory _name, uint8 _age, bool _overEighteen, uint256 _assetValue, int256 _debtValue) public returns (Person memory person) {
_assetValue *= 1e18; // Convert asset value to wei unit
_debtValue *= 1e18; // Convert debt value to wei unit
// Add information of the new person to the listOfPeople array
listOfPeople.push(Person({name: _name, age: _age, overEighteen: _overEighteen, uuid: msg.sender, assetValue: _assetValue, debtValue: _debtValue}));
// Add information of the new person to the uuidToPerson mapping
uuidToPerson[msg.sender] = Person({name: _name, age: _age, overEighteen: _overEighteen, uuid: msg.sender, assetValue: _assetValue, debtValue: _debtValue});
return Person({name: _name, age: _age, overEighteen: _overEighteen, uuid: msg.sender, assetValue: _assetValue, debtValue: _debtValue});
}
// this function will retrieve the information of a person based on the address
function retrievePerson(address _address) public view returns (Person memory person) {
return uuidToPerson[_address];
}
}