Getting an EOSIO currency balance using Anchor Link

When using Anchor Link in an application, one of the things it exposes for your use is the common APIs your application may need to retrieve further data from an EOSIO blockchain. As of anchor-link@2.1.2 this functionality is provided as we expose an eosjs RPC client on the link class. The full list of available methods are available for use in your application.

Note on Future Proofing: Anchor Link is going to stop using eosjs in a future version. It will be replaced with a new library providing similar functionality. For this reason, we would recommend you evaluate if you’re comfortable using the rpc client provided in Anchor Link or if you should initialize your own eosjs rpc client. Instructions for both can be found below.

Using the eosjs rpc provided by Anchor Link

Currently the Anchor Link instance exposes the JsonRpc client from an internal eosjs instance on AnchorLink itself. You can access this rpc anywhere you have access to the Link that you’ve created within your application.

// With the AnchorLink instance already established, e.g.
//    const link = new AnchorLink({ ... })
const { rpc } = link
const balance = await rpc.get_currency_balance('contract_name', 'user_account_name', 'token_symbol')

It can also be accessed from any LinkSessionChannel instance, which is the class returned either when you restore a session or perform the initial login method.


// With an LinkSessionChannel session already established, e.g.
//    const session = link.restoreSession('app-name')
//      --- or ---
//    const identity = await this.link.login('app-name', { chainId })
//    const session = identity.session
const { rpc } = session.link
const balance = await rpc.get_currency_balance('contract_name', 'user_account_name', 'token_symbol')

Using eosjs to get a currency balance

If you’d rather not rely on the internal eosjs instance from within AnchorLink, you can include and use eosjs natively. When using eosjs (20.x.x), to retrieve a balance you would make use of the JsonRpc client and the get_currency_balance method, like so:

const { JsonRpc } = require('eosjs');
const rpc = new JsonRpc('https://eos.greymass.com');

const balance = await rpc.get_currency_balance('eosio.token', 'teamgreymass', 'EOS')
1 Like