Token management

Introduction

Let's review some code examples on how to handle tokens with the Java client library:

Tip: if you're new to our Java library, we suggest you begin with our Get Started guide.

Creating a token

To create a token, an RRI (Radix Resource Identifier) must first be constructed:

RRI tokenRRI = RRI.of(api.getAddress(), "NEW");

Note: no validation is made whether the returned RRI actually points to a valid token in the internal address space of the client.

To create a fixed-supply token:

Result result = api.createFixedSupplyToken(tokenRRI, "New Token", "The Best Token", BigDecimal.valueOf(1000.0));
result.blockUntilComplete();

To create a multi-issuance token:

Result result = api.createMultiIssuance(tokenRRI, "New Token", "The Best Token");
result.blockUntilComplete();

Or equivalently,

CreateTokenAction createAction = CreateTokenAction.create(
tokenRRI,
"New Token",
"The Best Token",
BigDecimal.ZERO,
TokenUnitConversions.getMinimumGranularity(),
TokenSupplyType.MUTABLE
);
Result result = api.execute(createAction);
result.blockUntilComplete();

Minting tokens

To mint 1000 tokens (must be multi-issuance) in your account:

Result result = api.mintTokens(tokenRRI, BigDecimal.valueOf(1000.0));
result.blockUntilComplete();

Or equivalently,

MintTokensAction mintAction = MintTokensAction.create(tokenRRI, api.getAddress(), BigDecimal.valueOf(1000.0));
Result result = api.execute(mintAction);
result.blockUntilComplete();

Burning tokens

To burn 1000 tokens (must be multi-issuance) in your account:

Result result = api.burnTokens(tokenRRI, BigDecimal.valueOf(1000.0));
result.blockUntilComplete();

Or equivalently,

BurnTokensAction burnAction = BurnTokensAction.create(tokenRRI, api.getAddress(), BigDecimal.valueOf(1000.0));
Result result = api.execute(burnAction);
result.blockUntilComplete();

Sending tokens

To send an amount from my address to another address:

Result result = api.sendTokens(tokenRRI, BigDecimal.valueOf(10.99), <to-address>);
result.blockUntilComplete();

Or equivalently,

TransferTokensAction sendAction = TransferTokensAction.create(
tokenRRI,
api.getAddress(),
<to-address>,
BigDecimal.valueOf(10.00),
null
);
Result result = api.execute(sendAction);
result.blockUntilComplete();

Retrieving tokens

To retrieve all of the token transfers which have occurred in my account:

Observable<TokenTransfer> transfers = api.observeTokenTransfers();
transfers.subscribe(tx -> { ... });

To get a stream of the balance of tokens in my account:

Observable<BigDecimal> balance = api.observeBalance(tokenRRI);
balance.subscribe(bal -> { ... });