Stellar — Distributing token to multiple accounts
Let say, we would like to distribute token to multiple accounts. What options do we have? And what are their pros and cons?
Here we are going to compare 4 methods:
- Queue transactions
- Batch operations
- Use destination as source account
- Source account pool
1. Queue transaction
One of the Stellar transaction constraints is that the sequence number of a transaction must be one greater than the sequence number stored in the source account entry. If we submit multiple transactions with the same source account at the same time, we will see error tx_bad_seq
. The simplest method is to to queue up transactions and send them out one by one
Pros:
- Simple and straightforward.
- Able to set memo for each transaction
Cons:
- Time consumes. For 100 transactions, it would take up to 8 mins.
2. Batch operations
Stellar allows up to 100 operations per transaction. We can group transfer operations together and send them out in batch
https://gist.github.com/niponchi/bb6524d80bcaa7a4bf73f9bca758dfce
Pros:
- Fast: up to 100 operations in 5 seconds
Cons:
- Cannot set memo for each operation
3. Use destination as source account
If you have a control over destination account (using multi-signature), or able to obtain its secret key. This method is to use destination account as source account of the transaction. Each transaction then can use its own sequence number, so there is no conflict among them.
One thing to add here, transaction fee will be charged to destination account. You can compensate transaction fee along with this transaction too. Amount of fee are (#operations + 1) * base_fee
in xml.
https://gist.github.com/niponchi/732303b3acd5c86ea3ff9548969814ca
Pros:
- Can submit transactions in parallel
- Able to set memo for each transaction
- Fixed base reserve for an account
- Fast: up to 100 operations in 5 seconds
Cons:
- Need a control over destination account or its secret key
- In transaction history of destination account, there will be records of small amount compensation fee, i.e.
0.00002
xlm.
4. Source account pool
You can create set of Stellar accounts and manage them in source account pool. Like database connection pool, this pool means to be shared as a source account submitting Stellar transaction. If the is a free account available, use that account as source account. Otherwise wait until pool is available
You can also transfer to compensation fee to accounts in pool to maintain their base reserve
Pros:
- Flexible: can handle one operation at a time or set of operations
- Scalable by adjusting pool size
- Do not need a control over destination account or its secret key
- Able to set memo for each transaction
- General purpose
- Processing time depends on number of agents in pool
Cons:
- Complexity in pool manager
- Extra credentials to maintain
Full example can be found at https://github.com/niponchi/stellar_transfer_examples