phpblock/core

适用于区块链网络的轻量级异步PHP服务器/客户端。

v0.4.1 2021-03-13 22:29 UTC

This package is auto-updated.

Last update: 2024-09-25 04:36:34 UTC


README

适用于区块链网络的轻量级 PHP 服务器/客户端。

PHP Composer

目前处于开发中。并非所有端点都可用。欢迎帮助!

目录

待办事项

支持的平台

  • 以太坊

未来平台

  • 卡达诺

快速入门示例

调用以太坊 eth_protocolversion 端点。

<?php

require_once('./vendor/autoload.php');

$Ethereum = new PHPBlock\Network\Ethereum\Client();

$Ethereum->ethProtocolVersion()
    ->then(function (string $version) {
        echo "The protocol version is: " . $version . "\n";
    });

$Ethereum->run();

承诺示例

您可以将调用链式调用,并将附加信息传递给承诺链中的下一个链接。

<?php

require_once('./vendor/autoload.php');

use PHPBlock\Network\Ethereum\Model\Gwei;
use PHPBlock\Network\Ethereum\Model\Transaction;
use PHPBlock\Network\Ethereum\Type\Hash32;
use PHPBlock\Network\Ethereum\Type\HexAddress;

use function PHPBlock\Helper\pass;

$Ethereum = new PHPBlock\Network\Ethereum\Client();
# Lightweight types for all data elements.
$value = new Gwei(Gwei::ethToGwei('.0001'));
$to = new HexAddress('0x579ACE666FBbb2cE728B5F335E69Dc7A2C8623D4');
$from = new HexAddress('0x268d5eBe19aF845c23E0Fd4290725E9679Cd1B7d');
$sendTransaction = Transaction::make($to, $from, $value);

$Ethereum->ethSendTransaction($sendTransaction)
    ->then(function (Hash32 $hash32) use ($Ethereum) {
        # Pass additional values easily similar to a tuple.
        return pass($Ethereum->ethGetTransactionByHash($hash32), $hash32);
    })->then(function (array $data) {
        [$receiveTransaction, $hash32] = $data;

        if ($receiveTransaction->hash == $hash32) {
            echo "The hashes match!";
        }
    });

$Ethereum->run();

安装

快速开始,请使用 Composer

$ composer require phpblock/core

测试

要运行测试,您必须首先克隆此存储库,然后通过 Composer 安装依赖项

$ composer install

然后,您可以使用 geth 客户端ganache-cli 来测试以太坊 RPC 调用。您还可以在 Windows 上安装 ganache 客户端

#Using geth (Ubuntu):
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

#Using geth (OS X & brew):
brew tap ethereum/ethereum
brew install ethereum
brew install ethereum --devel

#Using ganache-cli (npm)
npm install -g ganache-cli
ganache-cli

此项目使用 PHPUnit 和 PHPBench。要运行测试套件,请进入项目根目录并运行

$ composer run-script check

许可协议

MIT,请参阅 LICENSE 文件

捐赠

比特币:bc1qkqsfuaptqcslwmxh5lz2utxls4pe7wnjhepa2s

以太坊:0x00E069d105F61564530859A35FE0D007C3536a35

卡达诺:addr1qywvljkfnyyey38te86tshjscn6yw25c069lf82jfjgv57m3txy8f0nf4wnjwcr8uxmlg9wk7lt6uu7g5w9x077v8lwqgsulw6

狗币:D949UWaLauvKyhX6PNuXGavmMNS6uFcjfS