Skip to main content

Tokens

All value in a ledger is represented as tokens. Each token is an indivisible, single unit of a flavor, which denotes a specific type of value - for example, USD or points.

To create new value in a ledger, you issue tokens into an account with a transaction. To transfer value in a ledger, you transfer tokens from one account to another. To remove value from a ledger, you retire tokens from an account.

Token Tags

When issuing or transferring tokens into an account, you may wish to tag them for later distinction by providing token tags.

You can then specify a filter when transferring or retiring tokens from the account to target the tokens with specific tags.

Unlike the tags on other objects in the ledger, token tags only exist on the current state of the tokens in an account. Once those tokens are transferred in a transaction, the tags are removed and new tags can be added as they land in the destination account.

Updating tags

Since tokens are the fundamental state of the ledger, in order to update the tags on a group of tokens, you must create a transaction. In the future we will provide a special method for building a transaction that updates tags on tokens, but for now, you transfer them to and from the same account.

Data Structure

The ledger does not store individual token objects, but rather token group objects, which each represent an amount of identical tokens.

If every token is unique, each token group will have an amount of 1.

Field Descriptions

FieldTypeDescription
amountintegerAmount of tokens.
tagsJSON objectArbitrary, user-supplied, key-value data about the tokens.
flavor idstringThe id of the flavor of the tokens.
flavor tagsJSON objectThe tags of the flavor of the tokens.
account idstringThe id of the account that holds the tokens.
account tagsstringThe tags of the account that holds the tokens.

Example Object

{
amount: 10,
tags: {},
flavorId: ""
flavorTags: {},
accountId: "",
accountTags: {},
}

Queries

There are two different types of queries that you can perform on tokens:

  1. A list query lists tokens in the ledger
  2. A sum query is an aggregate over the amount fields in a filtered list of tokens.

Both queries accept a filter to narrow the results.

List tokens

The list tokens query is useful when you use token tags to differentiate tokens of the same flavor in an account.

For example, if your application tracks stock certificates by tagging Acme stock tokens with a cost basis and purchase date in the token tags, you can list the tokens in Alice's account to see all her certificates.

Sum tokens

The sum tokens query is how you calculate balances in the ledger.

For example, to calculate all the USD balance in Alice's account, you sum all the USD tokens in her account. Or to calculate the total amount of USD in the ledger, you sum all the USD tokens across all accounts.

Examples

Issue tokens

Issue 100 USD tokens to Alice's account.

Transaction tx = new Transaction.Builder()
.addAction(new Transaction.Builder.Action.Issue()
.setFlavorId("usd")
.setAmount(100)
.setDestinationAccountId("alice")
).transact(ledger);

Issue tokens with tags

Issue 100 debt tokens to Alice's account that are due December, 2018:

Transaction tx = new Transaction.Builder()
.addAction(new Transaction.Builder.Action.Issue()
.setFlavorId("debt")
.setAmount(100)
.setDestinationAccountId("alice")
.addTokenTagsField("due", "Dec2018")
).transact(ledger);

Transfer tokens

Transfer 20 USD tokens from Alice's account to Bob's account.

Transaction tx = new Transaction.Builder()
.addAction(new Transaction.Builder.Action.Transfer()
.setFlavorId("usd")
.setAmount(20)
.setSourceAccountId("alice")
.setDestinationAccountId("bob")
).transact(ledger);

Update tags by transferring tokens to same account

Update the due date from Dec2018 to Feb2019 on 20 debt tokens in Alice's account:

Transaction tx = new Transaction.Builder()
.addAction(new Transaction.Builder.Action.Transfer()
.setFlavorId("debt")
.setAmount(20)
.setSourceAccountId("alice")
.setDestinationAccountId("alice")
.setFilter("tags.due=$1")
.addFilterParameter("Dec2018")
.addTokenTagsField("due", "Feb2019")
).transact(ledger);

Retire tokens

Retire 5 USD tokens from Bob's account.

Transaction tx = new Transaction.Builder()
.addAction(new Transaction.Builder.Action.Retire()
.setFlavorId("usd")
.setAmount(5)
.setSourceAccountId("bob")
).transact(ledger);

Retire tokens filtered by tags

Retire 80 of Alice's debt tokens due December 2018:

Transaction tx = new Transaction.Builder()
.addAction(new Transaction.Builder.Action.Retire()
.setFlavorId("debt")
.setAmount(80)
.setSourceAccountId("alice")
.setFilter("tags.due=$1")
.addFilterParameter("Dec2018")
).transact(ledger);

Sum tokens in an account

Query the first two pages of "balances" in Alice's account.

TokenSum.Page page1 = new Token.SumBuilder()
.setFilter("accountId=$1")
.addFilterParameter("alice")
.addGroupByField("flavorId")
.setPageSize(10)
.getPage(ledger);
for (TokenSum balance : page1.items) {
System.out.println("amount: " + balance.amount);
System.out.println("flavor: " + balance.flavorId);
}

String cursor = page1.cursor;

TokenSum.Page page2 = new Token.SumBuilder()
.getPage(ledger, cursor);
for (TokenSum balance : page2.items) {
System.out.println("amount: " + balance.amount);
System.out.println("flavor: " + balance.flavorId);
}

List tokens in an account

Query the first two pages of tokens in Alice's account.

TokenGroup.Page page1 = new Token.ListBuilder()
.setFilter("accountId=$1")
.addFilterParameter("alice")
.getPage(ledger);
for (TokenGroup group : page1.items) {
...
}

String cursor = page1.cursor;

TokenGroup.Page page2 = new Token.ListBuilder()
.getPage(ledger, cursor);
for (TokenGroup group : page2.items) {
...
}