ホームページ > バックエンド開発 > PHPチュートリアル > phpとイーサリアムクライアント間のやり取りの詳しい説明

phpとイーサリアムクライアント間のやり取りの詳しい説明

不言
リリース: 2023-03-25 06:18:02
オリジナル
2220 人が閲覧しました

この記事は、PHP と Ethereum クライアントの間の相互作用に関する関連知識のポイントを示しており、必要な友人がフォローして学ぶことができます。

phpはイーサリアムrpcサーバーと通信します

1. Json RPC

Json RPCはjsonに基づくリモートプロシージャコールです。 この説明は比較的抽象的です。簡単に言うと、rpcサーバーにメソッドを呼び出すためにjson形式でデータを投稿することですが、このjson形式は大まかにいくつかの項目があります:

method:メソッド名。
  • params: パラメータリスト
  • id: プロシージャ呼び出しの一意の識別番号
  • 2. Json RPC クライアントを構築します

1

2

3

4

5

{

  "method": "",

  "params": [],

  "id": idNumber

}

ログイン後にコピー

怠け者であれば、比較的単純なコードtake 過去に使用するだけです。また、packagist.org にアクセスして、RPC クライアントを自分で見つけることもできます

3. RPC を呼び出すための 2 種類のメソッド

呼び出す必要があるメソッドは 2 種類あります。1 つは RPC サーバー独自のメソッドです。 、もう 1 つはコントラクト メソッドです。

RPC サーバー メソッドは json 形式を呼び出します

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

<?php

 

class jsonRPCClient {

   

  /**

   * Debug state

   *

   * @var boolean

   */

  private $debug;

   

  /**

   * The server URL

   *

   * @var string

   */

  private $url;

  /**

   * The request id

   *

   * @var integer

   */

  private $id;

  /**

   * If true, notifications are performed instead of requests

   *

   * @var boolean

   */

  private $notification = false;

   

  /**

   * Takes the connection parameters

   *

   * @param string $url

   * @param boolean $debug

   */

  public function __construct($url,$debug = false) {

    // server URL

    $this->url = $url;

    // proxy

    empty($proxy) ? $this->proxy = &#39;&#39; : $this->proxy = $proxy;

    // debug state

    empty($debug) ? $this->debug = false : $this->debug = true;

    // message id

    $this->id = 1;

  }

   

  /**

   * Sets the notification state of the object. In this state, notifications are performed, instead of requests.

   *

   * @param boolean $notification

   */

  public function setRPCNotification($notification) {

    empty($notification) ?

              $this->notification = false

              :

              $this->notification = true;

  }

   

  /**

   * Performs a jsonRCP request and gets the results as an array

   *

   * @param string $method

   * @param array $params

   * @return array

   */

  public function __call($method,$params) {

     

    // check

    if (!is_scalar($method)) {

      throw new Exception(&#39;Method name has no scalar value&#39;);

    }

     

    // check

    if (is_array($params)) {

      // no keys

      $params = $params[0];

    } else {

      throw new Exception(&#39;Params must be given as array&#39;);

    }

     

    // sets notification or request task

    if ($this->notification) {

      $currentId = NULL;

    } else {

      $currentId = $this->id;

    }

     

    // prepares the request

    $request = array(

            &#39;method&#39; => $method,

            &#39;params&#39; => $params,

            &#39;id&#39; => $currentId

            );

    $request = json_encode($request);

    $this->debug && $this->debug.=&#39;***** Request *****&#39;."\n".$request."\n".&#39;***** End Of request *****&#39;."\n\n";

 

    // performs the HTTP POST

    $opts = array (&#39;http&#39; => array (

              &#39;method&#39; => &#39;POST&#39;,

              &#39;header&#39; => &#39;Content-type: application/json&#39;,

              &#39;content&#39; => $request

              ));

    $context = stream_context_create($opts);

    if ($fp = fopen($this->url, &#39;r&#39;, false, $context)) {

      $response = &#39;&#39;;

      while($row = fgets($fp)) {

        $response.= trim($row)."\n";

      }

      $this->debug && $this->debug.=&#39;***** Server response *****&#39;."\n".$response.&#39;***** End of server response *****&#39;."\n";

      $response = json_decode($response,true);

    } else {

      throw new Exception(&#39;Unable to connect to &#39;.$this->url);

    }

     

    // debug output

    if ($this->debug) {

      echo nl2br($debug);

    }

     

    // final checks and return

    if (!$this->notification) {

      // check

      if ($response[&#39;id&#39;] != $currentId) {

        throw new Exception(&#39;Incorrect response id (request id: &#39;.$currentId.&#39;, response id: &#39;.$response[&#39;id&#39;].&#39;)&#39;);

      }

      if (!is_null($response[&#39;error&#39;])) {

        throw new Exception(&#39;Request error: &#39;. var_export($response[&#39;error&#39;], true));

      }

       

      return $response[&#39;result&#39;];

       

    } else {

      return true;

    }

  }

}

?>

ログイン後にコピー

RPC サーバーの組み込みメソッドのリスト

組み込みメソッドの呼び出しは、上記のリンクを参照してください。

コントラクト メソッドは json 形式を呼び出します

コントラクト メソッドを呼び出すには、組み込みメソッドで eth_call を使用する必要があります。コントラクト メソッド名とコントラクト メソッドのパラメーター リストは、params を使用して反映されます。コントラクト内で BalanceOf メソッドを呼び出したい場合、JSON データはどのように構築されるべきでしょうか?

まず、getBalanace:

1

2

3

4

5

{

  "method": "eth_accounts",

  "params": [],

  "id": 1

}

ログイン後にコピー

関数のプロトタイプを抽出します。

1

function balanceOf(address _owner) public view returns (uint256 balance)

ログイン後にコピー

geth コンソールでコマンドを実行します:

1

balanceOf(address)

ログイン後にコピー

関数ハッシュ "0x70a08231" を取得します

仮定 クエリされるアドレスは address _owner = "0x38aabef4cd283ccd5091298dedc8" です8d27c5ec5750" を選択し、先頭の "0x" を削除します。左側に 24 個のゼロを追加し (一般的なアドレスの長さは 42 ビットで、「0x」を削除すると 40 ビットになります)、64 ビットの 16 進数を形成します。 パラメーターをカスタマイズします。

最後のパラメーターは、「0x70a082310000000000000000000000038aabef4cd283ccd5091298ded」です。 c88d27c5ec5750"

コントラクト アドレスが "0xaeab4084194B2a425096 fb583Fbcd67385210ac3" であるとします。

その場合、最終的な JSON データは次のようになります:

1

web3.sha3("balanceOf(address)").substring(0, 10)

ログイン後にコピー

上記の JSON データをポスト モードでサーバーに送信し、コントラクト メソッド "balanceOf" を呼び出してクエリを実行できます。指定されたアドレスのトークン残高。

コントラクト内の他のメソッドを呼び出す場合も、上記のメソッドに従う必要があります。印象を深めるために、転送メソッドを再度分析してみましょう:

まず、コード内の関数の実装を見てください:

1

2

3

4

5

{

  "method": "eth_call",

  "params": [{"from": "0x38aabef4cd283ccd5091298dedc88d27c5ec5750", "to": "0xaeab4084194B2a425096fb583Fbcd67385210ac3", "data": "0x70a0823100000000000000000000000038aabef4cd283ccd5091298dedc88d27c5ec5750"}, "latest"],

  "id": 1

}

ログイン後にコピー

次に、関数プロトタイプを抽出します:

1

function transfer(address _to, uint256 _value) public returns (bool)

ログイン後にコピー

第三に、コンソールで sha3 関数を実行します:

1

transfer(address,uint256) //注意逗号后面不能有空格

ログイン後にコピー

関数ハッシュ「0xa9059cbb」を取得します

最初のパラメータはアドレスを想定しています_へ= "0x38aabef4cd283ccd5091298dedc88d27c5ec5750"、次に "0x" に移動し、64 ビットにゼロを追加します。

2 番目のパラメーターは uint256 _value = 43776 を想定しており、その後 16 進数に変換されます "0xab00" の後、"0x" に移動し、 64 ビットにゼロを追加します

それらを接続します


"0xa9059cbb000000000000000000000000038aabef4cd283ccd5091298dedc88d27c5ec5750000000000000 00000000000 00000000000000000000000000000000ab00"

json データを構築:

1

web3.sha3("transfer(address,uint256)").substring(0, 10)

ログイン後にコピー

転送者アドレス

    から契約アドレス
  • データ 16進数上記の操作で取得したもの
  • 上記の手順をコードに変換します
  • Ethereum RPCクライアントを構築します

1

2

3

4

5

{

  "method": "eth_call",

  "params": [{"from": "0x38aabef4cd283ccd5091298dedc88d27c5ec5750", "to": "0xaeab4084194B2a425096fb583Fbcd67385210ac3", "data": "0xa9059cbb00000000000000000000000038aabef4cd283ccd5091298dedc88d27c5ec5750000000000000000000000000000000000000000000000000000000000000ab00"}, "latest"],

  "id": 1

}

ログイン後にコピー

コードは比較的単純ですが、注意すべき点がいくつかあります ポイント: 値の単位

伝達関数の は 10 ^ -18 と非常に小さいため、1000 回転送したい場合は、実際には 10 の 18 乗を掛ける必要があります (18 は小数です)。

    ポイント 1 では、pow 関数の代わりに bcpow を使用する必要があります。PHP に付属の dechex 関数は使用できません。これは、dechex では整数型が PHP_INT_MAX より大きくてはならず、この数値は 32 で 4294967295 であるためです。 -ビットマシン。ポイント 1 により、すべての数値に 10 の 18 乗を掛ける必要があるため、結果の数値は PHP_INT_MAX よりも大幅に大きくなります。実装方法がわからない場合は、自分で 10 進数を 16 進数に変換することをお勧めします。上記のコードに。
  • 転送などの特定のコントラクトメソッドを実行する場合は、まずユーザーのロックを解除する必要があります
  • トランザクションが実際にブロックに書き込まれるように、サーバー側でマイニングを開始する必要があります。 、転送を呼び出した後、相手がアカウントを受け取っていないことがわかりました。驚かないでください。自動コードマイニングを有効にしたい場合は、geth --rpc の最後に --mine を追加します... テスト:
  • 1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    <?php

     

    require &#39;./jsonRPCClient.php&#39;;

     

    //php自带的dechex无法把大整型转换为十六进制

    function bc_dechex($decimal)

    {

      $result = [];

     

      while ($decimal != 0) {

        $mod = $decimal % 16;

        $decimal = floor($decimal / 16);

        array_push($result, dechex($mod));   

      }

     

      return join(array_reverse($result));

    }

     

    class EthereumRPCClient

    {

      public static $client = null;

       

      //布署合约的账户地址

      const COINBASE = &#39;0x38aabef4cd283ccd5091298dedc88d27c5ec5750&#39;;

       

      //合约地址

      const CONTRACT = &#39;0xaeab4084194B2a425096fb583Fbcd67385210ac3&#39;;

     

      public static function __callStatic($method, $params)

      {

        $params = count($params) < 1 ? [] : $params[0];

     

        try {

          if (is_null(self::$client)) {

            self::$client = new jsonRPCClient(&#39;http://127.0.0.1:8545&#39;, true); 

          }

        } catch (\Exception $e) {

          echo $e->getMessage();

        }

     

        return call_user_func([self::$client, $method], $params);

     

      }

     

      public static function getBalance($address)

      {

        $method_hash = &#39;0x70a08231&#39;;

        $method_param1_hex = str_pad(substr($address, 2), 64, &#39;0&#39;, STR_PAD_LEFT);

        $data = $method_hash . $method_param1_hex;

     

        $params = [&#39;from&#39; => $address, &#39;to&#39; => self::CONTRACT, &#39;data&#39; => $data];

     

        $total_balance = self::eth_call([$params, "latest"]);

     

        return hexdec($total_balance) / (pow(10, 18));

      }

     

      public static function transfer($to, $value)

      {

        self::personal_unlockAccount([self::COINBASE, "123456", 3600]);

     

        $value = bcpow(10, 18) * $value;

     

        $method_hash = &#39;0xa9059cbb&#39;;

        $method_param1_hex =str_pad(substr($to, 2), 64, &#39;0&#39;, STR_PAD_LEFT); 

        $method_param2_hex = str_pad(strval(bc_dechex($value)), 64, &#39;0&#39;, STR_PAD_LEFT);

     

        $data = $method_hash . $method_param1_hex . $method_param2_hex;

        $params = [&#39;from&#39; => self::COINBASE, &#39;to&#39; => self::CONTRACT, &#39;data&#39; => $data];

     

        return self::eth_sendTransaction([$params]);

     

      }

     

    }

    ログイン後にコピー


  • 以上がphpとイーサリアムクライアント間のやり取りの詳しい説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート