DEV Community

esteblock
esteblock

Posted on

Token Playground #5: Get info about a token in Stellar (classic).

Token Playground Chapter 5: Get info about a token in Stellar (classic).

Introduction:

In this chapter we will show you how to get info about the asset that we have created and minted in the previous chapter.

The Horizon sever provides an HTTP API to request data in the stellar network, providing several endpoints. One of them allow us to request assets info. Our code uses the Stellar SDK sends request to these endpoints.

Remember to follow the code in the Token Playground's Repo. Also, you can clone the code by doing

git clone https://github.com/esteblock/token-playground/
Enter fullscreen mode Exit fullscreen mode

1. Get info about a token from stellar

Asset information are stored in stellar ledgers. In order to facitilate the access to the data, stellar provides the Horizon API with several endpoints. Here we show you the endpoint URL with the query string to request an asset info.

https://horizon-testnet.stellar.org/assets?asset_code=ASSET_CODE&asset_issuer=ISSUER_ADDRESS
Enter fullscreen mode Exit fullscreen mode

The query string includes two parameters, one is the asset_code and one is the asset_issuer. This request will return the info of a single asset as reponse.

An answer looks like this:

Futurenet Classic Info: 
{
  _links: { toml: { href: '' } },
  asset_type: 'credit_alphanum12',
  asset_code: 'MYASSETCODE',
  asset_issuer: 'GAM5XOXRUWPMKENBGOEAKMLRQ4ENHLHDSU2L2J7TVJ34ZI7S6PHMYIGI',
  paging_token: 'MYASSETCODE_GAM5XOXRUWPMKENBGOEAKMLRQ4ENHLHDSU2L2J7TVJ34ZI7S6PHMYIGI_credit_alphanum12',
  num_accounts: 1,
  num_claimable_balances: 0,
  num_liquidity_pools: 0,
  amount: '5.0000000',
  accounts: {
    authorized: 1,
    authorized_to_maintain_liabilities: 0,
    unauthorized: 0
  },
  claimable_balances_amount: '0.0000000',
  liquidity_pools_amount: '0.0000000',
  balances: {
    authorized: '5.0000000',
    authorized_to_maintain_liabilities: '0.0000000',
    unauthorized: '0.0000000'
  },
  flags: {
    auth_required: false,
    auth_revocable: false,
    auth_immutable: false,
    auth_clawback_enabled: false
  },
  toml: [Function (anonymous)]
}

Enter fullscreen mode Exit fullscreen mode

In the response you'll find relevant info about the asset as:

-asset code MYASSETCODE

-asset issuer GAM5XOXRUWPMKENBGOEAKMLRQ4ENHLHDSU2L2J7TVJ34ZI7S6PHMYIGI

-amount issued 5.0000000

2. Use our code

If you want to use our Token Playground's Repo code, we prepared the src/getInfo.js script that can be called by the soroban-preview-7 docker container:

You can run it by:

docker exec soroban-preview-7 node src/getInfo.js
Enter fullscreen mode Exit fullscreen mode

Also you can run it with a different asset code than the one in settings.json by passing it as argument:

 docker exec soroban-preview-7 node src/getInfo.js ASSET_CODE
Enter fullscreen mode Exit fullscreen mode

This script uses the stellar sdk to request to horizon api about asset info. Stellar sdk is a javascript library from stellar that offers a layer API for Horizon endpoints and facilities building, signing and submiting transactions.

Here, you'll find a fragmet of the code that exemplifies how stellar sdk access to horizon asset endpoint.


import StellarSdk from "stellar-sdk";

....

var server = new StellarSdk.Server(settings.horizonUrl, {allowHttp: true});

console.log("Futurenet Classic Info: ")
console.log((await server.assets().forCode(asset_code).call()).records[0])

Enter fullscreen mode Exit fullscreen mode

3. Next

In the next chapter we will wrap this asset from the classic Stellar blockchain into a Soroban smart contract. Are you ready?!


This Playground has been developed by @esteblock in collaboration with @marcos74 from @Dogstarcoin

Top comments (0)