Packages
stellar_sdk
0.18.1
0.23.0
0.22.0
0.21.2
0.21.1
0.21.0
0.20.0
0.19.0
0.18.1
0.18.0
0.17.1
0.17.0
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.13.1
0.13.0
0.12.0
0.11.8
0.11.7
0.11.6
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.2
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.1
0.8.0
0.7.1
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
Elixir SDK for the Stellar network.
Current section
Files
Jump to
Current section
Files
lib/horizon/assets.ex
defmodule Stellar.Horizon.Assets do
@moduledoc """
Exposes functions to interact with Assets in Horizon.
You can:
* List all assets.
* List assets by asset_code.
* List assets by asset_issuer.
Horizon API reference: https://developers.stellar.org/api/resources/assets/
"""
alias Stellar.Horizon.{Asset, Collection, Error, Request}
@type asset_code :: String.t()
@type asset_issuer :: String.t()
@type options :: Keyword.t()
@type resource :: Asset.t() | Collection.t()
@type response :: {:ok, resource()} | {:error, Error.t()}
@endpoint "assets"
@doc """
Lists all assets or by one of these filters: `asset_code` or `asset_issuer`.
## Options
* `asset_code`: The code of the asset you would like to filter by.
* `asset_issuer`: The issuer's Account ID for the asset you would like to filter by.
* `cursor`: A number that points to a specific location in a collection of responses and is pulled from the `paging_token` value of a record.
* `order`: A designation of the order in which records should appear. Options include `asc` (ascending) or `desc` (descending).
* `limit`: The maximum number of records returned. The limit can range from 1 to 200. Defaults to 10.
## Examples
iex> Assets.all(limit: 20, order: :desc)
{:ok, %Collection{records: [%Asset{}, ...]}}
# list by asset_code
iex> Assets.all(asset_code: "TEST")
{:ok, %Collection{records: [%Asset{}, ...]}}
# list by asset_issuer
iex> Assets.all(asset_issuer: "GCXMWUAUF37IWOOV2FRDKWEX3O2IHLM2FYH4WPI4PYUKAIFQEUU5X3TD")
{:ok, %Collection{records: [%Asset{}, ...]}}
"""
@spec all(options :: options()) :: response()
def all(options \\ []) do
:get
|> Request.new(@endpoint)
|> Request.add_query(options, extra_params: [:asset_code, :asset_issuer])
|> Request.perform()
|> Request.results(collection: {Asset, &all/1})
end
@doc """
Lists assets matching the given asset code.
## Parameters:
* `asset_code`: The code of the asset you would like to filter by.
## Options
* `cursor`: A number that points to a specific location in a collection of responses and is pulled from the `paging_token` value of a record.
* `order`: A designation of the order in which records should appear. Options include `asc` (ascending) or `desc` (descending).
* `limit`: The maximum number of records returned. The limit can range from 1 to 200. Defaults to 10.
## Examples
iex> Assets.list_by_asset_code("TEST")
{:ok, %Collection{records: [%Asset{asset_code: "TEST"}, ...]}}
"""
@spec list_by_asset_code(asset_code :: asset_code(), options :: options()) :: response()
def list_by_asset_code(asset_code, options \\ []) do
options
|> Keyword.put(:asset_code, asset_code)
|> all()
end
@doc """
Lists assets matching the given asset issuer.
## Parameters:
* `asset_issuer`: The issuer's Account ID for the asset you would like to filter by.
## Options
* `cursor`: A number that points to a specific location in a collection of responses and is pulled from the `paging_token` value of a record.
* `order`: A designation of the order in which records should appear. Options include `asc` (ascending) or `desc` (descending).
* `limit`: The maximum number of records returned. The limit can range from 1 to 200. Defaults to 10.
## Examples
iex> Assets.list_by_asset_issuer("GCXMWUAUF37IWOOV2FRDKWEX3O2IHLM2FYH4WPI4PYUKAIFQEUU5X3TD")
{:ok, %Collection{records: [%Asset{asset_issuer: "GCXMWUAUF37IWOOV2FRDKWEX3O2IHLM2FYH4WPI4PYUKAIFQEUU5X3TD"}, ...]}}
"""
@spec list_by_asset_issuer(asset_issuer :: asset_issuer(), options :: options()) :: response()
def list_by_asset_issuer(asset_issuer, options \\ []) do
options
|> Keyword.put(:asset_issuer, asset_issuer)
|> all()
end
end