Simple Token Distribution
Overview
In this section, we will go through the steps required to do a simple token distribution from a sender account to 5 receiver addresses.
Select a Network and Provider
Check here to select a network and provider.
Create a Sender account
Check here to setup an account.
The Account created need to have some Fungible Tokens so we can distribute them to other addresses. In This Example let’s assume we have some Charlie3 Tokens already in possession in our Sender Account Base Address. Unlike other account-based blockchains, Cardano supports multiple outputs in a single transaction. So let’s define a csv file with our receiving addresses and amount of tokens per address.
addr_test1qxz3zrldxkkwlawrdx2w8ycsnz578r5nrrj96d7e2na9n32cd7gh928plcchqknd3duaxpl5zu86g5gqkd3npv58vvgs8tgtk0,114.0
addr_test1q8ggrexl20slswsnlrct7wm4uhl48eu563rkl75sv3453murys96l34r0lvxd5576q7w806fd8qq3swv45hka0uehkls4vxjka,547.2
addr_test1qyx6htpm9smwvg2w3f4eldtpq3p5ty38perhaafw86gfhgqa4dnr2pglwk0wgejy788uss82tkfxs78qnd0uleds7a9qkadf08,91.2
addr_test1qxzgdkjhepeytjyls3u2ed2x9cfvd4pm40lwsjgk53hm0n7m9j088cqfhvm934lnlsprhwq2c3ha4hl72cs3ul057p2swdlz5u,15.2
addr_test1q9hp9mja7fjyh9hy8mkstn6uuxtn98z3fgryy75qh8hpmhp3hdmcfrjy06m5f7ht8mecgegjt8jerm6l8jwdxcvjuxgsl907rj,15.2Create Backend Service
Check here to create a backend provider instance.
Token Distribution Transaction - Using QuickTx API
The QuickTx API provides a simple and declarative way to build token distribution transactions.
Create QuickTxBuilder
QuickTxBuilder quickTxBuilder = new QuickTxBuilder(backendService);Define the Token
Specify the policy ID and asset name for the token you want to distribute. The unit is the concatenation
of policy ID and hex-encoded asset name.
String policyId = "8e51398904a5d3fc129fbf4f1589701de23c7824d5c90fdb9490e15a";
String assetName = "434841524c4933";
String unit = policyId + assetName;Build Transaction with Token Outputs from CSV
Read the CSV file and add each receiver as a transaction output using payToAddress with Amount.asset.
We multiply the quantity value by the number of decimals this specific token is registered under. In this case, for Charlie3 Token it is 6 zeros after the decimal point, hence we multiply the quantity by a million.
Tx tx = new Tx();
Scanner scanner = new Scanner(getFileFromResource("file1.csv"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
BigInteger qty = new BigDecimal(parts[1])
.multiply(BigDecimal.valueOf(1_000_000))
.toBigInteger();
tx.payToAddress(parts[0], Amount.asset(unit, qty));
}
tx.from(senderAddress);Build, Sign, and Submit
Result<String> result = quickTxBuilder.compose(tx)
.withSigner(SignerProviders.signerFrom(sender))
.completeAndWait(System.out::println);If successful, result.isSuccessful() will return true and result.getValue() will contain the transaction hash.
Full Source Code
import com.bloxbean.cardano.client.account.Account;
import com.bloxbean.cardano.client.api.model.Amount;
import com.bloxbean.cardano.client.api.model.Result;
import com.bloxbean.cardano.client.backend.api.BackendService;
import com.bloxbean.cardano.client.backend.koios.Constants;
import com.bloxbean.cardano.client.backend.koios.KoiosBackendService;
import com.bloxbean.cardano.client.common.model.Networks;
import com.bloxbean.cardano.client.function.helper.SignerProviders;
import com.bloxbean.cardano.client.quicktx.QuickTxBuilder;
import com.bloxbean.cardano.client.quicktx.Tx;
import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;
public class TokenDistribution {
private final BackendService backendService;
public TokenDistribution() {
backendService = new KoiosBackendService(Constants.KOIOS_PREPROD_URL);
}
public void distribute() throws Exception {
String senderMnemonic = "<mnemonic>"; //TODO Seed Phrase
Account sender = new Account(Networks.testnet(), senderMnemonic);
String senderAddress = sender.baseAddress();
String policyId = "8e51398904a5d3fc129fbf4f1589701de23c7824d5c90fdb9490e15a";
String assetName = "434841524c4933";
String unit = policyId + assetName;
// Create QuickTxBuilder
QuickTxBuilder quickTxBuilder = new QuickTxBuilder(backendService);
// Build transaction from CSV
Tx tx = new Tx();
Scanner scanner = new Scanner(getFileFromResource("file1.csv"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
BigInteger qty = new BigDecimal(parts[1])
.multiply(BigDecimal.valueOf(1_000_000))
.toBigInteger();
tx.payToAddress(parts[0], Amount.asset(unit, qty));
}
tx.from(senderAddress);
// Build, sign, submit and wait for confirmation
Result<String> result = quickTxBuilder.compose(tx)
.withSigner(SignerProviders.signerFrom(sender))
.completeAndWait(System.out::println);
System.out.println(result);
}
private File getFileFromResource(String fileName) throws URISyntaxException {
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(fileName);
if (resource == null) {
throw new IllegalArgumentException("file not found! " + fileName);
} else {
return new File(resource.toURI());
}
}
public static void main(String[] args) throws Exception {
new TokenDistribution().distribute();
}
}Looking for the Composable Functions version? Check the Simple Token Distribution - Composable Functions tutorial.