PHP code example of rootsoft / algorand-php

1. Go to this page and download the library: Download rootsoft/algorand-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rootsoft / algorand-php example snippets


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

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

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

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',
    ],
    'kmd' => [
        'api_url' => '127.0.0.1',
        'api_key' => '',
        'api_token_header' => 'X-KMD-API-Token',
    ],
];

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

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

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

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

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

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

// 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);

// 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);

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

// 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);

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

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

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

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

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

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

$arguments = [BigInteger::of(123)->toBytes()];

$result = $this->algorand->applicationManager()->compileTEAL($this->sampleArgsTeal);
$lsig = LogicSignature::fromProgram($result->program(), $arguments);
$receiver = 'KTFZ5SQU3AQ6UFYI2QOWF5X5XJTAFRHACWHXAZV6CPLNKS2KSGQWPT4ACE';

$transaction = TransactionBuilder::payment()
    ->sender($lsig->toAddress())
    ->note('Contract account')
    ->amount(100000)
    ->receiver(Address::fromAlgorandAddress($receiver))
    ->useSuggestedParams($this->algorand)
    ->build();

// Sign the logic transaction
$signedTx = $lsig->signTransaction($transaction);

// Send the transaction
$pendingTx = $this->algorand->sendTransaction($signedTx, true);

$arguments = [BigInteger::of(123)->toBytes()];

$result = $this->algorand->applicationManager()->compileTEAL($this->sampleArgsTeal);
$lsig = LogicSignature::fromProgram($result->program(), $arguments)->sign($this->account);
$receiver = 'KTFZ5SQU3AQ6UFYI2QOWF5X5XJTAFRHACWHXAZV6CPLNKS2KSGQWPT4ACE';

$transaction = TransactionBuilder::payment()
    ->sender($this->account->getAddress())
    ->note('Account delegation')
    ->amount(100000)
    ->receiver(Address::fromAlgorandAddress($receiver))
    ->useSuggestedParams($this->algorand)
    ->build();

// Sign the logic transaction
$signedTx = $lsig->signTransaction($transaction);

// Send the transaction
$pendingTx = $this->algorand->sendTransaction($signedTx, true);

$localInts = 1;
$localBytes = 1;
$globalInts = 1;
$globalBytes = 0;

$approvalProgram = $this->algorand->applicationManager()->compileTEAL($this->approvalProgramSource);
$clearProgram = $this->algorand->applicationManager()->compileTEAL($this->clearProgramSource);

$pendingTx = $this->algorand->applicationManager()->createApplication(
    $account,
    $approvalProgram->program(),
    $clearProgram->program(),
    new StateSchema($globalInts, $globalBytes),
    new StateSchema($localInts, $localBytes),
    true,
);

$localInts = 1;
$localBytes = 1;
$globalInts = 1;
$globalBytes = 0;

$approvalProgram = $this->algorand->applicationManager()->compileTEAL($this->approvalProgramSource);
$clearProgram = $this->algorand->applicationManager()->compileTEAL($this->clearProgramSource);

$transaction = TransactionBuilder::applicationCreate()
    ->sender($account->getAddress())
    ->approvalProgram($approvalProgram->program())
    ->clearStateProgram($clearProgram->program())
    ->globalStateSchema(new StateSchema($globalInts, $globalBytes))
    ->localStateSchema(new StateSchema($localInts, $localBytes))
    ->useSuggestedParams($this->algorand)
    ->build();

$signedTx = $transaction->sign($account);

$pendingTx = $this->algorand->sendTransaction($signedTx, true);

$txId = $this->algorand->applicationManager()->optIn($account, BigInteger::of(22266683));

$transaction = TransactionBuilder::applicationOptIn()
    ->sender($account->getAddress())
    ->applicationId(BigInteger::of(19964146))
    ->useSuggestedParams($this->algorand)
    ->build();

$signedTx = $transaction->sign($account);

$txId = $this->algorand->sendTransaction($signedTx);

$arguments = AlgorandUtils::parse_application_arguments('str:arg1,int:12');
$txId = $this->algorand->applicationManager()->call($account, BigInteger::of(22266683), $arguments);

$arguments = AlgorandUtils::parse_application_arguments('str:arg1,int:12');

$transaction = TransactionBuilder::applicationCall()
    ->sender($account->getAddress())
    ->applicationId(BigInteger::of(19964146))
    ->arguments($arguments)
    ->accounts([$account->getAddress()])
    ->foreignApps([22240890])
    ->foreignAssets([408947])
    ->useSuggestedParams($this->algorand)
    ->build();

$signedTx = $transaction->sign($account);

$txId = $this->algorand->sendTransaction($signedTx);

$approvalProgram = $this->algorand->applicationManager()->compileTEAL($this->approvalProgramSource);
$clearProgram = $this->algorand->applicationManager()->compileTEAL($this->clearProgramSource);

$txId = $this->algorand->applicationManager()->update(
    $account,
    BigInteger::of(19964146),
    $approvalProgram->program(),
    $clearProgram->program(),
);

$approvalProgram = $this->algorand->applicationManager()->compileTEAL($this->approvalProgramSource);
$clearProgram = $this->algorand->applicationManager()->compileTEAL($this->clearProgramSource);

$transaction = TransactionBuilder::applicationUpdate()
    ->sender($account->getAddress())
    ->applicationId(BigInteger::of(19964146))
    ->approvalProgram($approvalProgram->program())
    ->clearStateProgram($clearProgram->program())
    ->useSuggestedParams($this->algorand)
    ->build();

$signedTx = $transaction->sign($account);
$txId = $this->algorand->sendTransaction($signedTx);

$txId = $this->algorand->applicationManager()->deleteApplication($account, BigInteger::of(22257782));

$txId = $this->algorand->applicationManager()->closeOut($account, BigInteger::of(19964146));

$txId = $this->algorand->applicationManager()->clearState($account, BigInteger::of(19964146));

$one = Address::fromAlgorandAddress('XMHLMNAVJIMAW2RHJXLXKKK4G3J3U6VONNO3BTAQYVDC3MHTGDP3J5OCRU');
$two = Address::fromAlgorandAddress('HTNOX33OCQI2JCOLZ2IRM3BC2WZ6JUILSLEORBPFI6W7GU5Q4ZW6LINHLA');
$three = Address::fromAlgorandAddress('E6JSNTY4PVCY3IRZ6XEDHEO6VIHCQ5KGXCIQKFQCMB2N6HXRY4IB43VSHI');

$publicKeys = array_map(fn (Address $value) => new Ed25519PublicKey($value->address), [$one, $two, $three]);
$msigAddr = new MultiSignatureAddress(1, 2, $publicKeys);

$account1 = Account::seed($seed1);
$account2 = Account::seed($seed2);
$account3 = Account::seed($seed3);

$publicKeys = array_map(fn (Account $value) => new Ed25519PublicKey($value->getPublicKey()), [$account1, $account2, $account3]);
$msigAddr = new MultiSignatureAddress(1, 2, $publicKeys);

$transaction = TransactionBuilder::payment()
    ->sender($msigAddr->toAddress())
    ->note('MSA')
    ->amount(Algo::fromMicroAlgos(1000000))
    ->receiver($account3->getAddress())
    ->useSuggestedParams($this->algorand)
    ->build();

$signedTx = $msigAddr->sign($account1, $transaction);
$completeTx = $msigAddr->append($account2, $signedTx);
$completeTx2 = $msigAddr->append($account3, $completeTx);

$txId = $this->algorand->sendTransaction($completeTx2);

$request = new CreateWalletRequest([
   "wallet_name" => "test1",
   "wallet_password" => "test",
   "wallet_driver_name" => "sqlite",
]);

try {
    $result = $algorand->kmd()->createWallet($request);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling DefaultApi->createWallet: ', $e->getMessage(), PHP_EOL;
}

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

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

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

Algorand::indexer()
    ->applications()
    ->whereApplicationId(19964146)
    ->limit(5)
    ->search();
bash
composer 
algorand.php

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