8000 GitHub - RootSoft/algorand-php at 0.0.2
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

RootSoft/algorand-php

Repository files navigation

algorand-php

Packagist Downloads Issues MIT License

Algorand is a public blockchain and protocol that aims to deliver decentralization, scale and security for all participants. Their PURE PROOF OF STAKE™ consensus mechanism ensures full participation, protection, and speed within a truly decentralized network. With blocks finalized in seconds, Algorand’s transaction throughput is on par with large payment and financial networks. And Algorand is the first blockchain to provide immediate transaction finality. No forking. No uncertainty.

Introduction

Algorand-php is a community SDK with an elegant approach to connect your application to the Algorand blockchain, send transactions, create assets and query the indexer with just a few lines of code.

Once installed, you can simply connect your application to the blockchain and start sending payments

$algorand->sendPayment($account, $recipient, Algo::toMicroAlgos(10), 'Hi');

or create a new asset:

$algorand->assetManager()->createNewAsset($account, 'PHPCoin', 'PHP', 500000, 2);

Features

  • Algod
  • Indexer
  • Transactions
  • Atomic Transfers
  • Account management
  • Asset management
  • TEAL compilation
  • Laravel support ❤️

Getting started

Installation

Note: Algorand-php requires PHP 7.4+

You can install the package via composer:

composer require rootsoft/algorand-php

Usage

Create an AlgodClient and IndexerClient and pass them to the Algorand constructor. We added extra support for locally hosted nodes & third party services (like PureStake).

$algodClient = new AlgodClient(PureStake::MAINNET_ALGOD_API_URL, 'YOUR-API-KEY');
$indexerClient = new IndexerClient(PureStake::MAINNET_INDEXER_API_URL, 'YOUR-API-KEY');
$algorand = new Algorand($algodClient, $indexerClient);

Laravel ❤️

We've added special support to make the life of a Laravel developer even more easy!

Publish the algorand.php config file using:

php artisan vendor:publish --provider="Rootsoft\Algorand\AlgorandServiceProvider" --tag="config"

Open the config/algorand.php file in your project and insert your credentials

return [
    'algod' => [
        'api_url' => 'https://testnet-algorand.api.purestake.io/ps2',
        'api_key' => 'YOUR API KEY',
        'api_token_header' => 'x-api-key',
    ],
    'indexer' => [
        'api_url' => 'https://testnet-algorand.api.purestake.io/idx2',
        'api_key' => 'YOUR API KEY',
        'api_token_header' => 'x-api-key',
    ],
];

Now you can use the Algorand Facade!

Algorand::sendPayment($account, $recipient, Algo::toMicroAlgos(10), 'Hi');

Account Management

Accounts are entities on the Algorand blockchain associated with specific onchain data, like a balance. An Algorand Address is the identifier for an Algorand account. You can use the AccountManager to perform all account related tasks.

Creating a new account

Creating a new account is as easy as calling:

$account = $algorand->accountManager()->createNewAccount();

With the given account, you can easily extract the public Algorand address, signing keys and seedphrase/mnemonic.

$address = $account->getPublicAddress();
$seedphrase = $account->getSeedPhrase();

Loading an existing account

You can load an existing account using your generated secret key or binary seed.

$algorand->accountManager()->loadAccountFromSecret('secret key');
$algorand->accountManager()->loadAccountFromSeed(hex2bin($seed));

Restoring an account

Recovering an account from your 25-word mnemonic/seedphrase can be done by passing an array or space delimited string

$account = Algorand::accountManager()->restoreAccount($seedphrase);

Transactions

There are multiple ways to create a transaction. We've included helper functions to make our life easier.

$algorand->sendPayment($account, $recipient, Algo::toMicroAlgos(10), 'Hi');

Or you can use the TransactionBuilder to create more specific, raw transactions:

// Create a new transaction
$transaction = TransactionBuilder::payment()
    ->sender($account->getAddress())
    ->note('Algonauts assemble!')
    ->amount(Algo::toMicroAlgos(1.2)) // 5 Algo
    ->receiver($recipient)
    ->useSuggestedParams(Algorand::client())
    ->suggestedFeePerByte(10)
    ->build();

/// Sign the transaction
$signedTransaction = $transaction->sign($account);

// Send the transaction
$transactionId = $algorand->sendTransaction($signedTransaction);

Atomic Transfer

An Atomic Transfer means that transactions that are part of the transfer either all succeed or all fail. Atomic transfers allow complete strangers to trade assets without the need for a trusted intermediary, all while guaranteeing that each party will receive what they agreed to.

Atomic transfers enable use cases such as:

  • Circular trades - Alice pays Bob if and only if Bob pays Claire if and only if Claire pays Alice.
  • Group payments - Everyone pays or no one pays.
  • Decentralized exchanges - Trade one asset for another without going through a centralized exchange.
  • Distributed payments - Payments to multiple recipients.

An atomic transfer can be created as following:

// Create a new transaction
$transaction1 = TransactionBuilder::payment()
    ->sender($accountA->getAddress())
    ->note('Atomic transfer from account A to account B')
    ->amount(Algo::toMicroAlgos(1.2)) // 5 Algo
    ->receiver($accountB->getAddress())
    ->useSuggestedParams($algorand)
    ->build();

// Create a new transaction
$transaction2 = TransactionBuilder::payment()
    ->sender($accountB->getAddress())
    ->note('Atomic transfer from account B to account A')
    ->amount(Algo::toMicroAlgos(2)) // 5 Algo
    ->receiver($accountA->getAddress())
    ->useSuggestedParams($algorand)
    ->build();

// Combine the transactions and calculate the group id
$transactions = AtomicTransfer::group([$transaction1, $transaction2]);

// Sign the transaction
$signedTransaction1 = $transaction1->sign($accountA);
$signedTransaction2 = $transaction2->sign($accountB);

// Assemble transactions group
$signedTransactions = [$signedTransaction1, $signedTransaction2];

$algorand->sendTransactions($signedTransactions);

Asset Management

Create a new asset

Creating a new asset is as simple as using the AssetManager included in the Algorand SDK:

$algorand->assetManager()->createNewAsset($account, 'Laracoin', 'LARA', 500000, 2);

Or as usual, you can use the TransactionBuilder to create your asset:

// Create a new asset
$transaction = TransactionBuilder::assetConfig()
    ->assetName($assetName)
    ->unitName($unitName)
    ->totalAssetsToCreate(BigInteger::of($totalAssets))
    ->decimals($decimals)
    ->defaultFrozen($defaultFrozen)
    ->managerAddress($managerAddress)
    ->reserveAddress($reserveAddress)
    ->freezeAddress($freezeAddress
    ->clawbackAddress($clawbackAddress )
    ->sender($address)
    ->suggestedParams($params)
    ->build();

// Sign the transaction
$signedTransaction = $transaction->sign($account);

// Broadcast the transaction on the network
$algorand->sendTransaction($signedTransaction);

Edit an asset

After an asset has been created only the manager, reserve, freeze and clawback accounts can be changed. All other parameters are locked for the life of the asset.

If any of these addresses are set to "" that address will be cleared and can never be reset for the life of the asset. Only the manager account can make configuration changes and must authorize the transaction.

$algorand->assetManager()->editAsset(14192345, $account, $newAccount->getAddress());

Destroy an asset

$algorand->assetManager()->destroyAsset(14192345, $account);

Opt in to receive an asset

Before being able to receive an asset, you should opt in An opt-in transaction is simply an asset transfer with an amount of 0, both to and from the account opting in. Assets can be transferred between accounts that have opted-in to receiving the asset.

$algorand->assetManager()->optIn(14192345, $newAccount);

Transfer an asset

Transfer an asset from the account to the receiver. Assets can be transferred between accounts that have opted-in to receiving the asset. These are analogous to standard payment transactions but for Algorand Standard Assets.

$algorand->assetManager()->transfer(14192345, $account, 1000, $newAccount->getAddress());

Freeze an asset

Freezing or unfreezing an asset requires a transaction that is signed by the freeze account.

Upon creation of an asset, you can specify a freeze address and a defaultfrozen state. If the defaultfrozen state is set to true the corresponding freeze address must issue unfreeze transactions, to allow trading of the asset to and from that account. This may be useful in situations that require holders of the asset to pass certain checks prior to ownership.

$algorand->assetManager()->freeze(14192345, $account, $newAccount->getAddress(), false);

Revoking an asset

Revoking an asset for an account removes a specific number of the asset from the revoke target account. Revoking an asset from an account requires specifying an asset sender (the revoke target account) and an asset receiver (the account to transfer the funds back to).

$algorand->assetManager()->revoke(14192345, $account, 1000, $newAccount->getAddress());

Indexer

Algorand provides a standalone daemon algorand-indexer that reads committed blocks from the Algorand blockchain and maintains a local database of transactions and accounts that are searchable and indexed.

The PHP SDK makes it really easy to search the ledger in a fluent api and enables application developers to perform rich and efficient queries on accounts, transactions, assets, and so forth.

At the moment we support queries on transactions, assets and accounts.

Transactions

Allow searching all transactions that have occurred on the blockchain.

$algorand->indexer()
    ->transactions()
    ->whereCurrencyIsLessThan(Algo::toMicroAlgos(1000))
    ->whereCurrencyIsGreaterThan(Algo::toMicroAlgos(500))
    ->whereAssetId(14502)
    ->whereNotePrefix('PHP')
    ->whereTransactionType(TransactionType::PAYMENT())
    ->search();

Assets

Allow searching all assets that are created on the blockchain.

$algorand->indexer()
    ->assets()
    ->whereUnitName('PHP')
    ->whereAssetName('PHPCoin')
    ->whereCurrencyIsLessThan(Algo::toMicroAlgos(1000))
    ->whereCurrencyIsGreaterThan(Algo::toMicroAlgos(500))
    ->whereAssetId(14502)
    ->search();

Accounts

Allow searching all accounts that are created on the blockchain.

Algorand::indexer()
    ->accounts()
    ->whereAssetId(15205)
    ->whereAuthAddress('RQM43TQH4CHTOXKPLDWVH4FUZQVOWYHRXATHJSQLF7GN6CFFLC35FLNYHM')
    ->limit(5)
    ->search();

Roadmap

  • Better support for Big Integers
  • Participation in consensus
  • KMD
  • Smart contracts
  • Authorization & rekeying
  • Tests

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing & Pull Requests

Feel free to send pull requests.

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Unofficial community SDK to interact with the Algorand network, in PHP & Laravel

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
0