Permalink
Browse files

Added convenience method Server::accountExists

  • Loading branch information...
zulucrypto committed Sep 22, 2018
1 parent 57794ef commit d7c963bfd79b8fce271cc43c6d219fd3ac2751c8
Showing with 56 additions and 0 deletions.
  1. +32 −0 examples/account-exists.php
  2. +24 −0 src/Server.php
@@ -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;
}
@@ -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;
@@ -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

0 comments on commit d7c963b

Please sign in to comment.