项目作者: MatWaller

项目描述 :
Bitcoin Gold PHP JSON-RPC Client
高级语言: PHP
项目地址: git://github.com/MatWaller/php-btgrpc.git
创建时间: 2018-11-10T20:23:15Z
项目社区:https://github.com/MatWaller/php-btgrpc

开源协议:MIT License

下载


Simple Bitcoin Gold PHP JSON-RPC client.

Installation

Run php composer.phar require matwaller/php-goldrpc in your project directory or add following lines to composer.json

  1. "require": {
  2. "matwaller/php-goldrpc": "^2.0"
  3. }

and run php composer.phar install.

Requirements

PHP 7.0 or higher

Usage

Create new object with url as parameter

  1. /**
  2. * Don't forget to include composer autoloader by uncommenting line below
  3. * if you're not already done it anywhere else in your project.
  4. **/
  5. // require 'vendor/autoload.php';
  6. use Waller\Gold\Client as GoldClient;
  7. $Goldd = new GoldClient('http://rpcuser:rpcpassword@localhost:19998/');

or use array to define your Goldd settings

  1. /**
  2. * Don't forget to include composer autoloader by uncommenting line below
  3. * if you're not already done it anywhere else in your project.
  4. **/
  5. // require 'vendor/autoload.php';
  6. use Waller\Gold\Client as GoldClient;
  7. $Goldd = new GoldClient([
  8. 'scheme' => 'http', // optional, default http
  9. 'host' => 'localhost', // optional, default localhost
  10. 'port' => 19998, // optional, default 19998
  11. 'user' => 'goldcashrpc', // required
  12. 'password' => 'rpcpassword', // required
  13. 'ca' => '/etc/ssl/ca-cert.pem' // optional, for use with https scheme
  14. ]);
  1. /**
  2. * Get block info.
  3. */
  4. $block = $Goldd->getBlock('000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');
  5. $block('hash')->get(); // 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
  6. $block['height']; // 0 (array access)
  7. $block->get('tx.0'); // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
  8. $block->count('tx'); // 1
  9. $block->has('version'); // key must exist and CAN NOT be null
  10. $block->exists('version'); // key must exist and CAN be null
  11. $block->contains(0); // check if response contains value
  12. $block->values(); // array of values
  13. $block->keys(); // array of keys
  14. $block->random(1, 'tx'); // random block txid
  15. $block('tx')->random(2); // two random block txid's
  16. $block('tx')->first(); // txid of first transaction
  17. $block('tx')->last(); // txid of last transaction
  18. /**
  19. * Send transaction.
  20. */
  21. $result = $Goldd->sendToAddress('mmXgiR6KAhZCyQ8ndr2BCfEq1wNG2UnyG6', 0.1);
  22. $txid = $result->get();
  23. /**
  24. * Get transaction amount.
  25. */
  26. $result = $Goldd->listSinceBlock();
  27. $gold = $result->sum('transactions.*.amount');
  28. $goldtoshi = \Waller\Gold\to_goldtoshi($gold);

To send asynchronous request, add Async to method name:

  1. $Goldd->getBlockAsync(
  2. '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
  3. function ($response) {
  4. // success
  5. },
  6. function ($exception) {
  7. // error
  8. }
  9. );

You can also send requests using request method:

  1. /**
  2. * Get block info.
  3. */
  4. $block = $Goldd->request('getBlock', '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f');
  5. $block('hash'); // 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
  6. $block['height']; // 0 (array access)
  7. $block->get('tx.0'); // 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
  8. $block->count('tx'); // 1
  9. $block->has('version'); // key must exist and CAN NOT be null
  10. $block->exists('version'); // key must exist and CAN be null
  11. $block->contains(0); // check if response contains value
  12. $block->values(); // get response values
  13. $block->keys(); // get response keys
  14. $block->first('tx'); // get txid of the first transaction
  15. $block->last('tx'); // get txid of the last transaction
  16. $block->random(1, 'tx'); // get random txid
  17. /**
  18. * Send transaction.
  19. */
  20. $result = $Goldd->request('sendtoaddress', 'mmXgiR6KAhZCyQ8ndr2BCfEq1wNG2UnyG6', 0.06);
  21. $txid = $result->get();

or requestAsync method for asynchronous calls:

  1. $Goldd->requestAsync(
  2. 'getBlock',
  3. '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
  4. function ($response) {
  5. // success
  6. },
  7. function ($exception) {
  8. // error
  9. }
  10. );

Multi-Wallet RPC

  1. /**
  2. * Get wallet2.dat balance.
  3. */
  4. $balance = $Goldd->wallet('wallet2.dat')->getbalance();
  5. echo $balance->get(); // 0.10000000

Helpers

Package provides following helpers to assist with value handling.

to_gold()

Converts value in goldtoshi to gold.

  1. echo Waller\Gold\to_gold(100000); // 0.00100000

to_goldtoshi()

Converts value in gold to goldtoshi.

  1. echo Waller\Gold\to_goldtoshi(0.001); // 100000

to_ubtg()

Converts value in gold to ubtg/bits.

  1. echo Waller\Gold\to_ubtg(0.001); // 1000.0000

to_mbtg()

Converts value in gold to mbtg.

  1. echo Waller\Gold\to_mbtg(0.001); // 1.0000

to_fixed()

Trims float value to precision without rounding.

  1. echo Waller\Gold\to_fixed(0.1236, 3); // 0.123

License

This product is distributed under MIT license.