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. アドレス
addressはSolidityにおける特別なデータ型であり、Kaiaアカウントのアドレスの20バイト(サイズ)を格納できるようにしますaddress payableと同様ですがaddress、さらに2つのメソッドが追加されていますtransferおよびsend
address public exampleAddress = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
address payable public examplePayableAddress = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;1.5. バイト
Solidityにおいて、byteデータ型はバイトのシーケンスを表します。Solidityには2種類のbyte型が存在します。
- 固定サイズバイト配列
- 動的サイズバイト配列
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 {}コントラクト は、次のキーワードを使用して別のコントラクトを継承することもできます は
contract Mercedes is Car {}1.8. 列挙型(ENUM)
列挙型 は、Solidityでユーザー定義型を作成する方法の1つです。これらはすべての整数型との間で明示的に変換できますが、暗黙的な変換は許可されていません。整数からの明示的な変換では、値が列挙型の範囲内にあることが実行時にチェックされ、そうでない場合は パニックエラー が発生します。 列挙型 は少なくとも1つのメンバーを必要とし、宣言時のデフォルト値は最初のメンバーになります。列挙型に含めることができるメンバーは最大256個です。
データの表現形式は、次と同じです 列挙型 C言語の場合:オプションは0から始まる連続した符号なし整数値で表現されます。
Using type(NameOfEnum).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. 型
ユーザー定義値型により、基本値型に対するゼロコスト抽象化を作成できます。これはエイリアスに似ていますが、より厳格な型要件を持ちます。
ユーザー定義値型は、次を使用して定義されます: type C is Vここで、 C は新しく導入される型の名前であり、 V は組み込みの値型(「基底型」)である必要があります
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.8;
// ユーザー定義の値型を使用して、18桁の小数部を持つ256ビット幅の固定小数点型を表現します。
type UFixed256x18 is uint256;1.10. 関数
function キーワードは、Solidityで関数を宣言するために使用されます。
以下のように function を宣言できます:
contract Counter {
uint public count;
// Function to view count variable
function get() public view returns (uint) {
return count;
}
}2. 参照型
2.1. データストレージの場所
変数は、データを保存する場所を指定するために次のキーワードで宣言されます: storage, memory または calldata データを保存する場所を指定します。
storage- 変数は状態変数です(ブロックチェーンに保存されます)memory- 変数はメモリ内に存在し、functionの実行中のみ存在しますcalldata- 関数に渡されたデータを含む特別なデータストア
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract DataLocations {
uint storage varStorage
uint memory varMemory
uint calldata varCallData
}2.2. 配列
配列 は、同じ形式の値要素を組み合わせたものであり、 リスト のリストや 配列 から呼び出すことができるだけでなく、 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. マッピング型
マッピング
マッピング を使用して ハッシュマップ ( dict から呼び出すことができるだけでなく、 Pythonと同様) を作成できます 型 別のものへ 型.
注意:
を作成する場合、マッピング、すべてのキーが同時に存在します。つまり、次のようになります。
たとえば、mapping(address => uint256) addressToValue;を作成します。何も設定していない場合、キーおよび値、入力したすべてのaddressはデフォルト値を返します。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];
}
}