Checking account balances and sending tokens
In this flow, we are going to describe the process of checking an account balance and sending tokens to a hedera id. There are a number of applications of this including systems for access tokens and

Overview

This process of sending tokens to an external account isn't permissioned, that is any hedera wallet can register with a system, and you can use these actions.
Under Hedera's APIs, sending of tokens requires an association of a given token, at this time we are focused on using the Venly Wallet (Widget) which provides 25 free associations for tokens per wallet.
​

Check a token balance

Check a hedera token balance for any account, this focused on HTS tokens such that if you need to check the balance for access to a service or to gauge an account share of a staking pool.
Infrastructure testing needs to be conducted for high-volume account balance requests.

Imports

1
use Trustenterprises\LaravelHashgraph\LaravelHashgraph;
2
use Trustenterprises\LaravelHashgraph\Models\AccountTokenBalanceResponse;
Copied!

Code

1
$account_id = '0.0.15657776'; // A venly-created account on testnet
2
$token_id = '0.0.15657534'; // Token already created
3
​
4
$token_balance = LaravelHashgraph::getTokenBalance($account_id, $token_id);
5
​
6
// Token value in whole numbers, ignoring the "decimals places" of a created token.
7
$token_balance->getAmount();
Copied!

Send a token to an account

Send a token to a hedera account, expects a SendToken object to be created at the payload. The response will tell you if the transfer succeeded or not, if the latter an error can be used.
This assumes that the token you are sending has been created with the mint token methods of the API/client and has custody over them.
This method waits for the finality of the transfer transaction, thus you will receive an error that a transfer has failed that will describe the issue, if an originator account does not have enough balance to make the transfer or if there is an association error.
​
Expect the response to be between 2-3 seconds, jobs should be used.

Imports

1
use Trustenterprises\LaravelHashgraph\LaravelHashgraph;
2
use Trustenterprises\LaravelHashgraph\Models\SendToken;
3
use Trustenterprises\LaravelHashgraph\Models\SendTokenResponse;
Copied!

Code

1
// Pre-generated Venly wallet (testnet) (maxes out at 25 assocs)
2
$account_id = '0.0.15657776'; // A venly-created account on testnet
3
$token_id = '0.0.15657534'; // Token already created
4
$amount = 0.000001;
5
​
6
$send_token = new SendToken($token_id, $account_id, $amount);
7
$token_sent = LaravelHashgraph::sendToken($send_token);
8
​
9
if ($token_sent->hasTransferSucceeded()) {
10
// Continue action.
11
} else {
12
$token_sent->getError();
13
}
Copied!
Last modified 1mo ago