Coder Social home page Coder Social logo

minter-php-sdk's Introduction

About

This is a pure PHP SDK for working with Minter blockchain

Installing

composer require minter/minter-php-sdk

Using MinterAPI

You can get all valid responses and full documentation at Minter Node Api

Create MinterAPI instance

use Minter\MinterAPI;

$nodeUrl = 'https://minter-node-1.testnet.minter.network:8841'; // example of a node url

$api = new MinterAPI($nodeUrl);

getBalance

Returns coins list, balance and transaction count (for nonce) of an address.

getBalance(string $minterAddress, ?int $height = null): \stdClass

Example
$api->getBalance('Mxfe60014a6e9ac91618f5d1cab3fd58cded61ee99')

// {"jsonrpc": "2.0", "id": "", "result": { "balance": { ... }, "transaction_count": "0"}}

getNonce

Returns next transaction number (nonce) of an address.

getNonce(string $minterAddress): int

Example
$api->getNonce('Mxfe60014a6e9ac91618f5d1cab3fd58cded61ee99')

send

Returns the result of sending signed tx.

send(string $tx): \stdClass

Example
$api->send('f873010101aae98a4d4e540000000000000094fe60014a6e9ac91618f5d1cab3fd58cded61ee99880de0b6b3a764000080801ca0ae0ee912484b9bf3bee785f4cbac118793799450e0de754667e2c18faa510301a04f1e4ed5fad4b489a1065dc1f5255b356ab9a2ce4b24dde35bcb9dc43aba019c')

getStatus

Returns node status info.

getStatus(): \stdClass

getValidators

Returns list of active validators.

getValidators(?int $height = null): \stdClass

estimateCoinBuy

Return estimate of buy coin transaction.

estimateCoinBuy(string $coinToSell, string $valueToBuy, string $coinToBuy, ?int $height = null): \stdClass

estimateCoinSell

Return estimate of sell coin transaction.

estimateCoinSell(string $coinToSell, string $valueToSell, string $coinToBuy, ?int $height = null): \stdClass

getCoinInfo

Returns information about coin. Note: this method does not return information about base coins (MNT and BIP).

getCoinInfo(string $coin, ?int $height = null): \stdClass

getBlock

Returns block data at given height.

getBlock(int $height): \stdClass

getEvents

Returns events at given height.

getEvents(int $height): \stdClass

getTransaction

Returns transaction info.

getTransaction(string $hash): \stdClass

getCandidate

Returns candidate’s info by provided public_key. It will respond with 404 code if candidate is not found.

getCandidate(string $publicKey, ?int $height = null): \stdClass

getCandidates

Returns list of candidates.

$height is optional parameter.

getCandidates(?int $height = null, ?bool $includeStakes = false): \stdClass

estimateTxCommission

Return estimate of transaction.

estimateTxCommission(string $tx): \stdClass

getTransactions

Return transactions by query.

getTransactions(string $query, ?int $page = null, ?int $perPage = null): \stdClass

getUnconfirmedTxs

Returns unconfirmed transactions.

getUnconfirmedTxs(?int $limit = null): \stdClass

getMaxGasPrice

Returns current max gas price.

getMaxGasPrice(?int $height = null): \stdClass

getMinGasPrice

Returns current min gas price.

getMinGasPrice(): \stdClass

getMissedBlocks

Returns missed blocks by validator public key.

getMissedBlocks(string $pubKey, ?int $height = null): \stdClass

Error handling

Example of how you can handle errors and get the response body.

use Minter\MinterAPI;
use GuzzleHttp\Exception\RequestException;

// create instance
$api = new MinterAPI('node url here');

try {
    // success response
    $response = $api->send('signed tx here');
} catch(RequestException $exception) {
    // short exception message
    $message = $exception->getMessage();
    
    // error response in json
    $content = $exception->getResponse()
                    ->getBody()
                    ->getContents();
    
    // error response as array
    $error = json_decode($content, true);                
}

Using MinterSDK

Sign transaction

Returns a signed tx.

Example
  • Sign the SendCoin transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterSendCoinTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterSendCoinTx::TYPE,
    'data' => [
        'coin' => 'MNT',
        'to' => 'Mxfe60014a6e9ac91618f5d1cab3fd58cded61ee99',
        'value' => '10'
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the SellCoin transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterSellCoinTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterSellCoinTx::TYPE,
    'data' => [
         'coinToSell' => 'MNT',
         'valueToSell' => '1',
         'coinToBuy' => 'TEST',
         'minimumValueToBuy' => 1
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the SellAllCoin transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterSellAllCoinTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterSellAllCoinTx::TYPE,
    'data' => [
         'coinToSell' => 'TEST',
         'coinToBuy' => 'MNT',
         'minimumValueToBuy' => 1
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the BuyCoin transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterBuyCoinTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterBuyCoinTx::TYPE,
    'data' => [
         'coinToBuy' => 'MNT',
         'valueToBuy' => '1',
         'coinToSell' => 'TEST',
         'maximumValueToSell' => 1
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the CreateCoin transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterCreateCoinTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterCreateCoinTx::TYPE,
    'data' => [
        'name' => 'TEST COIN',
        'symbol' => 'TEST',
        'initialAmount' => '100',
        'initialReserve' => '10',
        'crr' => 10
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the DeclareCandidacy transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterDeclareCandidacyTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterDeclareCandidacyTx::TYPE,
    'data' => [
        'address' => 'Mxa7bc33954f1ce855ed1a8c768fdd32ed927def47',
        'pubkey' => 'Mp023853f15fc1b1073ad7a1a0d4490a3b1fadfac00f36039b6651bc4c7f52ba9c02',
        'commission' => 10,
        'coin' => 'MNT',
        'stake' => '5'
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the Delegate transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterDelegateTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterDelegateTx::TYPE,
    'data' => [
        'pubkey' => 'Mp0eb98ea04ae466d8d38f490db3c99b3996a90e24243952ce9822c6dc1e2c1a43',
        'coin' => 'MNT',
        'stake' => '5'
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the SetCandidateOn transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterSetCandidateOnTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterSetCandidateOnTx::TYPE,
    'data' => [
        'pubkey' => 'Mp0eb98ea04ae466d8d38f490db3c99b3996a90e24243952ce9822c6dc1e2c1a43'
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the SetCandidateOff transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterSetCandidateOffTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterSetCandidateOffTx::TYPE,
    'data' => [
        'pubkey' => 'Mp0eb98ea04ae466d8d38f490db3c99b3996a90e24243952ce9822c6dc1e2c1a43'
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the RedeemCheck transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterRedeemCheckTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterRedeemCheckTx::TYPE,
    'data' => [
        'check' => 'your check',
        'proof' => 'created by MinterCheck proof'
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the Unbond transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterUnbondTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterUnbondTx::TYPE,
    'data' => [
        'pubkey' => 'Mp....',
        'coin' => 'MNT',
        'value' => '1'
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the MultiSend transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterMultiSendTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterMultiSendTx::TYPE,
    'data' => [
        'list' => [
            [
                'coin' => 'MNT',
                'to' => 'Mxfe60014a6e9ac91618f5d1cab3fd58cded61ee99',
                'value' => '10'
            ], [
                'coin' => 'MNT',
                'to' => 'Mxfe60014a6e9ac91618f5d1cab3fd58cded61ee92',
                'value' => '15'
            ]
        ]
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')
Example
  • Sign the EditCandidate transaction
use Minter\SDK\MinterTx;
use Minter\SDK\MinterCoins\MinterEditCandidateTx;

$tx = new MinterTx([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'gasPrice' => 1,
    'gasCoin' => 'MNT',
    'type' => MinterEditCandidateTx::TYPE,
    'data' => [
        'pubkey' => 'candidate public key',
        'reward_address' => 'Minter address for rewards',
        'owner_address' => 'Minter address of owner'
    ],
    'payload' => '',
    'serviceData' => '',
    'signatureType' => MinterTx::SIGNATURE_SINGLE_TYPE // or SIGNATURE_MULTI_TYPE
]);

$tx->sign('your private key')

Get fee of transaction

  • Calculate fee of transaction. You can get fee AFTER signing or decoding transaction.
use Minter\SDK\MinterTx;

$tx = new MinterTx([....]);
$sign = $tx->sign('your private key');

$tx->getFee();

Get hash of transaction

  • Get hash of encoded transaction
use Minter\SDK\MinterTx;

$tx = new MinterTx([....]);
$sign = $tx->sign('your private key');

$hash = $tx->getHash();
  • Get hash of decoded transaction
use Minter\SDK\MinterTx;

$tx = new MinterTx('Mx....');

$hash = $tx->getHash();

Decode transaction

Returns an array with transaction data.

Example
  • Decode transaction
use Minter\SDK\MinterTx;

$tx = new MinterTx('string tx');

// $tx->from, $tx->data, $tx->nonce ...

Create Minter Check

Example
  • Create check
use Minter\SDK\MinterCheck;

$check = new MinterCheck([
    'nonce' => $nonce,
    'chainId' => MinterTx::MAINNET_CHAIN_ID, // or MinterTx::TESTNET_CHAIN_ID
    'dueBlock' => 999999,
    'coin' => 'MNT',
    'value' => '10'
], 'your pass phrase');

echo $check->sign('your private key here'); 

// Mc.......
  • Create proof
use Minter\SDK\MinterCheck;

$check = new MinterCheck('your Minter address here', 'your pass phrase');

echo $check->createProof(); 
  • Decode check
use Minter\SDK\MinterCheck;

$check = new MinterCheck('your Minter check here');

$check->getBody();  // check body

$check->getOwnerAddress(); // check owner address

Minter Wallet

Example
  • Create wallet. This method returns generated seed, private key, public key, mnemonic and Minter address.
use Minter\SDK\MinterWallet;

$wallet = MinterWallet::create();
  • Generate mnemonic.
use Minter\SDK\MinterWallet;

$mnemonic = MinterWallet::generateMnemonic();
  • Get seed from mnemonic.
use Minter\SDK\MinterWallet;

$seed = MinterWallet::mnemonicToSeed($mnemonic);
  • Get private key from seed.
use Minter\SDK\MinterWallet;

$privateKey = MinterWallet::seedToPrivateKey($seed);
  • Get public key from private key.
use Minter\SDK\MinterWallet;

$publicKey = MinterWallet::privateToPublic($privateKey);
  • Get Minter address from public key.
use Minter\SDK\MinterWallet;

$address = MinterWallet::getAddressFromPublicKey($publicKey);

Tests

To run unit tests:

vendor/bin/phpunit tests

minter-php-sdk's People

Contributors

grkamil avatar danil-lashin avatar i7495 avatar ingria avatar vmalyavin avatar shrpne avatar

Watchers

James Cloos avatar

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.