PHP Algorand SDK 2.0

Hello everyone, I have completed the development of the new 2.0 version of the SDK made in native PHP without dependencies that I use in my projects.

This version brings many improvements and optimizations such as the new Algokey. Feel free to use it and give suggestions.

I am available if you need any help.

Best Regards

5 Likes

Hi Felipe,

great job as usual :slight_smile:

I have a problem with switching from v1 to v2 API of algod.

 $return = $algorand->get("v2", "accounts", $mainReserveAddress);
            $return_array = json_decode($return['response']);
            $algoWifiAmount = $return_array->{'assets'}->{$algowifiAssetId}->{'amount'} / 10000;
            $algoAmount = $return_array->{'amount'} / 1000000;

I can’t retreive the value of $algoWifiAmount from $return_array.

This is the value of $return_array:

object(stdClass)#4 (18) { ["address"]=> string(58) "ZO3BFADA4BSREKRZIDFXFWJJIWFNQQ3W7PSCVYDFMEATJEJYA3RQG5657Q" ["amount"]=> int(11566000) ["amount-without-pending-rewards"]=> int(11566000) ["apps-local-state"]=> array(0) { } ["apps-total-schema"]=> object(stdClass)#5 (2) { ["num-byte-slice"]=> int(0) ["num-uint"]=> int(0) } ["assets"]=> array(2) { [0]=> object(stdClass)#6 (3) { ["amount"]=> int(68728524) ["asset-id"]=> int(338444324) ["is-frozen"]=> bool(false) } [1]=> object(stdClass)#7 (3) { ["amount"]=> int(1) ["asset-id"]=> int(1071835494) ["is-frozen"]=> bool(false) } } ["created-apps"]=> array(0) { } ["created-assets"]=> array(0) { } ["min-balance"]=> int(300000) ["pending-rewards"]=> int(0) ["reward-base"]=> int(218288) ["rewards"]=> int(0) ["round"]=> int(29131783) ["status"]=> string(7) "Offline" ["total-apps-opted-in"]=> int(0) ["total-assets-opted-in"]=> int(2) ["total-created-apps"]=> int(0) ["total-created-assets"]=> int(0) }

I try also this :

$algoWifiAmount = $return_array->{'assets'}->{'asset-id'}->{$algowifiAssetId}->{'amount'} / 1000000;

Any suggestions ?

Thank’s

Marco
p.s we use Php-Algosdk v.1

Hi Marco!

This function (v2) returns a serialized array with the balance of all assets. In this case, you would need to search in the array for the Asset ID.

Use as shown in the example below, already providing the Asset ID so that it directly returns the asset balance.

$return=$algorand->get(“v2”,“accounts”,“{address}”,“assets”,“{asset-id}”);
$return_array = json_decode($return[‘response’]);
$algoWifiAmount = $return_array->{‘asset-holding’}->amount;

Best Regards
Felipe Vieira

Thank’s Felipe,

now work for asset amount, but lost amount of account:

$return = $algorand->get("v2","accounts",$mainAccountAddress,"assets",$algowifiAssetId);
            $return_array = json_decode($return['response']);    
            $algoWifiAmount = $return_array->{'asset-holding'}->{'amount'} / 1000000;
---->      $algoAmount = $return_array->{'amount'} / 1000000;
            print_r($return_array);

One possible solution is :

$return = $algorand->get("v2","accounts",$mainAccountAddress,"assets",$algowifiAssetId);
$return1 = $algorand->get("v2", "account", $mainAccountAddress);
            $return_array = json_decode($return['response']);    
            $return_array1 = json_decode($return['response'];
            $algoWifiAmount = $return_array->{'asset-holding'}->{'amount'} / 1000000;
            $algoAmount = $return_array1->{'amount'} / 1000000;
            print_r($return_array);

but, is very bad to see … I think that algod v1 API work better with json manipulation.

Any other suggestion ?

Thank in advance

Marco

I agree, in v1 it was easier, but it will be removed soon.

But It is possible to create a simple function to search and extract the Amount of an Asset ID:

function asset_search($assets,$asset_id){
    foreach($assets as $obj) {
        if($asset_id==$obj->{'asset-id'}){
            return $obj;
        }
    }
}

How to use:

$return = $algorand->get("v2", "accounts", $mainReserveAddress);
$return_array = json_decode($return['response']);
$algoAmount=$return_array->amount;

$asset_data=asset_search($return_array->assets,{asset-id});
$algoWifiAmount=$asset_data->amount;

I put this in global configuration file for automatically use on every file.

function asset_search($assets,$asset_id){
    foreach($assets as $obj) {
        if($asset_id==$obj->{'asset-id'}){
            return $obj;
        }
    }
}

than I use that for select correct asset.

$asset_data=asset_search($return_array->assets,{asset-id});
$algoWifiAmount=$asset_data->amount;

It’s only one line to add…

Thank’s Felipe :slight_smile:

Marco

Nice.

You’re welcome