CityOfZion / neo-php

A php wrapper for the neo blockchain
MIT License
14 stars 14 forks source link

neo-php

PHP SDK for the NEO blockchain.

Overview

What does it currently do

What will (should) it do

Documentation

Currently there isn't much documentation besides this Readme. We could use it! Do a PR if you'd like to help us :). Though there are a lot of examples

Get help or give help

Getting started

To start using neo-php you need to have composer installed. When you're ready openup a terminal and type in:

composer require cityofzion/neo-php @dev

From there on include the autoloader and you can use all of the juicy neo-php features.

Wallet functionality:

The wallet part of neo-php consists out of initializers that have multiple functions.

Create new unencrypted wallet

$newWallet = new NeoPHP\NeoWallet();

Open an unencrypted wallet

$wallet = new NeoPHP\NeoWallet("KzfUdP9MbsuL4Ejo1rTWve4JZfa7m1hc397JGXTHhNqJDAqMxZYu");

Create new encrypted wallet

$wallet = new NeoPHP\NeoWallet();
$wallet->encryptWallet("passphrase");

Open an encrypted wallet

$wallet = new NeoPHP\NeoWallet("6PYMFa9gMAcBrTaAs8JyDrtoGLqb45P8dnmUfVVNcfLd9xKUdffSNfKWKp","passphrase");

Encrypt an existing wallet

$wallet = new NeoPHP\NeoWallet("KzfUdP9MbsuL4Ejo1rTWve4JZfa7m1hc397JGXTHhNqJDAqMxZYu");
$wallet->encryptWallet("passphrase");

BOOL to test if wallet is an encrypted wallet

$wallet->isNEP2();

String get the encrypted address, when isNEP2()

$wallet->getEncryptedKey();

String get wif for initialized wallet

$wallet->getWif();

String get address for initialized wallet

$wallet->getAddress();

String get key for initialized wallet

$wallet->getPrivateKey()

Minimal NEP-5 integration

We're working on NEP5 integration. For now we're able to request the majority of the NEP5 tokens balance with a specified address.

Requesting NEP-5 Token balance for address

$rpcObject = new NeoRPC();
$rpcObject->setNode("https://seed1.redpulse.com:10331");
\NeoPHP\NEP5::getTokenBalance($rpcObject,NeoPHP\NeoAssets::ASSET_ZPT,"AKDVzYGLczmykdtRaejgvWeZrvdkVEvQ1X");
\NeoPHP\NEP5::getTokenBalance($rpcObject,NeoPHP\NeoAssets::ASSET_TKY,"AKDVzYGLczmykdtRaejgvWeZrvdkVEvQ1X")

Right now we have the following "assets" which you can request the balance for:

Token Asset constant
Ontology NeoPHP\NeoAssets::ASSET_ONT
THEKEY NeoPHP\NeoAssets::ASSET_TKY
Congierce token NeoPHP\NeoAssets::ASSET_CGE
Alphacat NeoPHP\NeoAssets::ASSET_ACAT
Narrative Token NeoPHP\NeoAssets::ASSET_NRVE
Red Pulse NeoPHP\NeoAssets::ASSET_RPX
DeepBrainChain NeoPHP\NeoAssets::ASSET_DBC
QLink NeoPHP\NeoAssets::ASSET_QLC
Trinity Network Credit NeoPHP\NeoAssets::ASSET_TN
Zeepin Token NeoPHP\NeoAssets::ASSET_ZPT
PikcioChain NeoPHP\NeoAssets::ASSET_PKC

The RPC

The RPC is the way to talk to the different blockchain nodes. For example: We use it to request the balance for the NEP-5 tokens.

Connecting to a RPC Node

$neo = new NeoRPC(); #use false as argument to go to testnet
//$neo->setNode($neo->getFastestNode());
$neo->setNode("http://seed5.neo.org:10332");

Asking for balance using the CityOfZion API

$neo->getBalance($testAddress);

Query the account asset information, according to the account address.

$neo->getAccountState($testAddress);

Query the asset information, based on the specified asset number.

$neo->getAssetState($neoAssetID);

Returns the hash of the tallest block in the main chain.

$neo->getBestBlockHash();

Returns the corresponding block information according to the specified index OR hash.

$neo->getBlock("0x56adb8cc0de3e4fff7b8641988c83bfca214802d263495403055efdd437234c4");
$neo->getBlock(1533325);

Gets the number of blocks in the main chain.

$neo->getBlockCount();

Calculate claim transaction amounts in order use sendrawtransaction to make a claim.

$neo->getBlockSysFee($neo->getBlockCount()-1);

Returns the hash value of the corresponding block, based on the specified index.

$neo->getBlockHash($neo->getBlockCount()-1);

Gets the current number of connections for the node.

$neo->getConnectionCount();

Query contract information, according to the contract script hash.

$neo->getContractState("602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7");

Obtain the list of unconfirmed transactions in memory.

$neo->getRawMemPool();

Returns the corresponding transaction information, based on the specified hash value.

$neo->getRawTransaction("602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7",true);

Query contract information, according to the contract script hash.

$neo->getStorage("c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", "74657374");

Returns the corresponding transaction output information (returned change), based on the specified hash and index.

$neo->getTxOut("0e3c0f477d80acda1c45650b3260e2410287ef78c291f6e02f0214daca2bd2cf",0);

Broadcasts a transaction over the NEO network. There are many kinds of transactions, as specified in the network protocol documentation

$transaction_id = ""; //A hexadecimal string that has been serialized, after the signed transaction in the program.
$broadcastTransaction = $neo->sendRawTransaction($transaction_id);
if ($broadcastTransaction)
    echo "Sent";
else
    echo "Hasn't been sent";

Validate an address

if ($neo->validateAddress("AXCLjFvfi47R1sKLrebbRJnqWgbcsncfro"))
    echo "Address is valid";
else
    echo "Address is not valid";

NEO Cold wallet generator

You can also run the cli-create-wallet-interactive.php example to generate a new wallet. You can do so on a fresh virtual and disconnected Linux distro, you can do a clean run and keep your wallet safe.

CoinMarketCap integration

Neo-PHP Features a full CoinMarketCap API integration.

To initiate the object

//setup coinmarketcap object
$cmcObject = new \NeoPHP\CoinMarketCap();

//set currency, if not set it defaults to USD
$cmcObject->setCurrency("EUR");

To request the ticker

print_r($cmcObject->getTicker());

Arguments are start and limit. Similar to MySQL start and limit

To request the ticker for a specific currency

//get ticket for asset GAS
print_r($cmcObject->getTickerForAsset(\NeoPHP\Assets\NeoAssets::ASSET_GAS));
//get ticket for asset NEO
print_r($cmcObject->getTickerForAsset(\NeoPHP\Assets\NeoAssets::ASSET_NEO));

Check NEP-5 asset constants of this documentation for the right assets

To get global data

//get global data
print_r($cmcObject->getGlobalData());

Check NEP-5 asset constants of this documentation for the right assets

Created by

Check out Neodius

Licensed under MIT License

Enjoy!