0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

いまさら、Types - Solidity(その1)

Last updated at Posted at 2023-02-14

はじめに

基本は大事ということで、Solidityを使う機会が増えてきたので、型(Types)のドキュメントをGoogle翻訳DeepL翻訳を使用して翻訳してみます。

※:長いので記事を4分割します。
その1その2その3その4

※:誤字脱字、翻訳ミスなどありましたら、お知らせくださいm(__)m

型(Types)

原文

Solidity is a statically typed language, which means that the type of each variable (state and local) needs to be specified. Solidity provides several elementary types which can be combined to form complex types.

In addition, types can interact with each other in expressions containing operators. For a quick reference of the various operators, see Order of Precedence of Operators.

The concept of “undefined” or “null” values does not exist in Solidity, but newly declared variables always have a default value dependent on its type. To handle any unexpected values, you should use the revert function to revert the whole transaction, or return a tuple with a second bool value denoting success.

Solidity は静的型付き言語です。つまり、各変数 (ステートとローカル) の型を指定する必要があります。
Solidity はいくつかの基本的な型を提供し、それらを組み合わせて複雑な型を形成することができます。

さらに、型は演算子を含む式で互いに作用することができます。
様々な演算子についての簡単なリファレンスは、演算子の優先順位を参照してください。

Solidity には "undefined" または "null" 値の概念はありませんが、新しく宣言された変数には常にその型に依存するデフォルト値があります。
予期しない値を処理するには、トランザクション全体を元に戻す revert 関数を使用するか、成功を示す 2 番目の bool 値を持つタプルを返す必要があります。

値型(Value Types)

原文
The following types are also called value types because variables of these types will always be passed by value, i.e. they are always copied when they are used as function arguments or in assignments.

これらの型の変数は常に値で渡されるため、以下の型は値型とも呼ばれます。つまり、関数の引数や代入で使われるときは、常にコピーされます。

ブール値(Booleans)

原文

bool : The possible values are constants true and false.

Operators:

  • ! (logical negation)
  • && (logical conjunction, “and”)
  • || (logical disjunction, “or”)
  • == (equality)
  • != (inequality)

The operators || and && apply the common short-circuiting rules.
This means that in the expression f(x) || g(y), if f(x) evaluates to true, g(y) will not be evaluated even if it may have side-effects.

bool : 使用可能な値は定数truefalseです。

演算子:

  • ! (論理否定)
  • && (論理積、"and")
  • || (論理和、"or")
  • == (等号)
  • != (不等式)

演算子 ||&& は、一般的な短絡規則を適用します。
これは、式 f(x) || g(y) において、f(x)trueと評価されれば、たとえ副作用があるとしても g(y) は評価されません。

整数(Integers)

原文

int / uint : Signed and unsigned integers of various sizes.
Keywords uint8 to uint256 in steps of 8 (unsigned of 8 up to 256 bits) and int8 to int256. uint and int are aliases for uint256 and int256, respectively.

Operators:

  • Comparisons: <=, <, ==, !=, >=, > (evaluate to bool)
  • Bit operators: &, |, ^ (bitwise exclusive or), ~ (bitwise negation)
  • Shift operators: << (left shift), >> (right shift)
  • Arithmetic operators: +, -, unary - (only for signed integers), *, /, % (modulo), ** (exponentiation)

For an integer type X, you can use type(X).min and type(X).max to access the minimum and maximum value representable by the type.

Warning
Integers in Solidity are restricted to a certain range.
For example, with uint32, this is 0 up to 2**32 - 1.
There are two modes in which arithmetic is performed on these types: The “wrapping” or “unchecked” mode and the “checked” mode.
By default, arithmetic is always “checked”, which mean that if the result of an operation falls outside the value range of the type, the call is reverted through a failing assertion.
You can switch to “unchecked” mode using unchecked { ... }.
More details can be found in the section about unchecked.

int / uint : さまざまなサイズの符号付きおよび符号なし整数。
キーワード uint8 から uint256 までは 8 のステップ (256 ビットまでの符号なし 8) と int8 から int256uintint は、それぞれ uint256int256 のエイリアスです。

演算子:

  • 比較演算子 : <=, <, ==, !=, >=, > (bool に評価)
  • ビット演算子 : &, |, ^ (ビット単位の排他的論理和), ~ (ビット単位の否定)
  • シフト演算子 : << (左シフト)、>> (右シフト)
  • 算術演算子 : +, -, 単項の - (符号付き整数のみ), *, /, % (モジュロ), ** (指数関数)

整数の型 X に対して、 type(X).mintype(X).max を使用すると、その型が表現できる最小値と最大値にアクセスできます。

Warning
Solidity の整数は、ある範囲に制限されています。
例えば、uint32 では、0 から 2**32 - 1 までの範囲です。
これらの型に対して演算が実行されるモードには、"wrapping" または "unchecked" モードと "checked" モードの 2 つがあります。
デフォルトでは、算術演算は常に "checked" であり、演算の結果が型の値の範囲外にある場合、呼び出しはアサーションの失敗によって元に戻されます。
unchecked { ... } を使用すると、"unchecked" モードに切り替えることができます。
詳細は unchecked のセクションを参照してください。

比較(Comparisons)

原文
3The value of a comparison is the one obtained by comparing the integer value3

比較の値は、整数値を比較して得られる値です。

ビット演算(Bit operations)

原文
Bit operations are performed on the two’s complement representation of the number. This means that, for example `~int256(0) == int256(-1)`.

ビット操作は、数値の 2 の補数表現に対して実行されます。
これは例えば ~int256(0) == int256(-1) という意味です。

シフト(Shifts)

原文

The result of a shift operation has the type of the left operand, truncating the result to match the type.
The right operand must be of unsigned type, trying to shift by a signed type will produce a compilation error.

Shifts can be “simulated” using multiplication by powers of two in the following way.
Note that the truncation to the type of the left operand is always performed at the end, but not mentioned explicitly.

  • x << y is equivalent to the mathematical expression x * 2**y.
  • x >> y is equivalent to the mathematical expression x / 2**y, rounded towards negative infinity.

Warning
Before version 0.5.0 a right shift x >> y for negative x was equivalent to the mathematical expression x / 2**y rounded towards zero, i.e., right shifts used rounding up (towards zero) instead of rounding down (towards negative infinity).

Note
Overflow checks are never performed for shift operations as they are done for arithmetic operations. Instead, the result is always truncated.

シフト演算の結果は左オペランドの型になり、型に合わせて結果が切り捨てられます。
右側のオペランドは符号なしの型でなければなりません。符号付きの型でシフトしようとすると、コンパイル エラーが発生します。

シフトは、以下の方法で2の累乗を使用して「シミュレート」することができます。
左オペランドの型への切り捨ては常に最後に実行されますが、明示的に言及されていないことに注意してください。

  • x << y は、数式 x * 2**y と同等です。
  • x >> y は、数式 x / 2**y と同等であり、負の無限大に向かって丸められます。

Warning
バージョン 0.5.0 以前では、負の値 x に対する右シフト x >> y は、ゼロに向かって丸められた数式 x / 2**y と同じでした。つまり、右シフトでは切り捨て(負の無限大に向かって)の代わりに、切り上げ(ゼロに向かって)が使われていました。

Note
シフト演算では、算術演算のようなオーバーフローチェックは行われません。
その代わり、結果は常に切り捨てられます。

加算、減算、乗算(Addition, Subtraction and Multiplication)

原文

Addition, subtraction and multiplication have the usual semantics, with two different modes in regard to over- and underflow:

By default, all arithmetic is checked for under- or overflow, but this can be disabled using the unchecked block, resulting in wrapping arithmetic. More details can be found in that section.

The expression -x is equivalent to (T(0) - x) where T is the type of x.
It can only be applied to signed types.
The value of -x can be positive if x is negative.
There is another caveat also resulting from two’s complement representation:

If you have int x = type(int).min;, then -x does not fit the positive range.
This means that unchecked { assert(-x == x); } works, and the expression -x when used in checked mode will result in a failing assertion.

加算、減算、乗算は通常のセマンティクスを持ち、オーバーフローとアンダーフローに関して2つの異なるモードがあります。

デフォルトでは、すべての算術演算はアンダーフローまたはオーバーフローをチェックしますが、uncheckedブロックを使用してこれを無効にすることができ、結果として算術演算はラップされます。詳細はそのセクションに記載されています。

-x という式は、 (T(0) - x) と同等です。ここで、Tx の型です。
符号付き型にのみ適用できます。
x が負の場合、-x の値は正になります。
2 の補数表現に起因する別の警告もあります。

int x = type(int).min; とした場合、-x は正の範囲に収まりません。
つまり、unchecked { assert(-x == x); } は動作し、-x 式を checked モードで使用すると、アサーションで失敗することになります。

除算(Division)

原文

Since the type of the result of an operation is always the type of one of the operands, division on integers always results in an integer.
In Solidity, division rounds towards zero.
This means that int256(-5) / int256(2) == int256(-2).

Note that in contrast, division on literals results in fractional values of arbitrary precision.

Note
Division by zero causes a Panic error. This check can not be disabled through unchecked { ... }.

Note
The expression type(int).min / (-1) is the only case where division causes an overflow.
In checked arithmetic mode, this will cause a failing assertion, while in wrapping mode, the value will be type(int).min.

演算の結果の型は常にオペランドの 1 つの型であるため、整数の除算は常に整数になります。
Solidity では、除算は 0 に向かって丸められます。
つまり、int256(-5) / int256(2) == int256(-2) ということになります。

対照的に、リテラルの除算は、任意の精度の小数値になることに注意してください。

Note
ゼロによる除算はパニックエラーを引き起こします。このチェックは unchecked { ... } で無効にすることはできません。

Note
type(int).min / (-1) は、除算によってオーバーフローが発生する唯一のケースです。
checked演算モードでは、これによりアサーションが失敗しますが、ラッピングモードでは、値は type(int).min になります。

モジュロ(Modulo)

原文

The modulo operation a % n yields the remainder r after the division of the operand a by the operand n, where q = int(a / n) and r = a - (n * q).
This means that modulo results in the same sign as its left operand (or zero) and a % n == -(-a % n) holds for negative a:

  • int256(5) % int256(2) == int256(1)
  • int256(5) % int256(-2) == int256(1)
  • int256(-5) % int256(2) == int256(-1)
  • int256(-5) % int256(-2) == int256(-1)

Note
Modulo with zero causes a Panic error. This check can not be disabled through unchecked { ... }.

モジュロ演算 a % n は、オペランド a をオペランド n で割った余り r を求めます。ここで、 q = int(a / n)r = a - (n * q) が成り立ちます。
これは、モジュロの結果が左のオペランドと同じ符号(またはゼロ)になり、a % n == -(-a % n) が負の a に対して成立することを意味します。

  • int256(5) % int256(2) == int256(1)
  • int256(5) % int256(-2) == int256(1)
  • int256(-5) % int256(2) == int256(-1)
  • int256(-5) % int256(-2) == int256(-1)

Note
モジュロにゼロを指定すると、パニック エラーが発生します。 このチェックは unchecked { ... } で無効にすることはできません。

べき乗(Exponentiation)

原文

Exponentiation is only available for unsigned types in the exponent.
The resulting type of an exponentiation is always equal to the type of the base.
Please take care that it is large enough to hold the result and prepare for potential assertion failures or wrapping behaviour.

Note
In checked mode, exponentiation only uses the comparatively cheap exp opcode for small bases.
For the cases of x**3, the expression x*x*x might be cheaper.
In any case, gas cost tests and the use of the optimizer are advisable.

Note
Note that 0**0 is defined by the EVM as 1.

べき乗は、符号なし型でのみ使用できます。
べき乗の結果の型は、常に基数の型と等しくなります。
結果を保持し、潜在的なアサーションの失敗やラッピング動作に備えるのに十分な大きさであることに注意してください。

Note
checkedモードでは、指数表現は小さなベースに対して比較的安価な exp オペコードを使用するだけです。
x**3 の場合は、 x*x*x という式の方が安いかもしれません。
いずれにせよ、gasコストのテストとオプティマイザの利用が推奨されます。

Note
なお、0**0はEVMでは1と定義されています。

固定小数点数(Fixed Point Numbers)

原文

Warning
Fixed point numbers are not fully supported by Solidity yet. They can be declared, but cannot be assigned to or from.

fixed / ufixed: Signed and unsigned fixed point number of various sizes.
Keywords ufixedMxN and fixedMxN, where M represents the number of bits taken by the type and N represents how many decimal points are available.
M must be divisible by 8 and goes from 8 to 256 bits.
N must be between 0 and 80, inclusive.
ufixed and fixed are aliases for ufixed128x18 and fixed128x18, respectively.

Operators:

  • Comparisons: <=, <, ==, !=, >=, > (evaluate to bool)
  • Arithmetic operators: +, -, unary -, *, /, % (modulo)

Note
The main difference between floating point (float and double in many languages, more precisely IEEE 754 numbers) and fixed point numbers is that the number of bits used for the integer and the fractional part (the part after the decimal dot) is flexible in the former, while it is strictly defined in the latter.
Generally, in floating point almost the entire space is used to represent the number, while only a small number of bits define where the decimal point is.

Warning
固定小数点数は、Solidity ではまだ完全にサポートされていません。
固定小数点数は宣言することはできますが、代入することも、代入元を指定することもできません。

fixed / ufixed: さまざまなサイズの符号付きおよび符号なしの固定小数点数。
キーワード ufixedMxN および fixedMxNM は型が使用するビット数を表し、Nは使用可能な小数点以下の桁数を表します。
M は 8 で割り切れる必要があり、8 ~ 256 ビットになります。
N は 0 から 80 の間でなければなりません。
ufixed と fixed は、それぞれ ufixed128x18 と fixed128x18 のエイリアスです。

演算子:

  • 比較: <=<==!=>=> (bool に評価)
  • 算術演算子: +-、単項-*/% (モジュロ)

Note
浮動小数点数 (多くの言語では floatdouble 、より正確には IEEE 754 numbers) と固定小数点数の主な違いは、整数と小数部分 (小数点の後の部分) に使用されるビット数です。 は前者では柔軟ですが、後者では厳密に定義されています。
一般に、浮動小数点では、数値を表すためにほぼすべてのスペースが使用されますが、少数のビットのみが小数点の位置を定義します。

アドレス(Address)

原文

The address type comes in two flavours, which are largely identical:

  • address: Holds a 20 byte value (size of an Ethereum address).
  • address payable: Same as address, but with the additional members transfer and send.

The idea behind this distinction is that address payable is an address you can send Ether to, while you are not supposed to send Ether to a plain address, for example because it might be a smart contract that was not built to accept Ether.

Type conversions:

Implicit conversions from address payable to address are allowed, whereas conversions from address to address payable must be explicit via payable(<address>).

Explicit conversions to and from address are allowed for uint160, integer literals, bytes20 and contract types.

Only expressions of type address and contract-type can be converted to the type address payable via the explicit conversion payable(...).
For contract-type, this conversion is only allowed if the contract can receive Ether, i.e., the contract either has a receive or a payable fallback function.
Note that payable(0) is valid and is an exception to this rule.

Note
If you need a variable of type address and plan to send Ether to it, then declare its type as address payable to make this requirement visible.
Also, try to make this distinction or conversion as early as possible.

The distinction between address and address payable was introduced with version 0.5.0.
Also starting from that version, contracts are not implicitly convertible to the address type, but can still be explicitly converted to address or to address payable, if they have a receive or payable fallback function.

Operators:

  • <=, <, ==, !=, >= and >

Warning
If you convert a type that uses a larger byte size to an address, for example bytes32, then the address is truncated.
To reduce conversion ambiguity, starting with version 0.4.24, the compiler will force you to make the truncation explicit in the conversion. Take for example the 32-byte value 0x111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFFCCCC.

You can use address(uint160(bytes20(b))), which results in 0x111122223333444455556666777788889999aAaa, or you can use address(uint160(uint256(b))), which results in 0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc.

Note
Mixed-case hexadecimal numbers conforming to EIP-55 are automatically treated as literals of the address type.
See Address Literals.

アドレスの種類には 2 つの種類がありますが、ほとんど同じです。

  • address: 20 バイトの値 (イーサリアム アドレスのサイズ) を保持します。
  • address payable: address と同じですが、transfersend が追加されています。

この区別の背後にある考え方は、address payableはEtherを送ることができるアドレスであるということです。
例えば、Ether を受け入れるように構築されていないスマートコントラクトである可能性があるため、プレーンな address には Ether を送らないことになっています。

型の変換:

address payable から address への暗黙の変換は許可されますが、 address から address payable への変換は payable(<address>) を介して明示的に行わなければなりません。

uint160、整数リテラル、bytes20、およびコントラクト型では、address との間の明示的な変換が許可されています。

address 型と contract-type 型の式だけが、明示的な変換 payable(...) を使って address payable 型に変換することができます。
コントラクトタイプの場合、この変換はコントラクトがEtherを受け取ることができる場合、つまり、コントラクトがreceiveまたはpayableフォールバック関数を持つ場合にのみ許可されます。
payable(0) は有効であり、このルールの例外であることに注意してください。

Note
address 型の変数が必要で、それに Ether を送信する予定がある場合は、その型を address payable として宣言して、この要件を可視化します。
また、この区別または変換をできるだけ早く行うようにしてください。

addressaddress payable の区別はバージョン 0.5.0 で導入されました。
このバージョンから、コントラクトは暗黙のうちに address 型に変換されませんが、receive または payable フォールバック関数があれば、明示的に address または address payable 型に変換することは可能です。

演算子:

  • <=, <, ==, !=, >=>

Warning
もし、より大きなバイトサイズを使用する型、例えば bytes32address に変換すると、 address は切り捨てられます。
変換のあいまいさを減らすために、バージョン 0.4.24 からは、コンパイラは変換の際に切り捨てを明示的にするように強制します。例えば、32バイトの値 0x1111223344455667788889999AAABBBBCCDDEEFFCCCC を考えてみましょう。

address(uint160(bytes20(b))) を使用すると、0x111122223333444455556666777788889999aAaa になります。
address(uint160(uint256(b))) を使用すると、0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc になります。

Note
EIP-55 に準拠した大/小文字混合の 16 進数は、自動的に「アドレス」タイプのリテラルとして扱われます。
アドレス リテラルを参照してください。

アドレスのメンバ(Members of Addresses)

原文

For a quick reference of all members of address, see Members of Address Types.

  • balance and transfer

It is possible to query the balance of an address using the property balance and to send Ether (in units of wei) to a payable address using the transfer function:

address payable x = payable(0x123);
address myAddress = address(this);
if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10);

The transfer function fails if the balance of the current contract is not large enough or if the Ether transfer is rejected by the receiving account.
The transfer function reverts on failure.

Note
If x is a contract address, its code (more specifically: its Receive Ether Function, if present, or otherwise its Fallback Function, if present) will be executed together with the transfer call (this is a feature of the EVM and cannot be prevented).
If that execution runs out of gas or fails in any way, the Ether transfer will be reverted and the current contract will stop with an exception.

  • send

Send is the low-level counterpart of transfer.
If the execution fails, the current contract will not stop with an exception, but send will return false.

Warning
There are some dangers in using send: The transfer fails if the call stack depth is at 1024 (this can always be forced by the caller) and it also fails if the recipient runs out of gas.
So in order to make safe Ether transfers, always check the return value of send, use transfer or even better: use a pattern where the recipient withdraws the money.

  • call, delegatecall and staticcall

In order to interface with contracts that do not adhere to the ABI, or to get more direct control over the encoding, the functions call, delegatecall and staticcall are provided.
They all take a single bytes memory parameter and return the success condition (as a bool) and the returned data (bytes memory).
The functions abi.encode, abi.encodePacked, abi.encodeWithSelector and abi.encodeWithSignature can be used to encode structured data.

Example:

bytes memory payload = abi.encodeWithSignature("register(string)", "MyName");
(bool success, bytes memory returnData) = address(nameReg).call(payload);
require(success);

Warning
All these functions are low-level functions and should be used with care.
Specifically, any unknown contract might be malicious and if you call it, you hand over control to that contract which could in turn call back into your contract, so be prepared for changes to your state variables when the call returns.
The regular way to interact with other contracts is to call a function on a contract object (x.f()).

Note
Previous versions of Solidity allowed these functions to receive arbitrary arguments and would also handle a first argument of type bytes4 differently.
These edge cases were removed in version 0.5.0.

It is possible to adjust the supplied gas with the gas modifier:

address(nameReg).call{gas: 1000000}(abi.encodeWithSignature("register(string)", "MyName"));

Similarly, the supplied Ether value can be controlled too:

address(nameReg).call{value: 1 ether}(abi.encodeWithSignature("register(string)", "MyName"));

Lastly, these modifiers can be combined. Their order does not matter:

address(nameReg).call{gas: 1000000, value: 1 ether}(abi.encodeWithSignature("register(string)", "MyName"));

In a similar way, the function delegatecall can be used: the difference is that only the code of the given address is used, all other aspects (storage, balance, …) are taken from the current contract.
The purpose of delegatecall is to use library code which is stored in another contract.
The user has to ensure that the layout of storage in both contracts is suitable for delegatecall to be used.

Note
Prior to homestead, only a limited variant called callcode was available that did not provide access to the original msg.sender and msg.value values.
This function was removed in version 0.5.0.

Since byzantium staticcall can be used as well. This is basically the same as call, but will revert if the called function modifies the state in any way.

All three functions call, delegatecall and staticcall are very low-level functions and should only be used as a last resort as they break the type-safety of Solidity.

The gas option is available on all three methods, while the value option is only available on call.

Note
It is best to avoid relying on hardcoded gas values in your smart contract code, regardless of whether state is read from or written to, as this can have many pitfalls.
Also, access to gas might change in the future.

  • code and codehash

You can query the deployed code for any smart contract.
Use .code to get the EVM bytecode as a bytes memory, which might be empty.
Use .codehash to get the Keccak-256 hash of that code (as a bytes32).
Note that addr.codehash is cheaper than using keccak256(addr.code).

Note
All contracts can be converted to address type, so it is possible to query the balance of the current contract using address(this).balance.

アドレスのすべてのメンバのクイック リファレンスについては、「アドレス型のメンバ」を参照してください。

  • balancetransfer

残高プロパティを使ってアドレスの balance を問い合わせたり、transfer機能を使って支払い可能なアドレスに Ether(wei単位)を送ったりすることができます。

address payable x = payable(0x123);
address myAddress = address(this);
if (x.balance < 10 && myAddress.balance >= 10) x.transfer(10);

現在のコントラクトの残高が十分でない場合、または受信側のアカウントでEtherの転送が拒否された場合、transfer関数は失敗します。
転送に失敗すると、transfer関数は元の状態に戻ります。

Note
x がコントラクトのアドレスの場合、そのコード (より具体的には: Receive Ether Function (存在すれば) または Fallback Function (存在しなければ)) は transfer の呼び出しと共に実行されます (これは EVM の機能であり、阻止することはできません)。
この実行が gas 不足になったり、何らかの形で失敗したりすると、Ether 転送は元の状態に戻り、現在のコントラクトは例外を発生させて停止します。

  • send

Send は transfer の低水準の対応物です。
実行が失敗した場合、現在のコントラクトは例外で停止しませんが、sendfalse を返します。

Warning
send の使用にはいくつかの危険があります: 呼び出しスタックの深さが 1024 の場合、転送は失敗し (これは常に呼び出し元によって強制される可能性があります)、受信者がガスを使い果たした場合にも失敗します。
したがって、安全な Ether 送金を行うためには、常に send の戻り値を確認するか、transfer を使用するか、さらに良い方法として、受取人がお金を引き出すパターンを使用してください。

  • call, delegatecallstaticcall

ABI に準拠しないコントラクトとのインタフェースや、エンコーディングをより直接的に制御するために、関数 calldelegatecall および staticcall が提供されています。
これらは全て bytes memory パラメータをひとつだけ受け取り、成功条件 (bool) と返されたデータ (bytes memory) を返す。
構造化データをエンコードするには、関数 abi.encodeabi.encodePackedabi.encodeWithSelectorabi.encodeWithSignature を使用することができます。

例:

bytes memory payload = abi.encodeWithSignature("register(string)", "MyName");
(bool success, bytes memory returnData) = address(nameReg).call(payload);
require(success);

Warning
これらの関数はすべて低レベルの関数であり、注意して使用する必要があります。
特に、未知のコントラクトは悪意のあるものである可能性があり、それを呼び出すと、そのコントラクトに制御を渡してしまい、そのコントラクトがあなたのコントラクトにコールバックする可能性があるので、呼び出しが戻ってきたときにあなたの状態変数が変化することに備えておいてください。
他のコントラクトと対話するための通常の方法は、コントラクトオブジェクトの関数 (x.f()) を呼び出すことです。

Note
以前のバージョンの Solidity では、これらの関数が任意の引数を受け取ることができ、また bytes4 型の最初の引数を異なる方法で処理することができました。
これらのエッジケースはバージョン 0.5.0 で削除されました。

gas 修飾子で供給される gas を調整することが可能です。

address(nameReg).call{gas: 1000000}(abi.encodeWithSignature("register(string)", "MyName"));

同様に、供給するEtherの値も制御することができます。

address(nameReg).call{value: 1 ether}(abi.encodeWithSignature("register(string)", "MyName"));

最後に、これらの修飾語は組み合わせることができます。
その順番は関係ありません。

address(nameReg).call{gas: 1000000, value: 1 ether}(abi.encodeWithSignature("register(string)", "MyName"));

同様の方法で、関数 delegatecall を使用できます。違いは、指定されたアドレスのコードのみが使用され、他のすべての側面 (ストレージ、残高など) は現在のコントラクトから取得されることです。
delegatecall の目的は、別のコントラクトに格納されているライブラリ コードを使用することです。
ユーザーは、両方のコントラクトのストレージのレイアウトが、delegatecall の使用に適していることを確認する必要があります。

Note
homestead より前のバージョンでは、callcode という限られたバリエーションだけが利用可能で、オリジナルの msg.sendermsg.value の値へのアクセスはできませんでした。
この機能はバージョン 0.5.0 で削除されました。

byzantium 以降では、 staticcall も使用できます。
これは基本的に call と同じですが、呼び出された関数が何らかの方法で状態を変更すると元に戻ります。

calldelegatecallstaticcall の 3 つの関数はすべて非常に低水準の関数であり、Solidity の型セーフを破るため、最後の手段としてのみ使用する必要があります。

gas オプションは 3 つのメソッドすべてで使用できますが、value オプションは call でのみ使用できます。

Note
スマート コントラクト コードでハードコーディングされたgas値に依存することは避けたほうがよいでしょう。これには多くの落とし穴がある可能性があるため、状態が読み取られるか書き込まれるかにかかわらずです。
また、gasへのアクセスは将来変更される可能性があります。

  • codecodehash

任意のスマートコントラクトのデプロイされたコードを照会することができます。
.code を使用すると、EVM バイトコードを bytes memory として取得できます(空白の場合もあります)。
.codehash を使用すると、コードのKeccak-256ハッシュを取得できます(bytes32として)。
なお、 addr.codehashkeccak256(addr.code) を使用するよりも安価であることに注意してください。

Note
すべてのコントラクトは address 型に変換できるので、 address(this).balance を使用して現在のコントラクトの残高を照会することができます。

コントラクト型(Contract Types)

原文

Every contract defines its own type.
You can implicitly convert contracts to contracts they inherit from.
Contracts can be explicitly converted to and from the address type.

Explicit conversion to and from the address payable type is only possible if the contract type has a receive or payable fallback function.
The conversion is still performed using address(x).
If the contract type does not have a receive or payable fallback function, the conversion to address payable can be done using payable(address(x)).
You can find more information in the section about the address type.

Note
Before version 0.5.0, contracts directly derived from the address type and there was no distinction between address and address payable.

If you declare a local variable of contract type (MyContract c), you can call functions on that contract.
Take care to assign it from somewhere that is the same contract type.

You can also instantiate contracts (which means they are newly created). You can find more details in the ‘Contracts via new’ section.

The data representation of a contract is identical to that of the address type and this type is also used in the ABI.

Contracts do not support any operators.

The members of contract types are the external functions of the contract including any state variables marked as public.

For a contract C you can use type(C) to access type information about the contract.

すべてのコントラクトは独自の型を定義します。
コントラクトを継承元のコントラクトに暗黙的に変換できます。
コントラクトは、address 型との間で明示的に変換できます。

address payable 型との明示的な変換は、コントラクトタイプが receive または payable フォールバック関数を持っている場合にのみ可能です。
変換は address(x) を使って行われます。
コントラクトタイプが受信または支払いフォールバック関数を持っていない場合、address payable への変換は payable(address(x)) を使用して行うことができます。
より詳しい情報は、アドレスタイプについてのセクションを参照してください。

Note
バージョン0.5.0以前は、コントラクトはアドレスタイプから直接派生し、addressaddress payableの区別はありませんでした。

コントラクト型のローカル変数(MyContract c)を宣言すると、そのコントラクトに対して関数を呼び出すことができます。
同じコントラクト型のどこかから代入するように注意してください。

コントラクトをインスタンス化することもできます(つまり、新規に作成されます)。
詳細は、「新規作成によるコントラクト」セクションを参照してください。

コントラクトのデータ表現は address 型と同じで、この型は ABI でも使用されます。

コントラクトは、どの演算子もサポートしていません。

コントラクト型のメンバは、public とマークされた状態変数を含むコントラクトの外部関数です。

コントラクト C の場合、type(C) を使用してコントラクトに関する型情報にアクセスできます。

固定サイズのバイト配列(Fixed-size byte arrays)

原文

The value types bytes1, bytes2, bytes3, …, bytes32 hold a sequence of bytes from one to up to 32.

Operators:

  • Comparisons: <=, <, ==, !=, >=, > (evaluate to bool)
  • Bit operators: &, |, ^ (bitwise exclusive or), ~ (bitwise negation)
  • Shift operators: << (left shift), >> (right shift)
  • Index access: If x is of type bytesI, then x[k] for 0 <= k < I returns the k th byte (read-only).

The shifting operator works with unsigned integer type as right operand (but returns the type of the left operand), which denotes the number of bits to shift by.
Shifting by a signed type will produce a compilation error.

Members:

  • .length yields the fixed length of the byte array (read-only).

Note
The type bytes1[] is an array of bytes, but due to padding rules, it wastes 31 bytes of space for each element (except in storage).
It is better to use the bytes type instead.

Note
Prior to version 0.8.0, byte used to be an alias for bytes1.

bytes1, bytes2, bytes3, …, bytes32 は、1バイトから最大32バイトまでのバイト列を保持します。

演算子

  • 比較演算子 : <=, <, ==, !=, >=, > (bool に評価)
  • ビット演算子 : &, |, ^ (ビット単位の排他的論理和), ~ (ビット単位の否定)
  • シフト演算子 : << (左シフト)、>> (右シフト)
  • インデックスアクセス : xbytesI 型の場合、 0 <= k < Ix[k]k 番目のバイトを返す。 (読み込み専用)

シフト演算子は、符号なし整数型を右オペランドとして使用します (ただし、左オペランドの型を返します)。これは、シフトするビット数を示します。
符号付きの型でシフトすると、コンパイル エラーが発生します。

メンバ:

  • .length は、バイト配列の固定長を生成します。 (読み取り専用)

Note
bytes1[] 型はバイトの配列ですが、パディング ルールにより、要素ごとに 31 バイトのスペースが無駄になります。 (ストレージを除く)
代わりに bytes 型を使用することをお勧めします。

Note
バージョン 0.8.0 より前では、bytebytes1 のエイリアスでした。

動的サイズのバイト配列(Dynamically-sized byte array)

原文

bytes:
Dynamically-sized byte array, see Arrays.
Not a value-type!

string:
Dynamically-sized UTF-8-encoded string, see Arrays.
Not a value-type!

bytes:
動的サイズのバイト配列。Arraysを参照してください。
値型ではありません!

string:
動的サイズの UTF-8 でエンコードされた文字列。Arraysを参照してください。
値型ではありません!

アドレスリテラル(Address Literals)

原文

Hexadecimal literals that pass the address checksum test, for example 0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF are of address type.
Hexadecimal literals that are between 39 and 41 digits long and do not pass the checksum test produce an error.
You can prepend (for integer types) or append (for bytesNN types) zeros to remove the error.

Note
The mixed-case address checksum format is defined in EIP-55.

アドレス チェックサム テストに合格する 16 進数リテラル、たとえば 0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF はアドレス タイプです。
長さが 39 ~ 41 桁で、チェックサム テストに合格しない 16 進リテラルは、エラーを生成します。
エラーを削除するには、前に (整数型の場合) ゼロを追加するか (bytesNN 型の場合) ゼロを追加します。

Note
大文字と小文字が混在するアドレス チェックサム形式は、EIP-55 で定義されています。

有理数・整数リテラル(Rational and Integer Literals)

原文

Integer literals are formed from a sequence of digits in the range 0-9.
They are interpreted as decimals.
For example, 69 means sixty nine.
Octal literals do not exist in Solidity and leading zeros are invalid.

Decimal fractional literals are formed by a . with at least one number after the decimal point. Examples include .1 and 1.3 (but not 1.).

Scientific notation in the form of 2e10 is also supported, where the mantissa can be fractional but the exponent has to be an integer.
The literal MeE is equivalent to M * 10**E.
Examples include 2e10, -2e10, 2e-10, 2.5e1.

Underscores can be used to separate the digits of a numeric literal to aid readability.
For example, decimal 123_000, hexadecimal 0x2eff_abde, scientific decimal notation 1_2e345_678 are all valid.
Underscores are only allowed between two digits and only one consecutive underscore is allowed.
There is no additional semantic meaning added to a number literal containing underscores, the underscores are ignored.

Number literal expressions retain arbitrary precision until they are converted to a non-literal type (i.e. by using them together with anything other than a number literal expression (like boolean literals) or by explicit conversion).
This means that computations do not overflow and divisions do not truncate in number literal expressions.

For example, (2**800 + 1) - 2**800 results in the constant 1 (of type uint8) although intermediate results would not even fit the machine word size.
Furthermore, .5 * 8 results in the integer 4 (although non-integers were used in between).

Warning
While most operators produce a literal expression when applied to literals, there are certain operators that do not follow this pattern:

  • Ternary operator (... ? ... : ...),

  • Array subscript (<array>[<index>]).

You might expect expressions like 255 + (true ? 1 : 0) or 255 + [1, 2, 3][0] to be equivalent to using the literal 256 directly, but in fact they are computed within the type uint8 and can overflow.

Any operator that can be applied to integers can also be applied to number literal expressions as long as the operands are integers.
If any of the two is fractional, bit operations are disallowed and exponentiation is disallowed if the exponent is fractional (because that might result in a non-rational number).

Shifts and exponentiation with literal numbers as left (or base) operand and integer types as the right (exponent) operand are always performed in the uint256 (for non-negative literals) or int256 (for a negative literals) type, regardless of the type of the right (exponent) operand.

Warning
Division on integer literals used to truncate in Solidity prior to version 0.4.0, but it now converts into a rational number, i.e. 5 / 2 is not equal to 2, but to 2.5.

Note
Solidity has a number literal type for each rational number.
Integer literals and rational number literals belong to number literal types.
Moreover, all number literal expressions (i.e. the expressions that contain only number literals and operators) belong to number literal types.
So the number literal expressions 1 + 2 and 2 + 1 both belong to the same number literal type for the rational number three.

Note
Number literal expressions are converted into a non-literal type as soon as they are used with non-literal expressions.
Disregarding types, the value of the expression assigned to b below evaluates to an integer.
Because a is of type uint128, the expression 2.5 + a has to have a proper type, though.
Since there is no common type for the type of 2.5 and uint128, the Solidity compiler does not accept this code.

uint128 a = 1;
uint128 b = 2.5 + a + 0.5;

整数リテラルは、0-9の範囲の数字の並びで形成されます。
これらは10進数として解釈されます。
たとえば、69 は 69 を意味します。
Solidity には 8 進数のリテラルは存在せず、先頭のゼロは無効です。

10 進数の小数リテラルは、小数点の後に少なくとも 1 つの数字を含む . によって形成されます。
例としては、.11.3 が含まれます。 (ただし、1. は含まれません)

2e10 のような科学的記数法もサポートされており、仮数は小数でも指数は整数でなければなりません。
MeE というリテラルは M * 10**E と等価です。
例としては、 2e10-2e102e-102.5e1 などがあります。

アンダースコアは、数値リテラルの桁を区切るために使用することができます。
例えば、10進数の 123_000 、16進数の 0x2eff_abde 、科学的10進数表記法の 1_2e345_678 は全て有効です。
アンダースコアは2桁の間にのみ使用でき、連続したアンダースコアは1つだけ使用可能です。
アンダースコアを含む数値リテラルに追加の意味付けはなく、アンダースコアは無視されます。

数値リテラル式は、非リテラル型に変換されるまで(つまり、数値リテラル式以外のもの(ブーリアンリテラルなど)と一緒に使用したり、明示的に変換したりすることで)、任意の精度を保持します。
このため、数値リテラル式では計算がオーバーフローしたり、割り算が切り捨てられたりすることはありません。

例えば、(2**800 + 1) - 2**800 は定数 1 (uint8 型) になりますが、中間結果はマシンのワードサイズにさえ合わないでしょう。
さらに、.5 * 8 は整数 4 になります。 (ただし、この間は非整数が使用されます)

Warning
ほとんどの演算子は、リテラルに適用するとリテラル式を生成しますが、このパターンに従わない演算子もあります。

  • 三項演算子 (... ? ... : ...),

  • 配列添字 (<array>[<index>]).

255 + (true ? 1 : 0)255 + [1, 2, 3][0] といった式は,リテラル 256 を直接使うのと同じだと思うかもしれませんが,実際には uint8 型内で計算されており,オーバーフローする可能性があります。

整数に適用できる演算子は、オペランドが整数である限り、数リテラル式にも適用できます。
どちらかが小数である場合、ビット演算は禁止され、指数が小数である場合、指数演算は禁止されます。(非有理数になる可能性があるからです)

左(またはベース)オペランドにリテラル数、右(指数)オペランドに整数型を使用したシフトと指数は、右(指数)オペランドの型に関係なく、常に uint256 (非負のリテラルの場合)または int256 (負のリテラルの場合)型で実行されます。

Warning
バージョン 0.4.0 より前の Solidity では、整数リテラルの除算は切り捨てに使用されていましたが、現在は有理数に変換されます。つまり、5 / 22 ではなく、 2.5 に等しくなります。

Note
Solidity には、有理数ごとに数値リテラル型があります。
整数リテラルと有理数リテラルは、数値リテラル型に属します。
さらに、すべての数値リテラル式 (つまり、数値リテラルと演算子のみを含む式) は、数値リテラル型に属します。
したがって、数値リテラル式 1 + 22 + 1 は両方とも、有理数 3 の同じ数値リテラル型に属します。

Note
数値リテラル式は、非リテラル式で使用されるとすぐに非リテラル型に変換されます。
型に関係なく、以下の b に割り当てられた式の値は整数に評価されます。
ただし、 auint128 型なので、 2.5 + a という式は適切な型でなければなりません。
2.5uint128 の型には共通の型がないため、Solidity コンパイラはこのコードを受け入れません。

uint128 a = 1;
uint128 b = 2.5 + a + 0.5;

さいごに

この中で私が注目したのは、アドレス型(address)です。
これは、他の言語にはないSolidity特有の型です。

また、Solidityでは整数だけを扱う方が無難なようです。

その2

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?