4

How could I check Stellar wallet balance using API? For example with BTC one can check address in block explorer and see the balance. I also need to differentiate incoming transactions by MEMO.

I looked some transactions examples, but still I'm confused. Where can I find any examples of balance check also considering MEMOs?

3 Answers 3

4

As far as I know, there is no single endpoint in horizon API that allows you to check the account's balance and/ or filtered by memo at the same time. If you want to differentiate incoming transactions by memo, what you can do is to use event streaming transactions for a particular account to extract the memo and consider injecting your logic in there, let me know what you want to exactly achieve. sample code using the javascript sdk below:

var StellarSdk = require('stellar-sdk');
var server = new StellarSdk.Server('https://horizon-testnet.stellar.org');

var notifyAccount = 'GATGC7IEHN6SBRPTEGIACCWXB6VHGVA7UYZGOIBE4LFXRSKYCLWO5QH3';

var txHandler = function (txResponse) {    
    console.log("new transaction activity");
    //get memo here
    console.log('memo: ' + txResponse.memo);
    console.log('memo_type: ' + txResponse.memo_type);
    server.accounts().accountId(notifyAccount)
        .call()
        .then(function (result) {
            //check balance here
            console.log(JSON.stringify(result.balances, null, 2));
        })
        .catch(function (err) {
            console.error(err);
        });
}

console.log("listening for transactions to address " + notifyAccount);
var transStream = server.transactions()
    .cursor('now')
    .forAccount(notifyAccount)
    .stream({
        onmessage: txHandler
    });

OUTPUT:

listening for transactions to address GATGC7IEHN6SBRPTEGIACCWXB6VHGVA7UYZGOIBE4L
FXRSKYCLWO5QH3
new transaction activity
memo: testnow
memo_type: text
[
  {
    "balance": "0.0000000",
    "limit": "10000.0000000",
    "asset_type": "credit_alphanum4",
    "asset_code": "PHP",
    "asset_issuer": "GDENQQVYYVD4DWAF5243RLRW4GS7H2LAUM4VVRA2KDERFKP4N2XATCIJ"
  },
  {
    "balance": "2000.0000000",
    "limit": "10000.0000000",
    "asset_type": "credit_alphanum4",
    "asset_code": "USD",
    "asset_issuer": "GD5SBDV5OXBPSIDOQ6HGBNRE67ASQ6RHHYVXG7R7ZVGPM4AEYMT6BD7L"
  },
  {
    "balance": "0.0000000",
    "limit": "10000.0000000",
    "asset_type": "credit_alphanum4",
    "asset_code": "USD",
    "asset_issuer": "GDENQQVYYVD4DWAF5243RLRW4GS7H2LAUM4VVRA2KDERFKP4N2XATCIJ"
  },
  {
    "balance": "406.1607919",
    "asset_type": "native"
  }
]
3

Just pick a horizon server to query (e.g., https://horizon.stellar.org/). Then for account balances you want the Account endpoint, which you can query like this:

curl https://horizon.stellar.org/accounts/GDOZSTU2PMCVID4TYBMMES6IDBO7SYBQY366S4VSK6SG2AS2R44RLIUH

(but substitute a real account account). You'll find the balances in the balances field of the json object.

To get the transactions for your account, check the documentation on the Transactions for account endpoint. You will have to filter for memo yourself, but this should not be hard.

0

Here is complete method to check balance of an address:

const checkBalance = async (publicKey) => {
    return new Promise(async (resolve, reject) => {
        try {
            const account = await server.loadAccount(publicKey);
            let balances = [];
            account.balances.forEach(function (balance) {
                balances.push(balance);
            });
            resolve(balances);
        } catch (err) {
            reject(err);
        }
    })
}

Response will be:

{
            "balance": "9979.9999800",
            "buying_liabilities": "0.0000000",
            "selling_liabilities": "0.0000000",
            "asset_type": "native"
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.