Skip to content

Commit

Permalink
Added convenience method Server::accountExists
Browse files Browse the repository at this point in the history
  • Loading branch information
zulucrypto committed Sep 22, 2018
1 parent 57794ef commit d7c963b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/account-exists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Adds a new signer to an existing account
*/


require '../vendor/autoload.php';

use \ZuluCrypto\StellarSdk\Keypair;
use \ZuluCrypto\StellarSdk\Server;
use \phpseclib\Math\BigInteger;


$server = Server::testNet();

// Check if an account exists
try {
$exists = $server->accountExists('GCP6IHMHWRCF5TQ4ZP6TVIRNDZD56W42F42VHYWMVDGDAND75YGAHHBQ');

if ($exists) {
print "Account exists!" . PHP_EOL;
}
else {
print "Account does not exist." . PHP_EOL;
}
}
// If there's an exception it could be a temporary error, like a connection issue
// to Horizon, so we cannot tell for sure if the account exists or not
catch (\Exception $e) {
print "Error: " . $e->getMessage() . PHP_EOL;
}
24 changes: 24 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ZuluCrypto\StellarSdk;


use phpseclib\Math\BigInteger;
use Prophecy\Exception\InvalidArgumentException;
use ZuluCrypto\StellarSdk\Horizon\ApiClient;
use ZuluCrypto\StellarSdk\Horizon\Exception\HorizonException;
Expand Down Expand Up @@ -104,6 +105,29 @@ public function getAccount($accountId)
return $account;
}

/**
* Returns true if the account exists on this server and has been funded
*
* @param $accountId
* @return bool
* @throws HorizonException
* @throws \ErrorException
*/
public function accountExists($accountId)
{
// Handle basic errors such as malformed account IDs
try {
$account = $this->getAccount($accountId);
} catch (\InvalidArgumentException $e) {
return false;
}

// Account ID may be valid but hasn't been funded yet
if (!$account) return false;

return $account->getNativeBalanceStroops() != '0';
}

/**
* @param $accountId string|Keypair
* @return TransactionBuilder
Expand Down

0 comments on commit d7c963b

Please sign in to comment.