5
4

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 5 years have passed since last update.

Solidity v0.5.0 以上での transfer メソッド

5
Posted at

概要

  • Solidity 0.5.0 からは、Ether を受け取ることのアドレスの型を address payable とする必要があります。
  • Solidity 0.4.0 までと同様に普通の address 型を使用するとコンパイル時にエラーとなります。

サンプルコード(0.4系で動くコード)

  • tipEther メソッドは、関数を実行する際に送った Ether を beneficiary に送ります。
pragma solidity ^0.4.25;

contract AddressPayableTest {
    address beneficiary = 0x0089d53F703f7E0843953D48133f74cE247184c2;
    
    function tipEther() public payable {
        beneficiary.transfer(msg.value);
    }
}
  • 上記のコードは、Solidity 0.4 系までは動きますが、Solidity 0.5 系だと次のメッセージと共にコンパイルエラーとなります。

Member function “transfer” not found or not visible after argument-dependent lookup in contract?

サンプルコード(0.5系で動くコード)

  • tipEther メソッドは、関数を実行する際に送った Ether を beneficiary に送ります。
  • beneficiary は Ether を transfer メソッドによって受け取るため、address ではなく address payable とします。
pragma solidity ^0.5.0;

contract AddressPayableTest {
    address payable beneficiary = 0x0089d53F703f7E0843953D48133f74cE247184c2;
    
    function tipEther() public payable {
        beneficiary.transfer(msg.value);
    }
}

注意

  • 基本的には address payable はなるべく使用せず、特定のアドレスに対して Ether を送金する場合は、withdrawal パターン を使用すべきだと思います。
  • msg.senderaddress payable なので、普通に withdrawal に従えば、 address payable については意識する必要がないと思います。

参考

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?