Git Product home page Git Product logo

ethereum-php's Introduction

Ethereum Client for PHP

PHP 版本以太坊 JSON RPC 客户端。

可通过简单的添加合约地址和 ABI 来方便的调用合约内的方法,对于需要对交易签名的请求,客户端会自动完成。

支持对事件(Log)的监听,但需要通过定时器执行 \Ethereum\Client::synchronizer->sync() 方法来轮询, 当有事件到达会执行自定义的回调并传递一个 \Ethereum\Types\Event 的实例,该实例包含了反序列化后的事件输入和相关的区块、交易数据。

JSON RPC API 实现度

已实现

  • eth_*
  • net_*
  • web3_*

未实现

  • shh_*

依赖

php-64bit: ^7.2
ext-gmp: ^7.2
ext-scrypt: ^1.4
ext-secp256k1: ^0.1.0
ext-keccak: ^0.2
graze/guzzle-jsonrpc: ^3.2
bitwasp/buffertools: ^0.5.0

使用

安装

composer require ericychu/ethereum-php

示例

实例化客户端

$client = new Ethereum\Client(
    // JSON RPC 地址
    'https://api.infura.io/v1/jsonrpc/ropsten',
    // 以太坊网络 ID
    3,
    // 节点账户的 Keystore
    '',
    // Keystore 的密码
    '',
    // 存储实例,用来保存一些状态值,可以通过实现 \Ethereum\StorageInterface 接口使用你自己的存储
    new \Ethereum\Storage
);

添加合约

$client->contracts
    ->add(
        // 合约别名
        'test_contract',
        // 合约地址
        '',
        // 合约 ABI(JSON String)
        ''
    );

调用合约中的方法

pureview 的方法,可以直接以数组的形式返回反序列化后的数据;nonpayablepayable 的方法返回交易的哈希(\Ethereum\Types\Hash 实例)。

$result = $client->contracts->test_contract->call('test_function', ['test_arg_1', 'test_arg_2']);

监听事件

这里的事件名称是你在合约中定义的事件名称。

注意,监听事件需要通过定时器执行 \Ethereum\Client::synchronizer->sync() 方法来轮询以太坊节点。当有事件到达会执行自定义的回调并传递一个 \Ethereum\Types\Event 的实例,该实例包含了反序列化后的事件输入和相关的区块、交易数据。

$client->contracts->test_contract->watch('Event1', function (\Ethereum\Types\Event $data) {
    var_dump($data);
});

如果使用 Swoole,可以通过 Swoole 的定时器来来轮询。

swoole_timer_tick(1000, function() use ($client) {
    $client->synchronizer->sync();
});

调用 JSON RPC API

支持的方法请查阅 \Ethereum\Methods\Eth\Ethereum\Methods\Web3\Ethereum\Methods\Net 类。

echo $client->eth()->protocolVersion();
echo $client->web3()->clientVersion();
echo $client->net()->version();

ethereum-php's People

Contributors

ericyzhu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ethereum-php's Issues

composer require ericychu/ethereum-php

composer require ericychu/ethereum-php

Root package 'ericychu/ethereum-php' cannot require itself in its composer.json
Did you accidentally name your root package after an external package?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.